Spaces:
Sleeping
Sleeping
woletee
commited on
Commit
·
bd39949
1
Parent(s):
7146dfa
fixed the problem with visualzing both the input and the output pairs as well for the genetic programming
Browse files- app.py +15 -6
- templates/results.html +6 -10
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from flask import Flask, request, render_template
|
2 |
import os
|
3 |
import json
|
|
|
4 |
from Vit_concept import run_inference, model
|
5 |
from GP import genetic_programming
|
6 |
|
@@ -8,9 +9,15 @@ app = Flask(__name__)
|
|
8 |
UPLOAD_FOLDER = 'uploads'
|
9 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
@app.route('/')
|
12 |
def index():
|
13 |
return render_template('index.html')
|
|
|
14 |
@app.route('/upload', methods=['POST'])
|
15 |
def upload():
|
16 |
if 'file' not in request.files:
|
@@ -48,15 +55,18 @@ def upload():
|
|
48 |
predicted_HLCs=predicted_HLCs
|
49 |
)
|
50 |
|
51 |
-
#
|
52 |
-
last_input = input_output_pairs[-1][0]
|
53 |
-
last_ground_truth = input_output_pairs[-1][1]
|
54 |
|
55 |
try:
|
56 |
-
predicted_output = best_program.evaluate(last_input)
|
57 |
except Exception as e:
|
|
|
58 |
predicted_output = [["ERROR"]]
|
59 |
-
|
|
|
|
|
60 |
|
61 |
return render_template("results.html",
|
62 |
hlcs=predicted_HLCs,
|
@@ -66,6 +76,5 @@ def upload():
|
|
66 |
last_ground_truth=last_ground_truth,
|
67 |
predicted_output=predicted_output)
|
68 |
|
69 |
-
|
70 |
if __name__ == '__main__':
|
71 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
from flask import Flask, request, render_template
|
2 |
import os
|
3 |
import json
|
4 |
+
import numpy as np
|
5 |
from Vit_concept import run_inference, model
|
6 |
from GP import genetic_programming
|
7 |
|
|
|
9 |
UPLOAD_FOLDER = 'uploads'
|
10 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
11 |
|
12 |
+
def tolist_safe(obj):
|
13 |
+
if isinstance(obj, np.ndarray):
|
14 |
+
return obj.tolist()
|
15 |
+
return obj
|
16 |
+
|
17 |
@app.route('/')
|
18 |
def index():
|
19 |
return render_template('index.html')
|
20 |
+
|
21 |
@app.route('/upload', methods=['POST'])
|
22 |
def upload():
|
23 |
if 'file' not in request.files:
|
|
|
55 |
predicted_HLCs=predicted_HLCs
|
56 |
)
|
57 |
|
58 |
+
# Prepare final grids for evaluation section
|
59 |
+
last_input = tolist_safe(input_output_pairs[-1][0])
|
60 |
+
last_ground_truth = tolist_safe(input_output_pairs[-1][1])
|
61 |
|
62 |
try:
|
63 |
+
predicted_output = tolist_safe(best_program.evaluate(last_input))
|
64 |
except Exception as e:
|
65 |
+
print("Error during best_program evaluation:", e)
|
66 |
predicted_output = [["ERROR"]]
|
67 |
+
|
68 |
+
# Convert all pairs to lists (in case of NumPy values)
|
69 |
+
input_output_pairs = [(tolist_safe(i), tolist_safe(o)) for i, o in input_output_pairs]
|
70 |
|
71 |
return render_template("results.html",
|
72 |
hlcs=predicted_HLCs,
|
|
|
76 |
last_ground_truth=last_ground_truth,
|
77 |
predicted_output=predicted_output)
|
78 |
|
|
|
79 |
if __name__ == '__main__':
|
80 |
app.run(host="0.0.0.0", port=7860)
|
templates/results.html
CHANGED
@@ -91,11 +91,11 @@
|
|
91 |
</div>
|
92 |
|
93 |
<script>
|
94 |
-
const inputOutputPairs = {{ input_output_pairs | tojson }};
|
95 |
-
const hlcs = {{ hlcs | tojson }};
|
96 |
-
const lastInput = {{ last_input | tojson }};
|
97 |
-
const lastGroundTruth = {{ last_ground_truth | tojson }};
|
98 |
-
const predictedOutput = {{ predicted_output | tojson }};
|
99 |
|
100 |
const colorMap = {
|
101 |
0: "#FFFFFF", 1: "#0074D9", 2: "#FF4136", 3: "#2ECC40",
|
@@ -104,6 +104,7 @@
|
|
104 |
};
|
105 |
|
106 |
function drawGrid(container, gridData) {
|
|
|
107 |
container.style.gridTemplateRows = `repeat(${gridData.length}, 25px)`;
|
108 |
container.style.gridTemplateColumns = `repeat(${gridData[0].length}, 25px)`;
|
109 |
gridData.forEach(row => {
|
@@ -122,7 +123,6 @@
|
|
122 |
const container = document.createElement("div");
|
123 |
container.className = "grid-container";
|
124 |
|
125 |
-
// Input Grid
|
126 |
const inputDiv = document.createElement("div");
|
127 |
inputDiv.innerHTML = `<div class="grid-title">Input Grid</div>`;
|
128 |
const inputGridBox = document.createElement("div");
|
@@ -130,7 +130,6 @@
|
|
130 |
inputDiv.appendChild(inputGridBox);
|
131 |
drawGrid(inputGridBox, pair[0]);
|
132 |
|
133 |
-
// Output Grid
|
134 |
const outputDiv = document.createElement("div");
|
135 |
outputDiv.innerHTML = `<div class="grid-title">Output Grid</div>`;
|
136 |
const outputGridBox = document.createElement("div");
|
@@ -138,7 +137,6 @@
|
|
138 |
outputDiv.appendChild(outputGridBox);
|
139 |
drawGrid(outputGridBox, pair[1]);
|
140 |
|
141 |
-
// Concept Box
|
142 |
const conceptDiv = document.createElement("div");
|
143 |
conceptDiv.className = "concept-box";
|
144 |
const conceptTitle = document.createElement("div");
|
@@ -150,14 +148,12 @@
|
|
150 |
conceptDiv.appendChild(conceptTitle);
|
151 |
conceptDiv.appendChild(conceptText);
|
152 |
|
153 |
-
// Combine all parts
|
154 |
container.appendChild(inputDiv);
|
155 |
container.appendChild(outputDiv);
|
156 |
container.appendChild(conceptDiv);
|
157 |
pairsContainer.appendChild(container);
|
158 |
});
|
159 |
|
160 |
-
// Render best program application result
|
161 |
drawGrid(document.getElementById("last-input-grid"), lastInput);
|
162 |
drawGrid(document.getElementById("last-ground-truth-grid"), lastGroundTruth);
|
163 |
drawGrid(document.getElementById("predicted-output-grid"), predictedOutput);
|
|
|
91 |
</div>
|
92 |
|
93 |
<script>
|
94 |
+
const inputOutputPairs = {{ input_output_pairs | tojson | safe }};
|
95 |
+
const hlcs = {{ hlcs | tojson | safe }};
|
96 |
+
const lastInput = {{ last_input | tojson | safe }};
|
97 |
+
const lastGroundTruth = {{ last_ground_truth | tojson | safe }};
|
98 |
+
const predictedOutput = {{ predicted_output | tojson | safe }};
|
99 |
|
100 |
const colorMap = {
|
101 |
0: "#FFFFFF", 1: "#0074D9", 2: "#FF4136", 3: "#2ECC40",
|
|
|
104 |
};
|
105 |
|
106 |
function drawGrid(container, gridData) {
|
107 |
+
if (!Array.isArray(gridData) || !gridData.length || !Array.isArray(gridData[0])) return;
|
108 |
container.style.gridTemplateRows = `repeat(${gridData.length}, 25px)`;
|
109 |
container.style.gridTemplateColumns = `repeat(${gridData[0].length}, 25px)`;
|
110 |
gridData.forEach(row => {
|
|
|
123 |
const container = document.createElement("div");
|
124 |
container.className = "grid-container";
|
125 |
|
|
|
126 |
const inputDiv = document.createElement("div");
|
127 |
inputDiv.innerHTML = `<div class="grid-title">Input Grid</div>`;
|
128 |
const inputGridBox = document.createElement("div");
|
|
|
130 |
inputDiv.appendChild(inputGridBox);
|
131 |
drawGrid(inputGridBox, pair[0]);
|
132 |
|
|
|
133 |
const outputDiv = document.createElement("div");
|
134 |
outputDiv.innerHTML = `<div class="grid-title">Output Grid</div>`;
|
135 |
const outputGridBox = document.createElement("div");
|
|
|
137 |
outputDiv.appendChild(outputGridBox);
|
138 |
drawGrid(outputGridBox, pair[1]);
|
139 |
|
|
|
140 |
const conceptDiv = document.createElement("div");
|
141 |
conceptDiv.className = "concept-box";
|
142 |
const conceptTitle = document.createElement("div");
|
|
|
148 |
conceptDiv.appendChild(conceptTitle);
|
149 |
conceptDiv.appendChild(conceptText);
|
150 |
|
|
|
151 |
container.appendChild(inputDiv);
|
152 |
container.appendChild(outputDiv);
|
153 |
container.appendChild(conceptDiv);
|
154 |
pairsContainer.appendChild(container);
|
155 |
});
|
156 |
|
|
|
157 |
drawGrid(document.getElementById("last-input-grid"), lastInput);
|
158 |
drawGrid(document.getElementById("last-ground-truth-grid"), lastGroundTruth);
|
159 |
drawGrid(document.getElementById("predicted-output-grid"), predictedOutput);
|