-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
31 lines (23 loc) · 937 Bytes
/
api.py
File metadata and controls
31 lines (23 loc) · 937 Bytes
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
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import subprocess
import os
app = FastAPI()
@app.post("/run")
async def run_mallet(request: Request):
data = await request.json()
command = data.get("command")
args = data.get("args", {})
if not command:
return JSONResponse(content={"error": "No command specified"}, status_code=400)
cmd = ["mallet", command]
for key, value in args.items():
if isinstance(value, bool) and value is not None:
cmd.append(f"--{key}")
elif value is not None:
cmd.extend([f"--{key}", str(value)])
try:
subprocess.run(cmd, check=True)
return JSONResponse(content= {'status':'success', 'cmd': cmd})
except subprocess.CalledProcessError as e:
return JSONResponse(content= {'status':'error', 'cmd': cmd, 'details': str(e)}, status_code=500)