Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
# Backend HF clients
|
|
@@ -11,30 +12,47 @@ backend_engines = [
|
|
| 11 |
Client("ChocoLaboratory/SANDBOXBACKEND2")
|
| 12 |
]
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# -------------------------
|
| 15 |
# Helper functions
|
| 16 |
# -------------------------
|
| 17 |
def pick_engine():
|
| 18 |
-
# Random backend selection (simple load balancing)
|
| 19 |
return random.choice(backend_engines)
|
| 20 |
|
| 21 |
def launch_sandbox(code):
|
| 22 |
engine = pick_engine()
|
| 23 |
result = engine.predict(code=code, api_name="/launch_sandbox")
|
| 24 |
sandbox_id = result if isinstance(result, str) else result.get("sandbox_id", "unknown")
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
def
|
| 28 |
if not sandbox_id:
|
| 29 |
-
return "No sandbox ID.
|
| 30 |
engine = pick_engine()
|
| 31 |
-
# For simplicity, run command as a mini Python script
|
| 32 |
try:
|
| 33 |
-
engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command endpoint
|
| 34 |
logs = engine.predict(api_name="/fetch_logs")
|
| 35 |
-
return logs
|
| 36 |
except Exception as e:
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def kill_sandbox(sandbox_id):
|
| 40 |
if not sandbox_id:
|
|
@@ -46,20 +64,24 @@ def kill_sandbox(sandbox_id):
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error: {e}"
|
| 48 |
|
| 49 |
-
def get_status(sandbox_id):
|
| 50 |
if not sandbox_id:
|
| 51 |
-
return "No sandbox ID."
|
| 52 |
engine = pick_engine()
|
| 53 |
try:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
-
return f"Error: {e}"
|
| 57 |
|
| 58 |
# -------------------------
|
| 59 |
# Gradio UI
|
| 60 |
# -------------------------
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
# Global CSS for terminal style (white-on-black)
|
| 63 |
gr.HTML("""
|
| 64 |
<style>
|
| 65 |
.terminal-box textarea {
|
|
@@ -83,12 +105,15 @@ with gr.Blocks() as demo:
|
|
| 83 |
status_btn = gr.Button("Check Status")
|
| 84 |
sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
|
| 85 |
time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
|
| 86 |
-
gr.Markdown("💡 Note: Running on a Debian-based
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Callbacks
|
| 89 |
-
launch_btn.click(launch_sandbox, inputs=command_input, outputs=[terminal_output, sandbox_id_box, time_remaining])
|
| 90 |
-
run_btn.click(run_command, inputs=[command_input, sandbox_id_box], outputs=terminal_output)
|
|
|
|
| 91 |
kill_btn.click(kill_sandbox, inputs=sandbox_id_box, outputs=terminal_output)
|
| 92 |
-
status_btn.click(get_status, inputs=sandbox_id_box, outputs=terminal_output)
|
| 93 |
|
| 94 |
demo.launch()
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
+
import threading
|
| 6 |
|
| 7 |
# -------------------------
|
| 8 |
# Backend HF clients
|
|
|
|
| 12 |
Client("ChocoLaboratory/SANDBOXBACKEND2")
|
| 13 |
]
|
| 14 |
|
| 15 |
+
SANDBOX_TTL_SEC = 2 * 3600 # 2 hours
|
| 16 |
+
|
| 17 |
# -------------------------
|
| 18 |
# Helper functions
|
| 19 |
# -------------------------
|
| 20 |
def pick_engine():
|
|
|
|
| 21 |
return random.choice(backend_engines)
|
| 22 |
|
| 23 |
def launch_sandbox(code):
|
| 24 |
engine = pick_engine()
|
| 25 |
result = engine.predict(code=code, api_name="/launch_sandbox")
|
| 26 |
sandbox_id = result if isinstance(result, str) else result.get("sandbox_id", "unknown")
|
| 27 |
+
start_time = time.time()
|
| 28 |
+
return f"Sandbox launched! ID: {sandbox_id}\n", sandbox_id, SANDBOX_TTL_SEC, start_time
|
| 29 |
|
| 30 |
+
def fetch_logs_and_timer(sandbox_id, start_time):
|
| 31 |
if not sandbox_id:
|
| 32 |
+
return "No sandbox ID.", "0:00"
|
| 33 |
engine = pick_engine()
|
|
|
|
| 34 |
try:
|
|
|
|
| 35 |
logs = engine.predict(api_name="/fetch_logs")
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
+
logs = f"Error fetching logs: {e}"
|
| 38 |
+
elapsed = int(time.time() - start_time)
|
| 39 |
+
remaining = max(0, SANDBOX_TTL_SEC - elapsed)
|
| 40 |
+
minutes, seconds = divmod(remaining, 60)
|
| 41 |
+
time_str = f"{minutes:02d}:{seconds:02d}"
|
| 42 |
+
return logs, time_str
|
| 43 |
+
|
| 44 |
+
def run_command(cmd, sandbox_id, start_time):
|
| 45 |
+
if not sandbox_id:
|
| 46 |
+
return "No sandbox ID. Launch a sandbox first!", "0:00"
|
| 47 |
+
engine = pick_engine()
|
| 48 |
+
try:
|
| 49 |
+
# send code as mini script
|
| 50 |
+
engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command endpoint
|
| 51 |
+
# fetch logs after running
|
| 52 |
+
logs, remaining = fetch_logs_and_timer(sandbox_id, start_time)
|
| 53 |
+
return logs, remaining
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"Error: {e}", "0:00"
|
| 56 |
|
| 57 |
def kill_sandbox(sandbox_id):
|
| 58 |
if not sandbox_id:
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
return f"Error: {e}"
|
| 66 |
|
| 67 |
+
def get_status(sandbox_id, start_time):
|
| 68 |
if not sandbox_id:
|
| 69 |
+
return "No sandbox ID.", "0:00"
|
| 70 |
engine = pick_engine()
|
| 71 |
try:
|
| 72 |
+
status = engine.predict(api_name="/status_sandbox")
|
| 73 |
+
elapsed = int(time.time() - start_time)
|
| 74 |
+
remaining = max(0, SANDBOX_TTL_SEC - elapsed)
|
| 75 |
+
minutes, seconds = divmod(remaining, 60)
|
| 76 |
+
time_str = f"{minutes:02d}:{seconds:02d}"
|
| 77 |
+
return status, time_str
|
| 78 |
except Exception as e:
|
| 79 |
+
return f"Error: {e}", "0:00"
|
| 80 |
|
| 81 |
# -------------------------
|
| 82 |
# Gradio UI
|
| 83 |
# -------------------------
|
| 84 |
with gr.Blocks() as demo:
|
|
|
|
| 85 |
gr.HTML("""
|
| 86 |
<style>
|
| 87 |
.terminal-box textarea {
|
|
|
|
| 105 |
status_btn = gr.Button("Check Status")
|
| 106 |
sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
|
| 107 |
time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
|
| 108 |
+
gr.Markdown("💡 Note: Running on a Debian-based container!")
|
| 109 |
+
|
| 110 |
+
# State for start time
|
| 111 |
+
start_time_state = gr.State(value=0)
|
| 112 |
|
| 113 |
# Callbacks
|
| 114 |
+
launch_btn.click(launch_sandbox, inputs=command_input, outputs=[terminal_output, sandbox_id_box, time_remaining, start_time_state])
|
| 115 |
+
run_btn.click(run_command, inputs=[command_input, sandbox_id_box, start_time_state], outputs=[terminal_output, time_remaining])
|
| 116 |
+
status_btn.click(get_status, inputs=[sandbox_id_box, start_time_state], outputs=[terminal_output, time_remaining])
|
| 117 |
kill_btn.click(kill_sandbox, inputs=sandbox_id_box, outputs=terminal_output)
|
|
|
|
| 118 |
|
| 119 |
demo.launch()
|