Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
return str(e)
|
| 43 |
|
|
|
|
| 44 |
demo = gr.Interface(
|
| 45 |
-
fn=
|
| 46 |
inputs=gr.Textbox(label="Enter Linux Command"),
|
| 47 |
outputs=gr.Textbox(label="Output", lines=15),
|
| 48 |
-
title="Linux
|
| 49 |
-
description="Run Linux commands inside Hugging Face Space (
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
| 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)
|