Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from gradio_client import Client | |
| import random | |
| import time | |
| import threading | |
| # ------------------------- | |
| # Backend HF clients | |
| # ------------------------- | |
| backend_engines = [ | |
| Client("ChocoLaboratory/SANDBOX_BACKEND"), | |
| Client("ChocoLaboratory/SANDBOXBACKEND2") | |
| ] | |
| SANDBOX_TTL_SEC = 2 * 3600 # 2 hours | |
| # ------------------------- | |
| # Helper functions | |
| # ------------------------- | |
| def pick_engine(): | |
| return random.choice(backend_engines) | |
| def launch_sandbox(code): | |
| engine = pick_engine() | |
| result = engine.predict(code=code, api_name="/launch_sandbox") | |
| sandbox_id = result if isinstance(result, str) else result.get("sandbox_id", "unknown") | |
| start_time = time.time() | |
| return f"Sandbox launched! ID: {sandbox_id}\n", sandbox_id, SANDBOX_TTL_SEC, start_time, engine | |
| def run_command(cmd, sandbox_id, start_time, engine): | |
| if not sandbox_id: | |
| return "No sandbox ID. Launch a sandbox first!", "0:00" | |
| try: | |
| # Run the command on the backend sandbox | |
| engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command endpoint | |
| logs, remaining = fetch_logs_and_timer(sandbox_id, start_time, engine) | |
| return logs, remaining | |
| except Exception as e: | |
| return f"Error: {e}", "0:00" | |
| def fetch_logs_and_timer(sandbox_id, start_time, engine): | |
| try: | |
| logs = engine.predict(api_name="/fetch_logs") | |
| except Exception as e: | |
| logs = f"Error fetching logs: {e}" | |
| elapsed = int(time.time() - start_time) | |
| remaining = max(0, SANDBOX_TTL_SEC - elapsed) | |
| minutes, seconds = divmod(remaining, 60) | |
| time_str = f"{minutes:02d}:{seconds:02d}" | |
| return logs, time_str | |
| def kill_sandbox(sandbox_id, engine): | |
| if not sandbox_id: | |
| return "No sandbox ID to kill." | |
| try: | |
| result = engine.predict(api_name="/kill_sandbox") | |
| return result | |
| except Exception as e: | |
| return f"Error: {e}" | |
| def get_status(sandbox_id, start_time, engine): | |
| if not sandbox_id: | |
| return "No sandbox ID.", "0:00" | |
| try: | |
| status = engine.predict(api_name="/status_sandbox") | |
| elapsed = int(time.time() - start_time) | |
| remaining = max(0, SANDBOX_TTL_SEC - elapsed) | |
| minutes, seconds = divmod(remaining, 60) | |
| time_str = f"{minutes:02d}:{seconds:02d}" | |
| return status, time_str | |
| except Exception as e: | |
| return f"Error: {e}", "0:00" | |
| # ------------------------- | |
| # Gradio UI | |
| # ------------------------- | |
| with gr.Blocks() as demo: | |
| # CSS for terminal | |
| gr.HTML(""" | |
| <style> | |
| .terminal-box textarea { | |
| background-color: #000000; | |
| color: #ffffff; | |
| font-family: monospace; | |
| font-size: 14px; | |
| } | |
| </style> | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| gr.Markdown("## 🖥 Sandbox Terminal (Debian)") | |
| terminal_output = gr.Textbox(label="Terminal Output", lines=20, interactive=False, elem_classes=["terminal-box"]) | |
| command_input = gr.Textbox(label="Enter command", placeholder="print('Hello!')", interactive=True) | |
| run_btn = gr.Button("Run Command") | |
| with gr.Column(scale=1): | |
| launch_btn = gr.Button("Launch Sandbox") | |
| kill_btn = gr.Button("Kill Sandbox") | |
| status_btn = gr.Button("Check Status") | |
| sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False) | |
| time_remaining = gr.Textbox(label="Time Remaining", interactive=False) | |
| gr.Markdown("💡 Note: Running on a Debian-based HF container!") | |
| # State variables | |
| start_time_state = gr.State(value=0) | |
| sandbox_backend_state = gr.State(value=None) | |
| # ------------------------- | |
| # Callbacks | |
| # ------------------------- | |
| launch_btn.click( | |
| launch_sandbox, | |
| inputs=command_input, | |
| outputs=[terminal_output, sandbox_id_box, time_remaining, start_time_state, sandbox_backend_state] | |
| ) | |
| run_btn.click( | |
| run_command, | |
| inputs=[command_input, sandbox_id_box, start_time_state, sandbox_backend_state], | |
| outputs=[terminal_output, time_remaining] | |
| ) | |
| kill_btn.click( | |
| kill_sandbox, | |
| inputs=[sandbox_id_box, sandbox_backend_state], | |
| outputs=terminal_output | |
| ) | |
| status_btn.click( | |
| get_status, | |
| inputs=[sandbox_id_box, start_time_state, sandbox_backend_state], | |
| outputs=[terminal_output, time_remaining] | |
| ) | |
| demo.launch() | |