|
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 |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
iface = gradio_app() |
|
iface.launch() |
|
|