Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
-
# Backend
|
| 8 |
# -------------------------
|
| 9 |
BACKENDS = {
|
| 10 |
-
"Sandbox Backend 1": "
|
| 11 |
-
"Sandbox Backend 2": "
|
| 12 |
}
|
| 13 |
-
|
| 14 |
-
# Track running sandboxes per backend
|
| 15 |
backend_load = {name: 0 for name in BACKENDS.keys()}
|
| 16 |
-
# Store user sandboxes: sandbox_id -> {"backend": ..., "start_time": ..., "card": ...}
|
| 17 |
user_sandboxes = {}
|
| 18 |
SANDBOX_TTL = 2*3600 # 2 hours
|
| 19 |
|
| 20 |
# -------------------------
|
| 21 |
-
# API
|
| 22 |
# -------------------------
|
| 23 |
def choose_backend():
|
| 24 |
return min(backend_load, key=lambda k: backend_load[k])
|
| 25 |
|
| 26 |
-
def launch_sandbox_api(code):
|
| 27 |
backend_name = choose_backend()
|
| 28 |
-
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
r.raise_for_status()
|
| 32 |
-
sandbox_id = r.json()["sandbox_id"]
|
| 33 |
backend_load[backend_name] += 1
|
| 34 |
-
user_sandboxes[sandbox_id] = {
|
|
|
|
|
|
|
| 35 |
return sandbox_id, backend_name
|
| 36 |
except Exception as e:
|
| 37 |
return None, str(e)
|
|
@@ -40,13 +37,8 @@ def fetch_logs_api(sandbox_id):
|
|
| 40 |
info = user_sandboxes.get(sandbox_id)
|
| 41 |
if not info:
|
| 42 |
return "Sandbox not found"
|
| 43 |
-
backend = info["backend"]
|
| 44 |
-
url = f"{BACKENDS[backend]}/fetch_logs/{sandbox_id}"
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
r.raise_for_status()
|
| 48 |
-
logs = r.json()
|
| 49 |
-
return f"STDOUT:\n{logs['stdout']}\n\nSTDERR:\n{logs['stderr']}"
|
| 50 |
except Exception as e:
|
| 51 |
return str(e)
|
| 52 |
|
|
@@ -54,14 +46,11 @@ def kill_sandbox_api(sandbox_id):
|
|
| 54 |
info = user_sandboxes.get(sandbox_id)
|
| 55 |
if not info:
|
| 56 |
return "Sandbox not found"
|
| 57 |
-
backend = info["backend"]
|
| 58 |
-
url = f"{BACKENDS[backend]}/kill_sandbox/{sandbox_id}"
|
| 59 |
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
backend_load[backend] -= 1
|
| 63 |
del user_sandboxes[sandbox_id]
|
| 64 |
-
return
|
| 65 |
except Exception as e:
|
| 66 |
return str(e)
|
| 67 |
|
|
@@ -69,19 +58,15 @@ def status_sandbox_api(sandbox_id):
|
|
| 69 |
info = user_sandboxes.get(sandbox_id)
|
| 70 |
if not info:
|
| 71 |
return "Sandbox not found"
|
| 72 |
-
backend = info["backend"]
|
| 73 |
-
url = f"{BACKENDS[backend]}/status_sandbox/{sandbox_id}"
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
r.raise_for_status()
|
| 77 |
-
return r.json().get("status", "Unknown")
|
| 78 |
except Exception as e:
|
| 79 |
return str(e)
|
| 80 |
|
| 81 |
# -------------------------
|
| 82 |
# Sandbox card creation
|
| 83 |
# -------------------------
|
| 84 |
-
def create_sandbox_card(sandbox_id, sandbox_container):
|
| 85 |
info = user_sandboxes[sandbox_id]
|
| 86 |
backend = info["backend"]
|
| 87 |
|
|
@@ -92,13 +77,17 @@ def create_sandbox_card(sandbox_id, sandbox_container):
|
|
| 92 |
runtime_box = gr.Textbox(label="Runtime (sec)", value="0", interactive=False)
|
| 93 |
logs_box = gr.Textbox(label="Logs", lines=10, interactive=False)
|
| 94 |
|
| 95 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
logs_btn = gr.Button("Fetch Logs")
|
| 98 |
kill_btn = gr.Button("Kill Sandbox")
|
| 99 |
status_btn = gr.Button("Check Status")
|
| 100 |
|
| 101 |
-
# Button actions
|
| 102 |
logs_btn.click(lambda sid=sandbox_id: fetch_logs_api(sid), inputs=[], outputs=logs_box)
|
| 103 |
kill_btn.click(lambda sid=sandbox_id: kill_sandbox_api(sid), inputs=[], outputs=status_badge)
|
| 104 |
status_btn.click(lambda sid=sandbox_id: status_sandbox_api(sid), inputs=[], outputs=runtime_box)
|
|
@@ -109,12 +98,13 @@ def create_sandbox_card(sandbox_id, sandbox_container):
|
|
| 109 |
elapsed = int(time.time() - info["start_time"])
|
| 110 |
runtime_box.value = str(elapsed)
|
| 111 |
logs_box.value = fetch_logs_api(sandbox_id)
|
|
|
|
|
|
|
|
|
|
| 112 |
time.sleep(2)
|
| 113 |
-
# Remove card if sandbox killed/expired
|
| 114 |
sandbox_container.remove(card)
|
| 115 |
|
| 116 |
threading.Thread(target=updater, daemon=True).start()
|
| 117 |
-
info["card"] = card
|
| 118 |
return card
|
| 119 |
|
| 120 |
# -------------------------
|
|
@@ -129,21 +119,22 @@ with gr.Blocks(css="""
|
|
| 129 |
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
| 130 |
}
|
| 131 |
""") as demo:
|
| 132 |
-
gr.Markdown("#
|
| 133 |
|
| 134 |
-
code_input = gr.Textbox(label="
|
|
|
|
| 135 |
launch_btn = gr.Button("Launch Sandbox")
|
| 136 |
launch_status = gr.Textbox(label="Launch Status")
|
| 137 |
|
| 138 |
sandbox_container = gr.Column()
|
| 139 |
|
| 140 |
-
def on_launch(code):
|
| 141 |
-
sandbox_id, backend_or_error = launch_sandbox_api(code)
|
| 142 |
if not sandbox_id:
|
| 143 |
return f"Error: {backend_or_error}"
|
| 144 |
-
create_sandbox_card(sandbox_id, sandbox_container)
|
| 145 |
return f"Sandbox {sandbox_id} launched on {user_sandboxes[sandbox_id]['backend']}"
|
| 146 |
|
| 147 |
-
launch_btn.click(on_launch, inputs=code_input, outputs=launch_status)
|
| 148 |
|
| 149 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
+
# Backend info
|
| 8 |
# -------------------------
|
| 9 |
BACKENDS = {
|
| 10 |
+
"Sandbox Backend 1": "ChocoLaboratory/SANDBOX_BACKEND",
|
| 11 |
+
"Sandbox Backend 2": "ChocoLaboratory/SANDBOXBACKEND2"
|
| 12 |
}
|
|
|
|
|
|
|
| 13 |
backend_load = {name: 0 for name in BACKENDS.keys()}
|
|
|
|
| 14 |
user_sandboxes = {}
|
| 15 |
SANDBOX_TTL = 2*3600 # 2 hours
|
| 16 |
|
| 17 |
# -------------------------
|
| 18 |
+
# API functions
|
| 19 |
# -------------------------
|
| 20 |
def choose_backend():
|
| 21 |
return min(backend_load, key=lambda k: backend_load[k])
|
| 22 |
|
| 23 |
+
def launch_sandbox_api(code, requirements):
|
| 24 |
backend_name = choose_backend()
|
| 25 |
+
client = Client(BACKENDS[backend_name])
|
| 26 |
try:
|
| 27 |
+
sandbox_id = client.predict(code=code, requirements=requirements, api_name="/launch_sandbox")
|
|
|
|
|
|
|
| 28 |
backend_load[backend_name] += 1
|
| 29 |
+
user_sandboxes[sandbox_id] = {
|
| 30 |
+
"backend": backend_name, "start_time": time.time(), "client": client
|
| 31 |
+
}
|
| 32 |
return sandbox_id, backend_name
|
| 33 |
except Exception as e:
|
| 34 |
return None, str(e)
|
|
|
|
| 37 |
info = user_sandboxes.get(sandbox_id)
|
| 38 |
if not info:
|
| 39 |
return "Sandbox not found"
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
+
return info["client"].predict(api_name="/fetch_logs", sandbox_id=sandbox_id)
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return str(e)
|
| 44 |
|
|
|
|
| 46 |
info = user_sandboxes.get(sandbox_id)
|
| 47 |
if not info:
|
| 48 |
return "Sandbox not found"
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
result = info["client"].predict(api_name="/kill_sandbox", sandbox_id=sandbox_id)
|
| 51 |
+
backend_load[info["backend"]] -= 1
|
|
|
|
| 52 |
del user_sandboxes[sandbox_id]
|
| 53 |
+
return result
|
| 54 |
except Exception as e:
|
| 55 |
return str(e)
|
| 56 |
|
|
|
|
| 58 |
info = user_sandboxes.get(sandbox_id)
|
| 59 |
if not info:
|
| 60 |
return "Sandbox not found"
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
+
return info["client"].predict(api_name="/status_sandbox", sandbox_id=sandbox_id)
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
return str(e)
|
| 65 |
|
| 66 |
# -------------------------
|
| 67 |
# Sandbox card creation
|
| 68 |
# -------------------------
|
| 69 |
+
def create_sandbox_card(sandbox_id, sandbox_container, code, requirements):
|
| 70 |
info = user_sandboxes[sandbox_id]
|
| 71 |
backend = info["backend"]
|
| 72 |
|
|
|
|
| 77 |
runtime_box = gr.Textbox(label="Runtime (sec)", value="0", interactive=False)
|
| 78 |
logs_box = gr.Textbox(label="Logs", lines=10, interactive=False)
|
| 79 |
|
| 80 |
+
# Side-by-side panels: Main Code | requirements.txt
|
| 81 |
+
with gr.Row():
|
| 82 |
+
code_box = gr.Textbox(label="Main Code", value=code, lines=10, interactive=False)
|
| 83 |
+
req_box = gr.Textbox(label="requirements.txt", value=requirements, lines=10, interactive=False)
|
| 84 |
+
|
| 85 |
+
# Buttons
|
| 86 |
with gr.Row():
|
| 87 |
logs_btn = gr.Button("Fetch Logs")
|
| 88 |
kill_btn = gr.Button("Kill Sandbox")
|
| 89 |
status_btn = gr.Button("Check Status")
|
| 90 |
|
|
|
|
| 91 |
logs_btn.click(lambda sid=sandbox_id: fetch_logs_api(sid), inputs=[], outputs=logs_box)
|
| 92 |
kill_btn.click(lambda sid=sandbox_id: kill_sandbox_api(sid), inputs=[], outputs=status_badge)
|
| 93 |
status_btn.click(lambda sid=sandbox_id: status_sandbox_api(sid), inputs=[], outputs=runtime_box)
|
|
|
|
| 98 |
elapsed = int(time.time() - info["start_time"])
|
| 99 |
runtime_box.value = str(elapsed)
|
| 100 |
logs_box.value = fetch_logs_api(sandbox_id)
|
| 101 |
+
if elapsed >= SANDBOX_TTL:
|
| 102 |
+
kill_sandbox_api(sandbox_id)
|
| 103 |
+
break
|
| 104 |
time.sleep(2)
|
|
|
|
| 105 |
sandbox_container.remove(card)
|
| 106 |
|
| 107 |
threading.Thread(target=updater, daemon=True).start()
|
|
|
|
| 108 |
return card
|
| 109 |
|
| 110 |
# -------------------------
|
|
|
|
| 119 |
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
| 120 |
}
|
| 121 |
""") as demo:
|
| 122 |
+
gr.Markdown("# Sandbox Dashboard")
|
| 123 |
|
| 124 |
+
code_input = gr.Textbox(label="Main Code", lines=10)
|
| 125 |
+
req_input = gr.Textbox(label="requirements.txt", lines=5)
|
| 126 |
launch_btn = gr.Button("Launch Sandbox")
|
| 127 |
launch_status = gr.Textbox(label="Launch Status")
|
| 128 |
|
| 129 |
sandbox_container = gr.Column()
|
| 130 |
|
| 131 |
+
def on_launch(code, requirements):
|
| 132 |
+
sandbox_id, backend_or_error = launch_sandbox_api(code, requirements)
|
| 133 |
if not sandbox_id:
|
| 134 |
return f"Error: {backend_or_error}"
|
| 135 |
+
create_sandbox_card(sandbox_id, sandbox_container, code, requirements)
|
| 136 |
return f"Sandbox {sandbox_id} launched on {user_sandboxes[sandbox_id]['backend']}"
|
| 137 |
|
| 138 |
+
launch_btn.click(on_launch, inputs=[code_input, req_input], outputs=launch_status)
|
| 139 |
|
| 140 |
demo.launch()
|