tzk1212123 commited on
Commit
dab6235
·
verified ·
1 Parent(s): 4537bb8

Upload 4 files

Browse files
Files changed (4) hide show
  1. sd.sh +38 -0
  2. ssh.py +124 -0
  3. ssh7865.py +124 -0
  4. start.py +73 -0
sd.sh ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 可以存文件的地方:
2
+ """
3
+ 目录: / (可用空间: 60 GB)
4
+ 写入速度: 1556.90 MB/s
5
+ 读取速度: 5127.25 MB/s
6
+
7
+ 目录: /dev/shm (可用空间: 30 GB)
8
+ 写入速度: 3954.25 MB/s
9
+ 读取速度: 4598.10 MB/s
10
+
11
+ 目录: /etc/sgpu/pmem (可用空间: 48 GB)
12
+ 写入速度: 7371.38 MB/s
13
+ 读取速度: 6247.02 MB/s
14
+
15
+ 目录: /proc/driver/nvidia (可用空间: 32 GB) (容量超出会导致严重系统错误)
16
+ 写入速度: 3942.13 MB/s
17
+ 读取速度: 4594.20 MB/s
18
+ """
19
+ apt update && apt install -y aria2
20
+ aria2c -x 16 -s 16 "https://hf-mirror.com/datasets/nyan102/modelscope_code/resolve/main/ssh7865.py" -o ssh7865.py -d /root
21
+ echo 安装SD
22
+ cd /etc/sgpu/pmem/ && aria2c -x 16 -s 16 -c -k 1M "https://www.modelscope.cn/models/ACCC1380/sd_buckup_20241117_1940/resolve/master/sd-webui.zip" -o sd-webui.zip
23
+ cd /etc/sgpu/pmem/ && aria2c -x 16 -s 16 -c -k 1M "https://www.modelscope.cn/models/ACCC1380/sd_buckup_20241117_1940/resolve/master/sd-webui.zip" -o sd-webui.zip #多弄一条以免下载失败
24
+ cd /etc/sgpu/pmem/ && unzip ./sd-webui.zip && rm ./sd-webui.zip
25
+ echo 安装venv和模型
26
+ cd /etc/sgpu/pmem/ && aria2c -x 16 -s 16 -c -k 1M "https://www.modelscope.cn/models/ACCC1380/sd_buckup_20241117_1940/resolve/master/sd-venv.tar.safetensors" -o venv.tar
27
+ cd /etc/sgpu/pmem/ && tar -xvf venv.tar & aria2c -x 16 -s 16 -c -k 1M "https://www.modelscope.cn/models/ACCC1380/Noob_Final/resolve/master/Noob_v1.0.safetensors" -o NoobXL-v1.0.safetensors -d /etc/sgpu/pmem/sdmodels
28
+
29
+ echo 创建模型目录
30
+ mkdir -p /etc/sgpu/pmem/sdmodels
31
+ mkdir -p /etc/sgpu/pmem/lora
32
+ mkdir -p /etc/sgpu/pmem/vae
33
+ echo 安装必要文件
34
+ cd /etc/sgpu/pmem/tmp/stable-diffusion-webui/models/VAE-approx && wget -O vaeapprox-sdxl.pt "https://www.modelscope.cn/models/ACCC1380/Noobxl/resolve/master/vaeapprox-sdxl.pt" --no-check-certificate
35
+ cd /etc/sgpu/pmem/tmp/stable-diffusion-webui && wget -O rep_github.py "https://hf-mirror.com/datasets/ACCA225/useful_code/raw/main/github%E5%BC%BA%E5%88%B6%E6%8D%A2%E6%BA%90.py" --no-check-certificate
36
+ cd /etc/sgpu/pmem/tmp/stable-diffusion-webui && python rep_github.py
37
+ echo 开始启动sd
38
+ cd /etc/sgpu/pmem/tmp/stable-diffusion-webui && HF_ENDPOINT=https://hf-mirror.com /etc/sgpu/pmem/venv/bin/python launch.py --api --port=7865 --xformers --ckpt-dir=/etc/sgpu/pmem/sdmodels --lora-dir=/etc/sgpu/pmem/lora --vae-dir=/etc/sgpu/pmem/vae & python /root/ssh7865.py & aria2c -x 16 -s 16 -c -k 1M "https://www.modelscope.cn/models/ACCC1380/Noobxl/resolve/master/NoobXL-Epsilon.safetensors" -o NoobXL-v0.5.safetensors -d /etc/sgpu/pmem/sdmodels
ssh.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("pip install paramiko")
3
+ import socket
4
+ import sys
5
+ import threading
6
+ import paramiko
7
+ import select
8
+ import time
9
+ # 配置参数
10
+ USERNAME = 'root'
11
+ PASSWORD = os.environ.get('SSH_PASSWORD')
12
+ HOST = 'MikaOvO.com'
13
+ REMOTE_PORT = 65432
14
+ LOCAL_HOST = '127.0.0.1'
15
+ LOCAL_PORT = 65432
16
+ KEEP_ALIVE_INTERVAL = 60 # 发送保持连接的间隔时间(秒)
17
+
18
+ # 定义一个类来处理转发请求
19
+ class ReverseForwardServer(threading.Thread):
20
+ def __init__(self, transport, remote_port, local_host, local_port):
21
+ super(ReverseForwardServer, self).__init__()
22
+ self.transport = transport
23
+ self.remote_port = remote_port
24
+ self.local_host = local_host
25
+ self.local_port = local_port
26
+ self.daemon = True # 使线程随主线程退出
27
+ self.start()
28
+
29
+ def run(self):
30
+ # 请求远程端口转发
31
+ try:
32
+ self.transport.request_port_forward('', self.remote_port)
33
+ print(f"请求在远程服务器上监听端口 {self.remote_port},转发到本地 {self.local_host}:{self.local_port}")
34
+ except Exception as e:
35
+ print(f'无法请求远程端口转发: {e}')
36
+ sys.exit(1)
37
+
38
+ while True:
39
+ try:
40
+ chan = self.transport.accept(1000) # 等待新连接,超时1秒
41
+ if chan is None:
42
+ continue
43
+ thr = threading.Thread(target=self.handle_channel, args=(chan,))
44
+ thr.daemon = True
45
+ thr.start()
46
+ except Exception as e:
47
+ print(f"处理通道时发生错误: {e}")
48
+ break
49
+
50
+ def handle_channel(self, chan):
51
+ try:
52
+ # 连接到本地服务
53
+ sock = socket.socket()
54
+ sock.connect((self.local_host, self.local_port))
55
+ except Exception as e:
56
+ print(f"无法连接到本地服务 {self.local_host}:{self.local_port}: {e}")
57
+ chan.close()
58
+ return
59
+
60
+ print(f"建立连接: {chan.origin_addr} -> {self.local_host}:{self.local_port}")
61
+
62
+ # 双向转发数据
63
+ def forward(src, dst):
64
+ try:
65
+ while True:
66
+ data = src.recv(1024)
67
+ if not data:
68
+ break
69
+ dst.sendall(data)
70
+ except Exception:
71
+ pass
72
+ finally:
73
+ src.close()
74
+ dst.close()
75
+
76
+ # 启动两个线程进行数据转发
77
+ t1 = threading.Thread(target=forward, args=(chan, sock))
78
+ t2 = threading.Thread(target=forward, args=(sock, chan))
79
+ t1.daemon = True
80
+ t2.daemon = True
81
+ t1.start()
82
+ t2.start()
83
+
84
+ def main():
85
+ while True: # 自动重连循环
86
+ client = paramiko.SSHClient()
87
+ client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 自动添加主机密钥
88
+
89
+ try:
90
+ print(f"正在连接到 {HOST}...")
91
+ client.connect(hostname=HOST, username=USERNAME, password=PASSWORD)
92
+ print("连接成功!")
93
+
94
+ transport = client.get_transport()
95
+ if transport is None or not transport.is_active():
96
+ print("SSH Transport不可用")
97
+ raise Exception("SSH Transport不可用")
98
+
99
+ # 设置保持连接(发送心跳)
100
+ transport.set_keepalive(KEEP_ALIVE_INTERVAL)
101
+ print(f"已设置保持连接,每 {KEEP_ALIVE_INTERVAL} 秒发送一次心跳包")
102
+
103
+ # 启动反向端口转发服务器
104
+ ReverseForwardServer(transport, REMOTE_PORT, LOCAL_HOST, LOCAL_PORT)
105
+
106
+ print(f"已在远程服务器 {HOST} 上监听端口 {REMOTE_PORT},并转发到本地 {LOCAL_HOST}:{LOCAL_PORT}")
107
+
108
+ # 保持主线程运行,并监控连接状态
109
+ while transport.is_active():
110
+ time.sleep(1)
111
+
112
+ except KeyboardInterrupt:
113
+ print("用户中断,关闭连接。")
114
+ break
115
+ except Exception as e:
116
+ print(f"发生错误: {e}")
117
+ print("尝试重新连接...")
118
+ time.sleep(5) # 等待5秒后重试
119
+ finally:
120
+ client.close()
121
+ print("SSH连接已关闭。")
122
+
123
+ if __name__ == "__main__":
124
+ main()
ssh7865.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("pip install paramiko")
3
+ import socket
4
+ import sys
5
+ import threading
6
+ import paramiko
7
+ import select
8
+ import time
9
+ # 配置参数
10
+ USERNAME = 'root'
11
+ PASSWORD = os.environ.get('SSH_PASSWORD')
12
+ HOST = 'MikaOvO.com'
13
+ REMOTE_PORT = 7865
14
+ LOCAL_HOST = '127.0.0.1'
15
+ LOCAL_PORT = 7865
16
+ KEEP_ALIVE_INTERVAL = 60 # 发送保持连接的间隔时间(秒)
17
+
18
+ # 定义一个类来处理转发请求
19
+ class ReverseForwardServer(threading.Thread):
20
+ def __init__(self, transport, remote_port, local_host, local_port):
21
+ super(ReverseForwardServer, self).__init__()
22
+ self.transport = transport
23
+ self.remote_port = remote_port
24
+ self.local_host = local_host
25
+ self.local_port = local_port
26
+ self.daemon = True # 使线程随主线程退出
27
+ self.start()
28
+
29
+ def run(self):
30
+ # 请求远程端口转发
31
+ try:
32
+ self.transport.request_port_forward('', self.remote_port)
33
+ print(f"请求在远程服务器上监听端口 {self.remote_port},转发到本地 {self.local_host}:{self.local_port}")
34
+ except Exception as e:
35
+ print(f'无法请求远程端口转发: {e}')
36
+ sys.exit(1)
37
+
38
+ while True:
39
+ try:
40
+ chan = self.transport.accept(1000) # 等待新连接,超时1秒
41
+ if chan is None:
42
+ continue
43
+ thr = threading.Thread(target=self.handle_channel, args=(chan,))
44
+ thr.daemon = True
45
+ thr.start()
46
+ except Exception as e:
47
+ print(f"处理通道时发生错误: {e}")
48
+ break
49
+
50
+ def handle_channel(self, chan):
51
+ try:
52
+ # 连接到本地服务
53
+ sock = socket.socket()
54
+ sock.connect((self.local_host, self.local_port))
55
+ except Exception as e:
56
+ print(f"无法连接到本地服务 {self.local_host}:{self.local_port}: {e}")
57
+ chan.close()
58
+ return
59
+
60
+ print(f"建立连接: {chan.origin_addr} -> {self.local_host}:{self.local_port}")
61
+
62
+ # 双向转发数据
63
+ def forward(src, dst):
64
+ try:
65
+ while True:
66
+ data = src.recv(1024)
67
+ if not data:
68
+ break
69
+ dst.sendall(data)
70
+ except Exception:
71
+ pass
72
+ finally:
73
+ src.close()
74
+ dst.close()
75
+
76
+ # 启动两个线程进行数据转发
77
+ t1 = threading.Thread(target=forward, args=(chan, sock))
78
+ t2 = threading.Thread(target=forward, args=(sock, chan))
79
+ t1.daemon = True
80
+ t2.daemon = True
81
+ t1.start()
82
+ t2.start()
83
+
84
+ def main():
85
+ while True: # 自动重连循环
86
+ client = paramiko.SSHClient()
87
+ client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 自动添加主机密钥
88
+
89
+ try:
90
+ print(f"正在连接到 {HOST}...")
91
+ client.connect(hostname=HOST, username=USERNAME, password=PASSWORD)
92
+ print("连接成功!")
93
+
94
+ transport = client.get_transport()
95
+ if transport is None or not transport.is_active():
96
+ print("SSH Transport不可用")
97
+ raise Exception("SSH Transport不可用")
98
+
99
+ # 设置保持连接(发送心跳)
100
+ transport.set_keepalive(KEEP_ALIVE_INTERVAL)
101
+ print(f"已设置保持连接,每 {KEEP_ALIVE_INTERVAL} 秒发送一次心跳包")
102
+
103
+ # 启动反向端口转发服务器
104
+ ReverseForwardServer(transport, REMOTE_PORT, LOCAL_HOST, LOCAL_PORT)
105
+
106
+ print(f"已在远程服务器 {HOST} 上监听端口 {REMOTE_PORT},并转发到本地 {LOCAL_HOST}:{LOCAL_PORT}")
107
+
108
+ # 保持主线程运行,并监控连接状态
109
+ while transport.is_active():
110
+ time.sleep(1)
111
+
112
+ except KeyboardInterrupt:
113
+ print("用户中断,关闭连接。")
114
+ break
115
+ except Exception as e:
116
+ print(f"发生错误: {e}")
117
+ print("尝试重新连接...")
118
+ time.sleep(5) # 等待5秒后重试
119
+ finally:
120
+ client.close()
121
+ print("SSH连接已关闭。")
122
+
123
+ if __name__ == "__main__":
124
+ main()
start.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # python 3.10.9
2
+ # Nyan9 All rights reserved
3
+ import gradio as gr
4
+ import subprocess
5
+ import shlex
6
+ import os
7
+ import subprocess
8
+ import sys
9
+ import tarfile
10
+
11
+ # 清理环境
12
+ os.system("rm -rf /root/venv* && rm -rf /dev/shm/*")
13
+
14
+ # 定义仓库ID
15
+ repo_id = "tzk1212123/modelscope_code"
16
+
17
+ def nyan_ngrok():
18
+ def way1():
19
+ # 下载奇怪的GPG密钥
20
+ subprocess.run("curl -sSL https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc > /dev/null", shell=True, check=True)
21
+ # APT 源
22
+ subprocess.run("echo 'deb https://ngrok-agent.s3.amazonaws.com buster main' | sudo tee /etc/apt/sources.list.d/ngrok.list", shell=True, check=True)
23
+ # 更新 APT
24
+ subprocess.run("sudo apt update", shell=True, check=True)
25
+ subprocess.run("sudo apt install ngrok -y", shell=True, check=True)
26
+
27
+ def way2():
28
+ # 下载nyan9作者修改的ngrok
29
+ ngrok_url = f"https://hf-mirror.com/datasets/{repo_id}/resolve/main/ngrok-v3-stable-linux-amd64.tgz"
30
+ ngrok_path = os.path.expanduser("~/ngrok-v3-stable-linux-amd64.tgz")
31
+ print("Downloading ngrok...")
32
+ try:
33
+ # 使用 aria2c 替换 wget,并正确指定下载目录和保存名称
34
+ subprocess.run([
35
+ "aria2c", "-x", "16", "-s", "16", "-c", "-k", "1M",
36
+ "-d", os.path.dirname(ngrok_path), # 下载目录
37
+ "-o", os.path.basename(ngrok_path), # 保存文件名
38
+ ngrok_url
39
+ ], check=True)
40
+ except subprocess.CalledProcessError:
41
+ print("下载失败,将采用方案一")
42
+ way1()
43
+
44
+ # /usr/local/bin
45
+ print("Extracting ngrok...")
46
+ try:
47
+ # 使用 sudo 权限解压
48
+ subprocess.run([
49
+ "sudo", "tar", "-xvzf", ngrok_path, "-C", "/usr/local/bin"
50
+ ], check=True)
51
+ except Exception as e:
52
+ print("解压失败,将采用方案一APT安装")
53
+ way1()
54
+ print("ngrok has been installed successfully.")
55
+
56
+ way2()
57
+
58
+ # 更新 APT
59
+ subprocess.run("sudo apt update", shell=True, check=True)
60
+ nyan_ngrok()
61
+
62
+ # launch
63
+ os.chdir("/root")
64
+
65
+ # 下载必要文件
66
+ os.system(f"wget 'https://hf-mirror.com/datasets/{repo_id}/resolve/main/app.py' --no-check-certificate")
67
+ os.system(f"wget 'https://hf-mirror.com/datasets/{repo_id}/resolve/main/sd.sh' --no-check-certificate")
68
+ os.system(f"wget -O 'ssh.py' --no-check-certificate 'https://hf-mirror.com/datasets/{repo_id}/resolve/main/ssh.py?download=true'")
69
+
70
+ while True:
71
+ os.chdir("/root")
72
+ os.system("apt install -y aria2 sshpass")
73
+ os.system("python app.py & bash sd.sh & python ssh.py & jupyter-lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir=/ --port=65432 --LabApp.allow_origin=* --LabApp.token=Asdf1472580369 --LabApp.base_url=/loves & ngrok http 65432 --authtoken=2p4LB2DMncND09TtByIgYoPCiMd_4ngvZJW4VmhQPK3etQfLh")