666 commited on
Commit
eb37f21
1 Parent(s): 5c4253f

Rename colab803.py to main.py

Browse files
Files changed (2) hide show
  1. colab803.py +0 -122
  2. main.py +359 -0
colab803.py DELETED
@@ -1,122 +0,0 @@
1
- import argparse
2
- import binascii
3
-
4
- # 自定义类型转换函数
5
- def str_to_bool(value):
6
- if value.lower() in ('yes', 'true', 't', 'y', '1'):
7
- return True
8
- elif value.lower() in ('no', 'false', 'f', 'n', '0'):
9
- return False
10
- else:
11
- raise argparse.ArgumentTypeError('Invalid boolean value: {}'.format(value))
12
-
13
- # 创建参数解析器
14
- parser = argparse.ArgumentParser()
15
-
16
- parser.add_argument('--Version', type=str)
17
- parser.add_argument('--Drive_Map', type=str_to_bool)
18
-
19
- # 解析命令行参数
20
- args = parser.parse_args()
21
-
22
- Version = args.Version
23
- Drive_Map = args.Drive_Map
24
-
25
- ################################################################################################################################################
26
-
27
- import sys
28
- import os
29
- import base64
30
- import importlib.util
31
- from IPython.display import clear_output
32
- from google.colab import drive
33
- import tensorflow as tf
34
-
35
- # 检测是否为GPU运行
36
- print("TensorFlow version:", tf.__version__)
37
- if tf.test.gpu_device_name():
38
- drive.mount('/content/drive')
39
- else:
40
- raise Exception("\n请在《代码执行程序》-《更改运行时类型》-设置为GPU~")
41
-
42
- sdw = binascii.unhexlify("737461626c652d646966667573696f6e2d7765627569").decode('ascii')
43
- w = binascii.unhexlify("7765627569").decode('ascii')
44
- wb = f'/content/{sdw}'
45
- gwb = f'/content/drive/MyDrive/{sdw}'
46
-
47
- get_ipython().run_line_magic('cd', '/content')
48
- get_ipython().run_line_magic('env', 'TF_CPP_MIN_LOG_LEVEL=1')
49
-
50
- # 云盘同步
51
- def cloudDriveSync(cloudPath, localPath='', sync=False):
52
- # 云盘没有目录
53
- if not os.path.exists(cloudPath):
54
- # 创建云盘目录
55
- get_ipython().system(f'mkdir {cloudPath}')
56
-
57
- # 是否要同步
58
- if not sync:
59
- return
60
-
61
- # 删除本地目录
62
- get_ipython().system(f'rm -rf {localPath}')
63
- # 链接云盘目录
64
- get_ipython().system(f'ln -s {cloudPath} {localPath}')
65
-
66
- # 初始化云盘
67
- def initCloudDrive():
68
- cloudDriveSync(f'{gwb}')
69
- cloudDriveSync(f'{gwb}/models')
70
- cloudDriveSync(f'{gwb}/lora')
71
- cloudDriveSync(f'{gwb}/vae')
72
-
73
- # clong git
74
- def gitDownload(url, localPath):
75
- if os.path.exists(localPath):
76
- return
77
-
78
- get_ipython().system(f'git clone {url} {localPath}')
79
-
80
-
81
- # 安装附加功能
82
- def installAdditional():
83
- # 安装扩展
84
- urls = [
85
- f'https://github.com/camenduru/{sdw}-images-browser',
86
- f'https://github.com/camenduru/{sdw}-huggingface',
87
- # Add more extension URLs if needed
88
- ]
89
- for url in urls:
90
- filename = url.split('/')[-1]
91
- if 'github' in url:
92
- get_ipython().system(f'git clone {url} {wb}/extensions/{filename}')
93
-
94
- # Add more installation steps for additional functionalities if needed
95
-
96
- # 初始化本地环境
97
- def initLocal():
98
- # 初始化云盘
99
- initCloudDrive()
100
-
101
- # 安装附加功能
102
- installAdditional()
103
-
104
- # Set up environment variables if needed
105
-
106
- clear_output()
107
- print("初始化完毕,重启云盘,然后进入下一步~")
108
-
109
- # 运行
110
- def run(script):
111
- clear_output()
112
- get_ipython().run_line_magic('cd', f'{wb}')
113
- get_ipython().system(f'python {script} --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --no-half-vae --disable-console-progressbars --multiple --opt-sdp-attention --api --cors-allow-origins=*')
114
-
115
- # 运行脚本
116
- if os.path.exists(f'{wb}'):
117
- run('webui.py')
118
- else:
119
- # 初化本地环境
120
- initLocal()
121
- # 运行
122
- run('launch.py')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main.py ADDED
@@ -0,0 +1,359 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import binascii
3
+
4
+ # 自定义类型转换函数
5
+ def str_to_bool(value):
6
+ if value.lower() in ('yes', 'true', 't', 'y', '1'):
7
+ return True
8
+ elif value.lower() in ('no', 'false', 'f', 'n', '0'):
9
+ return False
10
+ else:
11
+ raise argparse.ArgumentTypeError('Invalid boolean value: {}'.format(value))
12
+
13
+ # 创建参数解析器
14
+ parser = argparse.ArgumentParser()
15
+
16
+ parser.add_argument('--UI', type=str)
17
+ parser.add_argument('--Version', type=str)
18
+ parser.add_argument('--ControlNet', type=str)
19
+ parser.add_argument('--Roop', type=str_to_bool)
20
+ parser.add_argument('--Drive_Map', type=str_to_bool)
21
+ parser.add_argument('--Key_words', type=str_to_bool)
22
+ parser.add_argument('--SadTalker', type=str_to_bool)
23
+ parser.add_argument('--Segment_Anything', type=str_to_bool)
24
+
25
+ # 解析命令行参数
26
+ args = parser.parse_args()
27
+
28
+
29
+ UI = args.UI
30
+ Roop = args.Roop
31
+ Version = args.Version
32
+ ControlNet = args.ControlNet
33
+ Drive_Map = args.Drive_Map
34
+ Key_words = args.Key_words
35
+ Segment_Anything = args.Segment_Anything
36
+ SadTalker = args.SadTalker
37
+
38
+ ################################################################################################################################################
39
+
40
+ import sys
41
+ import os
42
+ import json
43
+ import base64
44
+ import importlib.util
45
+ from IPython import get_ipython
46
+ from IPython.display import clear_output
47
+ from google.colab import drive
48
+ import tensorflow as tf
49
+
50
+ # 检测是否为GPU运行
51
+ print("TensorFlow version:", tf.__version__)
52
+ if tf.test.gpu_device_name():
53
+ drive.mount('/content/drive')
54
+ else:
55
+ raise Exception("\n请在《代码执行程序》-《更改运行时类型》-设置为GPU~")
56
+
57
+ w = base64.b64decode(("d2VidWk=").encode('ascii')).decode('ascii')
58
+ sdw = base64.b64decode(("c3RhYmxlLWRpZmZ1c2lvbi13ZWJ1aQ==").encode('ascii')).decode('ascii')
59
+ # sdw = binascii.unhexlify("737461626c652d646966667573696f6e2d7765627569").decode('ascii')
60
+ # w = binascii.unhexlify("7765627569").decode('ascii')
61
+ wb = f'/content/{sdw}'
62
+ gwb = f'/content/drive/MyDrive/SD_DATA'
63
+
64
+ get_ipython().run_line_magic('cd', '/content')
65
+ get_ipython().run_line_magic('env', 'TF_CPP_MIN_LOG_LEVEL=1')
66
+
67
+ # 云盘同步
68
+ def cloudDriveSync(cloudPath, localPath='', sync=False):
69
+ # 云盘没有目录
70
+ if not os.path.exists(cloudPath):
71
+ # 创建云盘目录
72
+ get_ipython().system(f'mkdir {cloudPath}')
73
+
74
+ # 是否要同步
75
+ if not sync:
76
+ return
77
+
78
+ # 删除本地目录
79
+ get_ipython().system(f'rm -rf {localPath}')
80
+ # 链接云盘目录
81
+ get_ipython().system(f'ln -s {cloudPath} {localPath}')
82
+
83
+ # 初始化云盘
84
+ def initCloudDrive():
85
+ cloudDriveSync(f'{gwb}')
86
+ cloudDriveSync(f'{gwb}/Config')
87
+ cloudDriveSync(f'{gwb}/Models', f'{wb}/models/Stable-diffusion', Drive_Map)
88
+ cloudDriveSync(f'{gwb}/Lora', f'{wb}/models/Lora', Drive_Map)
89
+ cloudDriveSync(f'{gwb}/LyCORIS', f'{wb}/models/LyCORIS', Drive_Map)
90
+ cloudDriveSync(f'{gwb}/hypernetworks', f'{wb}/models/hypernetworks', Drive_Map)
91
+ cloudDriveSync(f'{gwb}/Vae', f'{wb}/models/VAE', Drive_Map)
92
+ cloudDriveSync(f'{gwb}/Outputs', f'{wb}/outputs', Drive_Map)
93
+
94
+
95
+ # 云盘没有配置文件
96
+ if not os.path.exists(f'{gwb}/Config/config.json'):
97
+ get_ipython().system(f'wget -O {gwb}/Config/config.json "https://huggingface.co/wageguagua/main/raw/main/config.json"')
98
+ # 如果云盘没有styles.csv文件则手动创建
99
+ if not os.path.exists(f'{gwb}/styles.csv'):
100
+ #创建styles.csv文件
101
+ get_ipython().system(f'mkdir {gwb}/styles.csv')
102
+
103
+ # clong git
104
+ def gitDownload(url, localPath):
105
+ if os.path.exists(localPath):
106
+ return
107
+
108
+ get_ipython().system(f'git clone {url} {localPath}')
109
+
110
+ # 安装附加功能
111
+ def installAdditional():
112
+ # 安装扩展
113
+ urls = [
114
+ f'https://github.com/camenduru/{sdw}-images-browser', # 图像浏览器
115
+ f'https://github.com/camenduru/sd-{w}-tunnels', # Tunnel 网络支持
116
+ f'https://github.com/etherealxx/batchlinks-{w}', # 批量下载模型lora
117
+ f'https://github.com/camenduru/sd-civitai-browser', # Civitai 分类和搜索
118
+ f'https://github.com/AUTOMATIC1111/{sdw}-rembg', # 背景移除功能
119
+ f'https://github.com/thomasasfk/sd-{w}-aspect-ratio-helper', # 宽高比调整功能
120
+ f'https://github.com/hanamizuki-ai/{sdw}-localization-zh_Hans', # 中文
121
+ f'https://github.com/kohya-ss/sd-{w}-additional-networks', # 模型网络
122
+ f'https://github.com/fkunn1326/openpose-editor', # 人体姿态编辑功能
123
+ f'https://github.com/jexom/sd-{w}-depth-lib', # 深度图编辑器
124
+ f'https://github.com/hnmr293/posex', # 人体姿态估计
125
+ f'https://github.com/s9roll7/ebsynth_utility', # 视频的图像生成
126
+ f'https://github.com/ashen-sensored/{sdw}-two-shot', # 潜��量成对(双人特写)
127
+ f'https://github.com/nonnonstop/sd-{w}-3d-open-pose-editor', # 3D 人体姿态编辑功能
128
+ f'https://github.com/camenduru/{sdw}-huggingface', # 整合 Huggingface 的模型功能
129
+ f'https://github.com/camenduru/{sdw}-catppuccin', # Catppuccin 主题
130
+ f'https://github.com/IDEA-Research/DWPose', # cnt手部模型
131
+ f'https://github.com/KohakuBlueleaf/a1111-sd-{w}-lycoris', # 人物生成模型
132
+ f'https://github.com/Uminosachi/sd-{w}-inpaint-anything', # 抠图
133
+ # f'https://github.com/picobyte/{sdw}-wd14-tagger', # tag
134
+ # f'https://github.com/Gerschel/sd_web_ui_preset_utils', # 预设
135
+ # f'https://github.com/numz/sd-wav2lip-uhq', # 数字人说话
136
+ # f'https://github.com/a2569875/{sdw}-composable-lora', # 可自组 LoRA
137
+ # f'https://github.com/deforum-art/deforum-for-automatic1111-{w}', # Deforum动态视频创作
138
+ # f'https://github.com/Scholar01/sd-{w}-mov2mov', # 视频转换
139
+ # f'https://github.com/tjm35/asymmetric-tiling-sd-{w}', # 非对称平铺功能
140
+ # f'https://github.com/hako-mikan/sd-{w}-lora-block-weight', # LORA 模型的权重调整
141
+ # f'https://github.com/hako-mikan/sd-{w}-supermerger', # 图像融合功能
142
+ ]
143
+ for url in urls:
144
+
145
+ filename = url.split('/')[-1]
146
+
147
+ if 'github' in url:
148
+ get_ipython().system(f'git clone {url} {wb}/extensions/{filename}')
149
+
150
+ get_ipython().system(f'wget https://raw.githubusercontent.com/camenduru/{sdw}-scripts/main/run_n_times.py -O {wb}/scripts/run_n_times.py')
151
+ get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d {wb}/models/ESRGAN -o 4x-UltraSharp.pth')
152
+
153
+
154
+ # 优化embeddings
155
+ gitDownload(f'https://huggingface.co/embed/negative',f'{wb}/embeddings/negative')
156
+ get_ipython().system(f'rm -rf {wb}/embeddings/negative/.git')
157
+ get_ipython().system(f'rm {wb}/embeddings/negative/.gitattributes')
158
+
159
+ gitDownload(f'https://huggingface.co/embed/lora',f'{wb}/models/Lora/positive')
160
+ get_ipython().system(f'rm -rf {wb}/models/Lora/positive/.git')
161
+ get_ipython().system(f'rm {wb}/models/Lora/positive/.gitattributes')
162
+
163
+ #中文插件
164
+ gitDownload(f'https://github.com/DominikDoom/a1111-sd-{w}-tagcomplete',f'{wb}/extensions/a1111-sd-{w}-tagcomplete')
165
+ get_ipython().system(f'rm -f {wb}/extensions/a1111-sd-{w}-tagcomplete/tags/danbooru.csv')
166
+ get_ipython().system(f'wget https://beehomefile.oss-cn-beijing.aliyuncs.com/20210114/danbooru.csv -O {wb}/extensions/a1111-sd-{w}-tagcomplete/tags/danbooru.csv')
167
+ gitDownload(f'https://github.com/toriato/{sdw}-wd14-tagger',f'{wb}/extensions/{sdw}-wd14-tagge')
168
+ # get_ipython().system(f'rm -f {wb}/localizations')
169
+ # gitDownload(f'https://github.com/dtlnor/{sdw}-localization-zh_CN',f'{wb}/extensions/{sdw}-localization-zh_CN')
170
+
171
+ #附加插件=脸部修复/颜色细化/ps组件/漫画助手/分块vae
172
+ # gitDownload(f'https://github.com/hnmr293/sd-{w}-cutoff',f'{wb}/extensions/sd-{w}-cutoff')
173
+ gitDownload(f'https://github.com/Bing-su/adetailer',f'{wb}/extensions/adetailer')
174
+ gitDownload(f'https://github.com/antfu/sd-{w}-qrcode-toolkit',f'{wb}/extensions/sd-{w}-qrcode-toolkit')
175
+ gitDownload(f'https://github.com/yankooliveira/sd-{w}-photopea-embed',f'{wb}/extensions/sd-{w}-photopea-embed')
176
+ # get_ipython().system(f'wget https://huggingface.co/wageguagua/mhzs/raw/main/jubenchajian4_51.py -O {wb}/scripts/jubenchajian4_51.py')
177
+ gitDownload(f'https://github.com/pkuliyi2015/multidiffusion-upscaler-for-automatic1111',f'{wb}/extensions/multidiffusion-upscaler-for-automatic1111')
178
+ #Roop换脸插件
179
+ if Roop:
180
+ # gitDownload(f'https://github.com/s0md3v/sd-{w}-roop',f'{wb}/extensions/sd-{w}-roop')
181
+ gitDownload(f'https://github.com/Gourieff/sd-{w}-reactor',f'{wb}/extensions/sd-{w}-reactor')
182
+ # get_ipython().system(f'wget https://huggingface.co/henryruhs/roop/resolve/main/inswapper_128.onnx -O {wb}/models/roop/inswapper_128.onnx')
183
+ print('Roop换脸启用')
184
+ else:
185
+ print('Roop换脸不启用')
186
+
187
+ # ControlNet模型
188
+ Cnt_models = [
189
+ 'control_v11e_sd15_ip2p_fp16.safetensors',
190
+ 'control_v11e_sd15_shuffle_fp16.safetensors',
191
+ 'control_v11p_sd15_canny_fp16.safetensors',
192
+ 'control_v11f1p_sd15_depth_fp16.safetensors',
193
+ 'control_v11p_sd15_inpaint_fp16.safetensors',
194
+ 'control_v11p_sd15_lineart_fp16.safetensors',
195
+ 'control_v11p_sd15_mlsd_fp16.safetensors',
196
+ 'control_v11p_sd15_normalbae_fp16.safetensors',
197
+ 'control_v11p_sd15_openpose_fp16.safetensors',
198
+ 'control_v11p_sd15_scribble_fp16.safetensors',
199
+ 'control_v11p_sd15_seg_fp16.safetensors',
200
+ 'control_v11p_sd15_softedge_fp16.safetensors',
201
+ 'control_v11p_sd15s2_lineart_anime_fp16.safetensors',
202
+ 'control_v11f1e_sd15_tile_fp16.safetensors',
203
+ ]
204
+ get_ipython().system(f'rm -rf {wb}/extensions/sd-{w}-controlnet')
205
+ # 模型下载到Colab
206
+ if ControlNet == "Colab":
207
+ gitDownload(f'https://github.com/Mikubill/sd-{w}-controlnet',f'{wb}/extensions/sd-{w}-controlnet')
208
+ for v in Cnt_models:
209
+ get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/{v} -d {wb}/extensions/sd-{w}-controlnet/models -o {v}')
210
+
211
+ # 模型下载到Google_Drive
212
+ elif ControlNet == "Google_Drive":
213
+ cloudDriveSync(f'{gwb}/CntModels')
214
+ gitDownload(f'https://github.com/Mikubill/sd-{w}-controlnet',f'{wb}/extensions/sd-{w}-controlnet')
215
+ for v in Cnt_models:
216
+ if not os.path.exists(f'{gwb}/CntModels/{v}'):
217
+ get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/{v} -d {gwb}/CntModels -o {v}')
218
+ print("创建扩展",f'{v}')
219
+ else:
220
+ print("扩展存在",f'{v}')
221
+ # 遍历cntmodels目录下的文件,将文件银接到Colab目录下的extensions目录下
222
+ for v in os.listdir(f'{gwb}/CntModels'):
223
+ # 链接云盘目录
224
+ get_ipython().system(f'ln -s {gwb}/CntModels/{v} {wb}/extensions/sd-{w}-controlnet/models')
225
+
226
+ elif ControlNet == "No":
227
+ print("不使用 ControlNet")
228
+
229
+ # 各种UI界面
230
+ if UI == "Kitchen_Ui":
231
+ gitDownload(f'https://github.com/canisminor1990/sd-{w}-kitchen-theme-legacy', f'{wb}/extensions/sd-{w}-kitchen-theme-legacy')
232
+ print("Kitchen界面插件启用")
233
+ elif UI == "Lobe_Ui":
234
+ gitDownload(f'https://github.com/canisminor1990/sd-web-ui-kitchen-theme', f'{wb}/extensions/sd-web-ui-kitchen-theme')
235
+ print("Lobe界面插件启用")
236
+ elif UI == "No":
237
+ print("UI插件不启用")
238
+
239
+ # 关键词
240
+ if Key_words:
241
+ gitDownload(f'https://github.com/Physton/sd-{w}-prompt-all-in-one', f'{wb}/extensions/sd-{w}-prompt-all-in-one')
242
+ cloudDriveSync(f'{gwb}/Storage', f'{wb}/extensions/sd-{w}-prompt-all-in-one/storage', Key_words)
243
+ print("关键词插件启用")
244
+ else:
245
+ get_ipython().system(f'rm -rf {wb}/extensions/sd-{w}-prompt-all-in-one')
246
+ print("关键词插件不启用")
247
+
248
+ # 人物说话
249
+ if SadTalker:
250
+ if not os.path.exists(f'{wb}/extensions/SadTalker'):
251
+ get_ipython().system(f'git clone https://github.com/OpenTalker/SadTalker {wb}/extensions/SadTalker')
252
+ get_ipython().system(f'git -C {wb}/extensions/SadTalker clone https://huggingface.co/wageguagua/checkpoints')
253
+ get_ipython().system(f'git -C {wb}/models clone https://huggingface.co/wageguagua/GFPGAN')
254
+
255
+ get_ipython().system(f'git clone https://github.com/numz/sd-wav2lip-uhq {wb}/extensions/sd-wav2lip-uhq')
256
+ get_ipython().system(f'wget https://huggingface.co/wageguagua/wav2lip/resolve/main/wav2lip.pth -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/checkpoints/wav2lip.pth')
257
+ get_ipython().system(f'wget https://huggingface.co/wageguagua/wav2lip/resolve/main/wav2lip_gan.pth -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/checkpoints/wav2lip_gan.pth')
258
+ get_ipython().system(f'wget https://huggingface.co/wageguagua/wav2lip/resolve/main/s3fd-619a316812.pth -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/face_detection/detection/sfd/s3fd-619a316812.pth')
259
+ get_ipython().system(f'wget https://huggingface.co/wageguagua/wav2lip/resolve/main/shape_predictor_68_face_landmarks.dat -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/predicator/shape_predictor_68_face_landmarks.dat')
260
+
261
+ print("SadTalker启用")
262
+ else:
263
+ get_ipython().system(f'rm -rf {wb}/extensions/SadTalker')
264
+ print("SadTalker不启用")
265
+
266
+ # 智能抠图
267
+ if Segment_Anything:
268
+ get_ipython().system(f'git clone https://github.com/continue-revolution/sd-{w}-segment-anything {wb}/extensions/sd-{w}-segment-anything')
269
+
270
+ cloudDriveSync(f'{gwb}/Segment')
271
+ if not os.path.exists(f'{gwb}/Segment/sam_vit_h_4b8939.pth'):
272
+ get_ipython().system(f'wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth -O {gwb}/Segment/sam_vit_h_4b8939.pth')
273
+ # if not os.path.exists(f'{gwb}/Segment/groundingdino_swinb_cogcoor.pth'):
274
+ # get_ipython().system(f'wget https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth -O {gwb}/Segment/groundingdino_swinb_cogcoor.pth')
275
+
276
+ get_ipython().system(f'ln -s {gwb}/Segment/sam_vit_h_4b8939.pth {wb}/extensions/sd-{w}-segment-anything/models/sam')
277
+ # get_ipython().system(f'ln -s {gwb}/Segment/groundingdino_swinb_cogcoor.pth {wb}/extensions/sd-{w}-segment-anything/models/grounding-dino')
278
+ print("智能抠图插件启用")
279
+ else:
280
+ get_ipython().system(f'rm -rf {wb}/extensions/sd-{w}-segment-anything')
281
+ print("智能抠图插件不启用")
282
+
283
+ # 初始化本地环境
284
+ def initLocal():
285
+ #部署 env 环境变量
286
+ get_ipython().system(f'apt -y update -qq')
287
+ get_ipython().system(f'wget https://github.com/camenduru/gperftools/releases/download/v1.0/libtcmalloc_minimal.so.4 -O /content/libtcmalloc_minimal.so.4')
288
+ get_ipython().run_line_magic('env', 'LD_PRELOAD=/content/libtcmalloc_minimal.so.4')
289
+
290
+ #设置 python 环境
291
+ get_ipython().system(f'apt -y install -qq aria2 libcairo2-dev pkg-config python3-dev')
292
+ get_ipython().system(f'pip install -q torch==2.0.1+cu118 torchvision==0.15.2+cu118 torchaudio==2.0.2+cu118 torchtext==0.15.2 torchdata==0.6.1 --extra-index-url https://download.pytorch.org/whl/cu118 -U')
293
+ get_ipython().system(f'pip install -q xformers==0.0.20 triton==2.0.0 gradio_client==0.2.7 -U')
294
+
295
+ #主框架模块
296
+ if Version == "A1111":
297
+ get_ipython().system(f'git clone -b master https://github.com/AUTOMATIC1111/{sdw} {wb}')
298
+ elif Version == "V2.5":
299
+ get_ipython().system(f'git clone -b v2.4 https://github.com/camenduru/{sdw} {wb}')
300
+ elif Version == "V2.4":
301
+ get_ipython().system(f'git clone -b v2.2 https://github.com/camenduru/{sdw} {wb}')
302
+
303
+ get_ipython().system(f'git -C {wb}/repositories/stable-diffusion-stability-ai reset --hard')
304
+
305
+ # 初始化云盘
306
+ initCloudDrive()
307
+
308
+ # 安装附加功能
309
+ installAdditional()
310
+
311
+ # 删除原配置
312
+ get_ipython().system(f'rm -f {wb}/config.json')
313
+
314
+ # 链接用户配置
315
+ get_ipython().system(f'ln -s {gwb}/Config/config.json {wb}/config.json')
316
+
317
+ #如果{gwb}有styles.csv文件则同步到{wb},反之则取消该操作
318
+ if os.path.exists(f"{gwb}/styles.csv"):
319
+ get_ipython().system(f'ln -s {gwb}/styles.csv {wb}/styles.csv')
320
+
321
+ # 同步additional-networks
322
+ if os.path.exists(f"{wb}/extensions/sd-{w}-additional-networks"):
323
+ cloudDriveSync(f'{gwb}/Lora', f'{wb}/extensions/sd-{w}-additional-networks/models/lora', Drive_Map)
324
+ # cloudDriveSync(f'{gwb}/LyCORIS', f'{wb}/extensions/sd-{w}-additional-networks/models/lora/lycoris', Drive_Map)
325
+
326
+ # 如果云盘没有模型
327
+ if len(os.listdir(f"{gwb}/Models")) == 0:
328
+ #下载主模型
329
+ get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/chilloutmix/resolve/main/chilloutmix_NiPrunedFp32Fix.safetensors -d {wb}/models/Stable-diffusion -o chilloutmix_NiPrunedFp32Fix.safetensors')
330
+
331
+ # 如果云盘Vae模型
332
+ if len(os.listdir(f"{gwb}/Vae")) == 0:
333
+ # #VAE
334
+ get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -d {wb}/models/VAE -o vae-ft-mse-840000-ema-pruned.safetensors')
335
+
336
+ #放大
337
+ get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d {wb}/models/ESRGAN -o 4x-UltraSharp.pth')
338
+
339
+ model_dir = os.path.join(wb, "models", "Stable-diffusion")
340
+ if any(f.endswith(('.ckpt', '.safetensors')) for f in os.listdir(model_dir)):
341
+ get_ipython().system(f'sed -i \'s@weight_load_location =.*@weight_load_location = "cuda"@\' {wb}/modules/shared.py')
342
+ get_ipython().system(f'sed -i "s@os.path.splitext(model_file)@os.path.splitext(model_file); map_location=\'cuda\'@" {wb}/modules/sd_models.py')
343
+ get_ipython().system(f'sed -i "s@map_location=\'cpu\'@map_location=\'cuda\'@" {wb}/modules/extras.py')
344
+ get_ipython().system(f"sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' {wb}/webui.py")
345
+
346
+ # 运行
347
+ def run(script):
348
+ clear_output()
349
+ get_ipython().run_line_magic('cd', f'{wb}')
350
+ get_ipython().system(f'python {script} --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --disable-console-progressbars --multiple --api --cors-allow-origins=*')
351
+
352
+ # 运行脚本
353
+ if os.path.exists(f'{wb}'):
354
+ run('webui.py')
355
+ else:
356
+ # 初化本地环境
357
+ initLocal()
358
+ # 运行
359
+ run('launch.py')