Boning c commited on
Commit
392ec35
·
verified ·
1 Parent(s): 9458de5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()