Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,88 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
exec(request.code, local_vars, local_vars)
|
| 39 |
|
| 40 |
output = redirected_output.getvalue()
|
| 41 |
|
| 42 |
-
# Ambil
|
| 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:
|
|
@@ -54,7 +95,8 @@ async def execute_code(request: CodeRequest):
|
|
| 54 |
return ExecutionResult(
|
| 55 |
status="success",
|
| 56 |
output=output,
|
| 57 |
-
files=files_dict
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
except Exception as e:
|
|
@@ -65,14 +107,7 @@ async def execute_code(request: CodeRequest):
|
|
| 65 |
)
|
| 66 |
finally:
|
| 67 |
sys.stdout = old_stdout
|
| 68 |
-
# Bersihkan folder temporary
|
| 69 |
try:
|
| 70 |
-
|
| 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"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import sys
|
| 4 |
import io
|
| 5 |
import traceback
|
| 6 |
import base64
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
import glob
|
| 9 |
+
import tempfile
|
| 10 |
+
import shutil
|
| 11 |
+
import subprocess
|
| 12 |
+
import pkg_resources
|
| 13 |
+
from typing import Optional, List, Dict
|
| 14 |
|
| 15 |
+
app = FastAPI(title="Codexe TMA - AI Code Executor")
|
| 16 |
|
| 17 |
class CodeRequest(BaseModel):
|
| 18 |
code: str
|
| 19 |
timeout: int = 30
|
| 20 |
+
allow_shell: bool = False
|
| 21 |
|
| 22 |
class ExecutionResult(BaseModel):
|
| 23 |
status: str
|
| 24 |
output: str
|
| 25 |
error: Optional[str] = None
|
| 26 |
+
files: dict = {}
|
| 27 |
+
shell_output: Optional[str] = None
|
| 28 |
+
|
| 29 |
+
@app.get("/")
|
| 30 |
+
async def root():
|
| 31 |
+
return {
|
| 32 |
+
"status": "ok",
|
| 33 |
+
"message": "Codexe TMA Ready",
|
| 34 |
+
"endpoints": ["/execute", "/libraries", "/health"]
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# ================== NEW ENDPOINT ==================
|
| 38 |
+
@app.get("/libraries")
|
| 39 |
+
async def list_libraries():
|
| 40 |
+
"""Menampilkan semua library yang terinstall"""
|
| 41 |
+
libraries = []
|
| 42 |
+
try:
|
| 43 |
+
for pkg in pkg_resources.working_set:
|
| 44 |
+
libraries.append({
|
| 45 |
+
"package": pkg.project_name,
|
| 46 |
+
"version": pkg.version
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
# Sort by name
|
| 50 |
+
libraries.sort(key=lambda x: x["package"].lower())
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"status": "success",
|
| 54 |
+
"total": len(libraries),
|
| 55 |
+
"libraries": libraries
|
| 56 |
+
}
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return {"status": "error", "message": str(e)}
|
| 59 |
+
|
| 60 |
+
@app.get("/health")
|
| 61 |
+
async def health():
|
| 62 |
+
return {"status": "healthy"}
|
| 63 |
|
| 64 |
@app.post("/execute")
|
| 65 |
async def execute_code(request: CodeRequest):
|
| 66 |
old_stdout = sys.stdout
|
| 67 |
redirected_output = sys.stdout = io.StringIO()
|
|
|
|
| 68 |
temp_dir = tempfile.mkdtemp(dir="/tmp")
|
| 69 |
+
shell_output = None
|
| 70 |
|
| 71 |
try:
|
|
|
|
| 72 |
local_vars = {
|
| 73 |
"__builtins__": __builtins__,
|
| 74 |
+
"temp_dir": temp_dir,
|
| 75 |
+
"os": os,
|
| 76 |
+
"subprocess": subprocess if request.allow_shell else None,
|
| 77 |
}
|
| 78 |
|
| 79 |
exec(request.code, local_vars, local_vars)
|
| 80 |
|
| 81 |
output = redirected_output.getvalue()
|
| 82 |
|
| 83 |
+
# Ambil file hasil
|
| 84 |
files_dict = {}
|
| 85 |
+
for filepath in glob.glob(f"{temp_dir}/**/*.*", recursive=True):
|
| 86 |
if os.path.isfile(filepath):
|
| 87 |
rel_path = os.path.relpath(filepath, temp_dir)
|
| 88 |
try:
|
|
|
|
| 95 |
return ExecutionResult(
|
| 96 |
status="success",
|
| 97 |
output=output,
|
| 98 |
+
files=files_dict,
|
| 99 |
+
shell_output=shell_output
|
| 100 |
)
|
| 101 |
|
| 102 |
except Exception as e:
|
|
|
|
| 107 |
)
|
| 108 |
finally:
|
| 109 |
sys.stdout = old_stdout
|
|
|
|
| 110 |
try:
|
| 111 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
|
|
|
|
|
|
| 112 |
except:
|
| 113 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|