Boning c commited on
Commit
ee3d946
·
verified ·
1 Parent(s): 2b4c887

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -2,57 +2,54 @@ 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",
 
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",