Spaces:
Sleeping
Sleeping
File size: 2,366 Bytes
f60fef5 3537d48 bd39949 3537d48 bd39949 0c9440e bd39949 3537d48 bd39949 a37b981 3537d48 a37b981 3537d48 a37b981 3537d48 a37b981 3537d48 6fb64b4 3537d48 a37b981 3537d48 a37b981 3537d48 a37b981 6fb64b4 a37b981 6fb64b4 a37b981 0c9440e 6fb64b4 3537d48 0c9440e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
from flask import Flask, request, render_template
import os
import json
import numpy as np
from Vit_concept import run_inference, model
from GP import genetic_programming
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
def tolist_safe(obj):
return obj.tolist() if isinstance(obj, np.ndarray) else obj
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit_json', methods=['POST'])
def submit_json():
json_data = request.form.get('json_data')
if not json_data:
return "No JSON data provided"
try:
data = json.loads(json_data)
except json.JSONDecodeError:
return "Invalid JSON format!"
# Continue processing exactly as before:
input_output_pairs = []
predicted_HLCs = []
for sample in data.get("train", []):
input_grid = sample["input"]
output_grid = sample["output"]
concept_label, _ = run_inference(model, input_grid, output_grid)
predicted_HLCs.append(concept_label)
input_output_pairs.append((tolist_safe(input_grid), tolist_safe(output_grid)))
predicted_HLCs = list(set(predicted_HLCs))
best_program, generations = genetic_programming(
input_output_pairs=input_output_pairs,
population_size=300,
generations=500,
mutation_rate=0.2,
crossover_rate=0.7,
max_depth=3,
predicted_HLCs=predicted_HLCs
)
# Evaluate test pairs (same as before)
test_pairs = []
predicted_test_outputs = []
for sample in data.get("test", []):
test_input = tolist_safe(sample["input"])
test_output = tolist_safe(sample["output"])
test_pairs.append((test_input, test_output))
try:
predicted = tolist_safe(best_program.evaluate(test_input))
except Exception as e:
predicted = [["ERROR"]]
predicted_test_outputs.append(predicted)
return render_template("results.html",
hlcs=predicted_HLCs,
input_output_pairs=input_output_pairs,
test_pairs=test_pairs,
predicted_test_outputs=predicted_test_outputs,
best_program=str(best_program))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=False)
|