Download app.py from codeBOKER/hi: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/codeBOKER/hi/resolve/main/app.py
- Command line
-
hf download hf://spaces/codeBOKER/hi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/codeBOKER/hi/resolve/main/app.py
2.7 kB
| 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(): | |
| # 1. Download & Extract portable Node.js 22 if not present | |
| 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) | |
| # 2. Add Node 22 binary folder to environment PATH | |
| node_bin = os.path.join(NODE_DIR, "bin") | |
| os.environ["PATH"] = f"{node_bin}:{NPM_GLOBAL}/bin:{os.environ['PATH']}" | |
| # 3. Redirect global npm installs to /home/user/.npm-global to bypass permission errors | |
| os.makedirs(NPM_GLOBAL, exist_ok=True) | |
| subprocess.run(["npm", "config", "set", "prefix", NPM_GLOBAL], check=True) | |
| # 4. Install OpenClaw globally in user space | |
| 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) |