Spaces:
Sleeping
Sleeping
from fastapi import FastAPI | |
from pydantic import BaseModel | |
import subprocess | |
from regex import find_imports | |
class Item(BaseModel): | |
code: str | |
filename: str = '' | |
app = FastAPI() | |
def run_mojo_code(item:Item) -> dict: | |
try: | |
imports = find_imports(item.code) | |
for imported in imports: | |
subprocess.call(["python3", "-m", "pip", "install", imported], shell=True) | |
with open(item.filename, "w") as f: | |
f.write(item.code) | |
return {"sucess":True, "output": subprocess.check_output(["mojo", item.filename]).decode("utf-8")}, 200 | |
except: | |
return {"sucess":False}, 500 |