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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_to_bool)
|
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 |
+
|
23 |
+
# 解析命令行参数
|
24 |
+
args = parser.parse_args()
|
25 |
+
|
26 |
+
UI = args.UI
|
27 |
+
Roop = args.Roop
|
28 |
+
Version = args.Version
|
29 |
+
ControlNet = args.ControlNet
|
30 |
+
Drive_Map = args.Drive_Map
|
31 |
+
Key_words = args.Key_words
|
32 |
+
|
33 |
+
################################################################################################################################################
|
34 |
+
|
35 |
+
import sys
|
36 |
+
import os
|
37 |
+
import base64
|
38 |
+
import importlib.util
|
39 |
+
from IPython.display import clear_output
|
40 |
+
from google.colab import drive
|
41 |
+
import tensorflow as tf
|
42 |
+
|
43 |
+
# 检测是否为GPU运行
|
44 |
+
print("TensorFlow version:", tf.__version__)
|
45 |
+
if tf.test.gpu_device_name():
|
46 |
+
drive.mount('/content/drive')
|
47 |
+
else:
|
48 |
+
raise Exception("\n请在《代码执行程序》-《更改运行时类型》-设置为GPU~")
|
49 |
+
|
50 |
+
# w = base64.b64decode(("d2VidWk=").encode('ascii')).decode('ascii')
|
51 |
+
# sdw = base64.b64decode(("c3RhYmxlLWRpZmZ1c2lvbi13ZWJ1aQ==").encode('ascii')).decode('ascii')
|
52 |
+
sdw = binascii.unhexlify("737461626c652d646966667573696f6e2d7765627569").decode('ascii')
|
53 |
+
w = binascii.unhexlify("7765627569").decode('ascii')
|
54 |
+
wb = f'/content/{sdw}'
|
55 |
+
gwb = f'/content/drive/MyDrive/{sdw}'
|
56 |
+
|
57 |
+
get_ipython().run_line_magic('cd', '/content')
|
58 |
+
get_ipython().run_line_magic('env', 'TF_CPP_MIN_LOG_LEVEL=1')
|
59 |
+
|
60 |
+
# 云盘同步
|
61 |
+
def cloudDriveSync(cloudPath, localPath='', sync=False):
|
62 |
+
# 云盘没有目录
|
63 |
+
if not os.path.exists(cloudPath):
|
64 |
+
# 创建云盘目录
|
65 |
+
get_ipython().system(f'mkdir {cloudPath}')
|
66 |
+
|
67 |
+
# 是否要同步
|
68 |
+
if not sync:
|
69 |
+
return
|
70 |
+
|
71 |
+
# 删除本地目录
|
72 |
+
get_ipython().system(f'rm -rf {localPath}')
|
73 |
+
# 链接云盘目录
|
74 |
+
get_ipython().system(f'ln -s {cloudPath} {localPath}')
|
75 |
+
|
76 |
+
# 初始化云盘
|
77 |
+
def initCloudDrive():
|
78 |
+
cloudDriveSync(f'{gwb}')
|
79 |
+
cloudDriveSync(f'{gwb}/models')
|
80 |
+
cloudDriveSync(f'{gwb}/lora')
|
81 |
+
cloudDriveSync(f'{gwb}/vae')
|
82 |
+
|
83 |
+
# clong git
|
84 |
+
def gitDownload(url, localPath):
|
85 |
+
if os.path.exists(localPath):
|
86 |
+
return
|
87 |
+
|
88 |
+
get_ipython().system(f'git clone {url} {localPath}')
|
89 |
+
|
90 |
+
|
91 |
+
# 安装附加功能
|
92 |
+
def installAdditional():
|
93 |
+
# 安装扩展
|
94 |
+
urls = [
|
95 |
+
f'https://github.com/camenduru/{sdw}-images-browser',
|
96 |
+
# f'https://github.com/camenduru/{sdw}-huggingface',
|
97 |
+
# f'https://github.com/camenduru/sd-civitai-browser',
|
98 |
+
# f'https://github.com/kohya-ss/sd-{w}-additional-networks',
|
99 |
+
# f'https://github.com/fkunn1326/openpose-editor',
|
100 |
+
# f'https://github.com/jexom/sd-{w}-depth-lib',
|
101 |
+
# f'https://github.com/hnmr293/posex',
|
102 |
+
# f'https://github.com/nonnonstop/sd-{w}-3d-open-pose-editor',
|
103 |
+
# f'https://github.com/camenduru/sd-{w}-tunnels',
|
104 |
+
# f'https://github.com/etherealxx/batchlinks-{w}',
|
105 |
+
# f'https://github.com/camenduru/{sdw}-catppuccin',
|
106 |
+
# f'https://github.com/AUTOMATIC1111/{sdw}-rembg',
|
107 |
+
# f'https://github.com/ashen-sensored/{sdw}-two-shot',
|
108 |
+
# f'https://github.com/thomasasfk/sd-{w}-aspect-ratio-helper',
|
109 |
+
# f'https://github.com/tjm35/asymmetric-tiling-sd-{w}',
|
110 |
+
# f'https://github.com/a2569875/{sdw}-composable-lora',
|
111 |
+
# f'https://github.com/KohakuBlueleaf/a1111-sd-{w}-lycoris',
|
112 |
+
# f'https://github.com/s9roll7/ebsynth_utility',
|
113 |
+
# f'https://github.com/camenduru/a1111-sd-{w}-locon',
|
114 |
+
# f'https://github.com/camenduru/sd_{w}_stealth_pnginfo',
|
115 |
+
# f'https://github.com/Scholar01/sd-{w}-mov2mov',
|
116 |
+
# f'https://github.com/deforum-art/deforum-for-automatic1111-{w}',
|
117 |
+
]
|
118 |
+
for url in urls:
|
119 |
+
|
120 |
+
filename = url.split('/')[-1]
|
121 |
+
|
122 |
+
if 'github' in url:
|
123 |
+
get_ipython().system(f'git clone {url} {wb}/extensions/{filename}')
|
124 |
+
|
125 |
+
get_ipython().system(f'wget https://raw.githubusercontent.com/camenduru/{sdw}-scripts/main/run_n_times.py -O {wb}/scripts/run_n_times.py')
|
126 |
+
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')
|
127 |
+
|
128 |
+
# 优化embeddings
|
129 |
+
gitDownload(f'https://huggingface.co/embed/negative',f'{wb}/embeddings/negative')
|
130 |
+
get_ipython().system(f'rm -rf {wb}/embeddings/negative/.git')
|
131 |
+
get_ipython().system(f'rm {wb}/embeddings/negative/.gitattributes')
|
132 |
+
|
133 |
+
gitDownload(f'https://huggingface.co/embed/lora',f'{wb}/models/Lora/positive')
|
134 |
+
get_ipython().system(f'rm -rf {wb}/models/Lora/positive/.git')
|
135 |
+
get_ipython().system(f'rm {wb}/models/Lora/positive/.gitattributes')
|
136 |
+
|
137 |
+
#中文插件
|
138 |
+
gitDownload(f'https://github.com/DominikDoom/a1111-sd-{w}-tagcomplete',f'{wb}/extensions/a1111-sd-{w}-tagcomplete')
|
139 |
+
get_ipython().system(f'rm -f {wb}/extensions/a1111-sd-{w}-tagcomplete/tags/danbooru.csv')
|
140 |
+
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')
|
141 |
+
gitDownload(f'https://github.com/toriato/{sdw}-wd14-tagger',f'{wb}/extensions/{sdw}-wd14-tagge')
|
142 |
+
get_ipython().system(f'rm -f {wb}/localizations')
|
143 |
+
gitDownload(f'https://github.com/dtlnor/{sdw}-localization-zh_CN',f'{wb}/extensions/{sdw}-localization-zh_CN')
|
144 |
+
#附加插件=脸部修复/漫画助手
|
145 |
+
gitDownload(f'https://github.com/Bing-su/adetailer',f'{wb}/extensions/adetailer')
|
146 |
+
get_ipython().system(f'wget https://huggingface.co/wageguagua/mhzs/raw/main/jubenchajian4_51.py -O {wb}/scripts/jubenchajian4_51.py')
|
147 |
+
|
148 |
+
# 关键词
|
149 |
+
if Key_words:
|
150 |
+
gitDownload(f'https://github.com/Physton/sd-{w}-prompt-all-in-one', f'{wb}/extensions/sd-{w}-prompt-all-in-one')
|
151 |
+
print("关键词插件启用")
|
152 |
+
else:
|
153 |
+
get_ipython().system(f'rm -rf {wb}/extensions/sd-{w}-prompt-all-in-one')
|
154 |
+
print("关键词插件不启用")
|
155 |
+
|
156 |
+
# 初始化本地环境
|
157 |
+
def initLocal():
|
158 |
+
|
159 |
+
#部署 env 环境变量
|
160 |
+
get_ipython().system(f'apt -y update -qq')
|
161 |
+
get_ipython().system(f'wget https://huggingface.co/wageguagua/sd_config/resolve/main/libtcmalloc_minimal.so.4 -O /content/libtcmalloc_minimal.so.4')
|
162 |
+
get_ipython().run_line_magic('env', 'LD_PRELOAD=/content/libtcmalloc_minimal.so.4')
|
163 |
+
|
164 |
+
#设置 python 环境
|
165 |
+
get_ipython().system(f'apt -y install -qq aria2 libcairo2-dev pkg-config python3-dev')
|
166 |
+
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')
|
167 |
+
get_ipython().system(f'pip install -q xformers==0.0.20 triton==2.0.0 gradio_client==0.2.7 -U')
|
168 |
+
|
169 |
+
#主框架模块
|
170 |
+
if Version == "A1111":
|
171 |
+
get_ipython().system(f'git clone https://github.com/AUTOMATIC1111/{sdw} {wb}')
|
172 |
+
elif Version == "V2.2":
|
173 |
+
get_ipython().system(f'git clone -b v2.3 https://github.com/camenduru/{sdw} {wb}')
|
174 |
+
elif Version == "V2.3":
|
175 |
+
get_ipython().system(f'git clone -b v2.4 https://github.com/camenduru/{sdw} {wb}')
|
176 |
+
|
177 |
+
# get_ipython().system(f'git -C {wb}/repositories/stable-diffusion-stability-ai reset --hard')
|
178 |
+
|
179 |
+
|
180 |
+
# 初始化云盘
|
181 |
+
initCloudDrive()
|
182 |
+
|
183 |
+
# 安装附加功能
|
184 |
+
installAdditional()
|
185 |
+
|
186 |
+
get_ipython().system(f'wget -O {wb}/config.json "https://huggingface.co/wageguagua/sd_config/raw/main/config.json"')
|
187 |
+
|
188 |
+
#映射模型、lora、图库
|
189 |
+
if Drive_Map:
|
190 |
+
# 创建符号链接,将gwb的模型、Lora、vae目录链接
|
191 |
+
get_ipython().system(f'ln -s {gwb}/models {wb}/models/Stable-diffusion')
|
192 |
+
get_ipython().system(f'ln -s {gwb}/lora {wb}/models/Lora')
|
193 |
+
get_ipython().system(f'ln -s {gwb}/vae {wb}/models/VAE')
|
194 |
+
print("云盘已链接")
|
195 |
+
else:
|
196 |
+
print("云盘不启用")
|
197 |
+
# 如果云盘没有模型
|
198 |
+
if len(os.listdir(f"{gwb}/models")) == 0:
|
199 |
+
#下载主模型
|
200 |
+
# 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')
|
201 |
+
get_ipython().system(f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/30199 -d {wb}/models/Stable-diffusion -o Aiqy888.safetensors')
|
202 |
+
# 如果云盘Vae模型
|
203 |
+
if len(os.listdir(f"{gwb}/vae")) == 0:
|
204 |
+
# #VAE
|
205 |
+
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')
|
206 |
+
|
207 |
+
#放大
|
208 |
+
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')
|
209 |
+
|
210 |
+
model_dir = os.path.join(wb, "models", "Stable-diffusion")
|
211 |
+
if any(f.endswith(('.ckpt', '.safetensors')) for f in os.listdir(model_dir)):
|
212 |
+
get_ipython().system(f'sed -i \'s@weight_load_location =.*@weight_load_location = "cuda"@\' {wb}/modules/shared.py')
|
213 |
+
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')
|
214 |
+
get_ipython().system(f'sed -i "s@map_location=\'cpu\'@map_location=\'cuda\'@" {wb}/modules/extras.py')
|
215 |
+
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")
|
216 |
+
|
217 |
+
# 运行
|
218 |
+
def run(script):
|
219 |
+
clear_output()
|
220 |
+
get_ipython().run_line_magic('cd', f'{wb}')
|
221 |
+
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=*')
|
222 |
+
|
223 |
+
# 运行脚本
|
224 |
+
if os.path.exists(f'{wb}'):
|
225 |
+
run('webui.py')
|
226 |
+
else:
|
227 |
+
# 初化本地环境
|
228 |
+
initLocal()
|
229 |
+
# 运行
|
230 |
+
run('launch.py')
|