File size: 1,055 Bytes
8e264b0
 
 
 
 
8639f49
8e264b0
 
 
 
 
 
8639f49
 
 
 
 
 
 
50fb79c
 
8639f49
 
 
 
 
 
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
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 gradio_app():
    # 定義一個函數來處理指令的執行
    def run_command(command):
        output = execute_command(command)
        return output

    # 創建 Gradio 界面
    with gr.Blocks() as demo:
        command_input = gr.Textbox(label="輸入指令", placeholder="輸入你要執行的 Bash 指令", lines=1)
        command_output = gr.Textbox(label="指令輸出", placeholder="指令的輸出結果", lines=24, interactive=False)
        
        # 設置按鈕和回調函數
        execute_button = gr.Button("執行指令")
        execute_button.click(fn=run_command, inputs=command_input, outputs=command_output)
    
    return demo

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