-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
67 lines (52 loc) · 2.1 KB
/
server.py
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
from flask import Flask, request, jsonify, send_from_directory
import os
import json
from datetime import datetime
import importlib.util
app = Flask(__name__)
@app.route('/run', methods=['POST'])
def run_endpoint():
data = request.json
run_data = data.get('run', {})
timestamp = datetime.now().strftime('%Y%m%d%H%M%S%f')
exp_dir = f"Temp_Experiment/{timestamp}"
os.makedirs(exp_dir, exist_ok=True)
for filename, content in run_data.get('files', {}).items():
with open(os.path.join(exp_dir, filename), 'w') as f:
f.write(content)
spec = importlib.util.spec_from_file_location("run", "run.py")
run_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(run_module)
args = {
"dir": exp_dir,
"spec_prompt_file": run_data.get("spec_prompt_file", "Spec_template.prompt"),
"lang": run_data.get("lang", "JavaScript"),
"method": run_data.get("method", "nl"),
}
run_module.run_with_args(run_module.RunExperimentArgs(**args))
result_file = f"{exp_dir}/{timestamp}.html"
if os.path.exists(result_file):
with open(result_file, 'r') as f:
return jsonify({"html": f.read()})
else:
return jsonify({"error": f"no such file {timestamp}/{timestamp}.html"}), 404
@app.route('/get-benchmark/<path:benchmark_name>')
def get_benchmark(benchmark_name):
benchmark_dir = benchmark_name
if not os.path.isdir(benchmark_dir):
return jsonify({"error": "Benchmark directory not found"}), 404
benchmark_files = {}
for filename in os.listdir(benchmark_dir):
file_path = os.path.join(benchmark_dir, filename)
if os.path.isfile(file_path):
with open(file_path, 'r') as f:
benchmark_files[filename] = f.read()
return jsonify(benchmark_files)
@app.route('/')
def serve_frontend():
return send_from_directory('frontend', 'index.html')
@app.route('/asset/<path:filename>')
def serve_asset(filename):
return send_from_directory('frontend/asset', filename)
if __name__ == '__main__':
app.run(debug = True)