Spaces:
Paused
Paused
Upload 2 files
Browse files- Dockerfile (1).txt +27 -0
- app (1).py +131 -0
Dockerfile (1).txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# System dependencies yang sudah diperbaiki
|
| 6 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 7 |
+
gcc \
|
| 8 |
+
libgl1 \
|
| 9 |
+
libglib2.0-0 \
|
| 10 |
+
libsm6 \
|
| 11 |
+
libxext6 \
|
| 12 |
+
libxrender-dev \
|
| 13 |
+
libfontconfig1 \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
+
|
| 16 |
+
COPY requirements.txt .
|
| 17 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
+
|
| 19 |
+
COPY app.py .
|
| 20 |
+
|
| 21 |
+
ENV MPLCONFIGDIR=/tmp/matplotlib
|
| 22 |
+
ENV TMPDIR=/tmp
|
| 23 |
+
ENV PYTHONUNBUFFERED=1
|
| 24 |
+
|
| 25 |
+
EXPOSE 7860
|
| 26 |
+
|
| 27 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app (1).py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import sys
|
| 5 |
+
import io
|
| 6 |
+
import traceback
|
| 7 |
+
import base64
|
| 8 |
+
import os
|
| 9 |
+
import glob
|
| 10 |
+
import tempfile
|
| 11 |
+
import shutil
|
| 12 |
+
import time
|
| 13 |
+
import asyncio
|
| 14 |
+
from typing import Optional, List
|
| 15 |
+
|
| 16 |
+
app = FastAPI(title="Codexe TMA - AI Code Executor")
|
| 17 |
+
|
| 18 |
+
# ====================== CONFIG ======================
|
| 19 |
+
FILES_DIR = "/app/files"
|
| 20 |
+
os.makedirs(FILES_DIR, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
# Mount folder files agar bisa diakses publik
|
| 23 |
+
app.mount("/files", StaticFiles(directory=FILES_DIR), name="files")
|
| 24 |
+
|
| 25 |
+
class ExecutionResult(BaseModel):
|
| 26 |
+
status: str
|
| 27 |
+
output: str
|
| 28 |
+
error: Optional[str] = None
|
| 29 |
+
files: List[dict] = [] # [{"filename": , "url": , "size": }]
|
| 30 |
+
|
| 31 |
+
def cleanup_old_files(max_age=300): # 5 menit
|
| 32 |
+
now = time.time()
|
| 33 |
+
for f in os.listdir(FILES_DIR):
|
| 34 |
+
filepath = os.path.join(FILES_DIR, f)
|
| 35 |
+
if os.path.isfile(filepath) and (now - os.path.getmtime(filepath) > max_age):
|
| 36 |
+
try:
|
| 37 |
+
os.remove(filepath)
|
| 38 |
+
except:
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
@app.get("/")
|
| 42 |
+
async def root():
|
| 43 |
+
return {
|
| 44 |
+
"status": "ok",
|
| 45 |
+
"message": "Codexe TMA Production Ready",
|
| 46 |
+
"file_url_base": "https://satyadev13-codexe-tma.hf.space/files/"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
@app.get("/libraries")
|
| 50 |
+
async def list_libraries():
|
| 51 |
+
import pkg_resources
|
| 52 |
+
libs = sorted([{"package": pkg.project_name, "version": pkg.version}
|
| 53 |
+
for pkg in pkg_resources.working_set],
|
| 54 |
+
key=lambda x: x["package"].lower())
|
| 55 |
+
return {"total": len(libs), "libraries": libs}
|
| 56 |
+
|
| 57 |
+
@app.post("/execute")
|
| 58 |
+
async def execute_code(
|
| 59 |
+
code: str = Form(...),
|
| 60 |
+
timeout: int = Form(30),
|
| 61 |
+
file: UploadFile = File(None)
|
| 62 |
+
):
|
| 63 |
+
await asyncio.to_thread(cleanup_old_files)
|
| 64 |
+
temp_dir = tempfile.mkdtemp(dir="/tmp")
|
| 65 |
+
old_stdout = sys.stdout
|
| 66 |
+
redirected_output = sys.stdout = io.StringIO()
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
# Simpan uploaded file
|
| 70 |
+
uploaded_path = None
|
| 71 |
+
if file:
|
| 72 |
+
uploaded_path = os.path.join(temp_dir, file.filename)
|
| 73 |
+
with open(uploaded_path, "wb") as f:
|
| 74 |
+
f.write(await file.read())
|
| 75 |
+
|
| 76 |
+
local_vars = {
|
| 77 |
+
"__builtins__": __builtins__,
|
| 78 |
+
"temp_dir": temp_dir,
|
| 79 |
+
"files_dir": FILES_DIR,
|
| 80 |
+
"uploaded_file": uploaded_path
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
# Jalankan kode user
|
| 84 |
+
exec(code, local_vars, local_vars)
|
| 85 |
+
|
| 86 |
+
output = redirected_output.getvalue()
|
| 87 |
+
|
| 88 |
+
# Auto detect semua file di temp_dir dan files_dir
|
| 89 |
+
generated_files = []
|
| 90 |
+
|
| 91 |
+
# Scan di temp_dir (hasil kode)
|
| 92 |
+
for filepath in glob.glob(f"{temp_dir}/**/*.*", recursive=True):
|
| 93 |
+
if os.path.isfile(filepath):
|
| 94 |
+
filename = os.path.basename(filepath)
|
| 95 |
+
dest_path = os.path.join(FILES_DIR, filename)
|
| 96 |
+
shutil.copy2(filepath, dest_path)
|
| 97 |
+
|
| 98 |
+
public_url = f"https://satyadev13-codexe-tma.hf.space/files/{filename}"
|
| 99 |
+
generated_files.append({
|
| 100 |
+
"filename": filename,
|
| 101 |
+
"url": public_url,
|
| 102 |
+
"size": os.path.getsize(dest_path)
|
| 103 |
+
})
|
| 104 |
+
|
| 105 |
+
# Scan juga di FILES_DIR (jika user simpan langsung ke files_dir)
|
| 106 |
+
for filepath in glob.glob(f"{FILES_DIR}/*.*"):
|
| 107 |
+
if os.path.isfile(filepath):
|
| 108 |
+
filename = os.path.basename(filepath)
|
| 109 |
+
if not any(f["filename"] == filename for f in generated_files):
|
| 110 |
+
public_url = f"https://satyadev13-codexe-tma.hf.space/files/{filename}"
|
| 111 |
+
generated_files.append({
|
| 112 |
+
"filename": filename,
|
| 113 |
+
"url": public_url,
|
| 114 |
+
"size": os.path.getsize(filepath)
|
| 115 |
+
})
|
| 116 |
+
|
| 117 |
+
return ExecutionResult(
|
| 118 |
+
status="success",
|
| 119 |
+
output=output,
|
| 120 |
+
files=generated_files
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return ExecutionResult(
|
| 125 |
+
status="error",
|
| 126 |
+
output=redirected_output.getvalue(),
|
| 127 |
+
error=traceback.format_exc()
|
| 128 |
+
)
|
| 129 |
+
finally:
|
| 130 |
+
sys.stdout = old_stdout
|
| 131 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|