codeBOKER commited on
Commit
fa39d87
·
verified ·
1 Parent(s): 1d30c8b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import os
3
+ import gradio as gr
4
+
5
+ def run_command(cmd):
6
+ if not cmd.strip():
7
+ return "Please enter a valid command."
8
+
9
+ try:
10
+ # Run terminal command and capture output
11
+ result = subprocess.run(
12
+ cmd,
13
+ shell=True,
14
+ capture_output=True,
15
+ text=True,
16
+ timeout=60,
17
+ cwd=os.getcwd()
18
+ )
19
+
20
+ output = result.stdout
21
+ if result.stderr:
22
+ output += f"\n[STDERR]\n{result.stderr}"
23
+
24
+ return output if output else "Command completed with no output."
25
+ except Exception as e:
26
+ return f"Error executing command: {str(e)}"
27
+
28
+ # Build simple Gradio UI
29
+ with gr.Blocks(title="Gradio Terminal") as demo:
30
+ gr.Markdown("## 🛠️ Gradio Terminal Executor")
31
+
32
+ with gr.Row():
33
+ cmd_input = gr.Textbox(
34
+ placeholder="Type command here (e.g., node -v, pip list, or npm install)...",
35
+ label="Command",
36
+ scale=4
37
+ )
38
+ run_btn = gr.Button("Run", scale=1)
39
+
40
+ output_box = gr.Code(label="Terminal Output", language="shell", interactive=False)
41
+
42
+ # Trigger via button click or pressing Enter
43
+ run_btn.click(fn=run_command, inputs=cmd_input, outputs=output_box)
44
+ cmd_input.submit(fn=run_command, inputs=cmd_input, outputs=output_box)
45
+
46
+ if __name__ == "__main__":
47
+ demo.launch()