Spaces:
Runtime error
Runtime error
| 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() | |