Spaces:
Runtime error
Runtime error
Boning c
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,57 +2,54 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
WORKDIR = "
|
| 7 |
os.makedirs(WORKDIR, exist_ok=True)
|
| 8 |
|
| 9 |
-
#
|
| 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
|
| 17 |
-
# The noVNC server is already running; just return iframe
|
| 18 |
return NO_VNC_IFRAME
|
| 19 |
|
| 20 |
-
def run_shell(cmd
|
| 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 |
-
|
| 33 |
-
|
| 34 |
-
with open(dest, "wb") as f:
|
| 35 |
f.write(file.read())
|
| 36 |
-
return f"
|
| 37 |
|
| 38 |
with gr.Blocks(css="@import url('https://cdn.simplecss.org/simple.min.css');") as demo:
|
| 39 |
-
gr.Markdown("#
|
| 40 |
|
| 41 |
with gr.Tab("Desktop"):
|
| 42 |
-
|
| 43 |
desktop_view = gr.HTML()
|
| 44 |
-
|
| 45 |
|
| 46 |
with gr.Tab("Shell"):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
with gr.Tab("Upload"):
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
demo.launch(
|
| 58 |
server_name="0.0.0.0",
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Use container’s home directory to avoid permission errors
|
| 6 |
+
WORKDIR = os.path.join(os.environ['HOME'], "workspace")
|
| 7 |
os.makedirs(WORKDIR, exist_ok=True)
|
| 8 |
|
| 9 |
+
# HTML iframe to embed noVNC viewer
|
| 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_desktop():
|
|
|
|
| 17 |
return NO_VNC_IFRAME
|
| 18 |
|
| 19 |
+
def run_shell(cmd):
|
|
|
|
| 20 |
result = subprocess.run(
|
| 21 |
cmd,
|
| 22 |
shell=True,
|
| 23 |
capture_output=True,
|
| 24 |
text=True,
|
| 25 |
+
env={**os.environ, "DISPLAY": ":1"} # Supports GUI apps if needed
|
| 26 |
)
|
| 27 |
return result.stdout + result.stderr
|
| 28 |
|
| 29 |
def upload_file(file):
|
| 30 |
+
dest_path = os.path.join(WORKDIR, os.path.basename(file.name))
|
| 31 |
+
with open(dest_path, "wb") as f:
|
|
|
|
| 32 |
f.write(file.read())
|
| 33 |
+
return f"✅ Uploaded to: {dest_path}"
|
| 34 |
|
| 35 |
with gr.Blocks(css="@import url('https://cdn.simplecss.org/simple.min.css');") as demo:
|
| 36 |
+
gr.Markdown("# 🖥️ Offline Visual Sandbox (no API, no cost)")
|
| 37 |
|
| 38 |
with gr.Tab("Desktop"):
|
| 39 |
+
launch_btn = gr.Button("Launch Desktop")
|
| 40 |
desktop_view = gr.HTML()
|
| 41 |
+
launch_btn.click(start_desktop, outputs=desktop_view)
|
| 42 |
|
| 43 |
with gr.Tab("Shell"):
|
| 44 |
+
cmd_input = gr.Textbox(label="Shell Command", placeholder="Try 'ls /home/user/workspace'")
|
| 45 |
+
run_btn = gr.Button("Run")
|
| 46 |
+
cmd_output = gr.Textbox(label="Command Output", lines=10)
|
| 47 |
+
run_btn.click(run_shell, inputs=cmd_input, outputs=cmd_output)
|
| 48 |
|
| 49 |
with gr.Tab("Upload"):
|
| 50 |
+
file_input = gr.File(label="Upload File")
|
| 51 |
+
status_output = gr.Textbox(label="Upload Status")
|
| 52 |
+
file_input.change(upload_file, inputs=file_input, outputs=status_output)
|
| 53 |
|
| 54 |
demo.launch(
|
| 55 |
server_name="0.0.0.0",
|