zhuhai111 commited on
Commit
e9d6dcf
·
verified ·
1 Parent(s): 8e99678

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import threading
4
+ import gradio as gr
5
+ import time
6
+ import urllib.request
7
+ import tarfile
8
+ import zipfile
9
+
10
+ # 下载并解压工具函数
11
+ def download_and_extract(url, extract_to, is_zip=False):
12
+ filename = url.split("/")[-1]
13
+ if not os.path.exists(extract_to):
14
+ os.makedirs(extract_to)
15
+ filepath = os.path.join(extract_to, filename)
16
+ if not os.path.exists(filepath):
17
+ print(f"Downloading {filename} ...")
18
+ urllib.request.urlretrieve(url, filepath)
19
+ if is_zip:
20
+ with zipfile.ZipFile(filepath, "r") as zip_ref:
21
+ zip_ref.extractall(extract_to)
22
+ else:
23
+ with tarfile.open(filepath, "r:*") as tar:
24
+ tar.extractall(path=extract_to)
25
+
26
+ # 下载并解压 fluxbox
27
+ def setup_fluxbox():
28
+ url = "https://github.com/void-linux/void-packages/files/12496441/fluxbox-1.3.7-x86_64.tar.gz"
29
+ extract_to = "./fluxbox"
30
+ download_and_extract(url, extract_to)
31
+ return os.path.join(extract_to, "fluxbox-1.3.7-x86_64", "bin", "fluxbox")
32
+
33
+ # 下载并解压 TigerVNC
34
+ def setup_tigervnc():
35
+ url = "https://github.com/TigerVNC/tigervnc/releases/download/v1.13.1/tigervnc-1.13.1.x86_64.tar.gz"
36
+ extract_to = "./tigervnc"
37
+ download_and_extract(url, extract_to)
38
+ return os.path.join(extract_to, "tigervnc-1.13.1.x86_64", "vncserver")
39
+
40
+ # 下载并解压 noVNC
41
+ def setup_novnc():
42
+ url = "https://github.com/novnc/noVNC/archive/refs/heads/master.zip"
43
+ extract_to = "./novnc"
44
+ download_and_extract(url, extract_to, is_zip=True)
45
+ return os.path.join(extract_to, "noVNC-master")
46
+
47
+ # 启动 Xvfb
48
+ def start_xvfb():
49
+ return subprocess.Popen(["Xvfb", ":1", "-screen", "0", "1024x768x16"], env={**os.environ, "DISPLAY": ":1"})
50
+
51
+ # 启动 fluxbox
52
+ def start_fluxbox(fluxbox_path):
53
+ return subprocess.Popen([fluxbox_path], env={**os.environ, "DISPLAY": ":1"})
54
+
55
+ # 启动 VNC server
56
+ def start_vncserver(vncserver_path):
57
+ return subprocess.Popen([vncserver_path, ":1", "-geometry", "1024x768", "-SecurityTypes", "None"], env={**os.environ, "DISPLAY": ":1"})
58
+
59
+ # 启动 noVNC
60
+ def start_novnc(novnc_path):
61
+ websockify = os.path.join(novnc_path, "utils", "websockify", "run")
62
+ return subprocess.Popen(["python", websockify, "7860", "localhost:5901", "--web", novnc_path], env=os.environ)
63
+
64
+ # 启动 ssh_client.py
65
+ def start_ssh_client():
66
+ if os.path.exists("ssh_client.py"):
67
+ return subprocess.Popen(["python", "ssh_client.py"])
68
+ return None
69
+
70
+ def setup_and_run():
71
+ fluxbox_path = setup_fluxbox()
72
+ vncserver_path = setup_tigervnc()
73
+ novnc_path = setup_novnc()
74
+ # 启动 Xvfb
75
+ start_xvfb()
76
+ time.sleep(2)
77
+ # 启动 fluxbox
78
+ start_fluxbox(fluxbox_path)
79
+ time.sleep(2)
80
+ # 启动 VNC server
81
+ start_vncserver(vncserver_path)
82
+ time.sleep(2)
83
+ # 启动 noVNC
84
+ start_novnc(novnc_path)
85
+ # 启动 ssh_client.py
86
+ # start_ssh_client()
87
+
88
+ # 用线程后台启动桌面环境
89
+ threading.Thread(target=setup_and_run, daemon=True).start()
90
+
91
+ def hello():
92
+ return "欢迎来到 Huggingface Space!已启动 Fluxbox+TigerVNC+noVNC+SSH 客户端。\n" \
93
+ "请通过 Web VNC 访问桌面环境:\n" \
94
+ "http://<your-space-url>:7860/vnc.html"
95
+
96
+ demo = gr.Interface(fn=hello, inputs=None, outputs="text", title="Fluxbox TigerVNC Space")
97
+
98
+ if __name__ == "__main__":
99
+ demo.launch(server_name="0.0.0.0", server_port=7860)