Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +26 -0
- app.py +78 -0
- requirements.txt +21 -0
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
gcc \
|
| 7 |
+
libgl1-mesa-glx \
|
| 8 |
+
libglib2.0-0 \
|
| 9 |
+
libsm6 \
|
| 10 |
+
libxext6 \
|
| 11 |
+
libxrender-dev \
|
| 12 |
+
libfontconfig1 \
|
| 13 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
+
|
| 15 |
+
COPY requirements.txt .
|
| 16 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
+
|
| 18 |
+
COPY app.py .
|
| 19 |
+
|
| 20 |
+
ENV MPLCONFIGDIR=/tmp/matplotlib
|
| 21 |
+
ENV TMPDIR=/tmp
|
| 22 |
+
ENV PYTHONUNBUFFERED=1
|
| 23 |
+
|
| 24 |
+
EXPOSE 7860
|
| 25 |
+
|
| 26 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import sys
|
| 4 |
+
import io
|
| 5 |
+
import traceback
|
| 6 |
+
import base64
|
| 7 |
+
import os
|
| 8 |
+
from typing import Dict, Any, Optional
|
| 9 |
+
import tempfile
|
| 10 |
+
import glob
|
| 11 |
+
|
| 12 |
+
app = FastAPI(title="AI Code Executor")
|
| 13 |
+
|
| 14 |
+
class CodeRequest(BaseModel):
|
| 15 |
+
code: str
|
| 16 |
+
timeout: int = 30
|
| 17 |
+
|
| 18 |
+
class ExecutionResult(BaseModel):
|
| 19 |
+
status: str
|
| 20 |
+
output: str
|
| 21 |
+
error: Optional[str] = None
|
| 22 |
+
files: Dict[str, str] = {} # filename: base64
|
| 23 |
+
|
| 24 |
+
@app.post("/execute")
|
| 25 |
+
async def execute_code(request: CodeRequest):
|
| 26 |
+
old_stdout = sys.stdout
|
| 27 |
+
redirected_output = sys.stdout = io.StringIO()
|
| 28 |
+
|
| 29 |
+
temp_dir = tempfile.mkdtemp(dir="/tmp")
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# Environment yang lebih aman
|
| 33 |
+
local_vars = {
|
| 34 |
+
"__builtins__": __builtins__,
|
| 35 |
+
"temp_dir": temp_dir, # biar user bisa simpan file ke temp_dir
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
exec(request.code, local_vars, local_vars)
|
| 39 |
+
|
| 40 |
+
output = redirected_output.getvalue()
|
| 41 |
+
|
| 42 |
+
# Ambil semua file yang dibuat di temp_dir
|
| 43 |
+
files_dict = {}
|
| 44 |
+
for filepath in glob.glob(f"{temp_dir}/**/*", recursive=True):
|
| 45 |
+
if os.path.isfile(filepath):
|
| 46 |
+
rel_path = os.path.relpath(filepath, temp_dir)
|
| 47 |
+
try:
|
| 48 |
+
with open(filepath, "rb") as f:
|
| 49 |
+
b64 = base64.b64encode(f.read()).decode('utf-8')
|
| 50 |
+
files_dict[rel_path] = b64
|
| 51 |
+
except:
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
return ExecutionResult(
|
| 55 |
+
status="success",
|
| 56 |
+
output=output,
|
| 57 |
+
files=files_dict
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return ExecutionResult(
|
| 62 |
+
status="error",
|
| 63 |
+
output=redirected_output.getvalue(),
|
| 64 |
+
error=traceback.format_exc()
|
| 65 |
+
)
|
| 66 |
+
finally:
|
| 67 |
+
sys.stdout = old_stdout
|
| 68 |
+
# Bersihkan folder temporary
|
| 69 |
+
try:
|
| 70 |
+
for f in glob.glob(f"{temp_dir}/**/*", recursive=True):
|
| 71 |
+
os.remove(f)
|
| 72 |
+
os.rmdir(temp_dir)
|
| 73 |
+
except:
|
| 74 |
+
pass
|
| 75 |
+
|
| 76 |
+
@app.get("/")
|
| 77 |
+
async def root():
|
| 78 |
+
return {"status": "ready", "message": "AI Code Executor is running"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
numpy
|
| 4 |
+
pandas
|
| 5 |
+
scipy
|
| 6 |
+
sympy
|
| 7 |
+
scikit-learn
|
| 8 |
+
matplotlib
|
| 9 |
+
seaborn
|
| 10 |
+
plotly
|
| 11 |
+
pillow
|
| 12 |
+
python-pptx
|
| 13 |
+
reportlab
|
| 14 |
+
fpdf2
|
| 15 |
+
trimesh
|
| 16 |
+
pygltflib
|
| 17 |
+
requests
|
| 18 |
+
beautifulsoup4
|
| 19 |
+
openpyxl
|
| 20 |
+
python-multipart
|
| 21 |
+
pydantic
|