Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
def execute_command(command):
|
5 |
+
try:
|
6 |
+
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
|
7 |
+
except subprocess.CalledProcessError as e:
|
8 |
+
result = e.output
|
9 |
+
return result
|
10 |
+
|
11 |
+
def bash_terminal(command):
|
12 |
+
output = execute_command(command)
|
13 |
+
return output
|
14 |
+
|
15 |
+
# 定義 Gradio 界面
|
16 |
+
def gradio_app():
|
17 |
+
command_input = gr.Textbox(lines=1, placeholder="輸入指令")
|
18 |
+
execute_button = gr.Button(text="執行指令")
|
19 |
+
|
20 |
+
command_output = gr.Textbox(lines=24, cols=80, placeholder="指令輸出", readonly=True)
|
21 |
+
|
22 |
+
def execute_command_callback():
|
23 |
+
command = command_input.value
|
24 |
+
if command:
|
25 |
+
output = bash_terminal(command)
|
26 |
+
command_output.value = output
|
27 |
+
|
28 |
+
execute_button.onclick(execute_command_callback)
|
29 |
+
|
30 |
+
return gr.Interface(
|
31 |
+
fn=None,
|
32 |
+
inputs=command_input,
|
33 |
+
outputs=[command_output, execute_button],
|
34 |
+
live=True,
|
35 |
+
title="Bash Terminal",
|
36 |
+
description="在線 Bash Terminal,輸入指令並執行。",
|
37 |
+
capture_session=True
|
38 |
+
)
|
39 |
+
|
40 |
+
# 啟動 Gradio 應用程序
|
41 |
+
iface = gradio_app()
|
42 |
+
iface.launch()
|