File size: 1,308 Bytes
1e5e427
d4fa759
96bcc4a
 
1e5e427
 
 
 
 
 
 
 
 
 
 
 
 
96bcc4a
d4fa759
00a5428
 
96bcc4a
00a5428
d4fa759
 
 
 
96bcc4a
0ddab14
00a5428
 
 
 
96bcc4a
 
 
 
d4fa759
1e5e427
00a5428
1e5e427
 
00a5428
1e5e427
 
96bcc4a
00a5428
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
44
45
46
47
48
import os
import subprocess
import gradio as gr

EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
ZH2EN = {
    "输入 Linux 命令": "Linux Command",
    "状态栏": "Status",
    "执行结果": "Command Output",
    "Linux 命令执行测试工具": "Linux Command Executor",
    "输入一个 Linux 命令, 点击提交查看其执行结果": "Enter a Linux command and click submit to see the output.",
}


def _L(zh_txt: str):
    return ZH2EN[zh_txt] if EN_US else zh_txt


def infer(command):
    status = "Success"
    result = None
    try:
        result = subprocess.check_output(
            command,
            shell=True,
            stderr=subprocess.STDOUT,
            text=True,
        )

    except Exception as e:
        status = f"Error: {e.output}"

    return status, result


if __name__ == "__main__":
    gr.Interface(
        fn=infer,
        inputs=gr.Textbox(label=_L("输入 Linux 命令"), value="ls"),
        outputs=[
            gr.Textbox(label=_L("状态栏"), show_copy_button=True),
            gr.TextArea(label=_L("执行结果"), show_copy_button=True),
        ],
        title=_L("Linux 命令执行测试工具"),
        description=_L("输入一个 Linux 命令, 点击提交查看其执行结果"),
        flagging_mode="never",
    ).launch(ssr_mode=False)