Spaces:
Runtime error
Runtime error
Boning c
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def upload_file(file):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return f"
|
| 26 |
-
|
| 27 |
-
with gr.Blocks() as demo:
|
| 28 |
-
gr.Markdown("#
|
| 29 |
-
|
| 30 |
-
with gr.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|