File size: 1,401 Bytes
392ec35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from e2b import Sandbox
import os

# Session persistence across functions
sandbox = None

def start_sandbox():
    global sandbox
    sandbox = Sandbox(template="base")  # No api_key needed
    return sandbox.url or "Failed to launch sandbox"

def run_shell(cmd):
    global sandbox
    if not sandbox:
        return "Sandbox not launched"
    output = sandbox.run_shell(cmd)
    return output.stdout if output.stdout else output.stderr

def upload_file(file):
    global sandbox
    if not sandbox:
        return "Sandbox not launched"
    file_path = sandbox.upload_file(file.name, open(file.name, "rb"))
    return f"Uploaded to sandbox: {file_path}"

with gr.Blocks() as demo:
    gr.Markdown("# 🖥️ Sandbox Desktop with Shell & Upload")
    
    with gr.Row():
        launch_btn = gr.Button("Start Sandbox")
        url_output = gr.Textbox(label="Desktop URL", interactive=False)

    launch_btn.click(start_sandbox, outputs=url_output)

    shell_input = gr.Textbox(label="Shell Command")
    shell_btn = gr.Button("Run Command")
    shell_output = gr.Textbox(label="Command Output", lines=6)

    shell_btn.click(run_shell, inputs=shell_input, outputs=shell_output)

    file_input = gr.File(label="Upload File to Sandbox")
    file_output = gr.Textbox(label="Upload Status")

    file_input.change(upload_file, inputs=file_input, outputs=file_output)

demo.launch()