theguywhosucks commited on
Commit
4af518a
·
verified ·
1 Parent(s): 70b7735

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -22
app.py CHANGED
@@ -5,22 +5,31 @@ import requests
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}"
@@ -37,6 +46,7 @@ def kill_sandbox(sandbox_id, backend_name):
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)
@@ -51,51 +61,65 @@ def status_sandbox(sandbox_id, backend_name):
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()
 
5
  # Backend Space URLs
6
  # -------------------------
7
  BACKENDS = {
8
+ "Sandbox Backend 1": "https://chocolaboratory-sandbox-backend.hf.space/",
9
+ "Sandbox Backend 2": "https://chocolaboratory-sandboxbackend2.hf.space/"
10
  }
11
 
12
+ # Track running sandboxes per backend for automatic selection
13
+ backend_load = {name: 0 for name in BACKENDS.keys()}
14
+
15
  # -------------------------
16
  # Frontend functions
17
  # -------------------------
18
+ def choose_backend():
19
+ # Select backend with fewest sandboxes
20
+ return min(backend_load, key=lambda k: backend_load[k])
21
+
22
+ def launch_sandbox(code):
23
+ backend_name = choose_backend()
24
  url = BACKENDS[backend_name] + "/launch_sandbox"
25
  try:
26
  r = requests.post(url, json={"code": code})
27
  r.raise_for_status()
28
  sandbox_id = r.json()["sandbox_id"]
29
+ backend_load[backend_name] += 1
30
+ return f"Sandbox {sandbox_id} launched on {backend_name}", sandbox_id, backend_name
31
  except Exception as e:
32
+ return f"Error: {e}", None, None
33
 
34
  def fetch_logs(sandbox_id, backend_name):
35
  url = f"{BACKENDS[backend_name]}/fetch_logs/{sandbox_id}"
 
46
  try:
47
  r = requests.post(url)
48
  r.raise_for_status()
49
+ backend_load[backend_name] -= 1
50
  return r.json().get("status", "Killed")
51
  except Exception as e:
52
  return str(e)
 
61
  return str(e)
62
 
63
  # -------------------------
64
+ # AWS-style frontend UI
65
  # -------------------------
66
+ with gr.Blocks(css="""
67
+ .sandbox-card {
68
+ border-radius: 12px;
69
+ padding: 15px;
70
+ margin: 5px;
71
+ background-color: #ffffff;
72
+ box-shadow: 0 3px 6px rgba(0,0,0,0.1);
73
+ }
74
+ .header {font-size: 24px; font-weight: bold; margin-bottom: 15px;}
75
+ """) as demo:
76
 
77
+ gr.Markdown("<div class='header'>🛡 Sandbox Service Dashboard</div>", elem_classes="header")
 
78
 
79
+ # Code editor & launch
80
  code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
81
  launch_btn = gr.Button("Launch Sandbox")
82
+
83
+ # Status and storage
84
+ launch_status = gr.Textbox(label="Launch Status")
85
  sandbox_id_storage = gr.State(value=None)
86
+ backend_storage = gr.State(value=None)
87
 
88
+ # Sandbox cards container
89
+ with gr.Column():
90
  logs_btn = gr.Button("Fetch Logs")
91
  kill_btn = gr.Button("Kill Sandbox")
92
  status_btn = gr.Button("Check Status")
93
+
94
+ logs_output = gr.Textbox(label="Logs", lines=12)
95
  kill_output = gr.Textbox(label="Kill Status")
96
+ status_output = gr.Textbox(label="Sandbox Status")
97
 
98
  # -------------------------
99
  # Button actions
100
  # -------------------------
101
  launch_btn.click(
102
  launch_sandbox,
103
+ inputs=[code_input],
104
+ outputs=[launch_status, sandbox_id_storage, backend_storage]
105
  )
106
+
107
  logs_btn.click(
108
  fetch_logs,
109
+ inputs=[sandbox_id_storage, backend_storage],
110
  outputs=logs_output
111
  )
112
+
113
  kill_btn.click(
114
  kill_sandbox,
115
+ inputs=[sandbox_id_storage, backend_storage],
116
  outputs=kill_output
117
  )
118
+
119
  status_btn.click(
120
  status_sandbox,
121
+ inputs=[sandbox_id_storage, backend_storage],
122
+ outputs=status_output
123
  )
124
 
125
  demo.launch()