Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# -------------------------
|
| 5 |
+
# Backend Space URLs
|
| 6 |
+
# -------------------------
|
| 7 |
+
BACKENDS = {
|
| 8 |
+
"Sandbox Backend 1": "https://your-backend1.hf.space",
|
| 9 |
+
"Sandbox Backend 2": "https://your-backend2.hf.space"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# -------------------------
|
| 13 |
+
# Frontend functions
|
| 14 |
+
# -------------------------
|
| 15 |
+
def launch_sandbox(code, backend_name):
|
| 16 |
+
url = BACKENDS[backend_name] + "/launch_sandbox"
|
| 17 |
+
try:
|
| 18 |
+
r = requests.post(url, json={"code": code})
|
| 19 |
+
r.raise_for_status()
|
| 20 |
+
sandbox_id = r.json()["sandbox_id"]
|
| 21 |
+
return f"Sandbox launched! ID: {sandbox_id}", sandbox_id
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Error: {e}", None
|
| 24 |
+
|
| 25 |
+
def fetch_logs(sandbox_id, backend_name):
|
| 26 |
+
url = f"{BACKENDS[backend_name]}/fetch_logs/{sandbox_id}"
|
| 27 |
+
try:
|
| 28 |
+
r = requests.get(url)
|
| 29 |
+
r.raise_for_status()
|
| 30 |
+
logs = r.json()
|
| 31 |
+
return f"STDOUT:\n{logs['stdout']}\n\nSTDERR:\n{logs['stderr']}"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return str(e)
|
| 34 |
+
|
| 35 |
+
def kill_sandbox(sandbox_id, backend_name):
|
| 36 |
+
url = f"{BACKENDS[backend_name]}/kill_sandbox/{sandbox_id}"
|
| 37 |
+
try:
|
| 38 |
+
r = requests.post(url)
|
| 39 |
+
r.raise_for_status()
|
| 40 |
+
return r.json().get("status", "Killed")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return str(e)
|
| 43 |
+
|
| 44 |
+
def status_sandbox(sandbox_id, backend_name):
|
| 45 |
+
url = f"{BACKENDS[backend_name]}/status_sandbox/{sandbox_id}"
|
| 46 |
+
try:
|
| 47 |
+
r = requests.get(url)
|
| 48 |
+
r.raise_for_status()
|
| 49 |
+
return r.json().get("status", "Unknown")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return str(e)
|
| 52 |
+
|
| 53 |
+
# -------------------------
|
| 54 |
+
# Gradio AWS-style frontend UI
|
| 55 |
+
# -------------------------
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("# Sandbox Service Frontend")
|
| 58 |
+
|
| 59 |
+
# Backend selector
|
| 60 |
+
backend_selector = gr.Dropdown(list(BACKENDS.keys()), label="Choose Backend")
|
| 61 |
+
|
| 62 |
+
# Code editor
|
| 63 |
+
code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
|
| 64 |
+
launch_btn = gr.Button("Launch Sandbox")
|
| 65 |
+
status_output = gr.Textbox(label="Launch Status")
|
| 66 |
+
sandbox_id_storage = gr.State(value=None)
|
| 67 |
+
|
| 68 |
+
# Logs and Kill buttons
|
| 69 |
+
with gr.Row():
|
| 70 |
+
logs_btn = gr.Button("Fetch Logs")
|
| 71 |
+
kill_btn = gr.Button("Kill Sandbox")
|
| 72 |
+
status_btn = gr.Button("Check Status")
|
| 73 |
+
logs_output = gr.Textbox(label="Logs", lines=15)
|
| 74 |
+
kill_output = gr.Textbox(label="Kill Status")
|
| 75 |
+
status_output_box = gr.Textbox(label="Sandbox Status")
|
| 76 |
+
|
| 77 |
+
# -------------------------
|
| 78 |
+
# Button actions
|
| 79 |
+
# -------------------------
|
| 80 |
+
launch_btn.click(
|
| 81 |
+
launch_sandbox,
|
| 82 |
+
inputs=[code_input, backend_selector],
|
| 83 |
+
outputs=[status_output, sandbox_id_storage]
|
| 84 |
+
)
|
| 85 |
+
logs_btn.click(
|
| 86 |
+
fetch_logs,
|
| 87 |
+
inputs=[sandbox_id_storage, backend_selector],
|
| 88 |
+
outputs=logs_output
|
| 89 |
+
)
|
| 90 |
+
kill_btn.click(
|
| 91 |
+
kill_sandbox,
|
| 92 |
+
inputs=[sandbox_id_storage, backend_selector],
|
| 93 |
+
outputs=kill_output
|
| 94 |
+
)
|
| 95 |
+
status_btn.click(
|
| 96 |
+
status_sandbox,
|
| 97 |
+
inputs=[sandbox_id_storage, backend_selector],
|
| 98 |
+
outputs=status_output_box
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
demo.launch()
|