theguywhosucks commited on
Commit
fc4d50b
·
verified ·
1 Parent(s): 1c2ae2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -44
app.py CHANGED
@@ -2,11 +2,8 @@ import gradio as gr
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,9 +11,10 @@ backend_engines = [
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
 
@@ -27,17 +25,6 @@ def launch_sandbox(code):
27
  start_time = time.time()
28
  return f"Sandbox launched! ID: {sandbox_id}\n", sandbox_id, SANDBOX_TTL_SEC, start_time, engine
29
 
30
- def run_command(cmd, sandbox_id, start_time, engine):
31
- if not sandbox_id:
32
- return "No sandbox ID. Launch a sandbox first!", "0:00"
33
- try:
34
- # Run the command on the backend sandbox
35
- engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command endpoint
36
- logs, remaining = fetch_logs_and_timer(sandbox_id, start_time, engine)
37
- return logs, remaining
38
- except Exception as e:
39
- return f"Error: {e}", "0:00"
40
-
41
  def fetch_logs_and_timer(sandbox_id, start_time, engine):
42
  try:
43
  logs = engine.predict(api_name="/fetch_logs")
@@ -49,6 +36,16 @@ def fetch_logs_and_timer(sandbox_id, start_time, engine):
49
  time_str = f"{minutes:02d}:{seconds:02d}"
50
  return logs, time_str
51
 
 
 
 
 
 
 
 
 
 
 
52
  def kill_sandbox(sandbox_id, engine):
53
  if not sandbox_id:
54
  return "No sandbox ID to kill."
@@ -72,60 +69,54 @@ def get_status(sandbox_id, start_time, engine):
72
  return f"Error: {e}", "0:00"
73
 
74
  # -------------------------
75
- # Gradio UI
76
  # -------------------------
77
  with gr.Blocks() as demo:
78
- # CSS for terminal
79
  gr.HTML("""
80
  <style>
81
- .terminal-box textarea {
82
- background-color: #000000;
83
- color: #ffffff;
84
- font-family: monospace;
85
- font-size: 14px;
86
- }
87
  </style>
88
  """)
89
 
90
  with gr.Row():
 
91
  with gr.Column(scale=3):
92
- gr.Markdown("## 🖥 Sandbox Terminal (Debian)")
93
- terminal_output = gr.Textbox(label="Terminal Output", lines=20, interactive=False, elem_classes=["terminal-box"])
94
- command_input = gr.Textbox(label="Enter command", placeholder="print('Hello!')", interactive=True)
95
  run_btn = gr.Button("Run Command")
 
 
96
  with gr.Column(scale=1):
97
- launch_btn = gr.Button("Launch Sandbox")
98
- kill_btn = gr.Button("Kill Sandbox")
99
- status_btn = gr.Button("Check Status")
100
- sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
101
- time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
102
- gr.Markdown("💡 Note: Running on a Debian-based HF container!")
103
-
104
- # State variables
105
- start_time_state = gr.State(value=0)
106
- sandbox_backend_state = gr.State(value=None)
107
 
108
- # -------------------------
109
  # Callbacks
110
- # -------------------------
111
  launch_btn.click(
112
  launch_sandbox,
113
  inputs=command_input,
114
  outputs=[terminal_output, sandbox_id_box, time_remaining, start_time_state, sandbox_backend_state]
115
  )
116
-
117
  run_btn.click(
118
  run_command,
119
  inputs=[command_input, sandbox_id_box, start_time_state, sandbox_backend_state],
120
  outputs=[terminal_output, time_remaining]
121
  )
122
-
123
  kill_btn.click(
124
  kill_sandbox,
125
  inputs=[sandbox_id_box, sandbox_backend_state],
126
  outputs=terminal_output
127
  )
128
-
129
  status_btn.click(
130
  get_status,
131
  inputs=[sandbox_id_box, start_time_state, sandbox_backend_state],
 
2
  from gradio_client import Client
3
  import random
4
  import time
 
5
 
6
+ # Backend clients
 
 
7
  backend_engines = [
8
  Client("ChocoLaboratory/SANDBOX_BACKEND"),
9
  Client("ChocoLaboratory/SANDBOXBACKEND2")
 
11
 
12
  SANDBOX_TTL_SEC = 2 * 3600 # 2 hours
13
 
14
+ # States
15
+ sandbox_backend_state = gr.State(value=None)
16
+ start_time_state = gr.State(value=0)
17
+
18
  def pick_engine():
19
  return random.choice(backend_engines)
20
 
 
25
  start_time = time.time()
26
  return f"Sandbox launched! ID: {sandbox_id}\n", sandbox_id, SANDBOX_TTL_SEC, start_time, engine
27
 
 
 
 
 
 
 
 
 
 
 
 
28
  def fetch_logs_and_timer(sandbox_id, start_time, engine):
29
  try:
30
  logs = engine.predict(api_name="/fetch_logs")
 
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."
 
69
  return f"Error: {e}", "0:00"
70
 
71
  # -------------------------
72
+ # UI
73
  # -------------------------
74
  with gr.Blocks() as demo:
75
+ # Dark theme CSS
76
  gr.HTML("""
77
  <style>
78
+ body {background-color: #1e1e1e; color: #ffffff; font-family: monospace;}
79
+ .terminal-box textarea {background-color: #000000; color: #ffffff; font-family: monospace; font-size:14px; border-radius:6px;}
80
+ .side-panel {background-color: #2e2e2e; padding:10px; border-radius:6px;}
81
+ .side-panel button {margin-bottom:10px; width:100%;}
 
 
82
  </style>
83
  """)
84
 
85
  with gr.Row():
86
+ # Terminal Column
87
  with gr.Column(scale=3):
88
+ gr.Markdown("### 🖥 Sandbox Cloud Console")
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
+ with gr.Box(elem_classes=["side-panel"]):
96
+ gr.Markdown("### Sandbox Controls")
97
+ launch_btn = gr.Button("Launch Sandbox")
98
+ kill_btn = gr.Button("Kill Sandbox")
99
+ status_btn = gr.Button("Check Status")
100
+ sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
101
+ time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
102
+ gr.Markdown("💡 Running on a Debian-based container!")
 
 
103
 
 
104
  # Callbacks
 
105
  launch_btn.click(
106
  launch_sandbox,
107
  inputs=command_input,
108
  outputs=[terminal_output, sandbox_id_box, time_remaining, start_time_state, sandbox_backend_state]
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
  get_status,
122
  inputs=[sandbox_id_box, start_time_state, sandbox_backend_state],