-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
128 lines (101 loc) · 4.08 KB
/
serve.py
File metadata and controls
128 lines (101 loc) · 4.08 KB
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Minimal HTTP wrapper for Weber electrodynamics simulation on Cloud Run."""
import subprocess
import json
import os
import tempfile
import time
from flask import Flask, request, jsonify, send_file
app = Flask(__name__)
RESULTS_DIR = "/tmp/weber_results"
os.makedirs(RESULTS_DIR, exist_ok=True)
@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "ok", "service": "weber-electrodynamics"})
@app.route("/", methods=["GET"])
def index():
return jsonify({
"service": "weber-electrodynamics",
"description": "Weber bracket deviation in spinning toroidal geometry",
"endpoints": {
"GET /health": "Health check",
"GET /reference": "Reference CSV data (pre-computed)",
"POST /run": "Run simulation (accepts JSON config)",
"GET /results": "List completed runs",
"GET /results/<run_id>": "Download CSV for a run",
},
})
@app.route("/reference", methods=["GET"])
def reference():
csv_path = os.path.join("/app/data", "brake_recoil_gpu_single.csv")
if os.path.exists(csv_path):
return send_file(csv_path, mimetype="text/csv",
download_name="brake_recoil_gpu_single.csv")
return jsonify({"error": "reference data not found"}), 404
@app.route("/run", methods=["POST"])
def run_simulation():
"""Run the brake-recoil simulation. Accepts optional JSON config."""
body = request.get_json(silent=True) or {}
binary = body.get("binary", "brake-recoil")
args = body.get("args", [])
allowed = ["brake-recoil", "weber-anomaly", "debug-weber",
"field-residual", "shake-test"]
if binary not in allowed:
return jsonify({"error": f"unknown binary: {binary}",
"allowed": allowed}), 400
run_id = f"{int(time.time())}_{binary}"
run_dir = os.path.join(RESULTS_DIR, run_id)
os.makedirs(run_dir, exist_ok=True)
# If config JSON provided, write it to a temp file
config_path = None
cmd_args = list(args)
if "config" in body:
config_path = os.path.join(run_dir, "config.json")
with open(config_path, "w") as f:
json.dump(body["config"], f)
cmd_args = ["--config", config_path] + cmd_args
cmd = [f"/usr/local/bin/{binary}"] + cmd_args
try:
result = subprocess.run(
cmd, capture_output=True, text=True,
timeout=3500, cwd=run_dir
)
# Collect any CSV files produced
csvs = [f for f in os.listdir(run_dir) if f.endswith(".csv")]
return jsonify({
"run_id": run_id,
"binary": binary,
"args": cmd_args,
"returncode": result.returncode,
"stderr": result.stderr[-5000:] if result.stderr else "",
"csv_files": csvs,
})
except subprocess.TimeoutExpired:
return jsonify({"error": "simulation timed out (3500s limit)",
"run_id": run_id}), 504
except Exception as e:
return jsonify({"error": str(e), "run_id": run_id}), 500
@app.route("/results", methods=["GET"])
def list_results():
if not os.path.exists(RESULTS_DIR):
return jsonify({"runs": []})
runs = []
for d in sorted(os.listdir(RESULTS_DIR)):
run_dir = os.path.join(RESULTS_DIR, d)
if os.path.isdir(run_dir):
csvs = [f for f in os.listdir(run_dir) if f.endswith(".csv")]
runs.append({"run_id": d, "csv_files": csvs})
return jsonify({"runs": runs})
@app.route("/results/<run_id>", methods=["GET"])
def get_result(run_id):
run_dir = os.path.join(RESULTS_DIR, run_id)
if not os.path.isdir(run_dir):
return jsonify({"error": "run not found"}), 404
csvs = [f for f in os.listdir(run_dir) if f.endswith(".csv")]
if not csvs:
return jsonify({"error": "no CSV output", "run_id": run_id}), 404
# Return first CSV
return send_file(os.path.join(run_dir, csvs[0]), mimetype="text/csv",
download_name=csvs[0])
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
app.run(host="0.0.0.0", port=port)