Spaces:
Running
Running
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) | |