iryx commited on
Commit
78ad01c
·
1 Parent(s): 5096473

add container agent app

Browse files
Files changed (3) hide show
  1. README.md +0 -10
  2. app.py +89 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,3 @@
1
- ---
2
  title: Codexa Container Agent
3
- emoji: 🏢
4
- colorFrom: purple
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 6.20.0
8
- python_version: '3.12'
9
  app_file: app.py
10
- pinned: false
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  title: Codexa Container Agent
 
 
 
2
  sdk: gradio
 
 
3
  app_file: app.py
 
 
 
 
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
33
+ "true", "false", "sleep", "seq",
34
+ }
35
+
36
+ ALLOWLIST = {
37
+ "python3": ["python3", "--version"],
38
+ }
39
+
40
+
41
+ def is_allowed(args):
42
+ base = args[0] if args else ""
43
+ if base in ALLOWLIST:
44
+ return args == ALLOWLIST[base]
45
+ return base in SAFE_COMMANDS
46
+
47
+
48
+ @fastapi_app.get("/healthz")
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:
59
+ args = shlex.split(command)
60
+ except Exception:
61
+ return f"Error: invalid command: {command}\n"
62
+
63
+ if not is_allowed(args):
64
+ return f"Error: command '{args[0]}' not allowed in demo container\n"
65
+
66
+ try:
67
+ result = subprocess.run(
68
+ args,
69
+ capture_output=True,
70
+ text=True,
71
+ timeout=10,
72
+ cwd=os.environ.get("SPACE_ROOT", "/app"),
73
+ )
74
+ out = result.stdout + result.stderr
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(lambda x: "Container agent ready", "text", "text")
83
+ app = gr.mount_gradio_app(fastapi_app, demo, path="/demo")
84
+
85
+
86
+ if __name__ == "__main__":
87
+ import uvicorn
88
+ port = int(os.environ.get("PORT", 7860))
89
+ uvicorn.run(app, host="0.0.0.0", port=port)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi>=0.104.0
2
+ uvicorn>=0.24.0
3
+ pydantic>=2.0.0
4
+ gradio>=4.0.0