diamond-in commited on
Commit
c931a56
Β·
verified Β·
1 Parent(s): 95cb7b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -34
app.py CHANGED
@@ -1,34 +1,15 @@
1
  import gradio as gr
2
  import subprocess
3
- from fastapi import FastAPI
4
- from pydantic import BaseModel
5
 
6
- # Create FastAPI backend
7
- fastapi_app = FastAPI(title="MCP Linux Command Runner")
 
8
 
9
- class Command(BaseModel):
10
- cmd: str
11
-
12
- @fastapi_app.post("/run")
13
- def run_command(command: Command):
14
- try:
15
- result = subprocess.run(
16
- command.cmd,
17
- shell=True,
18
- capture_output=True,
19
- text=True,
20
- timeout=10
21
- )
22
- return {
23
- "stdout": result.stdout,
24
- "stderr": result.stderr,
25
- "returncode": result.returncode
26
- }
27
- except Exception as e:
28
- return {"error": str(e)}
29
-
30
- # Gradio UI
31
- def run_cmd_ui(cmd):
32
  try:
33
  result = subprocess.run(
34
  cmd,
@@ -37,17 +18,20 @@ def run_cmd_ui(cmd):
37
  text=True,
38
  timeout=10
39
  )
40
- return result.stdout if result.stdout else result.stderr
 
41
  except Exception as e:
42
- return str(e)
43
 
 
44
  demo = gr.Interface(
45
- fn=run_cmd_ui,
46
  inputs=gr.Textbox(label="Enter Linux Command"),
47
  outputs=gr.Textbox(label="Output", lines=15),
48
- title="Linux MCP Runner",
49
- description="Run Linux commands inside Hugging Face Space (sandboxed)"
50
  )
51
 
52
- # Mount FastAPI inside Gradio app (so HF initializes Gradio correctly)
53
- demo = gr.mount_gradio_app(fastapi_app, demo, path="/")
 
 
1
  import gradio as gr
2
  import subprocess
 
 
3
 
4
+ def run_command(cmd: str) -> str:
5
+ """
6
+ Run a Linux command and return stdout or stderr.
7
 
8
+ Args:
9
+ cmd (str): Linux command
10
+ Returns:
11
+ str: Command output (stdout or stderr)
12
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  try:
14
  result = subprocess.run(
15
  cmd,
 
18
  text=True,
19
  timeout=10
20
  )
21
+ output = result.stdout if result.stdout else result.stderr
22
+ return output.strip()
23
  except Exception as e:
24
+ return f"Error: {str(e)}"
25
 
26
+ # Gradio interface
27
  demo = gr.Interface(
28
+ fn=run_command,
29
  inputs=gr.Textbox(label="Enter Linux Command"),
30
  outputs=gr.Textbox(label="Output", lines=15),
31
+ title="MCP Linux Shell",
32
+ description="Run Linux commands inside a Hugging Face Space (MCP-compatible)"
33
  )
34
 
35
+ if __name__ == "__main__":
36
+ # 🚨 Key line: enable MCP server
37
+ demo.launch(mcp_server=True)