Spaces:
Sleeping
Sleeping
woletee
commited on
Commit
·
0c9440e
1
Parent(s):
729a903
this is the fixed commit
Browse files- app.py +20 -20
- templates/index.html +3 -4
- templates/results.html +106 -165
app.py
CHANGED
@@ -10,9 +10,7 @@ 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():
|
@@ -32,48 +30,50 @@ def upload():
|
|
32 |
with open(filepath, 'r') as f:
|
33 |
data = json.load(f)
|
34 |
|
|
|
35 |
input_output_pairs = []
|
36 |
predicted_HLCs = []
|
37 |
-
|
38 |
for sample in data.get("train", []):
|
39 |
input_grid = sample["input"]
|
40 |
output_grid = sample["output"]
|
41 |
concept_label, _ = run_inference(model, input_grid, output_grid)
|
42 |
predicted_HLCs.append(concept_label)
|
43 |
input_output_pairs.append((tolist_safe(input_grid), tolist_safe(output_grid)))
|
44 |
-
|
45 |
predicted_HLCs = list(set(predicted_HLCs))
|
46 |
|
|
|
47 |
best_program, generations = genetic_programming(
|
48 |
input_output_pairs=input_output_pairs,
|
49 |
-
population_size=
|
50 |
-
generations=
|
51 |
mutation_rate=0.2,
|
52 |
crossover_rate=0.7,
|
53 |
max_depth=3,
|
54 |
predicted_HLCs=predicted_HLCs
|
55 |
)
|
56 |
|
|
|
57 |
test_pairs = []
|
58 |
predicted_test_outputs = []
|
59 |
-
|
60 |
for sample in data.get("test", []):
|
61 |
test_input = tolist_safe(sample["input"])
|
62 |
test_output = tolist_safe(sample["output"])
|
63 |
test_pairs.append((test_input, test_output))
|
64 |
try:
|
65 |
-
|
66 |
except Exception as e:
|
67 |
-
print("
|
68 |
-
|
69 |
-
predicted_test_outputs.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
return render_template("results.html",
|
72 |
-
hlcs=predicted_HLCs,
|
73 |
-
input_output_pairs=input_output_pairs,
|
74 |
-
test_pairs=test_pairs,
|
75 |
-
predicted_test_outputs=predicted_test_outputs,
|
76 |
-
best_program=str(best_program))
|
77 |
-
print ("server is working ")
|
78 |
if __name__ == '__main__':
|
79 |
-
|
|
|
10 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
11 |
|
12 |
def tolist_safe(obj):
|
13 |
+
return obj.tolist() if isinstance(obj, np.ndarray) else obj
|
|
|
|
|
14 |
|
15 |
@app.route('/')
|
16 |
def index():
|
|
|
30 |
with open(filepath, 'r') as f:
|
31 |
data = json.load(f)
|
32 |
|
33 |
+
# Collect training data and predict HLCs
|
34 |
input_output_pairs = []
|
35 |
predicted_HLCs = []
|
|
|
36 |
for sample in data.get("train", []):
|
37 |
input_grid = sample["input"]
|
38 |
output_grid = sample["output"]
|
39 |
concept_label, _ = run_inference(model, input_grid, output_grid)
|
40 |
predicted_HLCs.append(concept_label)
|
41 |
input_output_pairs.append((tolist_safe(input_grid), tolist_safe(output_grid)))
|
|
|
42 |
predicted_HLCs = list(set(predicted_HLCs))
|
43 |
|
44 |
+
# GP optimization
|
45 |
best_program, generations = genetic_programming(
|
46 |
input_output_pairs=input_output_pairs,
|
47 |
+
population_size=200,
|
48 |
+
generations=200,
|
49 |
mutation_rate=0.2,
|
50 |
crossover_rate=0.7,
|
51 |
max_depth=3,
|
52 |
predicted_HLCs=predicted_HLCs
|
53 |
)
|
54 |
|
55 |
+
# Evaluate GP program on test inputs
|
56 |
test_pairs = []
|
57 |
predicted_test_outputs = []
|
|
|
58 |
for sample in data.get("test", []):
|
59 |
test_input = tolist_safe(sample["input"])
|
60 |
test_output = tolist_safe(sample["output"])
|
61 |
test_pairs.append((test_input, test_output))
|
62 |
try:
|
63 |
+
pred = tolist_safe(best_program.evaluate(test_input))
|
64 |
except Exception as e:
|
65 |
+
print(f"Prediction error: {e}")
|
66 |
+
pred = [["ERROR"]]
|
67 |
+
predicted_test_outputs.append(pred)
|
68 |
+
|
69 |
+
return render_template(
|
70 |
+
"results.html",
|
71 |
+
hlcs=predicted_HLCs,
|
72 |
+
input_output_pairs=input_output_pairs,
|
73 |
+
test_pairs=test_pairs,
|
74 |
+
predicted_test_outputs=predicted_test_outputs,
|
75 |
+
best_program=str(best_program)
|
76 |
+
)
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
if __name__ == '__main__':
|
79 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|
templates/index.html
CHANGED
@@ -2,14 +2,13 @@
|
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
-
<title>ARC Task
|
6 |
-
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
7 |
</head>
|
8 |
<body>
|
9 |
-
<h1>Upload ARC Task</h1>
|
10 |
<form action="/upload" method="post" enctype="multipart/form-data">
|
11 |
<input type="file" name="file" accept=".json" required>
|
12 |
-
<button type="submit">
|
13 |
</form>
|
14 |
</body>
|
15 |
</html>
|
|
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
+
<title>Upload ARC Task</title>
|
|
|
6 |
</head>
|
7 |
<body>
|
8 |
+
<h1>Upload ARC Task File</h1>
|
9 |
<form action="/upload" method="post" enctype="multipart/form-data">
|
10 |
<input type="file" name="file" accept=".json" required>
|
11 |
+
<button type="submit">Upload</button>
|
12 |
</form>
|
13 |
</body>
|
14 |
</html>
|
templates/results.html
CHANGED
@@ -2,180 +2,121 @@
|
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
-
<title>
|
6 |
<style>
|
7 |
-
body {
|
8 |
-
|
9 |
-
|
10 |
-
color: black;
|
11 |
-
text-align: center;
|
12 |
-
margin: 0;
|
13 |
-
padding: 20px;
|
14 |
-
}
|
15 |
-
.grid-container {
|
16 |
-
display: flex;
|
17 |
-
justify-content: center;
|
18 |
-
gap: 40px;
|
19 |
-
flex-wrap: wrap;
|
20 |
-
margin-bottom: 30px;
|
21 |
-
}
|
22 |
-
.grid-box {
|
23 |
-
display: grid;
|
24 |
-
gap: 2px;
|
25 |
-
margin-bottom: 10px;
|
26 |
-
}
|
27 |
.cell {
|
28 |
-
width: 25px;
|
29 |
-
height: 25px;
|
30 |
border: 1px solid #ccc;
|
31 |
}
|
32 |
-
.grid-title {
|
33 |
-
|
34 |
-
margin-bottom: 5px;
|
35 |
-
}
|
36 |
-
.concept-box {
|
37 |
-
display: flex;
|
38 |
-
flex-direction: column;
|
39 |
-
justify-content: center;
|
40 |
-
align-items: center;
|
41 |
-
min-width: 150px;
|
42 |
-
}
|
43 |
-
.concept-title {
|
44 |
-
font-weight: bold;
|
45 |
-
margin-bottom: 5px;
|
46 |
-
}
|
47 |
-
.concept-text {
|
48 |
-
font-size: 16px;
|
49 |
-
padding: 5px 10px;
|
50 |
-
border: 1px solid #888;
|
51 |
-
border-radius: 8px;
|
52 |
-
background-color: #f4f4f4;
|
53 |
-
}
|
54 |
-
pre {
|
55 |
-
background-color: #f5f5f5;
|
56 |
-
padding: 15px;
|
57 |
-
font-family: Consolas, monospace;
|
58 |
-
font-size: 14px;
|
59 |
-
border-radius: 8px;
|
60 |
-
max-width: 900px;
|
61 |
-
margin: auto;
|
62 |
-
white-space: pre-wrap;
|
63 |
-
border: 1px solid #ddd;
|
64 |
-
}
|
65 |
</style>
|
66 |
</head>
|
67 |
<body>
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
container.appendChild(div);
|
108 |
-
});
|
109 |
-
});
|
110 |
-
}
|
111 |
-
|
112 |
-
// TRAINING PAIRS
|
113 |
-
const pairsContainer = document.getElementById("pairs-container");
|
114 |
-
|
115 |
-
inputOutputPairs.forEach((pair, index) => {
|
116 |
-
const container = document.createElement("div");
|
117 |
-
container.className = "grid-container";
|
118 |
-
|
119 |
-
const inputDiv = document.createElement("div");
|
120 |
-
inputDiv.innerHTML = `<div class="grid-title">Input Grid</div>`;
|
121 |
-
const inputGridBox = document.createElement("div");
|
122 |
-
inputGridBox.className = "grid-box";
|
123 |
-
inputDiv.appendChild(inputGridBox);
|
124 |
-
drawGrid(inputGridBox, pair[0]);
|
125 |
-
|
126 |
-
const outputDiv = document.createElement("div");
|
127 |
-
outputDiv.innerHTML = `<div class="grid-title">Output Grid</div>`;
|
128 |
-
const outputGridBox = document.createElement("div");
|
129 |
-
outputGridBox.className = "grid-box";
|
130 |
-
outputDiv.appendChild(outputGridBox);
|
131 |
-
drawGrid(outputGridBox, pair[1]);
|
132 |
-
|
133 |
-
const conceptDiv = document.createElement("div");
|
134 |
-
conceptDiv.className = "concept-box";
|
135 |
-
conceptDiv.innerHTML = `
|
136 |
-
<div class="concept-title">Concept</div>
|
137 |
-
<div class="concept-text">${hlcs[index] || 'N/A'}</div>
|
138 |
-
`;
|
139 |
-
|
140 |
-
container.appendChild(inputDiv);
|
141 |
-
container.appendChild(outputDiv);
|
142 |
-
container.appendChild(conceptDiv);
|
143 |
-
pairsContainer.appendChild(container);
|
144 |
-
});
|
145 |
-
|
146 |
-
const testContainer = document.getElementById("test-pairs-container");
|
147 |
-
|
148 |
-
testPairs.forEach((pair, index) => {
|
149 |
-
const container = document.createElement("div");
|
150 |
-
container.className = "grid-container";
|
151 |
-
|
152 |
-
const inputDiv = document.createElement("div");
|
153 |
-
inputDiv.innerHTML = `<div class="grid-title">Test Input Grid</div>`;
|
154 |
-
const inputGridBox = document.createElement("div");
|
155 |
-
inputGridBox.className = "grid-box";
|
156 |
-
inputDiv.appendChild(inputGridBox);
|
157 |
-
drawGrid(inputGridBox, pair[0]);
|
158 |
-
|
159 |
-
const gtDiv = document.createElement("div");
|
160 |
-
gtDiv.innerHTML = `<div class="grid-title">Ground Truth Output</div>`;
|
161 |
-
const gtGridBox = document.createElement("div");
|
162 |
-
gtGridBox.className = "grid-box";
|
163 |
-
gtDiv.appendChild(gtGridBox);
|
164 |
-
drawGrid(gtGridBox, pair[1]);
|
165 |
-
|
166 |
-
const predDiv = document.createElement("div");
|
167 |
-
predDiv.innerHTML = `<div class="grid-title">Predicted Output</div>`;
|
168 |
-
const predGridBox = document.createElement("div");
|
169 |
-
predGridBox.className = "grid-box";
|
170 |
-
predDiv.appendChild(predGridBox);
|
171 |
-
drawGrid(predGridBox, predictedTestOutputs[index]);
|
172 |
-
|
173 |
-
container.appendChild(inputDiv);
|
174 |
-
container.appendChild(gtDiv);
|
175 |
-
container.appendChild(predDiv);
|
176 |
-
testContainer.appendChild(container);
|
177 |
});
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
</body>
|
181 |
</html>
|
|
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
+
<title>Results</title>
|
6 |
<style>
|
7 |
+
body { font-family: sans-serif; padding: 20px; }
|
8 |
+
.grid-container { display: flex; gap: 30px; flex-wrap: wrap; margin-bottom: 30px; }
|
9 |
+
.grid-box { display: grid; gap: 2px; margin-bottom: 10px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
.cell {
|
11 |
+
width: 25px; height: 25px;
|
|
|
12 |
border: 1px solid #ccc;
|
13 |
}
|
14 |
+
.grid-title { font-weight: bold; margin-bottom: 5px; }
|
15 |
+
pre { background: #f4f4f4; padding: 10px; border-radius: 5px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
</style>
|
17 |
</head>
|
18 |
<body>
|
19 |
|
20 |
+
<h2>Predicted High-Level Concepts</h2>
|
21 |
+
<ul>
|
22 |
+
{% for concept in hlcs %}
|
23 |
+
<li>{{ concept }}</li>
|
24 |
+
{% endfor %}
|
25 |
+
</ul>
|
26 |
+
|
27 |
+
<h2>Training Grids</h2>
|
28 |
+
<div id="train-container"></div>
|
29 |
+
|
30 |
+
<h2>Test Grids</h2>
|
31 |
+
<div id="test-container"></div>
|
32 |
+
|
33 |
+
<h2>Best Program</h2>
|
34 |
+
<pre>{{ best_program }}</pre>
|
35 |
+
|
36 |
+
<script>
|
37 |
+
const inputOutputPairs = {{ input_output_pairs | tojson | safe }};
|
38 |
+
const testPairs = {{ test_pairs | tojson | safe }};
|
39 |
+
const predictedTestOutputs = {{ predicted_test_outputs | tojson | safe }};
|
40 |
+
|
41 |
+
const colorMap = {
|
42 |
+
0: "#000000", 1: "#0074D9", 2: "#FF4136", 3: "#2ECC40",
|
43 |
+
4: "#FFDC00", 5: "#AAAAAA", 6: "#F012BE", 7: "#FF851B",
|
44 |
+
8: "#7FDBFF", 9: "#870C25"
|
45 |
+
};
|
46 |
+
|
47 |
+
function drawGrid(container, gridData) {
|
48 |
+
if (!Array.isArray(gridData)) return;
|
49 |
+
container.style.gridTemplateRows = `repeat(${gridData.length}, 25px)`;
|
50 |
+
container.style.gridTemplateColumns = `repeat(${gridData[0].length}, 25px)`;
|
51 |
+
container.innerHTML = '';
|
52 |
+
gridData.forEach(row => {
|
53 |
+
row.forEach(cell => {
|
54 |
+
const div = document.createElement('div');
|
55 |
+
div.className = "cell";
|
56 |
+
div.style.backgroundColor = colorMap[cell] || "#000";
|
57 |
+
container.appendChild(div);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
});
|
59 |
+
});
|
60 |
+
}
|
61 |
+
|
62 |
+
// Show training pairs
|
63 |
+
const trainContainer = document.getElementById("train-container");
|
64 |
+
inputOutputPairs.forEach((pair, index) => {
|
65 |
+
const container = document.createElement("div");
|
66 |
+
container.className = "grid-container";
|
67 |
+
|
68 |
+
const inputDiv = document.createElement("div");
|
69 |
+
inputDiv.innerHTML = `<div class="grid-title">Train Input</div>`;
|
70 |
+
const inputBox = document.createElement("div");
|
71 |
+
inputBox.className = "grid-box";
|
72 |
+
inputDiv.appendChild(inputBox);
|
73 |
+
drawGrid(inputBox, pair[0]);
|
74 |
+
|
75 |
+
const outputDiv = document.createElement("div");
|
76 |
+
outputDiv.innerHTML = `<div class="grid-title">Train Output</div>`;
|
77 |
+
const outputBox = document.createElement("div");
|
78 |
+
outputBox.className = "grid-box";
|
79 |
+
outputDiv.appendChild(outputBox);
|
80 |
+
drawGrid(outputBox, pair[1]);
|
81 |
+
|
82 |
+
container.appendChild(inputDiv);
|
83 |
+
container.appendChild(outputDiv);
|
84 |
+
trainContainer.appendChild(container);
|
85 |
+
});
|
86 |
+
|
87 |
+
// Show test + prediction pairs
|
88 |
+
const testContainer = document.getElementById("test-container");
|
89 |
+
testPairs.forEach((pair, index) => {
|
90 |
+
const container = document.createElement("div");
|
91 |
+
container.className = "grid-container";
|
92 |
+
|
93 |
+
const inputDiv = document.createElement("div");
|
94 |
+
inputDiv.innerHTML = `<div class="grid-title">Test Input</div>`;
|
95 |
+
const inputBox = document.createElement("div");
|
96 |
+
inputBox.className = "grid-box";
|
97 |
+
inputDiv.appendChild(inputBox);
|
98 |
+
drawGrid(inputBox, pair[0]);
|
99 |
+
|
100 |
+
const gtDiv = document.createElement("div");
|
101 |
+
gtDiv.innerHTML = `<div class="grid-title">Ground Truth</div>`;
|
102 |
+
const gtBox = document.createElement("div");
|
103 |
+
gtBox.className = "grid-box";
|
104 |
+
gtDiv.appendChild(gtBox);
|
105 |
+
drawGrid(gtBox, pair[1]);
|
106 |
+
|
107 |
+
const predDiv = document.createElement("div");
|
108 |
+
predDiv.innerHTML = `<div class="grid-title">Predicted</div>`;
|
109 |
+
const predBox = document.createElement("div");
|
110 |
+
predBox.className = "grid-box";
|
111 |
+
predDiv.appendChild(predBox);
|
112 |
+
drawGrid(predBox, predictedTestOutputs[index]);
|
113 |
+
|
114 |
+
container.appendChild(inputDiv);
|
115 |
+
container.appendChild(gtDiv);
|
116 |
+
container.appendChild(predDiv);
|
117 |
+
testContainer.appendChild(container);
|
118 |
+
});
|
119 |
+
</script>
|
120 |
|
121 |
</body>
|
122 |
</html>
|