NekoMirra commited on
Commit
6016e90
·
verified ·
1 Parent(s): 5b4c32e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -166
app.py CHANGED
@@ -1,5 +1,10 @@
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'
@@ -10,142 +15,120 @@ api_auth = 'Echoflare:Tt25faj8'
10
  download_tool = 'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M'
11
 
12
  webui_args = [
13
- '--api',
14
- '--xformers',
15
- '--no-hashing',
16
- '--disable-nan-check',
17
- '--disable-console-progressbars',
18
- '--enable-console-prompts',
19
- #'--no-gradio-queue',
20
- '--no-half-vae',
21
- "--skip-torch-cuda-test",
22
- "--allow-code"
23
- # f'--api-auth={api_auth}',
24
- # '--freeze-settings',
25
  ]
26
 
27
  extensions = [
28
- 'https://gitcode.com/zanllp/sd-webui-infinite-image-browsing',
29
- 'https://gitcode.com/dtlnor/stable-diffusion-webui-localization-zh_CN', # 汉化
30
- 'https://gitcode.com/DominikDoom/a1111-sd-webui-tagcomplete', # 提示词提示器
31
- 'https://kkgithub.com/Mikubill/sd-webui-controlnet', # ControlNet
32
- "https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans",
33
- #"https://gitcode.net/ranting8323/adetailer",
34
- "https://gitcode.net/ranting8323/sd-webui-inpaint-anything",
35
- "https://openi.pcl.ac.cn/2575044704/sd-extension-system-info",
36
- "https://openi.pcl.ac.cn/2575044704/batchlinks-webui",
37
- 'https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-localization-zh_CN',
38
- 'https://openi.pcl.ac.cn/2575044704/sd-webui-lora-block-weight',
39
- 'https://openi.pcl.ac.cn/2575044704/sd-skin-extension',
40
- "https://kkgithub.com/continue-revolution/sd-webui-animatediff.git",
41
- "https://kkgithub.com/Iyashinouta/sd-model-downloader.git",
42
- "https://kkgithub.com/fkunn1326/openpose-editor.git",
43
- "https://kkgithub.com/zero01101/openOutpaint-webUI-extension.git",
44
- "https://kkgithub.com/LonicaMewinsky/gif2gif.git",
45
- "https://openi.pcl.ac.cn/2575044704/sd-webui-agent-scheduler",
46
- "https://openi.pcl.ac.cn/Echoflare/letest",
47
- "https://github.com/Physton/sd-webui-prompt-all-in-one",
48
  ]
49
 
50
- sd_models = [
51
- ]
52
 
53
- lora_models = [
54
-
55
- ]
56
 
57
- vae_models = ["https://hf-mirror.com/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors",
 
58
  ]
59
 
60
- ControlNet = False
61
 
62
-
63
- embedding_models = [
64
- ]
65
 
66
  hypernetwork_models = []
67
 
68
- esrgan_models = ["https://hf-mirror.com/mihirkothari0702/RealESRGAN/resolve/main/RealESRGAN_x4plus.pth",
69
- "https://hf-mirror.com/com-adm/RealESRGAN_x4plus_anime_6B/resolve/main/RealESRGAN_x4plus_anime_6B.pth",
70
- ]
 
71
 
72
  custom_commands = [
73
- f'rm -rf {install_path}/{rename_repo}/config.json',
74
- f'rm -rf {install_path}/{rename_repo}/ui-config.json',
75
- f'rm -rf {install_path}/{rename_repo}/modules/ui_settings.py',
76
- 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",
77
- 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",
78
- 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",
79
  ]
80
 
81
- # 网页UI部署
82
-
83
-
84
 
 
85
  def monitor_gpu():
86
-
87
- import os
88
- import re
89
- import os
90
- import time
91
- import threading
92
- import wandb
93
  import pynvml as nvidia_smi
94
-
95
 
96
  def download_extensions(extensions):
97
  os.chdir(f'{install_path}/{rename_repo}/extensions')
98
  for extension in extensions:
 
99
  os.system(f'git clone {extension}')
100
 
101
  def model_download(models, type_w):
102
  for model in models:
103
  download_files(model, type_w)
 
104
  def remove_restart():
105
- os.chdir("/home/xlab-app-center/stable-diffusion-webui/html")
106
  os.system("rm ./footer.html && wget -O footer.html https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/footer.html")
107
- #os.chdir("/home/xlab-app-center/stable-diffusion-webui/modules")
108
- #os.system("rm ./ui_settings.py && wget -O ui_settings.py https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui_settings.py")
109
-
110
 
111
  def download_files(url, source):
112
- if '@' in url and (not url.startswith('http://') and not url.startswith('https://')):
 
113
  parts = url.split('@', 1)
114
- name = parts[0]
115
- url = parts[1]
116
  rename = f"-o '{name}'"
117
- if 'huggingface.co' in url:
118
- url = url.replace("huggingface.co", "hf-mirror.com")
119
  else:
120
- if ('huggingface.co' or 'hf-mirror.com' or 'huggingface.sukaka.top') in url:
121
  url = url.replace("huggingface.co", "hf-mirror.com")
122
  match_name = re.search(r'/([^/?]+)(?:\?download=true)?$', url).group(1)
123
- if match_name:
124
- rename = f"-o '{match_name}'"
125
- else:
126
- rename = ''
127
  else:
128
  rename = ''
129
  source_dir = f'{install_path}/{rename_repo}/{source}'
130
  os.makedirs(source_dir, exist_ok=True)
131
  os.chdir(source_dir)
132
  os.system(f"{download_tool} '{url}' {rename}")
 
133
  def run_webui():
134
  os.system("pip install nvidia-ml-py3 wandb")
135
- # 创建并启动监控线程
136
  monitor_thread = threading.Thread(target=monitor_gpu)
137
  monitor_thread.start()
138
  os.chdir(install_path)
139
  if not os.path.exists(f'{install_path}/{rename_repo}'):
140
  os.system(f"git clone https://kkgithub.com/AUTOMATIC1111/stable-diffusion-webui {install_path}/{rename_repo}")
141
  remove_restart()
142
- 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")
143
  if not os.path.exists(f'{install_path}/{rename_repo}'):
144
- print(f'在克隆 https://{git_url}/{webui_repo} 时出错')
145
  run_webui()
146
-
147
  download_extensions(extensions)
148
-
149
  model_download(sd_models, 'models/Stable-diffusion')
150
  model_download(lora_models, 'models/Lora')
151
  model_download(vae_models, 'models/VAE')
@@ -154,18 +137,18 @@ def run_webui():
154
  model_download(hypernetwork_models, 'models/hypernetworks')
155
  model_download(embedding_models, 'embeddings')
156
  model_download(esrgan_models, 'models/ESRGAN')
157
-
158
  os.chdir(f"{install_path}/{rename_repo}")
159
  package_envs = [
160
- {"env": "STABLE_DIFFUSION_XL_REPO", "url": os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://gitcode.net/overbill1683/generative-models")},
161
- {"env": "K_DIFFUSION_REPO", "url": os.environ.get('K_DIFFUSION_REPO', "https://gitcode.net/overbill1683/k-diffusion")},
162
- {"env": "CODEFORMER_REPO", "url": os.environ.get('CODEFORMER_REPO', "https://gitcode.net/overbill1683/CodeFormer")},
163
- {"env": "BLIP_REPO", "url": os.environ.get('BLIP_REPO', "https://gitcode.net/overbill1683/BLIP")},
164
- {"env": "CLIP_REPO", "url": os.environ.get('CLIP_REPO', "https://kkgithub.com/openai/CLIP")},
165
  ]
166
  os.environ["PIP_INDEX_URL"] = "https://mirrors.aliyun.com/pypi/simple/"
167
- for i in package_envs:
168
- os.environ[i["env"]] = i["url"]
169
 
170
  os.chdir(install_path)
171
  for custom_command in custom_commands:
@@ -175,87 +158,14 @@ def run_webui():
175
  os.system(f"python launch.py {' '.join(webui_args)} --port {webui_port}")
176
 
177
  # 实例保活
178
- import time
179
-
180
  def session_saver():
181
  try:
182
  import cupy as cp
183
  except ImportError:
184
  print("cupy模块未安装,正在安装...")
185
- try:
186
- import pip
187
- pip.main(['install', 'cupy'])
188
- import cupy as cp
189
- except ImportError:
190
- print("无法安装模块,请确保已正确安装pip。")
191
- return
192
-
193
  while True:
194
- for _ in range(1):
195
- matrix_a = cp.random.rand(2000, 2000)
196
- matrix_b = cp.random.rand(2000, 2000)
197
- result = cp.dot(matrix_a, matrix_b)
198
- print("实例保活:", result)
199
- del matrix_a, matrix_b, result
200
- cp.cuda.Stream.null.synchronize()
201
  time.sleep(60)
202
-
203
- # 启动
204
- import threading
205
- import subprocess
206
- import time
207
- import threading
208
-
209
- def setup_and_run_services():
210
- packages = ["pyngrok", "jupyterlab"]
211
- ngrok_token = "2CXyNlT9xGfFoL5ruI6hQV20FNq_7tbmuzS9RtyNTkyEe1J6C"
212
- http_port = "8083"
213
-
214
- # 安装Python包
215
- subprocess.Popen(["pip", "install"] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
216
-
217
- # 等待包安装完成
218
- time.sleep(20)
219
-
220
- # 启动ngrok进程
221
- ngrok_command = ["ngrok", "http", http_port, "--authtoken=" + ngrok_token]
222
- ngrok_process = subprocess.Popen(ngrok_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
223
-
224
- # 启动jupyter-lab进程
225
- jupyter_command = [
226
- "jupyter-lab",
227
- "--no-browser",
228
- "--ip=0.0.0.0",
229
- "--allow-root",
230
- "--notebook-dir=/",
231
- "--port=" + http_port,
232
- "--LabApp.allow_origin=*",
233
- "--LabApp.token=",
234
- "--LabApp.base_url="
235
- ]
236
- jupyter_process = subprocess.Popen(jupyter_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
237
-
238
- # 等待进程完成
239
- ngrok_out, ngrok_err = ngrok_process.communicate()
240
- jupyter_out, jupyter_err = jupyter_process.communicate()
241
-
242
- # 打印输出和错误信息
243
- print("Ngrok Output:", ngrok_out.decode())
244
- print("Ngrok Error:", ngrok_err.decode())
245
- print("Jupyter Output:", jupyter_out.decode())
246
- print("Jupyter Error:", jupyter_err.decode())
247
-
248
- def run_in_background():
249
- """在后台线程中运行服务设置函数"""
250
- thread = threading.Thread(target=setup_and_run_services)
251
- thread.start()
252
- return thread
253
-
254
- def run():
255
- background_thread = run_in_background()
256
- print("jupyterlab服务正在后台运行...")
257
- webui = threading.Thread(target=run_webui)
258
- #saver = threading.Thread(target=session_saver)
259
-
260
- webui.start()
261
- #saver.start()
 
1
  # 配置
2
  import os
3
+ import re
4
+ import threading
5
+ import subprocess
6
+ import time
7
+
8
  install_path = '/home/xlab-app-center'
9
  webui_repo = 'AUTOMATIC1111/stable-diffusion-webui --branch v1.9.4'
10
  rename_repo = 'stable-diffusion-webui'
 
15
  download_tool = 'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M'
16
 
17
  webui_args = [
18
+ '--api',
19
+ '--xformers',
20
+ '--no-hashing',
21
+ '--disable-nan-check',
22
+ '--disable-console-progressbars',
23
+ '--enable-console-prompts',
24
+ '--no-half-vae',
25
+ '--skip-torch-cuda-test',
26
+ '--allow-code'
 
 
 
27
  ]
28
 
29
  extensions = [
30
+ 'https://gitcode.com/zanllp/sd-webui-infinite-image-browsing',
31
+ 'https://gitcode.com/dtlnor/stable-diffusion-webui-localization-zh_CN', # 汉化
32
+ 'https://gitcode.com/DominikDoom/a1111-sd-webui-tagcomplete', # 提示词提示器
33
+ 'https://kkgithub.com/Mikubill/sd-webui-controlnet', # ControlNet
34
+ "https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans",
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/continue-revolution/sd-webui-animatediff.git",
42
+ "https://kkgithub.com/Iyashinouta/sd-model-downloader.git",
43
+ "https://kkgithub.com/fkunn1326/openpose-editor.git",
44
+ "https://kkgithub.com/zero01101/openOutpaint-webUI-extension.git",
45
+ "https://kkgithub.com/LonicaMewinsky/gif2gif.git",
46
+ "https://openi.pcl.ac.cn/2575044704/sd-webui-agent-scheduler",
47
+ "https://openi.pcl.ac.cn/Echoflare/letest",
48
+ "https://github.com/Physton/sd-webui-prompt-all-in-one",
 
49
  ]
50
 
51
+ sd_models = []
 
52
 
53
+ lora_models = []
 
 
54
 
55
+ vae_models = [
56
+ "https://hf-mirror.com/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors",
57
  ]
58
 
59
+ ControlNet = False
60
 
61
+ embedding_models = []
 
 
62
 
63
  hypernetwork_models = []
64
 
65
+ esrgan_models = [
66
+ "https://hf-mirror.com/mihirkothari0702/RealESRGAN/resolve/main/RealESRGAN_x4plus.pth",
67
+ "https://hf-mirror.com/com-adm/RealESRGAN_x4plus_anime_6B/resolve/main/RealESRGAN_x4plus_anime_6B.pth",
68
+ ]
69
 
70
  custom_commands = [
71
+ f'rm -rf {install_path}/{rename_repo}/config.json',
72
+ f'rm -rf {install_path}/{rename_repo}/ui-config.json',
73
+ f'rm -rf {install_path}/{rename_repo}/modules/ui_settings.py',
74
+ 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",
75
+ 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",
76
+ 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",
77
  ]
78
 
79
+ def redirect_github(url):
80
+ return url.replace("github.com", "kkgithub.com")
 
81
 
82
+ # 网页UI部署
83
  def monitor_gpu():
 
 
 
 
 
 
 
84
  import pynvml as nvidia_smi
85
+ import wandb
86
 
87
  def download_extensions(extensions):
88
  os.chdir(f'{install_path}/{rename_repo}/extensions')
89
  for extension in extensions:
90
+ extension = redirect_github(extension)
91
  os.system(f'git clone {extension}')
92
 
93
  def model_download(models, type_w):
94
  for model in models:
95
  download_files(model, type_w)
96
+
97
  def remove_restart():
98
+ os.chdir(f"{install_path}/{rename_repo}/html")
99
  os.system("rm ./footer.html && wget -O footer.html https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/footer.html")
 
 
 
100
 
101
  def download_files(url, source):
102
+ url = redirect_github(url)
103
+ if '@' in url and not url.startswith(('http://', 'https://')):
104
  parts = url.split('@', 1)
105
+ name, url = parts
 
106
  rename = f"-o '{name}'"
 
 
107
  else:
108
+ if any(x in url for x in ['huggingface.co', 'hf-mirror.com', 'huggingface.sukaka.top']):
109
  url = url.replace("huggingface.co", "hf-mirror.com")
110
  match_name = re.search(r'/([^/?]+)(?:\?download=true)?$', url).group(1)
111
+ rename = f"-o '{match_name}'" if match_name else ''
 
 
 
112
  else:
113
  rename = ''
114
  source_dir = f'{install_path}/{rename_repo}/{source}'
115
  os.makedirs(source_dir, exist_ok=True)
116
  os.chdir(source_dir)
117
  os.system(f"{download_tool} '{url}' {rename}")
118
+
119
  def run_webui():
120
  os.system("pip install nvidia-ml-py3 wandb")
 
121
  monitor_thread = threading.Thread(target=monitor_gpu)
122
  monitor_thread.start()
123
  os.chdir(install_path)
124
  if not os.path.exists(f'{install_path}/{rename_repo}'):
125
  os.system(f"git clone https://kkgithub.com/AUTOMATIC1111/stable-diffusion-webui {install_path}/{rename_repo}")
126
  remove_restart()
127
+ os.system(f"{download_tool} https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/kaggle/input/museum/131-half.safetensors -d {install_path}/{rename_repo}/models/Stable-diffusion -o [萌二次元]131-half.safetensors")
128
  if not os.path.exists(f'{install_path}/{rename_repo}'):
129
+ print(f'在克隆 https://kkgithub.com/{webui_repo} 时出错')
130
  run_webui()
 
131
  download_extensions(extensions)
 
132
  model_download(sd_models, 'models/Stable-diffusion')
133
  model_download(lora_models, 'models/Lora')
134
  model_download(vae_models, 'models/VAE')
 
137
  model_download(hypernetwork_models, 'models/hypernetworks')
138
  model_download(embedding_models, 'embeddings')
139
  model_download(esrgan_models, 'models/ESRGAN')
140
+
141
  os.chdir(f"{install_path}/{rename_repo}")
142
  package_envs = [
143
+ {"env": "STABLE_DIFFUSION_XL_REPO", "url": os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://gitcode.net/overbill1683/generative-models")},
144
+ {"env": "K_DIFFUSION_REPO", "url": os.environ.get('K_DIFFUSION_REPO', "https://gitcode.net/overbill1683/k-diffusion")},
145
+ {"env": "CODEFORMER_REPO", "url": os.environ.get('CODEFORMER_REPO', "https://gitcode.net/overbill1683/CodeFormer")},
146
+ {"env": "BLIP_REPO", "url": os.environ.get('BLIP_REPO', "https://gitcode.net/overbill1683/BLIP")},
147
+ {"env": "CLIP_REPO", "url": os.environ.get('CLIP_REPO', "https://kkgithub.com/openai/CLIP")},
148
  ]
149
  os.environ["PIP_INDEX_URL"] = "https://mirrors.aliyun.com/pypi/simple/"
150
+ for package_env in package_envs:
151
+ os.environ[package_env["env"]] = package_env["url"]
152
 
153
  os.chdir(install_path)
154
  for custom_command in custom_commands:
 
158
  os.system(f"python launch.py {' '.join(webui_args)} --port {webui_port}")
159
 
160
  # 实例保活
 
 
161
  def session_saver():
162
  try:
163
  import cupy as cp
164
  except ImportError:
165
  print("cupy模块未安装,正在安装...")
166
+ import pip
167
+ pip.main(['install', 'cupy'])
168
+ import cupy as cp
169
+
 
 
 
 
170
  while True:
 
 
 
 
 
 
 
171
  time.sleep(60)