Spaces:
Building
Building
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import threading
|
4 |
+
|
5 |
+
# Shared variable for storing command output
|
6 |
+
output = ""
|
7 |
+
output_lock = threading.Lock()
|
8 |
+
|
9 |
+
# Function to execute CLI commands in a separate thread
|
10 |
+
def execute_command(cmd):
|
11 |
+
global output
|
12 |
+
try:
|
13 |
+
# Execute command
|
14 |
+
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
15 |
+
|
16 |
+
# Capture the output and errors
|
17 |
+
stdout, stderr = process.communicate()
|
18 |
+
|
19 |
+
# Update the output string safely using a lock
|
20 |
+
with output_lock:
|
21 |
+
if stdout:
|
22 |
+
output += stdout.decode('utf-8') + '\n'
|
23 |
+
if stderr:
|
24 |
+
output += stderr.decode('utf-8') + '\n'
|
25 |
+
|
26 |
+
except Exception as e:
|
27 |
+
# Handle any exceptions and append to the output
|
28 |
+
with output_lock:
|
29 |
+
output += f"Error: {str(e)}\n"
|
30 |
+
|
31 |
+
# Function to be called by Gradio to run the command
|
32 |
+
def run_command(cmd):
|
33 |
+
global output
|
34 |
+
# Clear previous output
|
35 |
+
output = ""
|
36 |
+
|
37 |
+
# Start a new thread to execute the command
|
38 |
+
command_thread = threading.Thread(target=execute_command, args=(cmd,))
|
39 |
+
command_thread.start()
|
40 |
+
|
41 |
+
# Return immediately while the thread processes the command
|
42 |
+
return "Running command..."
|
43 |
+
|
44 |
+
# Function to return the current output
|
45 |
+
def get_output():
|
46 |
+
global output
|
47 |
+
with output_lock:
|
48 |
+
return output
|
49 |
+
|
50 |
+
# Gradio interface setup
|
51 |
+
with gr.Blocks() as demo:
|
52 |
+
cmd_input = gr.Textbox(label="Command", placeholder="Type your command here...")
|
53 |
+
cmd_output = gr.Textbox(label="Output", placeholder="Command output will appear here...", lines=10)
|
54 |
+
|
55 |
+
run_button = gr.Button("Run Command")
|
56 |
+
output_button = gr.Button("Get Output")
|
57 |
+
|
58 |
+
run_button.click(fn=run_command, inputs=cmd_input, outputs=cmd_output)
|
59 |
+
output_button.click(fn=get_output, inputs=None, outputs=cmd_output)
|
60 |
+
|
61 |
+
# Launch the Gradio interface
|
62 |
+
demo.launch()
|