Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,11 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
| 7 |
backend_engines = [
|
| 8 |
Client("ChocoLaboratory/SANDBOX_BACKEND"),
|
| 9 |
Client("ChocoLaboratory/SANDBOXBACKEND2")
|
|
@@ -11,10 +14,9 @@ backend_engines = [
|
|
| 11 |
|
| 12 |
SANDBOX_TTL_SEC = 2 * 3600 # 2 hours
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
def pick_engine():
|
| 19 |
return random.choice(backend_engines)
|
| 20 |
|
|
@@ -23,31 +25,19 @@ def launch_sandbox(code):
|
|
| 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 |
start_time = time.time()
|
| 26 |
-
return f"Sandbox launched! ID: {sandbox_id}\n", sandbox_id,
|
| 27 |
|
| 28 |
-
def
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
logs = engine.predict(api_name="/fetch_logs")
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
|
| 33 |
-
elapsed = int(time.time() - start_time)
|
| 34 |
-
remaining = max(0, SANDBOX_TTL_SEC - elapsed)
|
| 35 |
-
minutes, seconds = divmod(remaining, 60)
|
| 36 |
-
time_str = f"{minutes:02d}:{seconds:02d}"
|
| 37 |
-
return logs, time_str
|
| 38 |
-
|
| 39 |
-
def run_command(cmd, sandbox_id, start_time, engine):
|
| 40 |
-
if not sandbox_id:
|
| 41 |
-
return "No sandbox ID. Launch a sandbox first!", "0:00"
|
| 42 |
-
try:
|
| 43 |
-
engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command
|
| 44 |
-
logs, remaining = fetch_logs_and_timer(sandbox_id, start_time, engine)
|
| 45 |
-
return logs, remaining
|
| 46 |
-
except Exception as e:
|
| 47 |
-
return f"Error: {e}", "0:00"
|
| 48 |
|
| 49 |
def kill_sandbox(sandbox_id, engine):
|
| 50 |
-
if not sandbox_id:
|
| 51 |
return "No sandbox ID to kill."
|
| 52 |
try:
|
| 53 |
result = engine.predict(api_name="/kill_sandbox")
|
|
@@ -55,21 +45,52 @@ def kill_sandbox(sandbox_id, engine):
|
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error: {e}"
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
if not sandbox_id:
|
| 60 |
return "No sandbox ID.", "0:00"
|
| 61 |
try:
|
| 62 |
status = engine.predict(api_name="/status_sandbox")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
return f"Error: {e}", "0:00"
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# -------------------------
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# -------------------------
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
# Dark theme CSS
|
|
@@ -85,42 +106,51 @@ with gr.Blocks() as demo:
|
|
| 85 |
with gr.Row():
|
| 86 |
# Terminal Column
|
| 87 |
with gr.Column(scale=3):
|
| 88 |
-
gr.Markdown("### 🖥 Sandbox
|
| 89 |
terminal_output = gr.Textbox(label="", lines=20, interactive=False, elem_classes=["terminal-box"])
|
| 90 |
command_input = gr.Textbox(label="Command Input", placeholder="print('Hello!')", interactive=True)
|
| 91 |
run_btn = gr.Button("Run Command")
|
| 92 |
|
| 93 |
# Side Panel
|
| 94 |
-
with gr.Column(scale=1):
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Callbacks
|
| 105 |
launch_btn.click(
|
| 106 |
launch_sandbox,
|
| 107 |
inputs=command_input,
|
| 108 |
-
outputs=[terminal_output, sandbox_id_box,
|
| 109 |
)
|
|
|
|
| 110 |
run_btn.click(
|
| 111 |
run_command,
|
| 112 |
inputs=[command_input, sandbox_id_box, start_time_state, sandbox_backend_state],
|
| 113 |
outputs=[terminal_output, time_remaining]
|
| 114 |
)
|
|
|
|
| 115 |
kill_btn.click(
|
| 116 |
kill_sandbox,
|
| 117 |
inputs=[sandbox_id_box, sandbox_backend_state],
|
| 118 |
outputs=terminal_output
|
| 119 |
)
|
|
|
|
| 120 |
status_btn.click(
|
| 121 |
-
|
| 122 |
inputs=[sandbox_id_box, start_time_state, sandbox_backend_state],
|
| 123 |
outputs=[terminal_output, time_remaining]
|
| 124 |
)
|
| 125 |
|
|
|
|
|
|
|
|
|
|
| 126 |
demo.launch()
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
+
import threading
|
| 6 |
|
| 7 |
+
# -------------------------
|
| 8 |
+
# Backend HF clients
|
| 9 |
+
# -------------------------
|
| 10 |
backend_engines = [
|
| 11 |
Client("ChocoLaboratory/SANDBOX_BACKEND"),
|
| 12 |
Client("ChocoLaboratory/SANDBOXBACKEND2")
|
|
|
|
| 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 |
|
|
|
|
| 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, start_time, engine
|
| 29 |
|
| 30 |
+
def fetch_logs(sandbox_id, engine):
|
| 31 |
+
if not sandbox_id or not engine:
|
| 32 |
+
return "No sandbox ID. Launch a sandbox first!"
|
| 33 |
try:
|
| 34 |
logs = engine.predict(api_name="/fetch_logs")
|
| 35 |
+
return logs
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Error fetching logs: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def kill_sandbox(sandbox_id, engine):
|
| 40 |
+
if not sandbox_id or not engine:
|
| 41 |
return "No sandbox ID to kill."
|
| 42 |
try:
|
| 43 |
result = engine.predict(api_name="/kill_sandbox")
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error: {e}"
|
| 47 |
|
| 48 |
+
def status_sandbox(sandbox_id, start_time, engine):
|
| 49 |
+
if not sandbox_id or not engine:
|
| 50 |
return "No sandbox ID.", "0:00"
|
| 51 |
try:
|
| 52 |
status = engine.predict(api_name="/status_sandbox")
|
| 53 |
+
except:
|
| 54 |
+
status = "Unknown"
|
| 55 |
+
elapsed = int(time.time() - start_time)
|
| 56 |
+
remaining = max(0, SANDBOX_TTL_SEC - elapsed)
|
| 57 |
+
minutes, seconds = divmod(remaining, 60)
|
| 58 |
+
time_str = f"{minutes:02d}:{seconds:02d}"
|
| 59 |
+
return status, time_str
|
| 60 |
+
|
| 61 |
+
def run_command(cmd, sandbox_id, start_time, engine):
|
| 62 |
+
if not sandbox_id or not engine:
|
| 63 |
+
return "No sandbox ID. Launch a sandbox first!", "0:00"
|
| 64 |
+
try:
|
| 65 |
+
engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command
|
| 66 |
+
logs, time_str = status_and_logs(sandbox_id, start_time, engine)
|
| 67 |
+
return logs, time_str
|
| 68 |
except Exception as e:
|
| 69 |
return f"Error: {e}", "0:00"
|
| 70 |
|
| 71 |
+
def status_and_logs(sandbox_id, start_time, engine):
|
| 72 |
+
logs = fetch_logs(sandbox_id, engine)
|
| 73 |
+
_, time_str = status_sandbox(sandbox_id, start_time, engine)
|
| 74 |
+
return logs, time_str
|
| 75 |
+
|
| 76 |
+
# -------------------------
|
| 77 |
+
# Auto-update logs thread
|
| 78 |
# -------------------------
|
| 79 |
+
def start_auto_update(terminal_output, sandbox_id_box, start_time_state, sandbox_backend_state, time_remaining):
|
| 80 |
+
def updater():
|
| 81 |
+
while True:
|
| 82 |
+
sandbox_id = sandbox_id_box.value
|
| 83 |
+
start_time = start_time_state.value
|
| 84 |
+
engine = sandbox_backend_state.value
|
| 85 |
+
if sandbox_id and engine:
|
| 86 |
+
logs, time_str = status_and_logs(sandbox_id, start_time, engine)
|
| 87 |
+
terminal_output.value = logs
|
| 88 |
+
time_remaining.value = time_str
|
| 89 |
+
time.sleep(1)
|
| 90 |
+
threading.Thread(target=updater, daemon=True).start()
|
| 91 |
+
|
| 92 |
+
# -------------------------
|
| 93 |
+
# Gradio UI
|
| 94 |
# -------------------------
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
# Dark theme CSS
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
# Terminal Column
|
| 108 |
with gr.Column(scale=3):
|
| 109 |
+
gr.Markdown("### 🖥 Sandbox Terminal")
|
| 110 |
terminal_output = gr.Textbox(label="", lines=20, interactive=False, elem_classes=["terminal-box"])
|
| 111 |
command_input = gr.Textbox(label="Command Input", placeholder="print('Hello!')", interactive=True)
|
| 112 |
run_btn = gr.Button("Run Command")
|
| 113 |
|
| 114 |
# Side Panel
|
| 115 |
+
with gr.Column(scale=1, elem_classes=["side-panel"]):
|
| 116 |
+
gr.Markdown("### Sandbox Controls")
|
| 117 |
+
launch_btn = gr.Button("Launch Sandbox")
|
| 118 |
+
kill_btn = gr.Button("Kill Sandbox")
|
| 119 |
+
status_btn = gr.Button("Check Status")
|
| 120 |
+
sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
|
| 121 |
+
time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
|
| 122 |
+
gr.Markdown("💡 Running on a Debian-based HF container!")
|
| 123 |
+
|
| 124 |
+
# States
|
| 125 |
+
start_time_state = gr.State(value=0)
|
| 126 |
+
sandbox_backend_state = gr.State(value=None)
|
| 127 |
|
| 128 |
# Callbacks
|
| 129 |
launch_btn.click(
|
| 130 |
launch_sandbox,
|
| 131 |
inputs=command_input,
|
| 132 |
+
outputs=[terminal_output, sandbox_id_box, start_time_state, sandbox_backend_state]
|
| 133 |
)
|
| 134 |
+
|
| 135 |
run_btn.click(
|
| 136 |
run_command,
|
| 137 |
inputs=[command_input, sandbox_id_box, start_time_state, sandbox_backend_state],
|
| 138 |
outputs=[terminal_output, time_remaining]
|
| 139 |
)
|
| 140 |
+
|
| 141 |
kill_btn.click(
|
| 142 |
kill_sandbox,
|
| 143 |
inputs=[sandbox_id_box, sandbox_backend_state],
|
| 144 |
outputs=terminal_output
|
| 145 |
)
|
| 146 |
+
|
| 147 |
status_btn.click(
|
| 148 |
+
status_sandbox,
|
| 149 |
inputs=[sandbox_id_box, start_time_state, sandbox_backend_state],
|
| 150 |
outputs=[terminal_output, time_remaining]
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# Start auto-update logs thread
|
| 154 |
+
start_auto_update(terminal_output, sandbox_id_box, start_time_state, sandbox_backend_state, time_remaining)
|
| 155 |
+
|
| 156 |
demo.launch()
|