Spaces:
Running
Running
File size: 686 Bytes
4ec23ab f7cb3f7 7efb37a 4ec23ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from fastapi import FastAPI
import subprocess
import re
def find_imports(code):
pattern = r'Python\.import_module\("([^"]*)"\)'
matches = re.findall(pattern, code)
return matches
app = FastAPI()
@app.post("/code")
def run_mojo_code(code:str, filename:str) -> dict[str]:
try:
imports = find_imports(code)
for imported in imports:
subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
with open(filename, "w") as f:
f.write(code)
return {"sucess":True, "output": subprocess.check_output(["mojo", filename]).decode("utf-8")}, 200
except:
return {"sucess":False}, 500 |