Upload folder using huggingface_hub
Browse files- Dockerfile +12 -0
- app.py +167 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HuggingFace Space: Code Execution
|
| 3 |
+
Sandboxed Python code execution service
|
| 4 |
+
"""
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
import subprocess
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
+
from typing import Optional
|
| 11 |
+
|
| 12 |
+
app = FastAPI(
|
| 13 |
+
title="Code Execution Space",
|
| 14 |
+
description="Sandboxed Python code execution"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class CodeRequest(BaseModel):
|
| 19 |
+
code: str
|
| 20 |
+
language: str = "python"
|
| 21 |
+
timeout: int = 10 # seconds
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class CodeResponse(BaseModel):
|
| 25 |
+
stdout: str
|
| 26 |
+
stderr: str
|
| 27 |
+
returncode: int
|
| 28 |
+
error: Optional[str] = None
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Allowed modules for security
|
| 32 |
+
ALLOWED_MODULES = {
|
| 33 |
+
"math", "datetime", "json", "re", "random", "collections",
|
| 34 |
+
"itertools", "functools", "string", "statistics", "decimal",
|
| 35 |
+
"fractions", "numbers", "operator", "copy", "pprint",
|
| 36 |
+
"textwrap", "unicodedata", "difflib", "enum", "typing",
|
| 37 |
+
"dataclasses", "abc", "contextlib", "hashlib", "base64"
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def validate_code(code: str) -> Optional[str]:
|
| 42 |
+
"""Basic security validation"""
|
| 43 |
+
|
| 44 |
+
# Blocked patterns
|
| 45 |
+
blocked = [
|
| 46 |
+
"import os", "import sys", "import subprocess",
|
| 47 |
+
"__import__", "eval(", "exec(", "compile(",
|
| 48 |
+
"open(", "file(", "input(",
|
| 49 |
+
"globals(", "locals(", "vars(",
|
| 50 |
+
"__builtins__", "__class__", "__bases__",
|
| 51 |
+
"breakpoint", "exit", "quit"
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
for pattern in blocked:
|
| 55 |
+
if pattern in code:
|
| 56 |
+
return f"Blocked: {pattern} not allowed"
|
| 57 |
+
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@app.get("/")
|
| 62 |
+
async def root():
|
| 63 |
+
return {
|
| 64 |
+
"status": "running",
|
| 65 |
+
"service": "code_execution",
|
| 66 |
+
"allowed_modules": list(ALLOWED_MODULES)
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@app.post("/api/execute", response_model=CodeResponse)
|
| 71 |
+
async def execute_code(request: CodeRequest):
|
| 72 |
+
"""Execute Python code in sandbox"""
|
| 73 |
+
|
| 74 |
+
# Validate
|
| 75 |
+
error = validate_code(request.code)
|
| 76 |
+
if error:
|
| 77 |
+
return CodeResponse(
|
| 78 |
+
stdout="",
|
| 79 |
+
stderr=error,
|
| 80 |
+
returncode=1,
|
| 81 |
+
error=error
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Only Python supported
|
| 85 |
+
if request.language != "python":
|
| 86 |
+
return CodeResponse(
|
| 87 |
+
stdout="",
|
| 88 |
+
stderr=f"Unsupported language: {request.language}",
|
| 89 |
+
returncode=1,
|
| 90 |
+
error="Only Python is supported"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
# Write to temp file
|
| 95 |
+
with tempfile.NamedTemporaryFile(
|
| 96 |
+
mode="w",
|
| 97 |
+
suffix=".py",
|
| 98 |
+
delete=False
|
| 99 |
+
) as f:
|
| 100 |
+
f.write(request.code)
|
| 101 |
+
temp_path = f.name
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
# Execute with resource limits
|
| 105 |
+
result = subprocess.run(
|
| 106 |
+
["python", temp_path],
|
| 107 |
+
capture_output=True,
|
| 108 |
+
text=True,
|
| 109 |
+
timeout=request.timeout,
|
| 110 |
+
cwd=tempfile.gettempdir(),
|
| 111 |
+
env={
|
| 112 |
+
"PATH": os.environ.get("PATH", ""),
|
| 113 |
+
"PYTHONDONTWRITEBYTECODE": "1"
|
| 114 |
+
}
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
return CodeResponse(
|
| 118 |
+
stdout=result.stdout[:10000], # Limit output
|
| 119 |
+
stderr=result.stderr[:10000],
|
| 120 |
+
returncode=result.returncode
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
finally:
|
| 124 |
+
# Clean up
|
| 125 |
+
os.unlink(temp_path)
|
| 126 |
+
|
| 127 |
+
except subprocess.TimeoutExpired:
|
| 128 |
+
return CodeResponse(
|
| 129 |
+
stdout="",
|
| 130 |
+
stderr=f"Execution timeout ({request.timeout}s)",
|
| 131 |
+
returncode=1,
|
| 132 |
+
error="Timeout"
|
| 133 |
+
)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
return CodeResponse(
|
| 136 |
+
stdout="",
|
| 137 |
+
stderr=str(e),
|
| 138 |
+
returncode=1,
|
| 139 |
+
error=str(e)
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# For Gradio interface (HF Spaces requirement)
|
| 144 |
+
def gradio_interface():
|
| 145 |
+
import gradio as gr
|
| 146 |
+
|
| 147 |
+
def execute_wrapper(code):
|
| 148 |
+
response = execute_code(CodeRequest(code=code))
|
| 149 |
+
if response.returncode == 0:
|
| 150 |
+
return response.stdout
|
| 151 |
+
else:
|
| 152 |
+
return f"Error: {response.stderr}"
|
| 153 |
+
|
| 154 |
+
iface = gr.Interface(
|
| 155 |
+
fn=execute_wrapper,
|
| 156 |
+
inputs=gr.Textbox(lines=10, label="Python Code"),
|
| 157 |
+
outputs=gr.Textbox(lines=10, label="Output"),
|
| 158 |
+
title="Code Execution",
|
| 159 |
+
description="Run Python code in a sandbox"
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
return iface
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
if __name__ == "__main__":
|
| 166 |
+
import uvicorn
|
| 167 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.104.0
|
| 2 |
+
uvicorn>=0.24.0
|
| 3 |
+
pydantic>=2.5.0
|
| 4 |
+
gradio>=4.0.0
|