Spaces:
Runtime error
Runtime error
| import subprocess | |
| import os | |
| # Use container’s home directory to avoid permission errors | |
| WORKDIR = os.path.join(os.environ['HOME'], "workspace") | |
| os.makedirs(WORKDIR, exist_ok=True) | |
| # HTML iframe to embed noVNC viewer | |
| NO_VNC_IFRAME = """ | |
| <iframe src="http://localhost:6080/vnc.html?autoconnect=true&resize=remote" | |
| style="width:100%; height:600px; border:none;"> | |
| </iframe> | |
| """ | |
| def start_desktop(): | |
| return NO_VNC_IFRAME | |
| def run_shell(cmd): | |
| result = subprocess.run( | |
| cmd, | |
| shell=True, | |
| capture_output=True, | |
| text=True, | |
| env={**os.environ, "DISPLAY": ":1"} # Supports GUI apps if needed | |
| ) | |
| return result.stdout + result.stderr | |
| def upload_file(file): | |
| dest_path = os.path.join(WORKDIR, os.path.basename(file.name)) | |
| with open(dest_path, "wb") as f: | |
| f.write(file.read()) | |
| return f"✅ Uploaded to: {dest_path}" | |
| with gr.Blocks(css="@import url('https://cdn.simplecss.org/simple.min.css');") as demo: | |
| gr.Markdown("# 🖥️ Offline Visual Sandbox (no API, no cost)") | |
| with gr.Tab("Desktop"): | |
| launch_btn = gr.Button("Launch Desktop") | |
| desktop_view = gr.HTML() | |
| launch_btn.click(start_desktop, outputs=desktop_view) | |
| with gr.Tab("Shell"): | |
| cmd_input = gr.Textbox(label="Shell Command", placeholder="Try 'ls /home/user/workspace'") | |
| run_btn = gr.Button("Run") | |
| cmd_output = gr.Textbox(label="Command Output", lines=10) | |
| run_btn.click(run_shell, inputs=cmd_input, outputs=cmd_output) | |
| with gr.Tab("Upload"): | |
| file_input = gr.File(label="Upload File") | |
| status_output = gr.Textbox(label="Upload Status") | |
| file_input.change(upload_file, inputs=file_input, outputs=status_output) | |
| demo.launch( | |
| server_name="0.0.0.0",) |