| import os |
| import sys |
| import shutil |
| import subprocess |
| import urllib.request |
|
|
| NODE_DIR = "/home/user/node22" |
| NPM_GLOBAL = "/home/user/.npm-global" |
|
|
| def setup_openclaw(): |
| |
| if not os.path.exists(NODE_DIR): |
| print("Downloading Node.js v22...") |
| node_url = "https://nodejs.org/dist/v22.14.0/node-v22.14.0-linux-x64.tar.xz" |
| tar_path = "/home/user/node22.tar.xz" |
| |
| urllib.request.urlretrieve(node_url, tar_path) |
| |
| print("Extracting Node.js v22...") |
| os.makedirs(NODE_DIR, exist_ok=True) |
| subprocess.run(["tar", "-xJf", tar_path, "-C", NODE_DIR, "--strip-components=1"], check=True) |
| os.remove(tar_path) |
|
|
| |
| node_bin = os.path.join(NODE_DIR, "bin") |
| os.environ["PATH"] = f"{node_bin}:{NPM_GLOBAL}/bin:{os.environ['PATH']}" |
|
|
| |
| os.makedirs(NPM_GLOBAL, exist_ok=True) |
| subprocess.run(["npm", "config", "set", "prefix", NPM_GLOBAL], check=True) |
|
|
| |
| if not shutil.which("openclaw"): |
| print("Installing OpenClaw globally under user directory...") |
| subprocess.run(["npm", "install", "-g", "openclaw@latest"], check=True) |
| print("OpenClaw installed successfully!") |
|
|
| setup_openclaw() |
|
|
| import gradio as gr |
|
|
| def run_command(cmd): |
| if not cmd.strip(): |
| return "Please enter a valid command." |
| try: |
| result = subprocess.run( |
| cmd, |
| shell=True, |
| capture_output=True, |
| text=True, |
| timeout=60, |
| cwd=os.getcwd(), |
| env=os.environ |
| ) |
| output = result.stdout |
| if result.stderr: |
| output += f"\n[STDERR]\n{result.stderr}" |
| return output if output else "Command completed with no output." |
| except Exception as e: |
| return f"Error executing command: {str(e)}" |
|
|
| with gr.Blocks(title="Gradio Terminal") as demo: |
| gr.Markdown("## 🛠️ Gradio Terminal Executor (Node 22 + OpenClaw)") |
| |
| with gr.Row(): |
| cmd_input = gr.Textbox(placeholder="Type command here (e.g., openclaw --version)...", label="Command", scale=4) |
| run_btn = gr.Button("Run", scale=1) |
| |
| output_box = gr.Code(label="Terminal Output", language="shell", interactive=False) |
|
|
| run_btn.click(fn=run_command, inputs=cmd_input, outputs=output_box) |
| cmd_input.submit(fn=run_command, inputs=cmd_input, outputs=output_box) |
|
|
| if __name__ == "__main__": |
| demo.launch(ssr_mode=False, server_name="0.0.0.0", server_port=7860) |