Spaces:
Sleeping
Sleeping
use pure gradio with @spaces.GPU marker for ZeroGPU
Browse files- app.py +13 -36
- requirements.txt +1 -3
app.py
CHANGED
|
@@ -1,32 +1,9 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 4 |
-
from starlette.middleware.base import BaseHTTPMiddleware
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
import subprocess
|
| 7 |
import shlex
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
fastapi_app = FastAPI()
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class MaxSizeMiddleware(BaseHTTPMiddleware):
|
| 14 |
-
async def dispatch(self, request, call_next):
|
| 15 |
-
content_length = request.headers.get("content-length")
|
| 16 |
-
if content_length and int(content_length) > 4096:
|
| 17 |
-
return JSONResponse({"error": "payload too large"}, status_code=413)
|
| 18 |
-
return await call_next(request)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
fastapi_app.add_middleware(MaxSizeMiddleware)
|
| 22 |
-
fastapi_app.add_middleware(
|
| 23 |
-
CORSMiddleware,
|
| 24 |
-
allow_origins=["*"],
|
| 25 |
-
allow_credentials=False,
|
| 26 |
-
allow_methods=["*"],
|
| 27 |
-
allow_headers=["*"],
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
SAFE_COMMANDS = {
|
| 31 |
"ls", "pwd", "whoami", "echo", "cat", "date",
|
| 32 |
"uname", "env", "id", "python3", "hostname",
|
|
@@ -38,6 +15,11 @@ ALLOWLIST = {
|
|
| 38 |
}
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def is_allowed(args):
|
| 42 |
base = args[0] if args else ""
|
| 43 |
if base in ALLOWLIST:
|
|
@@ -45,14 +27,7 @@ def is_allowed(args):
|
|
| 45 |
return base in SAFE_COMMANDS
|
| 46 |
|
| 47 |
|
| 48 |
-
|
| 49 |
-
async def health():
|
| 50 |
-
return {"status": "ok"}
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
@fastapi_app.post("/")
|
| 54 |
-
async def exec_command(body: dict):
|
| 55 |
-
command = body.get("command", "")
|
| 56 |
if not command:
|
| 57 |
return ""
|
| 58 |
try:
|
|
@@ -71,13 +46,15 @@ async def exec_command(body: dict):
|
|
| 71 |
timeout=10,
|
| 72 |
cwd=os.environ.get("SPACE_ROOT", "/app"),
|
| 73 |
)
|
| 74 |
-
|
| 75 |
-
return out
|
| 76 |
except subprocess.TimeoutExpired:
|
| 77 |
return "Error: command timed out\n"
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error: {e}\n"
|
| 80 |
|
| 81 |
|
| 82 |
-
demo = gr.Interface(
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
import subprocess
|
| 4 |
import shlex
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
SAFE_COMMANDS = {
|
| 8 |
"ls", "pwd", "whoami", "echo", "cat", "date",
|
| 9 |
"uname", "env", "id", "python3", "hostname",
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
|
| 18 |
+
@spaces.GPU
|
| 19 |
+
def _gpu_marker():
|
| 20 |
+
return "ok"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def is_allowed(args):
|
| 24 |
base = args[0] if args else ""
|
| 25 |
if base in ALLOWLIST:
|
|
|
|
| 27 |
return base in SAFE_COMMANDS
|
| 28 |
|
| 29 |
|
| 30 |
+
def spawn(command: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if not command:
|
| 32 |
return ""
|
| 33 |
try:
|
|
|
|
| 46 |
timeout=10,
|
| 47 |
cwd=os.environ.get("SPACE_ROOT", "/app"),
|
| 48 |
)
|
| 49 |
+
return (result.stdout + result.stderr) or "(no output)"
|
|
|
|
| 50 |
except subprocess.TimeoutExpired:
|
| 51 |
return "Error: command timed out\n"
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {e}\n"
|
| 54 |
|
| 55 |
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn=spawn,
|
| 58 |
+
inputs=gr.Textbox(label="command", lines=1),
|
| 59 |
+
outputs=gr.Textbox(label="output", lines=10),
|
| 60 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,2 @@
|
|
| 1 |
-
fastapi>=0.104.0
|
| 2 |
-
uvicorn>=0.24.0
|
| 3 |
-
pydantic>=2.0.0
|
| 4 |
gradio>=4.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
+
huggingface_hub>=0.20.0
|