iryx commited on
Commit
e39a94f
·
1 Parent(s): 7897893

switch to docker sdk: FastAPI + Uvicorn

Browse files
Files changed (4) hide show
  1. Dockerfile +8 -0
  2. README.md +1 -3
  3. app.py +18 -22
  4. requirements.txt +3 -2
Dockerfile ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-bookworm
2
+
3
+ WORKDIR /app
4
+ COPY requirements.txt .
5
+ RUN pip install --no-cache-dir -r requirements.txt
6
+ COPY app.py .
7
+
8
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -3,10 +3,8 @@ 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
 
 
3
  emoji: 🏢
4
  colorFrom: purple
5
  colorTo: red
6
+ sdk: docker
 
7
  python_version: "3.12"
 
8
  pinned: false
9
  ---
10
 
app.py CHANGED
@@ -1,8 +1,13 @@
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",
@@ -15,28 +20,26 @@ ALLOWLIST = {
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:
26
  return args == ALLOWLIST[base]
27
  return base in SAFE_COMMANDS
28
 
29
 
30
- def spawn(command: str):
 
 
31
  if not command:
32
- return ""
 
33
  try:
34
  args = shlex.split(command)
35
  except Exception:
36
- return f"Error: invalid command: {command}\n"
37
 
38
  if not is_allowed(args):
39
- return f"Error: command '{args[0]}' not allowed in demo container\n"
40
 
41
  try:
42
  result = subprocess.run(
@@ -46,15 +49,8 @@ def spawn(command: str):
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
- )
 
 
 
1
  import subprocess
2
  import shlex
3
  import os
4
+ from fastapi import FastAPI
5
+ from pydantic import BaseModel
6
+
7
+ app = FastAPI()
8
+
9
+ class ExecRequest(BaseModel):
10
+ command: str
11
 
12
  SAFE_COMMANDS = {
13
  "ls", "pwd", "whoami", "echo", "cat", "date",
 
20
  }
21
 
22
 
23
+ def is_allowed(args: list[str]) -> bool:
 
 
 
 
 
24
  base = args[0] if args else ""
25
  if base in ALLOWLIST:
26
  return args == ALLOWLIST[base]
27
  return base in SAFE_COMMANDS
28
 
29
 
30
+ @app.post("/")
31
+ def exec_endpoint(req: ExecRequest):
32
+ command = req.command.strip()
33
  if not command:
34
+ return {"output": ""}
35
+
36
  try:
37
  args = shlex.split(command)
38
  except Exception:
39
+ return {"output": f"Error: invalid command: {command}\n"}
40
 
41
  if not is_allowed(args):
42
+ return {"output": f"Error: command '{args[0]}' not allowed in demo container\n"}
43
 
44
  try:
45
  result = subprocess.run(
 
49
  timeout=10,
50
  cwd=os.environ.get("SPACE_ROOT", "/app"),
51
  )
52
+ return {"output": (result.stdout + result.stderr) or "(no output)"}
53
  except subprocess.TimeoutExpired:
54
+ return {"output": "Error: command timed out\n"}
55
  except Exception as e:
56
+ return {"output": f"Error: {e}\n"}
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
- gradio>=4.0.0
2
- huggingface_hub>=0.20.0
 
 
1
+ fastapi>=0.100.0
2
+ uvicorn>=0.23.0
3
+ pydantic>=2.0.0