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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -28
app.py CHANGED
@@ -25,12 +25,20 @@ def launch_sandbox(code):
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, SANDBOX_TTL_SEC, start_time
29
 
30
- def fetch_logs_and_timer(sandbox_id, start_time):
31
  if not sandbox_id:
32
- return "No sandbox ID.", "0:00"
33
- engine = pick_engine()
 
 
 
 
 
 
 
 
34
  try:
35
  logs = engine.predict(api_name="/fetch_logs")
36
  except Exception as e:
@@ -41,33 +49,18 @@ def fetch_logs_and_timer(sandbox_id, start_time):
41
  time_str = f"{minutes:02d}:{seconds:02d}"
42
  return logs, time_str
43
 
44
- def run_command(cmd, sandbox_id, start_time):
45
- if not sandbox_id:
46
- return "No sandbox ID. Launch a sandbox first!", "0:00"
47
- engine = pick_engine()
48
- try:
49
- # send code as mini script
50
- engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command endpoint
51
- # fetch logs after running
52
- logs, remaining = fetch_logs_and_timer(sandbox_id, start_time)
53
- return logs, remaining
54
- except Exception as e:
55
- return f"Error: {e}", "0:00"
56
-
57
- def kill_sandbox(sandbox_id):
58
  if not sandbox_id:
59
  return "No sandbox ID to kill."
60
- engine = pick_engine()
61
  try:
62
  result = engine.predict(api_name="/kill_sandbox")
63
  return result
64
  except Exception as e:
65
  return f"Error: {e}"
66
 
67
- def get_status(sandbox_id, start_time):
68
  if not sandbox_id:
69
  return "No sandbox ID.", "0:00"
70
- engine = pick_engine()
71
  try:
72
  status = engine.predict(api_name="/status_sandbox")
73
  elapsed = int(time.time() - start_time)
@@ -82,6 +75,7 @@ def get_status(sandbox_id, start_time):
82
  # Gradio UI
83
  # -------------------------
84
  with gr.Blocks() as demo:
 
85
  gr.HTML("""
86
  <style>
87
  .terminal-box textarea {
@@ -95,7 +89,7 @@ with gr.Blocks() as demo:
95
 
96
  with gr.Row():
97
  with gr.Column(scale=3):
98
- gr.Markdown("## 🖥 Sandbox Cloud Console")
99
  terminal_output = gr.Textbox(label="Terminal Output", lines=20, interactive=False, elem_classes=["terminal-box"])
100
  command_input = gr.Textbox(label="Enter command", placeholder="print('Hello!')", interactive=True)
101
  run_btn = gr.Button("Run Command")
@@ -105,15 +99,37 @@ with gr.Blocks() as demo:
105
  status_btn = gr.Button("Check Status")
106
  sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
107
  time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
108
- gr.Markdown("💡 Note: Running on a Debian-based container!")
109
 
110
- # State for start time
111
  start_time_state = gr.State(value=0)
 
112
 
 
113
  # Callbacks
114
- launch_btn.click(launch_sandbox, inputs=command_input, outputs=[terminal_output, sandbox_id_box, time_remaining, start_time_state])
115
- run_btn.click(run_command, inputs=[command_input, sandbox_id_box, start_time_state], outputs=[terminal_output, time_remaining])
116
- status_btn.click(get_status, inputs=[sandbox_id_box, start_time_state], outputs=[terminal_output, time_remaining])
117
- kill_btn.click(kill_sandbox, inputs=sandbox_id_box, outputs=terminal_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  demo.launch()
 
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, 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")
44
  except Exception as e:
 
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."
 
55
  try:
56
  result = engine.predict(api_name="/kill_sandbox")
57
  return result
58
  except Exception as e:
59
  return f"Error: {e}"
60
 
61
+ def get_status(sandbox_id, start_time, engine):
62
  if not sandbox_id:
63
  return "No sandbox ID.", "0:00"
 
64
  try:
65
  status = engine.predict(api_name="/status_sandbox")
66
  elapsed = int(time.time() - start_time)
 
75
  # Gradio UI
76
  # -------------------------
77
  with gr.Blocks() as demo:
78
+ # CSS for terminal
79
  gr.HTML("""
80
  <style>
81
  .terminal-box textarea {
 
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")
 
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],
132
+ outputs=[terminal_output, time_remaining]
133
+ )
134
 
135
  demo.launch()