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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 配置
|
2 |
+
import os
|
3 |
+
install_path = '/home/xlab-app-center'
|
4 |
+
webui_repo = 'AUTOMATIC1111/stable-diffusion-webui --branch v1.9.4'
|
5 |
+
rename_repo = 'stable-diffusion-webui'
|
6 |
+
git_url = 'gitcode.com'
|
7 |
+
webui_port = 7860
|
8 |
+
|
9 |
+
api_auth = 'Echoflare:Tt25faj8'
|
10 |
+
|
11 |
+
download_tool = 'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M'
|
12 |
+
|
13 |
+
webui_args = [
|
14 |
+
'--api',
|
15 |
+
'--xformers',
|
16 |
+
'--no-hashing',
|
17 |
+
'--disable-nan-check',
|
18 |
+
'--disable-console-progressbars',
|
19 |
+
'--enable-console-prompts',
|
20 |
+
#'--no-gradio-queue',
|
21 |
+
'--no-half-vae',
|
22 |
+
"--skip-torch-cuda-test",
|
23 |
+
"--allow-code"
|
24 |
+
# f'--api-auth={api_auth}',
|
25 |
+
# '--freeze-settings',
|
26 |
+
]
|
27 |
+
|
28 |
+
extensions = [
|
29 |
+
'https://gitcode.com/zanllp/sd-webui-infinite-image-browsing',
|
30 |
+
'https://gitcode.com/dtlnor/stable-diffusion-webui-localization-zh_CN', # 汉化
|
31 |
+
'https://gitcode.com/DominikDoom/a1111-sd-webui-tagcomplete', # 提示词提示器
|
32 |
+
'https://gitcode.com/Mikubill/sd-webui-controlnet', # ControlNet
|
33 |
+
"https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans",
|
34 |
+
"https://gitcode.net/ranting8323/adetailer",
|
35 |
+
"https://gitcode.net/ranting8323/sd-webui-inpaint-anything",
|
36 |
+
"https://openi.pcl.ac.cn/2575044704/sd-extension-system-info",
|
37 |
+
"https://openi.pcl.ac.cn/2575044704/batchlinks-webui",
|
38 |
+
'https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-localization-zh_CN',
|
39 |
+
'https://openi.pcl.ac.cn/2575044704/sd-webui-lora-block-weight',
|
40 |
+
'https://openi.pcl.ac.cn/2575044704/sd-skin-extension',
|
41 |
+
"https://kkgithub.com/thygate/stable-diffusion-webui-depthmap-script.git",
|
42 |
+
"https://kkgithub.com/continue-revolution/sd-webui-animatediff.git",
|
43 |
+
"https://kkgithub.com/Iyashinouta/sd-model-downloader.git",
|
44 |
+
"https://kkgithub.com/fkunn1326/openpose-editor.git",
|
45 |
+
"https://kkgithub.com/zero01101/openOutpaint-webUI-extension.git",
|
46 |
+
"https://kkgithub.com/LonicaMewinsky/gif2gif.git",
|
47 |
+
"https://openi.pcl.ac.cn/2575044704/sd-webui-agent-scheduler",
|
48 |
+
"https://openi.pcl.ac.cn/2575044704/sd-webui-depth-lib",
|
49 |
+
"https://openi.pcl.ac.cn/Echoflare/letest"
|
50 |
+
]
|
51 |
+
|
52 |
+
sd_models = [
|
53 |
+
#"kohakuXLDelta_rev1.safetensors@https://civitai-delivery-worker-prod.5ac0637cfd0766c97916cefa3764fbdf.r2.cloudflarestorage.com/model/478208/kohakuXlDeltaRev1.OzRM.safetensors?X-Amz-Expires=86400&response-content-disposition=attachment%3B%20filename%3D%22kohakuXLDelta_rev1.safetensors%22&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=e01358d793ad6966166af8b3064953ad/20240323/us-east-1/s3/aws4_request&X-Amz-Date=20240323T165433Z&X-Amz-SignedHeaders=host&X-Amz-Signature=7a14ba6161f355f0dd93229089f503fbbe94fbe994d42ba1e7efc1566ba584a6",
|
54 |
+
]
|
55 |
+
lora_models = [
|
56 |
+
"https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/ba.safetensors",
|
57 |
+
"https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/racaco2.safetensors",
|
58 |
+
|
59 |
+
]
|
60 |
+
|
61 |
+
vae_models = [
|
62 |
+
"https://hf-mirror.com/datasets/VASVASVAS/vae/resolve/main/pastel-waifu-diffusion.vae.pt"
|
63 |
+
]
|
64 |
+
|
65 |
+
ControlNet = True
|
66 |
+
|
67 |
+
controlnet_models = [
|
68 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors',
|
69 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11e_sd15_shuffle_fp16.safetensors',
|
70 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11f1p_sd15_depth_fp16.safetensors',
|
71 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_canny_fp16.safetensors',
|
72 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_inpaint_fp16.safetensors',
|
73 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_lineart_fp16.safetensors',
|
74 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_mlsd_fp16.safetensors',
|
75 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_normalbae_fp16.safetensors',
|
76 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_openpose_fp16.safetensors',
|
77 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_scribble_fp16.safetensors',
|
78 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_softedge_fp16.safetensors',
|
79 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15s2_lineart_anime_fp16.safetensors',
|
80 |
+
'https://hf-mirror.com/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11u_sd15_tile_fp16.safetensors',
|
81 |
+
'https://hf-mirror.com/DionTimmer/controlnet_qrcode-control_v1p_sd15/resolve/main/control_v1p_sd15_qrcode.safetensors',
|
82 |
+
]
|
83 |
+
|
84 |
+
embedding_models = [
|
85 |
+
"unaestheticXL_Alb2.safetensors@https://civitai-delivery-worker-prod.5ac0637cfd0766c97916cefa3764fbdf.r2.cloudflarestorage.com/model/61235/unaestheticxlAlb2.7VDr.safetensors?X-Amz-Expires=86400&response-content-disposition=attachment%3B%20filename%3D%22unaestheticXL_Alb2.safetensors%22&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=e01358d793ad6966166af8b3064953ad/20240324/us-east-1/s3/aws4_request&X-Amz-Date=20240324T042909Z&X-Amz-SignedHeaders=host&X-Amz-Signature=9d4dd419dd2e579ca1ae88fe3315f4245edc5dbfb82d38e6aaa0cd383b8889c0",
|
86 |
+
]
|
87 |
+
|
88 |
+
hypernetwork_models = []
|
89 |
+
|
90 |
+
esrgan_models = []
|
91 |
+
|
92 |
+
custom_commands = [
|
93 |
+
f'rm -rf {install_path}/{rename_repo}/config.json',
|
94 |
+
f'rm -rf {install_path}/{rename_repo}/ui-config.json',
|
95 |
+
f'rm -rf {install_path}/{rename_repo}/modules/ui_settings.py',
|
96 |
+
f"{download_tool} https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/config-pub.json -d {install_path}/{rename_repo} -o config.json --allow-overwrite=true",
|
97 |
+
f"{download_tool} https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub3.json -d {install_path}/{rename_repo} -o ui-config.json --allow-overwrite=true",
|
98 |
+
f"{download_tool} https://hf-mirror.com/datasets/Mira-LeafTown/sd-webui-openxlab/raw/main/ui_settings.py -d {install_path}/{rename_repo}/modules -o ui_settings.py --allow-overwrite=true",
|
99 |
+
]
|
100 |
+
|
101 |
+
# 网页UI部署
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
def monitor_gpu():
|
106 |
+
|
107 |
+
import os
|
108 |
+
import re
|
109 |
+
import os
|
110 |
+
import time
|
111 |
+
import threading
|
112 |
+
import wandb
|
113 |
+
import pynvml as nvidia_smi
|
114 |
+
# WandB登录
|
115 |
+
os.system('wandb login 5c00964de1bb95ec1ab24869d4c523c59e0fb8e3')
|
116 |
+
|
117 |
+
# 初始化 NVML
|
118 |
+
nvidia_smi.nvmlInit()
|
119 |
+
|
120 |
+
# 初始化WandB项目
|
121 |
+
wandb.init(project="gpu-temperature-monitor")
|
122 |
+
while True:
|
123 |
+
try:
|
124 |
+
# 获取 GPU 句柄
|
125 |
+
handle = nvidia_smi.nvmlDevice获取HandleByIndex(0) # 0 表示第一个 GPU
|
126 |
+
|
127 |
+
# 获取 GPU 温度
|
128 |
+
gpu_temperature = nvidia_smi.nvmlDevice获取Temperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)
|
129 |
+
|
130 |
+
# 获取 GPU 使用率
|
131 |
+
utilization = nvidia_smi.nvmlDevice获取UtilizationRates(handle)
|
132 |
+
gpu_usage = utilization.gpu
|
133 |
+
|
134 |
+
# 使用 WandB 记录 GPU 温度和使用率
|
135 |
+
wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(f"Error: {e}")
|
139 |
+
|
140 |
+
time.sleep(60)
|
141 |
+
|
142 |
+
|
143 |
+
def download_extensions(extensions):
|
144 |
+
os.chdir(f'{install_path}/{rename_repo}/extensions')
|
145 |
+
for extension in extensions:
|
146 |
+
os.system(f'git clone {extension}')
|
147 |
+
|
148 |
+
def model_download(models, type_w):
|
149 |
+
for model in models:
|
150 |
+
download_files(model, type_w)
|
151 |
+
def remove_restart():
|
152 |
+
os.chdir("/home/xlab-app-center/stable-diffusion-webui/html")
|
153 |
+
os.system("rm ./footer.html && wget -O footer.html https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/footer.html")
|
154 |
+
#os.chdir("/home/xlab-app-center/stable-diffusion-webui/modules")
|
155 |
+
#os.system("rm ./ui_settings.py && wget -O ui_settings.py https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui_settings.py")
|
156 |
+
|
157 |
+
|
158 |
+
def download_files(url, source):
|
159 |
+
if '@' in url and (not url.startswith('http://') and not url.startswith('https://')):
|
160 |
+
parts = url.split('@', 1)
|
161 |
+
name = parts[0]
|
162 |
+
url = parts[1]
|
163 |
+
rename = f"-o '{name}'"
|
164 |
+
if 'huggingface.co' in url:
|
165 |
+
url = url.replace("huggingface.co", "hf-mirror.com")
|
166 |
+
else:
|
167 |
+
if ('huggingface.co' or 'hf-mirror.com' or 'huggingface.sukaka.top') in url:
|
168 |
+
url = url.replace("huggingface.co", "hf-mirror.com")
|
169 |
+
match_name = re.search(r'/([^/?]+)(?:\?download=true)?$', url).group(1)
|
170 |
+
if match_name:
|
171 |
+
rename = f"-o '{match_name}'"
|
172 |
+
else:
|
173 |
+
rename = ''
|
174 |
+
else:
|
175 |
+
rename = ''
|
176 |
+
source_dir = f'{install_path}/{rename_repo}/{source}'
|
177 |
+
os.makedirs(source_dir, exist_ok=True)
|
178 |
+
os.chdir(source_dir)
|
179 |
+
os.system(f"{download_tool} '{url}' {rename}")
|
180 |
+
def run_webui():
|
181 |
+
os.system("pip install nvidia-ml-py3 wandb")
|
182 |
+
# 创建并启动监控线程
|
183 |
+
monitor_thread = threading.Thread(target=monitor_gpu)
|
184 |
+
monitor_thread.start()
|
185 |
+
os.chdir(install_path)
|
186 |
+
if not os.path.exists(f'{install_path}/{rename_repo}'):
|
187 |
+
os.system(f"git clone https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-v1.8.0 {install_path}/{rename_repo}")
|
188 |
+
remove_restart()
|
189 |
+
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")
|
190 |
+
if not os.path.exists(f'{install_path}/{rename_repo}'):
|
191 |
+
print(f'在克隆 https://{git_url}/{webui_repo} 时出错')
|
192 |
+
run_webui()
|
193 |
+
|
194 |
+
download_extensions(extensions)
|
195 |
+
|
196 |
+
model_download(sd_models, 'models/Stable-diffusion')
|
197 |
+
model_download(lora_models, 'models/Lora')
|
198 |
+
model_download(vae_models, 'models/VAE')
|
199 |
+
if ControlNet:
|
200 |
+
model_download(controlnet_models, 'extensions/sd-webui-controlnet/models')
|
201 |
+
model_download(hypernetwork_models, 'models/hypernetworks')
|
202 |
+
model_download(embedding_models, 'embeddings')
|
203 |
+
model_download(esrgan_models, 'models/ESRGAN')
|
204 |
+
|
205 |
+
os.chdir(f"{install_path}/{rename_repo}")
|
206 |
+
package_envs = [
|
207 |
+
{"env": "STABLE_DIFFUSION_REPO", "url": os.environ.get('STABLE_DIFFUSION_REPO', "https://gitcode.net/overbill1683/stablediffusion")},
|
208 |
+
{"env": "STABLE_DIFFUSION_XL_REPO", "url": os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://gitcode.net/overbill1683/generative-models")},
|
209 |
+
{"env": "K_DIFFUSION_REPO", "url": os.environ.get('K_DIFFUSION_REPO', "https://gitcode.net/overbill1683/k-diffusion")},
|
210 |
+
{"env": "CODEFORMER_REPO", "url": os.environ.get('CODEFORMER_REPO', "https://gitcode.net/overbill1683/CodeFormer")},
|
211 |
+
{"env": "BLIP_REPO", "url": os.environ.get('BLIP_REPO', "https://gitcode.net/overbill1683/BLIP")},
|
212 |
+
]
|
213 |
+
os.environ["PIP_INDEX_URL"] = "https://mirrors.aliyun.com/pypi/simple/"
|
214 |
+
for i in package_envs:
|
215 |
+
os.environ[i["env"]] = i["url"]
|
216 |
+
|
217 |
+
os.chdir(install_path)
|
218 |
+
for custom_command in custom_commands:
|
219 |
+
os.system(custom_command)
|
220 |
+
|
221 |
+
os.chdir(f"{install_path}/{rename_repo}")
|
222 |
+
os.system(f"python launch.py {' '.join(webui_args)} --port {webui_port}")
|
223 |
+
|
224 |
+
# 实例保活
|
225 |
+
import time
|
226 |
+
|
227 |
+
def session_saver():
|
228 |
+
try:
|
229 |
+
import cupy as cp
|
230 |
+
except ImportError:
|
231 |
+
print("cupy模块未安装,正在安装...")
|
232 |
+
try:
|
233 |
+
import pip
|
234 |
+
pip.main(['install', 'cupy'])
|
235 |
+
import cupy as cp
|
236 |
+
except ImportError:
|
237 |
+
print("无法安装模块,请确保已正确安装pip。")
|
238 |
+
return
|
239 |
+
|
240 |
+
while True:
|
241 |
+
for _ in range(1):
|
242 |
+
matrix_a = cp.random.rand(2000, 2000)
|
243 |
+
matrix_b = cp.random.rand(2000, 2000)
|
244 |
+
result = cp.dot(matrix_a, matrix_b)
|
245 |
+
print("实例保活:", result)
|
246 |
+
del matrix_a, matrix_b, result
|
247 |
+
cp.cuda.Stream.null.synchronize()
|
248 |
+
time.sleep(600)
|
249 |
+
|
250 |
+
# 启动
|
251 |
+
import threading
|
252 |
+
import subprocess
|
253 |
+
import time
|
254 |
+
import threading
|
255 |
+
|
256 |
+
def setup_and_run_services():
|
257 |
+
packages = ["pyngrok", "jupyterlab"]
|
258 |
+
ngrok_token = "2jIH20HEgKSt1T4IzmMtg8STeZo_3eLMT3QN32hLrNUKjARwo"
|
259 |
+
http_port = "8083"
|
260 |
+
|
261 |
+
# 安装Python包
|
262 |
+
subprocess.Popen(["pip", "install"] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
|
263 |
+
|
264 |
+
# 等待包安装完成
|
265 |
+
time.sleep(20)
|
266 |
+
|
267 |
+
# 启动ngrok进程
|
268 |
+
ngrok_command = ["ngrok", "http", http_port, "--authtoken=" + ngrok_token]
|
269 |
+
ngrok_process = subprocess.Popen(ngrok_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
270 |
+
|
271 |
+
# 启动jupyter-lab进程
|
272 |
+
jupyter_command = [
|
273 |
+
"jupyter-lab",
|
274 |
+
"--no-browser",
|
275 |
+
"--ip=0.0.0.0",
|
276 |
+
"--allow-root",
|
277 |
+
"--notebook-dir=/",
|
278 |
+
"--port=" + http_port,
|
279 |
+
"--LabApp.allow_origin=*",
|
280 |
+
"--LabApp.token=",
|
281 |
+
"--LabApp.base_url="
|
282 |
+
]
|
283 |
+
jupyter_process = subprocess.Popen(jupyter_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
284 |
+
|
285 |
+
# 等待进程完成
|
286 |
+
ngrok_out, ngrok_err = ngrok_process.communicate()
|
287 |
+
jupyter_out, jupyter_err = jupyter_process.communicate()
|
288 |
+
|
289 |
+
# 打印输出和错误信息
|
290 |
+
print("Ngrok Output:", ngrok_out.decode())
|
291 |
+
print("Ngrok Error:", ngrok_err.decode())
|
292 |
+
print("Jupyter Output:", jupyter_out.decode())
|
293 |
+
print("Jupyter Error:", jupyter_err.decode())
|
294 |
+
|
295 |
+
def run_in_background():
|
296 |
+
"""在后台线程中运行服务设置函数"""
|
297 |
+
thread = threading.Thread(target=setup_and_run_services)
|
298 |
+
thread.start()
|
299 |
+
return thread
|
300 |
+
|
301 |
+
def run():
|
302 |
+
background_thread = run_in_background()
|
303 |
+
print("jupyterlab服务正在后台运行...")
|
304 |
+
webui = threading.Thread(target=run_webui)
|
305 |
+
#saver = threading.Thread(target=session_saver)
|
306 |
+
|
307 |
+
webui.start()
|
308 |
+
#saver.start()
|
309 |
+
time.sleep(99999999)
|