Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +9 -0
- main.py +19 -0
- regex.py +19 -0
Dockerfile
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM ubuntu:latest
|
2 |
+
|
3 |
+
RUN apt-get update
|
4 |
+
RUN apt-get install -y curl && apt-get install -y python3 && apt-get install -y python3-pip
|
5 |
+
RUN curl https://get.modular.com | \ MODULAR_AUTH=mut_e87f7861fb9a4d4aa311afb0491b0398 \ sh -
|
6 |
+
RUN modular install mojo
|
7 |
+
|
8 |
+
COPY . .
|
9 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import subprocess, importlib
|
3 |
+
from tools.regex import find_imports
|
4 |
+
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
@app.post("/code")
|
9 |
+
def run_mojo_code(code:str, filename:str) -> dict[str]:
|
10 |
+
try:
|
11 |
+
imports = find_imports(code)
|
12 |
+
for imported in imports:
|
13 |
+
subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
|
14 |
+
with open(filename, "w") as f:
|
15 |
+
f.write(code)
|
16 |
+
|
17 |
+
return {"sucess":True, "output": subprocess.check_output(["mojo", filename]).decode("utf-8")}, 200
|
18 |
+
except:
|
19 |
+
return {"sucess":False}, 500
|
regex.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
def find_imports(code):
|
4 |
+
pattern = r'Python\.import_module\("([^"]*)"\)'
|
5 |
+
matches = re.findall(pattern, code)
|
6 |
+
return matches
|
7 |
+
|
8 |
+
if __name__ == "__main__":
|
9 |
+
code = '''
|
10 |
+
from python import Python
|
11 |
+
Python.import_module("numpy")
|
12 |
+
Python.import_module("pandas")
|
13 |
+
Python.import_module("matplotlib")
|
14 |
+
|
15 |
+
def main():
|
16 |
+
print("hello world")
|
17 |
+
'''
|
18 |
+
|
19 |
+
print(find_imports(code)) # Output: ['numpy', 'pandas', 'matplotlib']
|