Boning c commited on
Commit
2b4c887
·
verified ·
1 Parent(s): e0e6526

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -39
app.py CHANGED
@@ -1,47 +1,63 @@
1
  import gradio as gr
2
- from e2b import Sandbox
3
  import os
4
 
5
- # Session persistence across functions
6
- sandbox = None
 
7
 
8
- def start_sandbox():
9
- global sandbox
10
- sandbox = Sandbox(template="base") # No api_key needed
11
- return sandbox.url or "Failed to launch sandbox"
 
 
12
 
13
- def run_shell(cmd):
14
- global sandbox
15
- if not sandbox:
16
- return "Sandbox not launched"
17
- output = sandbox.run_shell(cmd)
18
- return output.stdout if output.stdout else output.stderr
 
 
 
 
 
 
 
 
19
 
20
  def upload_file(file):
21
- global sandbox
22
- if not sandbox:
23
- return "Sandbox not launched"
24
- file_path = sandbox.upload_file(file.name, open(file.name, "rb"))
25
- return f"Uploaded to sandbox: {file_path}"
26
-
27
- with gr.Blocks() as demo:
28
- gr.Markdown("# 🖥️ Sandbox Desktop with Shell & Upload")
29
-
30
- with gr.Row():
31
- launch_btn = gr.Button("Start Sandbox")
32
- url_output = gr.Textbox(label="Desktop URL", interactive=False)
33
-
34
- launch_btn.click(start_sandbox, outputs=url_output)
35
-
36
- shell_input = gr.Textbox(label="Shell Command")
37
- shell_btn = gr.Button("Run Command")
38
- shell_output = gr.Textbox(label="Command Output", lines=6)
39
-
40
- shell_btn.click(run_shell, inputs=shell_input, outputs=shell_output)
41
-
42
- file_input = gr.File(label="Upload File to Sandbox")
43
- file_output = gr.Textbox(label="Upload Status")
44
-
45
- file_input.change(upload_file, inputs=file_input, outputs=file_output)
 
 
 
 
 
 
46
 
47
- demo.launch()
 
1
  import gradio as gr
2
+ import subprocess
3
  import os
4
 
5
+ # Where uploads land inside the container
6
+ WORKDIR = "/workspace"
7
+ os.makedirs(WORKDIR, exist_ok=True)
8
 
9
+ # Embed noVNC iframe HTML
10
+ NO_VNC_IFRAME = """
11
+ <iframe src="http://localhost:6080/vnc.html?autoconnect=true&resize=remote"
12
+ style="width:100%; height:600px; border:none;">
13
+ </iframe>
14
+ """
15
 
16
+ def start_sandbox():
17
+ # The noVNC server is already running; just return iframe
18
+ return NO_VNC_IFRAME
19
+
20
+ def run_shell(cmd: str):
21
+ # Run command inside container; keep DISPLAY so GUI apps can launch
22
+ result = subprocess.run(
23
+ cmd,
24
+ shell=True,
25
+ capture_output=True,
26
+ text=True,
27
+ env={**os.environ, "DISPLAY": ":1"}
28
+ )
29
+ return result.stdout + result.stderr
30
 
31
  def upload_file(file):
32
+ # Save uploaded file into WORKDIR
33
+ dest = os.path.join(WORKDIR, os.path.basename(file.name))
34
+ with open(dest, "wb") as f:
35
+ f.write(file.read())
36
+ return f"Saved to sandbox: {dest}"
37
+
38
+ with gr.Blocks(css="@import url('https://cdn.simplecss.org/simple.min.css');") as demo:
39
+ gr.Markdown("# 🔒 Offline Sandbox Desktop")
40
+
41
+ with gr.Tab("Desktop"):
42
+ btn = gr.Button("Show Desktop")
43
+ desktop_view = gr.HTML()
44
+ btn.click(start_sandbox, outputs=desktop_view)
45
+
46
+ with gr.Tab("Shell"):
47
+ cmd = gr.Textbox(label="Shell Command", placeholder="e.g. ls -al /workspace")
48
+ run = gr.Button("Run")
49
+ out = gr.Textbox(label="Output", lines=10)
50
+ run.click(run_shell, inputs=cmd, outputs=out)
51
+
52
+ with gr.Tab("Upload"):
53
+ uploader = gr.File(label="Upload to Sandbox")
54
+ status = gr.Textbox(label="Upload Status")
55
+ uploader.change(upload_file, inputs=uploader, outputs=status)
56
+
57
+ demo.launch(
58
+ server_name="0.0.0.0",
59
+ server_port=7860,
60
+ share=False,
61
+ ssr_mode=False
62
+ )
63