Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,25 +1,25 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
| 2 |
import subprocess
|
| 3 |
from regex import find_imports
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
def index():
|
| 9 |
-
return "Hello World!"
|
| 10 |
|
| 11 |
-
@app.
|
| 12 |
-
def run_mojo_code():
|
| 13 |
-
item = request.get_json()
|
| 14 |
try:
|
| 15 |
-
imports = find_imports(item
|
| 16 |
for imported in imports:
|
| 17 |
subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
|
| 18 |
-
with open(item
|
| 19 |
-
f.write(item
|
| 20 |
|
| 21 |
-
return
|
| 22 |
except:
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
-
app.run(port=7860, debug=True)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
import subprocess
|
| 5 |
from regex import find_imports
|
| 6 |
+
class Item(BaseModel):
|
| 7 |
+
code: str
|
| 8 |
+
filename: str = ''
|
| 9 |
+
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
+
app = FastAPI()
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@app.post("/code")
|
| 15 |
+
def run_mojo_code(item:Item) -> dict:
|
|
|
|
| 16 |
try:
|
| 17 |
+
imports = find_imports(item.code)
|
| 18 |
for imported in imports:
|
| 19 |
subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
|
| 20 |
+
with open(item.filename, "w") as f:
|
| 21 |
+
f.write(item.code)
|
| 22 |
|
| 23 |
+
return {"sucess":True, "output": subprocess.check_output(["mojo", item.filename]).decode("utf-8")}, 200
|
| 24 |
except:
|
| 25 |
+
return {"sucess":False}, 500
|
|
|
|
|
|