File size: 3,367 Bytes
e9d6dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import subprocess
import threading
import gradio as gr
import time
import urllib.request
import tarfile
import zipfile

# 下载并解压工具函数
def download_and_extract(url, extract_to, is_zip=False):
    filename = url.split("/")[-1]
    if not os.path.exists(extract_to):
        os.makedirs(extract_to)
    filepath = os.path.join(extract_to, filename)
    if not os.path.exists(filepath):
        print(f"Downloading {filename} ...")
        urllib.request.urlretrieve(url, filepath)
    if is_zip:
        with zipfile.ZipFile(filepath, "r") as zip_ref:
            zip_ref.extractall(extract_to)
    else:
        with tarfile.open(filepath, "r:*") as tar:
            tar.extractall(path=extract_to)

# 下载并解压 fluxbox
def setup_fluxbox():
    url = "https://github.com/void-linux/void-packages/files/12496441/fluxbox-1.3.7-x86_64.tar.gz"
    extract_to = "./fluxbox"
    download_and_extract(url, extract_to)
    return os.path.join(extract_to, "fluxbox-1.3.7-x86_64", "bin", "fluxbox")

# 下载并解压 TigerVNC
def setup_tigervnc():
    url = "https://github.com/TigerVNC/tigervnc/releases/download/v1.13.1/tigervnc-1.13.1.x86_64.tar.gz"
    extract_to = "./tigervnc"
    download_and_extract(url, extract_to)
    return os.path.join(extract_to, "tigervnc-1.13.1.x86_64", "vncserver")

# 下载并解压 noVNC
def setup_novnc():
    url = "https://github.com/novnc/noVNC/archive/refs/heads/master.zip"
    extract_to = "./novnc"
    download_and_extract(url, extract_to, is_zip=True)
    return os.path.join(extract_to, "noVNC-master")

# 启动 Xvfb
def start_xvfb():
    return subprocess.Popen(["Xvfb", ":1", "-screen", "0", "1024x768x16"], env={**os.environ, "DISPLAY": ":1"})

# 启动 fluxbox
def start_fluxbox(fluxbox_path):
    return subprocess.Popen([fluxbox_path], env={**os.environ, "DISPLAY": ":1"})

# 启动 VNC server
def start_vncserver(vncserver_path):
    return subprocess.Popen([vncserver_path, ":1", "-geometry", "1024x768", "-SecurityTypes", "None"], env={**os.environ, "DISPLAY": ":1"})

# 启动 noVNC
def start_novnc(novnc_path):
    websockify = os.path.join(novnc_path, "utils", "websockify", "run")
    return subprocess.Popen(["python", websockify, "7860", "localhost:5901", "--web", novnc_path], env=os.environ)

# 启动 ssh_client.py
def start_ssh_client():
    if os.path.exists("ssh_client.py"):
        return subprocess.Popen(["python", "ssh_client.py"])
    return None

def setup_and_run():
    fluxbox_path = setup_fluxbox()
    vncserver_path = setup_tigervnc()
    novnc_path = setup_novnc()
    # 启动 Xvfb
    start_xvfb()
    time.sleep(2)
    # 启动 fluxbox
    start_fluxbox(fluxbox_path)
    time.sleep(2)
    # 启动 VNC server
    start_vncserver(vncserver_path)
    time.sleep(2)
    # 启动 noVNC
    start_novnc(novnc_path)
    # 启动 ssh_client.py
    # start_ssh_client()

# 用线程后台启动桌面环境
threading.Thread(target=setup_and_run, daemon=True).start()

def hello():
    return "欢迎来到 Huggingface Space!已启动 Fluxbox+TigerVNC+noVNC+SSH 客户端。\n" \
           "请通过 Web VNC 访问桌面环境:\n" \
           "http://<your-space-url>:7860/vnc.html"

demo = gr.Interface(fn=hello, inputs=None, outputs="text", title="Fluxbox TigerVNC Space")

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)