ACCA225 commited on
Commit
9077355
1 Parent(s): 4336b6e

Upload app3.py

Browse files
Files changed (1) hide show
  1. app3.py +220 -0
app3.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 给我的朋友写的
2
+ import os
3
+ import threading
4
+ #使用的库
5
+ from pathlib import Path
6
+ import subprocess
7
+ import pandas as pd
8
+ import shutil
9
+ import os
10
+ import time
11
+ import re
12
+ import gc
13
+ import requests
14
+ import zipfile
15
+ import threading
16
+ import time
17
+ import socket
18
+ from concurrent.futures import ProcessPoolExecutor
19
+ # import wandb
20
+ os.system("pip install nvidia-ml-py3")
21
+ os.chdir(f"/home/xlab-app-center")
22
+ os.system(f"git clone https://openi.pcl.ac.cn/2575044704/sd-v1.7.0 /home/xlab-app-center/stable-diffusion-webui")
23
+ os.system(f"cp /home/xlab-app-center/styles.csv /home/xlab-app-center/stable-diffusion-webui/styles.csv")
24
+ os.chdir(f"/home/xlab-app-center/stable-diffusion-webui")
25
+ os.system(f"git lfs install")
26
+ os.system(f"git reset --hard")
27
+ import os
28
+
29
+ def create_directory(directory_path):
30
+ if not os.path.exists(directory_path):
31
+ os.makedirs(directory_path)
32
+
33
+ def download_file(url, destination_path):
34
+ os.system(f'wget -O {destination_path} {url}')
35
+
36
+ # 设置基本路径
37
+ base_directory = '/home/xlab-app-center/stable-diffusion-webui'
38
+ configs_directory = os.path.join(base_directory, 'configs')
39
+
40
+ # 创建configs文件夹
41
+ create_directory(configs_directory)
42
+
43
+ # 下载配置文件
44
+ #download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/config-pub.json', os.path.join(configs_directory, 'config.json'))
45
+ #download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pri.json', os.path.join(configs_directory, 'ui-config-pri.json'))
46
+ #download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub.json', os.path.join(configs_directory, 'ui-config.json'))
47
+ # 下载配置文件
48
+ download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/config-pub.json', os.path.join(base_directory, 'config.json'))
49
+ download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pri.json', os.path.join(base_directory, 'ui-config-pri.json'))
50
+ download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub.json', os.path.join(base_directory, 'ui-config.json'))
51
+
52
+ os.chdir(f"/home/xlab-app-center/stable-diffusion-webui/extensions")
53
+ show_shell_info = False
54
+ def run(command, cwd=None, desc=None, errdesc=None, custom_env=None,try_error:bool=True) -> str:
55
+ global show_shell_info
56
+ if desc is not None:
57
+ print(desc)
58
+
59
+ run_kwargs = {
60
+ "args": command,
61
+ "shell": True,
62
+ "cwd": cwd,
63
+ "env": os.environ if custom_env is None else custom_env,
64
+ "encoding": 'utf8',
65
+ "errors": 'ignore',
66
+ }
67
+
68
+ if not show_shell_info:
69
+ run_kwargs["stdout"] = run_kwargs["stderr"] = subprocess.PIPE
70
+
71
+ result = subprocess.run(**run_kwargs)
72
+
73
+ if result.returncode != 0:
74
+ error_bits = [
75
+ f"{errdesc or 'Error running command'}.",
76
+ f"Command: {command}",
77
+ f"Error code: {result.returncode}",
78
+ ]
79
+ if result.stdout:
80
+ error_bits.append(f"stdout: {result.stdout}")
81
+ if result.stderr:
82
+ error_bits.append(f"stderr: {result.stderr}")
83
+ if try_error:
84
+ print((RuntimeError("\n".join(error_bits))))
85
+ else:
86
+ raise RuntimeError("\n".join(error_bits))
87
+
88
+ if show_shell_info:
89
+ print((result.stdout or ""))
90
+ return (result.stdout or "")
91
+
92
+ def mkdirs(path, exist_ok=True):
93
+ if path and not Path(path).exists():
94
+ os.makedirs(path,exist_ok=exist_ok)
95
+ plugins = [
96
+ "https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans",
97
+ "https://gitcode.net/ranting8323/multidiffusion-upscaler-for-automatic1111",
98
+ "https://gitcode.net/ranting8323/adetailer",
99
+ "https://gitcode.net/ranting8323/sd-webui-prompt-all-in-one",
100
+ "https://gitcode.net/ranting8323/sd-webui-inpaint-anything",
101
+ "https://gitcode.net/ranting8323/a1111-sd-webui-tagcomplete",
102
+ "https://gitcode.net/nightaway/sd-webui-infinite-image-browsing",
103
+ "https://openi.pcl.ac.cn/2575044704/sd-extension-system-info",
104
+ "https://openi.pcl.ac.cn/2575044704/batchlinks-webui",
105
+ 'https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-localization-zh_CN',
106
+ #'https://openi.pcl.ac.cn/2575044704/sd-webui-supermerger',
107
+ 'https://openi.pcl.ac.cn/2575044704/sd-webui-lora-block-weight',
108
+ 'https://openi.pcl.ac.cn/2575044704/sd-skin-extension',
109
+
110
+
111
+ ]
112
+
113
+ for plugin in plugins:
114
+ os.system(f"git clone {plugin}")
115
+ os.makedirs('/home/xlab-app-center/stable-diffusion-webui/models/adetailer', exist_ok=True)
116
+ os.chdir(f"/home/xlab-app-center/stable-diffusion-webui/models/adetailer")
117
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/Bingsu/adetailer/resolve/main/hand_yolov8s.pt -d /home/xlab-app-center/stable-diffusion-webui/models/adetailer -o hand_yolov8s.pt")
118
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/Bingsu/adetailer/resolve/main/hand_yolov8n.pt -d /home/xlab-app-center/stable-diffusion-webui/models/adetailer -o hand_yolov8n.pt")
119
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/kaggle/input/museum/131-half.safetensors -d /home/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion -o [萌二次元]131-half.safetensors")
120
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/ba.safetensors -d /home/xlab-app-center/stable-diffusion-webui/models/Lora -o ba.safetensors")
121
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/racaco2.safetensors -d /home/xlab-app-center/stable-diffusion-webui/models/Lora -o racaco2.safetensors")
122
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/coinz/Add-detail/resolve/main/add_detail.safetensors -d /home/xlab-app-center/stable-diffusion-webui/models/Lora -o add_detail.safetensors")
123
+ os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://hf-mirror.com/datasets/VASVASVAS/vae/resolve/main/pastel-waifu-diffusion.vae.pt -d /home/xlab-app-center/stable-diffusion-webui/models/VAE -o pastel-waifu-diffusion.vae.pt")
124
+ # os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://download.openxlab.org.cn/models/camenduru/sdxl-refiner-1.0/weight//sd_xl_refiner_1.0.safetensors -d /home/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion -o sd_xl_refiner_1.0.safetensors")
125
+ # os.system(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://download.openxlab.org.cn/models/camenduru/cyber-realistic/weight//cyberrealistic_v32.safetensors -d /home/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion -o cyberrealistic_v32.safetensors")
126
+ os.chdir(f"/home/xlab-app-center/stable-diffusion-webui")
127
+ print('webui launching...')
128
+ package_envs = [
129
+ {"env": "STABLE_DIFFUSION_REPO", "url": os.environ.get('STABLE_DIFFUSION_REPO', "https://gitcode.net/overbill1683/stablediffusion")},
130
+ {"env": "STABLE_DIFFUSION_XL_REPO", "url": os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://gitcode.net/overbill1683/generative-models")},
131
+ {"env": "K_DIFFUSION_REPO", "url": os.environ.get('K_DIFFUSION_REPO', "https://gitcode.net/overbill1683/k-diffusion")},
132
+ {"env": "CODEFORMER_REPO", "url": os.environ.get('CODEFORMER_REPO', "https://gitcode.net/overbill1683/CodeFormer")},
133
+ {"env": "BLIP_REPO", "url": os.environ.get('BLIP_REPO', "https://gitcode.net/overbill1683/BLIP")},
134
+ ]
135
+ os.environ["PIP_INDEX_URL"] = "https://mirrors.aliyun.com/pypi/simple/"
136
+ for i in package_envs:
137
+ os.environ[i["env"]] = i["url"]
138
+
139
+ import os
140
+ import time
141
+ import wandb
142
+
143
+ import os
144
+ import time
145
+ import wandb
146
+
147
+ # nginx 反向代理配置文件
148
+ def echoToFile(content:str,path:str):
149
+ if path.find('/') >= 0:
150
+ _path = '/'.join(path.split('/')[:-1])
151
+ run(f'''mkdir -p {_path}''')
152
+ with open(path,'w') as sh:
153
+ sh.write(content)
154
+ # 检查网络
155
+ def check_service(host, port):
156
+ try:
157
+ socket.create_connection((host, port), timeout=5)
158
+ return True
159
+ except socket.error:
160
+ return False
161
+ def localProxy():
162
+ os.system('sudo apt install nginx -y')
163
+ download_file('https://huggingface.co/datasets/ACCA225/openxlab/resolve/main/proxy_nginx.conf', os.path.join(base_directory, 'proxy_nginx.conf'))
164
+ if not check_service('localhost',_server_port):
165
+ run(f'''nginx -c /home/xlab-app-center/stable-diffusion-webui/proxy_nginx.conf''')
166
+ run(f'''nginx -s reload''')
167
+
168
+ # WandB登录
169
+ os.system('wandb login 5c00964de1bb95ec1ab24869d4c523c59e0fb8e3')
170
+
171
+ # 初始化WandB项目
172
+ wandb.init(project="gpu-temperature-monitor")
173
+
174
+ import os
175
+ import threading
176
+ import wandb
177
+ import time
178
+ def monitor_gpu():
179
+ while True:
180
+ try:
181
+ # 获取 GPU 温度
182
+ handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0) # 0 表示第一个 GPU
183
+ gpu_temperature = nvidia_smi.nvmlDeviceGetTemperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)
184
+
185
+ # 获取 GPU 使用率
186
+ utilization = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
187
+ gpu_usage = utilization.gpu
188
+
189
+ # 使用 WandB 记录 GPU 温度和使用率
190
+ wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})
191
+
192
+ except Exception as e:
193
+ print(f"Error: {e}")
194
+
195
+ time.sleep(60)
196
+
197
+ def start():
198
+ try:
199
+ print('启动proxy')
200
+ threading.Thread(target = localProxy,daemon=True).start()
201
+ except Exception as e:
202
+ # 在这里处理异常的代码
203
+ print(f"proxy An error occurred: {e}")
204
+ try:
205
+ os.system(f"python launch.py --api --xformers --exit --enable-insecure-extension-access --ui-settings-file /home/xlab-app-center/config.json --ui-config-file /home/xlab-app-center/ui-config.json --gradio-queue --disable-safe-unpickle")
206
+ os.system(f"python launch.py --ngrok=2YypH9d2VXVL78HxF7g0lintH17_2GbtkFNFFngUkTZcyQ4SD --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle & python launch.py --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle --port=7862 & python launch.py --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle --port=7863")
207
+ except Exception as e:
208
+ # 在这里处理异常的代码
209
+ print(f"An error occurred: {e}")
210
+ # Create threads for each function
211
+ wandb_thread = threading.Thread(target=monitor_gpu)
212
+ start_thread = threading.Thread(target=start)
213
+
214
+ # Start the threads
215
+ wandb_thread.start()
216
+ start_thread.start()
217
+
218
+ # Wait for both threads to finish
219
+ wandb_thread.join()
220
+ start_thread.join()