Spaces:
Sleeping
Sleeping
Create api/server.py
Browse files- api/server.py +22 -0
api/server.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
@app.post("/run")
|
| 8 |
+
def run_program(program_hex: str):
|
| 9 |
+
with tempfile.NamedTemporaryFile(delete=False) as f:
|
| 10 |
+
f.write(bytes.fromhex(program_hex))
|
| 11 |
+
f.flush()
|
| 12 |
+
|
| 13 |
+
result = subprocess.run(
|
| 14 |
+
["/app/bcn-vm", f.name],
|
| 15 |
+
capture_output=True,
|
| 16 |
+
text=True
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
return {
|
| 20 |
+
"stdout": result.stdout,
|
| 21 |
+
"stderr": result.stderr
|
| 22 |
+
}
|