File size: 1,204 Bytes
8e264b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
import subprocess

def execute_command(command):
    try:
        result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
    except subprocess.CalledProcessError as e:
        result = e.output
    return result

def bash_terminal(command):
    output = execute_command(command)
    return output

# 定義 Gradio 界面
def gradio_app():
    command_input = gr.Textbox(lines=1, placeholder="輸入指令")
    execute_button = gr.Button(text="執行指令")

    command_output = gr.Textbox(lines=24, cols=80, placeholder="指令輸出", readonly=True)

    def execute_command_callback():
        command = command_input.value
        if command:
            output = bash_terminal(command)
            command_output.value = output

    execute_button.onclick(execute_command_callback)

    return gr.Interface(
        fn=None,
        inputs=command_input,
        outputs=[command_output, execute_button],
        live=True,
        title="Bash Terminal",
        description="在線 Bash Terminal,輸入指令並執行。",
        capture_session=True
    )

# 啟動 Gradio 應用程序
iface = gradio_app()
iface.launch()