File size: 22,196 Bytes
993e740
73d1d2f
 
af02a9d
73d1d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aefc91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa8dfd0
 
 
 
 
 
 
 
 
73d1d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aefc91
fa8dfd0
73d1d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
993e740
 
73d1d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb5c93a
 
73d1d2f
bb5c93a
 
 
73d1d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e674768
73d1d2f
 
 
e674768
73d1d2f
 
 
 
 
 
 
 
 
 
e674768
 
73d1d2f
e674768
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# 适用于任何linux
import os
os.system("pip install wandb nvidia-ml-py3")
#os.system("apt update && apt install -y aria2")
os.system("mkdir -p /root/xlab-app-center/")
import threading
#使用的库
from pathlib import Path
import subprocess
import shutil
import os
import time
import re
import gc
import requests
import zipfile
import threading
import time
import socket
from concurrent.futures import ProcessPoolExecutor
# import wandb
import base64
import subprocess

import os

def replace_in_file(file_path, old_str, new_str):
    with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
        content = file.read()
    
    content = content.replace(old_str, new_str)
    
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(content)

def traverse_directory(directory_path, old_str1, new_str1, old_str2, new_str2):
    for root, _, files in os.walk(directory_path):
        for file_name in files:
            file_path = os.path.join(root, file_name)
            replace_in_file(file_path, old_str1, new_str1)
            replace_in_file(file_path, old_str2, new_str2)

def git_replace():
    directory_path = '/root/xlab-app-center/stable-diffusion-webui/modules'  # 修改为你要遍历的目录路径
    old_str1 = 'https://github.com/'
    new_str1 = 'https://mirror.ghproxy.com/https://github.com/'
    old_str2 = 'https://raw.githubusercontent.com/'
    new_str2 = 'https://mirror.ghproxy.com/https://raw.githubusercontent.com/'
    
    traverse_directory(directory_path, old_str1, new_str1, old_str2, new_str2)
    print("Replacement completed.")

def git_replace2():
    directory_path = '/root/xlab-app-center/stable-diffusion-webui/extensions-builtin'  # 修改为你要遍历的目录路径
    old_str1 = 'https://github.com/'
    new_str1 = 'https://mirror.ghproxy.com/https://github.com/'
    old_str2 = 'https://raw.githubusercontent.com/'
    new_str2 = 'https://mirror.ghproxy.com/https://raw.githubusercontent.com/'
    
    traverse_directory(directory_path, old_str1, new_str1, old_str2, new_str2)
    print("Replacement completed.")

def notbook():
    os.system("pip install jupyterlab")
    os.system("pip install pyngrok")
    # 构建命令字符串
    ngrok_command = f"ngrok http 8889 --authtoken=2cdw5pJsqgsq1igQKeHIpNwTNG7_7LQD3jojKKQ7PzcnNkok5"
    ngrok_command2 = f"ngrok tunnel --label edge=edghts_2doueG9BDi9rCXUGnsSsNbTab8H --authtoken=2douOsr61tUyYwMVF3lfj9uZGoa_6FPJ2x1xhKPbL6z4euKkM --region=ap http://localhost:8889"
    jupyter_command = "jupyter-lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir=/ --port=8889 --LabApp.allow_origin=* --LabApp.token= --LabApp.base_url=/8889/"
    
    # 启动 ngrok 进程
    ngrok_process = subprocess.Popen(ngrok_command, shell=True)
    time.sleep(30)
    ngrok_process2 = subprocess.Popen(ngrok_command2, shell=True)
    # 启动 Jupyter 进程
    jupyter_process = subprocess.Popen(jupyter_command, shell=True)
    #os.system(f"ngrok tunnel --authtoken={ngrok_token} --region=ap http://localhost:8888 & python jupyter-lab --ServerApp.shutdown_no_activity_timeout=1800 --TerminalManager.cull_inactive_timeout=1800 --TerminalManager.cull_interval=300 --MappingKernelManager.cull_idle_timeout=1800 --MappingKernelManager.cull_interval=300 --MappingKernelManager.cull_connected=True --MappingKernelManager.cull_busy=True --no-browser --ip=0.0.0.0 --allow-root --notebook-dir=/ --port=8888 --LabApp.token= --LabApp.allow_origin=* --LabApp.base_url=")

ngrok_token = "2bgXLjjKFvxfbuZFlR2NMZkvL8n_4WrK7f15FLtWb8p7v3oaF"
_ngrok_token = "2CXyNlT9xGfFoL5ruI6hQV20FNq_7tbmuzS9RtyNTkyEe1J6C"

os.system("pip install nvidia-ml-py3")
os.chdir(f"/root/xlab-app-center")
os.system(f"git clone https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-v1.8.0 /root/xlab-app-center/stable-diffusion-webui")
git_replace()
git_replace2()
os.system(f"cp /root/xlab-app-center/styles.csv /root/xlab-app-center/stable-diffusion-webui/styles.csv")
os.chdir(f"/root/xlab-app-center/stable-diffusion-webui")
os.system(f"git lfs install")
os.system(f"git reset --hard")
import os

ngrok_token = "2bgXLjjKFvxfbuZFlR2NMZkvL8n_4WrK7f15FLtWb8p7v3oaF" #7862端口
_ngrok_token = "2CXyNlT9xGfFoL5ruI6hQV20FNq_7tbmuzS9RtyNTkyEe1J6C" #7863端口

def remove_restart():
    os.chdir("/root/xlab-app-center/stable-diffusion-webui/html")
    os.system("rm ./footer.html && wget -O footer.html https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/footer.html")
    os.chdir("/root/xlab-app-center/stable-diffusion-webui/modules")
    os.system("rm ./ui_settings.py && wget -O ui_settings.py https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui_settings.py")
remove_restart()
def create_directory(directory_path):
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)

def download_file(url, destination_path):
    os.system(f'wget -O {destination_path} {url}')

# 设置基本路径
base_directory = '/root/xlab-app-center/stable-diffusion-webui'
configs_directory = os.path.join(base_directory, 'configs')

# 创建configs文件夹
create_directory(configs_directory)

# 下载配置文件
#download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/config-pub.json', os.path.join(configs_directory, 'config.json'))
#download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pri2.json', os.path.join(configs_directory, 'ui-config-pri.json'))
#download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub.json', os.path.join(configs_directory, 'ui-config.json'))
# 下载配置文件
download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/config-pub.json', os.path.join(base_directory, 'config.json'))
download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pri2.json', os.path.join(base_directory, 'ui-config-pri.json'))
download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub.json', os.path.join(base_directory, 'ui-config.json'))
download_file('https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub2.json', os.path.join(base_directory, 'ui-config2.json'))

os.chdir(f"/root/xlab-app-center/stable-diffusion-webui/extensions")
os.system(f"rm -rf ./batchlinks-webui")
os.system(f"rm -rf ./sd-extension-system-info")
show_shell_info = False
def run(command, cwd=None, desc=None, errdesc=None, custom_env=None,try_error:bool=True) -> str:
    global show_shell_info
    if desc is not None:
        print(desc)

    run_kwargs = {
        "args": command,
        "shell": True,
        "cwd": cwd,
        "env": os.environ if custom_env is None else custom_env,
        "encoding": 'utf8',
        "errors": 'ignore',
    }

    if not show_shell_info:
        run_kwargs["stdout"] = run_kwargs["stderr"] = subprocess.PIPE

    result = subprocess.run(**run_kwargs)

    if result.returncode != 0:
        error_bits = [
            f"{errdesc or 'Error running command'}.",
            f"Command: {command}",
            f"Error code: {result.returncode}",
        ]
        if result.stdout:
            error_bits.append(f"stdout: {result.stdout}")
        if result.stderr:
            error_bits.append(f"stderr: {result.stderr}")
        if try_error:
            print((RuntimeError("\n".join(error_bits))))
        else:
            raise RuntimeError("\n".join(error_bits))

    if show_shell_info:
        print((result.stdout or ""))
    return (result.stdout or "")
import subprocess
def sdmodel():
    def download_with_aria2(urls):
        # 使用循环遍历所有的下载链接
        for url in urls:
            # 将huggingface.co替换为hf-mirror.com
            url = url.replace('huggingface.co', 'hf-mirror.com')
            file_name = url.split('/')[-1].split('?')[0]
            # 构建aria2c命令
            command = f'aria2c -x 16 -s 16 -c -k 1M -o "{file_name}" "{url}" -d /root/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion'
            
            try:
                # 使用subprocess模块运行aria2c命令
                subprocess.run(command, shell=True, check=True)
            except subprocess.CalledProcessError as e:
                # 打印错误信息(如果有)
                print(f'Error downloading {url}: {e}')
    
    # 直接将链接作为多行字符串传递给函数
    download_urls = """
    https://huggingface.co/HiroHiroHirook/meinamix_meinaV8/resolve/main/meinamix_meinaV8.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/anything_v50.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/blindbox_v1_mix.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/cuteyukimixAdorable_naiV3style.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/ddicon_v10.ckpt?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/meinamix_meinaV11.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/meinapastel_v6-inpainting.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/meinapastel_v6Pastel.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/midjourney_20230624181825.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/mixProV4_v4.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/qteamixQ_omegaFp16.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/uberRealisticPornMerge_urpmv13.safetensors?download=true
    https://huggingface.co/datasets/ACCA225/kagglemodel/resolve/main/kaggle/temp/velaMix_velaMixVersion2.safetensors?download=true
    """
    
    # 使用字符串的splitlines方法将字符串分割成列表
    urls_list = download_urls.strip().splitlines()
    
    # 调用函数开始下载
    download_with_aria2(urls_list)
#sdmodel()
def mkdirs(path, exist_ok=True):
    if path and not Path(path).exists():
        os.makedirs(path,exist_ok=exist_ok)
plugins = [
    "https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans",
    "https://gitcode.net/ranting8323/multidiffusion-upscaler-for-automatic1111",
    "https://gitcode.net/ranting8323/adetailer",
    "https://gitcode.net/ranting8323/sd-webui-inpaint-anything",
    "https://gitcode.net/ranting8323/a1111-sd-webui-tagcomplete",
    "https://gitcode.net/nightaway/sd-webui-infinite-image-browsing",
    "https://openi.pcl.ac.cn/2575044704/sd-extension-system-info",
    "https://openi.pcl.ac.cn/2575044704/batchlinks-webui",
    'https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-localization-zh_CN',
    'https://openi.pcl.ac.cn/2575044704/sd-webui-lora-block-weight',
    'https://openi.pcl.ac.cn/2575044704/sd-skin-extension',
    #"https://kkgithub.com/thygate/stable-diffusion-webui-depthmap-script.git",
    #"https://gitcode.net/ranting8323/sd-webui-controlnet",
    #"https://kkgithub.com/SignalFlagZ/sd-webui-civbrowser.git",
    #"https://kkgithub.com/continue-revolution/sd-webui-animatediff.git",
    #"https://kkkkgithub.com/aigc-apps/sd-webui-EasyPhoto.git",
    "https://kkgithub.com/Iyashinouta/sd-model-downloader.git",
    "https://kkgithub.com/fkunn1326/openpose-editor.git",
    #"https://kkgithub.com/zero01101/openOutpaint-webUI-extension.git",
    #"https://kkgithub.com/LonicaMewinsky/gif2gif.git",
    #"https://kkgithub.com/modelscope/facechain.git",
    "https://openi.pcl.ac.cn/2575044704/sd-webui-controlnet",
    "https://openi.pcl.ac.cn/2575044704/sd-webui-agent-scheduler",
    "https://openi.pcl.ac.cn/2575044704/sd-webui-depth-lib",
    "https://openi.pcl.ac.cn/Echoflare/letest"
]
suffix_1 = "Nyan9"
suffix_2 = "BiliBili"
needed_extensions = [
    "https://openi.pcl.ac.cn/2575044704/sd-extension-system-info",
    "https://openi.pcl.ac.cn/2575044704/batchlinks-webui",
]
for plugin in plugins:
    os.system(f"git clone {plugin}")
#for plugin in needed_extensions:
#    os.system(f"git clone {plugin}")
os.makedirs('/root/xlab-app-center/stable-diffusion-webui/models/adetailer', exist_ok=True)
os.chdir(f"/root/xlab-app-center/stable-diffusion-webui/models/adetailer")
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 /root/xlab-app-center/stable-diffusion-webui/models/adetailer -o hand_yolov8s.pt")
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 /root/xlab-app-center/stable-diffusion-webui/models/adetailer -o hand_yolov8n.pt")
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 /root/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion -o [萌二次元]131-half.safetensors")
os.system(f"rm /root/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion/*porn*.safetensors")

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 /root/xlab-app-center/stable-diffusion-webui/models/Lora -o ba.safetensors")
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 /root/xlab-app-center/stable-diffusion-webui/models/Lora -o racaco2.safetensors")
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 /root/xlab-app-center/stable-diffusion-webui/models/Lora -o add_detail.safetensors")
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 /root/xlab-app-center/stable-diffusion-webui/models/VAE -o pastel-waifu-diffusion.vae.pt")
# 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 /root/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion -o sd_xl_refiner_1.0.safetensors")
# 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 /root/xlab-app-center/stable-diffusion-webui/models/Stable-diffusion -o cyberrealistic_v32.safetensors")
os.chdir(f"/root/xlab-app-center/stable-diffusion-webui")
print('webui launching...')
pwd_1 = ngrok_token + suffix_1
pwd_2 = _ngrok_token + suffix_2
package_envs = [
    {"env": "STABLE_DIFFUSION_REPO", "url": os.environ.get('STABLE_DIFFUSION_REPO', "https://gitcode.net/overbill1683/stablediffusion")},
    {"env": "STABLE_DIFFUSION_XL_REPO", "url": os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://gitcode.net/overbill1683/generative-models")},
    {"env": "K_DIFFUSION_REPO", "url": os.environ.get('K_DIFFUSION_REPO', "https://gitcode.net/overbill1683/k-diffusion")},
    {"env": "CODEFORMER_REPO", "url": os.environ.get('CODEFORMER_REPO', "https://gitcode.net/overbill1683/CodeFormer")},
    {"env": "BLIP_REPO", "url": os.environ.get('BLIP_REPO', "https://gitcode.net/overbill1683/BLIP")},
]
os.environ["PIP_INDEX_URL"] = "https://mirrors.aliyun.com/pypi/simple/"
for i in package_envs:
    os.environ[i["env"]] = i["url"]

import os
import time
import wandb
import nvidia_smi
import os
import time
import wandb

# nginx 反向代理配置文件
def echoToFile(content:str,path:str):
    if path.find('/') >= 0:
        _path = '/'.join(path.split('/')[:-1])
        run(f'''mkdir -p {_path}''')
    with open(path,'w') as sh:
        sh.write(content)
# 检查网络
def check_service(host, port):
    try:
        socket.create_connection((host, port), timeout=5)
        return True
    except socket.error:
        return False
def localProxy():
    os.system('sudo apt install nginx -y')
    download_file('https://huggingface.co/datasets/ACCA225/openxlab/resolve/main/proxy_nginx.conf', os.path.join(base_directory, 'proxy_nginx.conf'))
    if not check_service('localhost',_server_port):
        run(f'''nginx -c /root/xlab-app-center/stable-diffusion-webui/proxy_nginx.conf''')
    run(f'''nginx -s reload''')
    
# WandB登录
os.system('wandb login 5c00964de1bb95ec1ab24869d4c523c59e0fb8e3')
nvidia_smi.nvmlInit()
# 初始化WandB项目
wandb.init(project="gpu-temperature-monitor")

import threading
import requests

def check_website(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            print(f"The website {url} is accessible.")
        else:
            print(f"The website {url} returned status code: {response.status_code}")
            notbook()
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while trying to access {url}: {e}")

def run_check_periodically(url, interval):
    check_website(url)
    threading.Timer(interval, run_check_periodically, args=(url, interval)).start()


import os
import threading
import wandb
import time
def monitor_gpu():
    start_thread = threading.Thread(target=start)
    start_thread.start()
    while True:
        try:
            # 获取 GPU 温度
            handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)  # 0 表示第一个 GPU
            gpu_temperature = nvidia_smi.nvmlDeviceGetTemperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)

            # 获取 GPU 使用率
            utilization = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
            gpu_usage = utilization.gpu

            # 使用 WandB 记录 GPU 温度和使用率
            wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})

        except Exception as e:
            print(f"Error: {e}")

        time.sleep(60)
def zrok():
    
    #os.system("wget https://hf-mirror.com/datasets/ACCA225/frp-1/resolve/main/frpc")
    #os.system("chmod +x ./frpc")
    #os.system("./frpc -f bsnehoeds17ett71i2cr79ujoa7ndkxz:13738352 & ./frpc -f bsnehoeds17ett71i2cr79ujoa7ndkxz:13738353")
    pass

_pwd_1 = base64.b64encode(pwd_1.encode()).decode()
_pwd_2 = base64.b64encode(pwd_2.encode()).decode()

apitoken = "{}:{}".format(_pwd_1, _pwd_2)
def start():
    
    #notbook()
    #try:
     #   print('启动proxy')
      #  threading.Thread(target = localProxy,daemon=True).start()
    #except Exception as e:
     #   # 在这里处理异常的代码
      #  print(f"proxy An error occurred: {e}")
    try:
    #安装环境
        os.system(f"python launch.py --api --xformers --exit --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle")
        #time.sleep(5)
        
        command = f"python launch.py --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle --port=7861"
        command1 = "python launch.py --ui-config=/root/xlab-app-center/stable-diffusion-webui/ui-config2.json --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle --port=7862"
        command2 = "python launch.py --ui-config=/root/xlab-app-center/stable-diffusion-webui/ui-config2.json --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle --port=7863"
        #process = subprocess.Popen(command, shell=True)
        #time.sleep(250)
        #process = subprocess.Popen(command1, shell=True)
        #time.sleep(100)
        #print("启动ngrok中")
        #command_ngrok = f"ngrok http 8888 --authtoken={ngrok_token} --region=ap & ngrok http 7863 --authtoken=2H8A6felNlmleMqWBF0UUt2C72e_7mqBMs1nVibXbfnK28KrX --region=ap"
        #process = subprocess.Popen(command2, shell=True)
        def start_ngrok():
            try:
                os.system(f"{command_ngrok}")
            except Exception as e:
                print(f"ngrok启动失败: {e}")
                start_ngrok()
        #start_ngrok()
    except Exception as e:
        # 在这里处理异常的代码
        print(f"启动SD发生错误: {e}")
    try:
        #os.system(f"python launch.py --api --xformers --exit --enable-insecure-extension-access --ui-settings-file /root/xlab-app-center/config.json --ui-config-file /root/xlab-app-center/ui-config.json --gradio-queue --disable-safe-unpickle")
        os.system(f"HF_ENDPOINT=https://hf-mirror.com python launch.py --api --xformers --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle")
    except Exception as e:
        # 在这里处理异常的代码
        print(f"An error occurred: {e}")

# 实例保活
import time

def session_saver():
    try:
        import cupy as cp
    except ImportError:
        print("cupy模块未安装,正在安装...")
        try:
            import pip
            pip.main(['install', 'cupy'])
            import cupy as cp
        except ImportError:
            print("无法安装模块,请确保已正确安装pip。")
            return
    
    while True:
        for _ in range(5):
            matrix_a = cp.random.rand(10000, 10000)
            matrix_b = cp.random.rand(10000, 10000)
            result = cp.dot(matrix_a, matrix_b)
            print("实例保活:", result)
            del matrix_a, matrix_b, result
            cp.cuda.Stream.null.synchronize()
        time.sleep(600)
        
keepliving_thread = threading.Thread(target=session_saver)
net_thread = threading.Thread(target=zrok)

net_thread.start()
keepliving_thread.start()

# 要检测的网站
#website_url = "https://surely-definite-monarch.ngrok-free.app"

# 间隔时间(秒)
#interval = 1800

# 第一次立即运行一次
#run_check_periodically(website_url, interval)

start_thread = threading.Thread(target=start)
start_thread.start()
monitor_gpu()
keepliving_thread.join()