Spaces:
Sleeping
Sleeping
| 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 | |
| def index(): | |
| return render_template('index.html') | |
| def submit_json(): | |
| try: | |
| raw_json = request.form.get('json_input') | |
| data = json.loads(raw_json) | |
| except Exception as e: | |
| return f"Invalid JSON input: {e}" | |
| # Collect training data and predict HLCs | |
| 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)) | |
| # GP optimization | |
| best_program, generations = genetic_programming( | |
| input_output_pairs=input_output_pairs, | |
| population_size=200, | |
| generations=200, | |
| mutation_rate=0.2, | |
| crossover_rate=0.7, | |
| max_depth=3, | |
| predicted_HLCs=predicted_HLCs | |
| ) | |
| # Evaluate GP program on test inputs | |
| 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: | |
| pred = tolist_safe(best_program.evaluate(test_input)) | |
| except Exception as e: | |
| print(f"Prediction error: {e}") | |
| pred = [["ERROR"]] | |
| predicted_test_outputs.append(pred) | |
| 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) | |