theguywhosucks commited on
Commit
d4507be
·
verified ·
1 Parent(s): c2302aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -25
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from gradio_client import Client
3
- import time
4
  import random
 
5
 
6
  # -------------------------
7
  # Backend HF clients
@@ -15,7 +15,7 @@ backend_engines = [
15
  # Helper functions
16
  # -------------------------
17
  def pick_engine():
18
- # Simple load balancing (random for now)
19
  return random.choice(backend_engines)
20
 
21
  def launch_sandbox(code):
@@ -25,32 +25,57 @@ def launch_sandbox(code):
25
  return f"Sandbox launched! ID: {sandbox_id}", sandbox_id, "120:00" # 2h TTL
26
 
27
  def run_command(cmd, sandbox_id):
 
 
28
  engine = pick_engine()
29
- # Here we assume the backend runs the code immediately in the sandbox
30
- # For demo, we send each command as a mini script
31
- code = cmd
32
- engine.predict(code=code, api_name="/launch_sandbox") # you could create a /run_command endpoint
33
- logs = engine.predict(api_name="/fetch_logs")
34
- return logs
 
35
 
36
  def kill_sandbox(sandbox_id):
 
 
37
  engine = pick_engine()
38
- result = engine.predict(api_name="/kill_sandbox")
39
- return result
 
 
 
40
 
41
  def get_status(sandbox_id):
 
 
42
  engine = pick_engine()
43
- return engine.predict(api_name="/status_sandbox")
 
 
 
44
 
45
  # -------------------------
46
  # Gradio UI
47
  # -------------------------
48
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
49
  with gr.Row():
50
  with gr.Column(scale=3):
51
- gr.Markdown("## 🖥 Sandbox Cloud")
52
  terminal_output = gr.Textbox(label="Terminal Output", lines=20, interactive=False, elem_classes=["terminal-box"])
53
- command_input = gr.Textbox(label="Enter command", placeholder="ls -la", interactive=True)
54
  run_btn = gr.Button("Run Command")
55
  with gr.Column(scale=1):
56
  launch_btn = gr.Button("Launch Sandbox")
@@ -58,7 +83,7 @@ with gr.Blocks() as demo:
58
  status_btn = gr.Button("Check Status")
59
  sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
60
  time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
61
- gr.Markdown("💡 Note: Running on a Debian-based container!")
62
 
63
  # Callbacks
64
  launch_btn.click(launch_sandbox, inputs=command_input, outputs=[terminal_output, sandbox_id_box, time_remaining])
@@ -66,14 +91,4 @@ with gr.Blocks() as demo:
66
  kill_btn.click(kill_sandbox, inputs=sandbox_id_box, outputs=terminal_output)
67
  status_btn.click(get_status, inputs=sandbox_id_box, outputs=terminal_output)
68
 
69
- # CSS for terminal look
70
- css = """
71
- .terminal-box textarea {
72
- background-color: #1e1e1e;
73
- color: #00ff00;
74
- font-family: monospace;
75
- font-size: 14px;
76
- }
77
- """
78
-
79
- demo.launch(css=css)
 
1
  import gradio as gr
2
  from gradio_client import Client
 
3
  import random
4
+ import time
5
 
6
  # -------------------------
7
  # Backend HF clients
 
15
  # Helper functions
16
  # -------------------------
17
  def pick_engine():
18
+ # Random backend selection (simple load balancing)
19
  return random.choice(backend_engines)
20
 
21
  def launch_sandbox(code):
 
25
  return f"Sandbox launched! ID: {sandbox_id}", sandbox_id, "120:00" # 2h TTL
26
 
27
  def run_command(cmd, sandbox_id):
28
+ if not sandbox_id:
29
+ return "No sandbox ID. Launch a sandbox first!"
30
  engine = pick_engine()
31
+ # For simplicity, run command as a mini Python script
32
+ try:
33
+ engine.predict(code=cmd, api_name="/launch_sandbox") # ideally /run_command endpoint
34
+ logs = engine.predict(api_name="/fetch_logs")
35
+ return logs
36
+ except Exception as e:
37
+ return f"Error: {e}"
38
 
39
  def kill_sandbox(sandbox_id):
40
+ if not sandbox_id:
41
+ return "No sandbox ID to kill."
42
  engine = pick_engine()
43
+ try:
44
+ result = engine.predict(api_name="/kill_sandbox")
45
+ return result
46
+ except Exception as e:
47
+ return f"Error: {e}"
48
 
49
  def get_status(sandbox_id):
50
+ if not sandbox_id:
51
+ return "No sandbox ID."
52
  engine = pick_engine()
53
+ try:
54
+ return engine.predict(api_name="/status_sandbox")
55
+ except Exception as e:
56
+ return f"Error: {e}"
57
 
58
  # -------------------------
59
  # Gradio UI
60
  # -------------------------
61
  with gr.Blocks() as demo:
62
+ # Global CSS for terminal style (white-on-black)
63
+ gr.HTML("""
64
+ <style>
65
+ .terminal-box textarea {
66
+ background-color: #000000;
67
+ color: #ffffff;
68
+ font-family: monospace;
69
+ font-size: 14px;
70
+ }
71
+ </style>
72
+ """)
73
+
74
  with gr.Row():
75
  with gr.Column(scale=3):
76
+ gr.Markdown("## 🖥 Sandbox Cloud Console")
77
  terminal_output = gr.Textbox(label="Terminal Output", lines=20, interactive=False, elem_classes=["terminal-box"])
78
+ command_input = gr.Textbox(label="Enter command", placeholder="print('Hello!')", interactive=True)
79
  run_btn = gr.Button("Run Command")
80
  with gr.Column(scale=1):
81
  launch_btn = gr.Button("Launch Sandbox")
 
83
  status_btn = gr.Button("Check Status")
84
  sandbox_id_box = gr.Textbox(label="Sandbox ID", interactive=False)
85
  time_remaining = gr.Textbox(label="Time Remaining", interactive=False)
86
+ gr.Markdown("💡 Note: Running on a Debian-based HF container!")
87
 
88
  # Callbacks
89
  launch_btn.click(launch_sandbox, inputs=command_input, outputs=[terminal_output, sandbox_id_box, time_remaining])
 
91
  kill_btn.click(kill_sandbox, inputs=sandbox_id_box, outputs=terminal_output)
92
  status_btn.click(get_status, inputs=sandbox_id_box, outputs=terminal_output)
93
 
94
+ demo.launch()