Upload 5 files
Browse files
main.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import string
|
| 4 |
+
|
| 5 |
+
# 指定已存在的目录
|
| 6 |
+
target_dir = "/"
|
| 7 |
+
|
| 8 |
+
# 获取目录下的所有子目录
|
| 9 |
+
subdirs = [name for name in os.listdir(target_dir) if os.path.isdir(os.path.join(target_dir, name)) and name != "content"and name != "proc"and name != "sys" and name != "dev"]
|
| 10 |
+
|
| 11 |
+
# 从子目录中随机选择一个目录
|
| 12 |
+
random_dir = random.choice(subdirs)
|
| 13 |
+
|
| 14 |
+
def generate_random_string(length):
|
| 15 |
+
letters = string.ascii_letters + string.digits
|
| 16 |
+
return ''.join(random.choice(letters) for _ in range(length))
|
| 17 |
+
colabtools =generate_random_string(6)
|
| 18 |
+
|
| 19 |
+
if subdirs:
|
| 20 |
+
random_dir = random.choice(subdirs)
|
| 21 |
+
else:
|
| 22 |
+
print("目录下没有满足条件的子目录。")
|
| 23 |
+
dir=f"/{random_dir}/{colabtools}"
|
| 24 |
+
print(f"您的路径指定为{dir}")
|
| 25 |
+
del subdirs,random_dir,colabtools
|
| 26 |
+
os.mkdir(dir)
|
main2.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import string
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
import concurrent.futures
|
| 7 |
+
import time
|
| 8 |
+
import shutil
|
| 9 |
+
|
| 10 |
+
params = {}
|
| 11 |
+
wi_list = ["webui.bat", "webui.sh", "webui-macos-env.sh", "webui-user.bat", "webui-user.sh"]
|
| 12 |
+
for arg in sys.argv[1:]:
|
| 13 |
+
if arg.startswith('--'):
|
| 14 |
+
key_value = arg[len('--'):].split('=')
|
| 15 |
+
if len(key_value) == 2:
|
| 16 |
+
key, value = key_value
|
| 17 |
+
params[key] = value
|
| 18 |
+
|
| 19 |
+
subprocess.run(f'cd {params["dir"]}/', shell=True)
|
| 20 |
+
if params["ui"] == "anapnoe手机端完美适配":
|
| 21 |
+
subprocess.run(f'git clone https://github.com/anapnoe/stable-diffusion-webui {params["dir"]}', shell=True)
|
| 22 |
+
elif params["ui"] == "AUTOMATIC1111原版v1.3.0(稳定)":
|
| 23 |
+
subprocess.run(
|
| 24 |
+
f'git clone -b v1.6.0 https://github.com/s4afa451dgf415f/sdwi {params["dir"]}', shell=True)
|
| 25 |
+
else:
|
| 26 |
+
subprocess.run(
|
| 27 |
+
f'git clone -b v1.5.1 https://github.com/s4afa451dgf415f/sdwi {params["dir"]}', shell=True)
|
| 28 |
+
for wi_file in wi_list:
|
| 29 |
+
try:
|
| 30 |
+
os.remove(f"{params['dir']}/{wi_file}")
|
| 31 |
+
except FileNotFoundError:
|
| 32 |
+
print(f"文件 '{wi_file}' 不存在")
|
| 33 |
+
try:
|
| 34 |
+
os.rename(f"{params['dir']}/webui.py", f"{params['dir']}/wi.py")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"文件命名失败: {str(e)}")
|
| 37 |
+
|
| 38 |
+
file_path = f'{params["dir"]}/modules/launch_utils.py'
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
with open(file_path, 'r') as file:
|
| 42 |
+
content = file.read()
|
| 43 |
+
content = content.replace('import webui', 'import wi')
|
| 44 |
+
content = content.replace('webui.webui', 'wi.webui')
|
| 45 |
+
modified_content = content.replace('webui.api_only','wi.api_only')
|
| 46 |
+
file.close()
|
| 47 |
+
|
| 48 |
+
# 将修改后的内容写回到原文件
|
| 49 |
+
with open(file_path, 'w') as file:
|
| 50 |
+
file.write(modified_content)
|
| 51 |
+
file.close()
|
| 52 |
+
|
| 53 |
+
except FileNotFoundError:
|
| 54 |
+
print(f"文件 '{file_path}' 不存在")
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"修改文件 '{file_path}' 时出错: {str(e)}")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
checkpoint_url = {
|
| 60 |
+
"Dark_sushi_mix.safetensors": "https://huggingface.co/mdl-mirror/dark-sushi-mix/resolve/main/darkSushiMixMix_brighter.safetensors",
|
| 61 |
+
"AnythingV5V3_v5PrtRE.safetensors": "https://huggingface.co/ckpt/anything-v5.0/resolve/main/AnythingV5V3_v5PrtRE.safetensors",
|
| 62 |
+
"chilloutmix_NiPrunedFp16Fix.safetensors": "https://huggingface.co/naonovn/chilloutmix_NiPrunedFp32Fix/resolve/main/chilloutmix_NiPrunedFp32Fix.safetensors",
|
| 63 |
+
"rpg_V4.safetensors": "https://huggingface.co/Anashel/rpg/resolve/main/RPG-V4-Model-Download/RPG-v4.safetensors",
|
| 64 |
+
"ProtoGen_X5.8-pruned-fp16.safetensors": "https://huggingface.co/darkstorm2150/Protogen_x5.8_Official_Release/resolve/main/ProtoGen_X5.8-pruned-fp16.safetensors",
|
| 65 |
+
"none": "",
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def run_git_download():
|
| 70 |
+
start_time = time.time()
|
| 71 |
+
# subprocess.run(
|
| 72 |
+
# f'git clone https://github.com/s4afa451dgf415f/sd-wi-aio {params["dir"]}/extensions/prompt-aio',
|
| 73 |
+
# shell=True)
|
| 74 |
+
subprocess.run(
|
| 75 |
+
f'git clone https://github.com/Mikubill/sd-webui-controlnet {params["dir"]}/extensions/sd-wi-controlnet',
|
| 76 |
+
shell=True)
|
| 77 |
+
subprocess.run(
|
| 78 |
+
f'git clone https://github.com/dtlnor/stable-diffusion-webui-localization-zh_CN {params["dir"]}/extensions/stable-diffusion-wi-localization-zh_CN',
|
| 79 |
+
shell=True)
|
| 80 |
+
# subprocess.run(
|
| 81 |
+
# f'git clone https://github.com/fkunn1326/openpose-editor {params["dir"]}/extensions/openpose-editor',
|
| 82 |
+
# shell=True)
|
| 83 |
+
subprocess.run(
|
| 84 |
+
f'git clone https://github.com/s4afa451dgf415f/sd-wi-tagcomplet {params["dir"]}/extensions/a1111-sd-wi-tagcomplete',
|
| 85 |
+
shell=True)
|
| 86 |
+
subprocess.run(
|
| 87 |
+
f'git clone https://github.com/Coyote-A/ultimate-upscale-for-automatic1111 {params["dir"]}/extensions/ultimate-upscale',
|
| 88 |
+
shell=True)
|
| 89 |
+
subprocess.run(
|
| 90 |
+
f'git clone https://github.com/toriato/stable-diffusion-webui-wd14-tagger {params["dir"]}/extensions/stable-diffusion-wi-wd14-tagger',
|
| 91 |
+
shell=True)
|
| 92 |
+
# subprocess.run(
|
| 93 |
+
# f'git clone https://github.com/nonnonstop/sd-webui-3d-open-pose-editor {params["dir"]}/extensions/sd-wi-3d-open-pose-editor',
|
| 94 |
+
# shell=True)
|
| 95 |
+
subprocess.run(
|
| 96 |
+
f'git clone https://github.com/hako-mikan/sd-webui-lora-block-weight {params["dir"]}/extensions/sd-wi-lora-block-weight',
|
| 97 |
+
shell=True)
|
| 98 |
+
end_time = time.time()
|
| 99 |
+
print("已克隆git耗时:", end_time - start_time, "秒")
|
| 100 |
+
def run_aria2c_download():
|
| 101 |
+
start_time = time.time()
|
| 102 |
+
subprocess.run(
|
| 103 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {checkpoint_url[params["model"]]} -d {params["dir"]}/models/Stable-diffusion -o {params["model"]}',
|
| 104 |
+
shell=True)
|
| 105 |
+
subprocess.run(
|
| 106 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11e_sd15_ip2p.safetensors',
|
| 107 |
+
shell=True)
|
| 108 |
+
subprocess.run(
|
| 109 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11f1p_sd15_depth_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11f1p_sd15_depth.safetensors',
|
| 110 |
+
shell=True)
|
| 111 |
+
subprocess.run(
|
| 112 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_canny_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_canny.safetensors',
|
| 113 |
+
shell=True)
|
| 114 |
+
subprocess.run(
|
| 115 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_inpaint_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_inpaint.safetensors',
|
| 116 |
+
shell=True)
|
| 117 |
+
subprocess.run(
|
| 118 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_lineart_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11f1p_sd15_depth.safetensors',
|
| 119 |
+
shell=True)
|
| 120 |
+
subprocess.run(
|
| 121 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_mlsd_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_mlsd.safetensors',
|
| 122 |
+
shell=True)
|
| 123 |
+
subprocess.run(
|
| 124 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_normalbae_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_normalbae.safetensors',
|
| 125 |
+
shell=True)
|
| 126 |
+
subprocess.run(
|
| 127 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_openpose_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_openpose.safetensors',
|
| 128 |
+
shell=True)
|
| 129 |
+
subprocess.run(
|
| 130 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_scribble_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_scribble.safetensors',
|
| 131 |
+
shell=True)
|
| 132 |
+
subprocess.run(
|
| 133 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_seg_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_seg.safetensors',
|
| 134 |
+
shell=True)
|
| 135 |
+
subprocess.run(
|
| 136 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_softedge_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15_softedge.safetensors',
|
| 137 |
+
shell=True)
|
| 138 |
+
subprocess.run(
|
| 139 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15s2_lineart_anime_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11p_sd15s2_lineart_anime.safetensors',
|
| 140 |
+
shell=True)
|
| 141 |
+
subprocess.run(
|
| 142 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11f1e_sd15_tile_fp16.safetensors -d {params["dir"]}/models/ControlNet -o control_v11f1e_sd15_tile_fp16.safetensors',
|
| 143 |
+
shell=True)
|
| 144 |
+
subprocess.run(
|
| 145 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/lokCX/4x-Ultrasharp/resolve/main/4x-UltraSharp.pth -d {params["dir"]}/models/ESRGAN/ -o 4x-UltraSharp.pth',
|
| 146 |
+
shell=True)
|
| 147 |
+
# subprocess.run(
|
| 148 |
+
# f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/datasets/daasd/sd_backup/resolve/main/CN.csv -d {params["dir"]}/extensions/a1111-sd-webui-tagcomplete/tags -o CN.csv',
|
| 149 |
+
# shell=True)
|
| 150 |
+
end_time = time.time()
|
| 151 |
+
print("aria2c完成下载耗时:", end_time - start_time, "秒")
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def curl_download():
|
| 155 |
+
start_time = time.time()
|
| 156 |
+
subprocess.run(
|
| 157 |
+
f"curl -Lo '{params['dir']}/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors' https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors",
|
| 158 |
+
shell=True)
|
| 159 |
+
subprocess.run(
|
| 160 |
+
f"curl -Lo '{params['dir']}/models/VAE/kl-f8-anime2.ckpt' https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/4c4f05104055c029ad577c18ac176462f0d1d7c1/vae/kl-f8-anime2.ckpt",
|
| 161 |
+
shell=True)
|
| 162 |
+
subprocess.run(
|
| 163 |
+
f"curl -Lo '{params['dir']}/models/VAE/animevae.pt' https://huggingface.co/swl-models/animvae/resolve/main/animevae.pt",
|
| 164 |
+
shell=True)
|
| 165 |
+
end_time = time.time()
|
| 166 |
+
print("curl完成下载耗时:", end_time - start_time, "秒")
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def wget_download():
|
| 170 |
+
start_time = time.time()
|
| 171 |
+
subprocess.run("apt install libunwind8-dev -yqq", shell=True)
|
| 172 |
+
os.environ["LD_PRELOAD"] = "libtcmalloc.so.4"
|
| 173 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
| 174 |
+
subprocess.run("sudo apt-get install sox ffmpeg libcairo2 libcairo2-dev", shell=True)
|
| 175 |
+
end_time = time.time()
|
| 176 |
+
print("wget完成下载耗时:", end_time - start_time, "秒")
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def pip_download():
|
| 180 |
+
start_time = time.time()
|
| 181 |
+
# subprocess.run("pip install xformers xformers==0.0.20", shell=True)
|
| 182 |
+
subprocess.run("pip install torch==2.0.1+cu118 torchvision==0.15.2+cu118 torchaudio==2.0.2 torchtext==0.15.2+cpu torchdata==0.6.1 --index-url https://download.pytorch.org/whl/cu118", shell=True)
|
| 183 |
+
end_time = time.time()
|
| 184 |
+
print("pip完成下载耗时:", end_time - start_time, "秒")
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
|
| 188 |
+
task1 = executor.submit(run_git_download)
|
| 189 |
+
task2 = executor.submit(run_aria2c_download)
|
| 190 |
+
task3 = executor.submit(curl_download)
|
| 191 |
+
task4 = executor.submit(wget_download)
|
| 192 |
+
task5 = executor.submit(pip_download)
|
| 193 |
+
concurrent.futures.wait([ task1,task3, task4, task5])
|
| 194 |
+
|
| 195 |
+
if os.path.exists(f'{params["dir"]}/embeddings'):
|
| 196 |
+
shutil.rmtree(f'{params["dir"]}/embeddings')
|
| 197 |
+
subprocess.run(f'git clone https://huggingface.co/nolanaatama/embeddings {params["dir"]}/embeddings', shell=True)
|
| 198 |
+
|
| 199 |
+
# 个人插件从云盘的extensions文件夹与VAE文件夹加载
|
| 200 |
+
if params["extensions"] == "True":
|
| 201 |
+
if os.path.exists("/content/drive/MyDrive/extensions"):
|
| 202 |
+
subprocess.run(f'rsync -a /content/drive/MyDrive/extensions/* {params["dir"]}/extensions', shell=True)
|
| 203 |
+
print('已加载云盘里的插件')
|
| 204 |
+
if os.path.exists("/content/drive/MyDrive/VAE"):
|
| 205 |
+
subprocess.run(f'rsync -a /content/drive/MyDrive/VAE/* {params["dir"]}/models/VAE', shell=True)
|
| 206 |
+
print('已加载云盘里的VAE')
|
| 207 |
+
if os.path.exists("/content/drive/MyDrive/embeddings"):
|
| 208 |
+
subprocess.run(f'rsync -a /content/drive/MyDrive/embeddings/* {params["dir"]}/embeddings', shell=True)
|
| 209 |
+
print('已加载云盘里的embeddings')
|
| 210 |
+
if os.path.exists("/content/drive/MyDrive/lora"):
|
| 211 |
+
subprocess.run(f'mkdir -p {params["dir"]}/models/Lora', shell=True)
|
| 212 |
+
subprocess.run(f'rsync -a /content/drive/MyDrive/lora/* {params["dir"]}/models/Lora', shell=True)
|
| 213 |
+
print('已加载云盘里的lora')
|
| 214 |
+
if os.path.exists("/content/drive/MyDrive/checkpoint"):
|
| 215 |
+
subprocess.run(f'rsync -a /content/drive/MyDrive/checkpoint/* {params["dir"]}/models/Stable-diffusion',
|
| 216 |
+
shell=True)
|
| 217 |
+
print('已加载云盘里的Stable-diffusion')
|
main22.py
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import re
|
| 3 |
+
import subprocess
|
| 4 |
+
import shutil
|
| 5 |
+
import os
|
| 6 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
params = {}
|
| 12 |
+
for arg in sys.argv[1:]:
|
| 13 |
+
if arg.startswith('--'):
|
| 14 |
+
key_value = arg[len('--'):].split('=')
|
| 15 |
+
if len(key_value) == 2:
|
| 16 |
+
key, value = key_value
|
| 17 |
+
params[key] = value
|
| 18 |
+
|
| 19 |
+
check_dir = f"{params['dir']}/models/Stable-diffusion"
|
| 20 |
+
lora_dir = f"{params['dir']}/models/Lora"
|
| 21 |
+
def swap(arr, l, r):
|
| 22 |
+
temp = arr[l]
|
| 23 |
+
arr[l] = arr[r]
|
| 24 |
+
arr[r] = temp
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def checkRelateMod(oldMod, newMod):
|
| 28 |
+
try:
|
| 29 |
+
return oldMod and newMod and oldMod['downloadLink'] == newMod['downloadLink']
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(e)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def sanitize_filename(filename):
|
| 35 |
+
# 定义不兼容字符的正则表达式模式
|
| 36 |
+
pattern = r'[\\/:*?"<>|]'
|
| 37 |
+
# 使用下划线替换不兼容字符
|
| 38 |
+
sanitized_filename = re.sub(pattern, '_', filename)
|
| 39 |
+
return sanitized_filename
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def compatibility(preContent):
|
| 43 |
+
for item in preContent:
|
| 44 |
+
if bool(item['path']):
|
| 45 |
+
item['path'] = re.sub(r'^(/[^/]+/[^/]+)', params['dir'], item['path'])
|
| 46 |
+
|
| 47 |
+
def dealRelateMod(oldRenameMod, newRenameMod):
|
| 48 |
+
old_file_path = f"{oldRenameMod['path']}/{oldRenameMod['name']}"
|
| 49 |
+
# 新模型的定义不带后缀名,所以需要加上处理,且在预处理情况下不知道路径那么只能沿用老模型的路径和名字
|
| 50 |
+
new_file_dir = (
|
| 51 |
+
newRenameMod['path']
|
| 52 |
+
if newRenameMod.get('path') and newRenameMod['path'] != ""
|
| 53 |
+
else oldRenameMod['path']
|
| 54 |
+
)
|
| 55 |
+
new_file_name = (
|
| 56 |
+
sanitize_filename(newRenameMod['name']) + os.path.splitext(oldRenameMod['name'])[1]
|
| 57 |
+
if newRenameMod['name'] and newRenameMod['name'] != "未命名"
|
| 58 |
+
else oldRenameMod['name']
|
| 59 |
+
)
|
| 60 |
+
new_file_path = f"{new_file_dir}/{new_file_name}"
|
| 61 |
+
if old_file_path != new_file_path:
|
| 62 |
+
try:
|
| 63 |
+
if os.path.exists(old_file_path):
|
| 64 |
+
os.rename(old_file_path, new_file_path)
|
| 65 |
+
oldRenameMod['path'] = new_file_dir
|
| 66 |
+
oldRenameMod['name'] = new_file_name
|
| 67 |
+
print(f"{old_file_path}更改为{new_file_path}成功")
|
| 68 |
+
else:
|
| 69 |
+
print(f"{old_file_path}不存在,重命名失败")
|
| 70 |
+
except OSError as e:
|
| 71 |
+
print(f"更改{old_file_path}时发生错误: {e}")
|
| 72 |
+
else:
|
| 73 |
+
print(f"{newRenameMod['name']}已存在且无任何改变,将忽视")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def deleteMod(oldDeMod):
|
| 77 |
+
file_path = f"{oldDeMod['path']}/{oldDeMod['name']}"
|
| 78 |
+
try:
|
| 79 |
+
if os.path.exists(file_path):
|
| 80 |
+
os.remove(file_path)
|
| 81 |
+
# oldCo的行也要进行同步删除
|
| 82 |
+
print(f"{oldDeMod['name']}删除成功")
|
| 83 |
+
else:
|
| 84 |
+
print(f"{oldDeMod['name']}不存在,删除失败")
|
| 85 |
+
except OSError as e:
|
| 86 |
+
print(f"删除{oldDeMod['name']}时发生错误: {e}")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def LCS(oldCo, newCo):
|
| 90 |
+
oldStartIdx = 0 # 旧前指针
|
| 91 |
+
newStartIdx = 0 # 新前指针
|
| 92 |
+
oldEndIdx = len(oldCo) - 1 # 旧后指针
|
| 93 |
+
newEndIdx = len(newCo) - 1 # 新后指针
|
| 94 |
+
oldStartMod = oldCo[oldStartIdx] # 旧前Mod
|
| 95 |
+
oldEndMod = oldCo[oldEndIdx] # 旧后Mod
|
| 96 |
+
newStartMod = newCo[newStartIdx] # 新前Mod
|
| 97 |
+
newEndMod = newCo[newEndIdx] # 新后Mod
|
| 98 |
+
linkMap = {}
|
| 99 |
+
while oldStartIdx <= oldEndIdx and newStartIdx <= newEndIdx:
|
| 100 |
+
# 新前旧前
|
| 101 |
+
if checkRelateMod(oldStartMod, newStartMod):
|
| 102 |
+
dealRelateMod(oldStartMod, newStartMod)
|
| 103 |
+
oldStartIdx += 1
|
| 104 |
+
newStartIdx += 1
|
| 105 |
+
if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
|
| 106 |
+
break
|
| 107 |
+
oldStartMod = oldCo[oldStartIdx]
|
| 108 |
+
newStartMod = newCo[newStartIdx]
|
| 109 |
+
# 新后旧后
|
| 110 |
+
elif checkRelateMod(oldEndMod, newEndMod):
|
| 111 |
+
dealRelateMod(oldEndMod, newEndMod)
|
| 112 |
+
oldEndIdx -= 1
|
| 113 |
+
newEndIdx -= 1
|
| 114 |
+
if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
|
| 115 |
+
break
|
| 116 |
+
oldEndMod = oldCo[oldEndIdx]
|
| 117 |
+
newEndMod = newCo[newEndIdx]
|
| 118 |
+
# 新后旧前
|
| 119 |
+
elif checkRelateMod(oldStartMod, newEndMod):
|
| 120 |
+
dealRelateMod(oldStartMod, newEndMod)
|
| 121 |
+
oldStartIdx += 1
|
| 122 |
+
newEndIdx -= 1
|
| 123 |
+
if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
|
| 124 |
+
break
|
| 125 |
+
oldStartMod = oldCo[oldStartIdx]
|
| 126 |
+
newEndMod = newCo[newEndIdx]
|
| 127 |
+
# 新前旧后
|
| 128 |
+
elif checkRelateMod(oldEndMod, newStartMod):
|
| 129 |
+
dealRelateMod(oldEndMod, newStartMod)
|
| 130 |
+
oldEndIdx -= 1
|
| 131 |
+
newStartIdx += 1
|
| 132 |
+
if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
|
| 133 |
+
break
|
| 134 |
+
oldEndMod = oldCo[oldEndIdx]
|
| 135 |
+
newStartMod = newCo[newStartIdx]
|
| 136 |
+
# 四种均未找到
|
| 137 |
+
else:
|
| 138 |
+
# if not linkMap:
|
| 139 |
+
linkMap = {}
|
| 140 |
+
# 从 oldStartIdx 开始,到oldEndIdx结束,创建linkMap映射对象
|
| 141 |
+
for i in range(oldStartIdx, oldEndIdx + 1):
|
| 142 |
+
downloadLink = oldCo[i]['downloadLink']
|
| 143 |
+
if downloadLink is not None:
|
| 144 |
+
linkMap[downloadLink] = i
|
| 145 |
+
idxInOld = linkMap.get(newStartMod['downloadLink'], None)
|
| 146 |
+
if idxInOld is None:
|
| 147 |
+
print(f"{newStartMod['name']}不存在于旧mod,{newStartMod['downloadLink']}将添加到下载任务队列")
|
| 148 |
+
content.append(newStartMod)
|
| 149 |
+
|
| 150 |
+
else:
|
| 151 |
+
dealRelateMod(oldCo[idxInOld], newStartMod)
|
| 152 |
+
swap(oldCo, idxInOld, oldEndIdx)
|
| 153 |
+
oldEndIdx -= 1
|
| 154 |
+
# 指针下移,移动新的头
|
| 155 |
+
newStartIdx += 1
|
| 156 |
+
if newStartIdx > newEndIdx:
|
| 157 |
+
break
|
| 158 |
+
newStartMod = newCo[newStartIdx]
|
| 159 |
+
oldEndMod = oldCo[oldEndIdx]
|
| 160 |
+
# oldStartIdx += 1
|
| 161 |
+
print('new', newStartIdx, newEndIdx)
|
| 162 |
+
print('old', oldStartIdx, oldEndIdx)
|
| 163 |
+
# new这里还有剩余Mod没有处理
|
| 164 |
+
if newStartIdx <= newEndIdx:
|
| 165 |
+
for i in range(newStartIdx, newEndIdx + 1):
|
| 166 |
+
print(f"{newCo[i]['name']}为新且不重复mod,将添加到下载任务队列")
|
| 167 |
+
content.append(newCo[i])
|
| 168 |
+
|
| 169 |
+
elif oldStartIdx <= oldEndIdx:
|
| 170 |
+
i = oldStartIdx
|
| 171 |
+
while i <= oldEndIdx:
|
| 172 |
+
if i < len(oldCo):
|
| 173 |
+
print(f"{oldCo[i]['name']}为旧不重复mod,将删除")
|
| 174 |
+
deleteMod(oldCo[i])
|
| 175 |
+
del oldCo[i]
|
| 176 |
+
oldEndIdx -= 1
|
| 177 |
+
else:
|
| 178 |
+
break
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
# LCS(oldArr,oldArr)
|
| 182 |
+
with open(params["json_dir"], 'r') as modelFile:
|
| 183 |
+
preContent = json.loads(modelFile.read())
|
| 184 |
+
compatibility(preContent)
|
| 185 |
+
|
| 186 |
+
oldCo = json.loads(os.environ["oldCo"])
|
| 187 |
+
|
| 188 |
+
# 更新阶段
|
| 189 |
+
if 'oldCo' and len(oldCo) > 0:
|
| 190 |
+
content = []
|
| 191 |
+
print(oldCo)
|
| 192 |
+
print(preContent)
|
| 193 |
+
LCS(oldCo, preContent)
|
| 194 |
+
print(content)
|
| 195 |
+
# 初始化阶段
|
| 196 |
+
else:
|
| 197 |
+
oldCo = []
|
| 198 |
+
content = preContent
|
| 199 |
+
|
| 200 |
+
# 部分下载的情况
|
| 201 |
+
selected_mods = [x.strip() for x in params["name"].split('与') if x.strip()]
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def get_file_size(file_path):
|
| 205 |
+
try:
|
| 206 |
+
file_size_bytes = os.path.getsize(file_path)
|
| 207 |
+
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
| 208 |
+
if file_size_bytes < 1024.0:
|
| 209 |
+
break
|
| 210 |
+
file_size_bytes /= 1024.0
|
| 211 |
+
return f"{file_size_bytes:.2f} {unit}"
|
| 212 |
+
except OSError as e:
|
| 213 |
+
print(f"Error: {e}")
|
| 214 |
+
return None
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def get_civitai_file(str):
|
| 218 |
+
match = re.search(r'[^/]+$', str.decode())
|
| 219 |
+
if match:
|
| 220 |
+
return match.group(0).split('\n')[0]
|
| 221 |
+
else:
|
| 222 |
+
print("无法从输出中提取文件名")
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def move_model(source_path, target_path):
|
| 226 |
+
try:
|
| 227 |
+
shutil.move(source_path, target_path)
|
| 228 |
+
except OSError as e:
|
| 229 |
+
print(f"Error: {e}")
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def download_file(item):
|
| 233 |
+
# for item in content:
|
| 234 |
+
if selected_mods and item['name'] not in selected_mods:
|
| 235 |
+
return
|
| 236 |
+
download_url = item['downloadLink']
|
| 237 |
+
# huggingface
|
| 238 |
+
match = re.search(r'/([^/]*)$', download_url)
|
| 239 |
+
file_name = match.group(1)
|
| 240 |
+
# 开始下载
|
| 241 |
+
cmd = f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {lora_dir}" if '.' not in match.group(
|
| 242 |
+
1) else f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {lora_dir} -o {file_name}"
|
| 243 |
+
result = subprocess.run(cmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
| 244 |
+
if result.returncode == 0:
|
| 245 |
+
# 判断是civitai还是hugging face还是自定义
|
| 246 |
+
# civitai
|
| 247 |
+
if '.' not in file_name:
|
| 248 |
+
file_name = get_civitai_file(result.stdout)
|
| 249 |
+
# 如果定义了名字则重命名
|
| 250 |
+
if item['name'] != '未命名':
|
| 251 |
+
# 需要进行文件名的兼容处理
|
| 252 |
+
temp_name = sanitize_filename(item['name']) + os.path.splitext(file_name)[1]
|
| 253 |
+
os.rename(f"{lora_dir}/{file_name}", f"{lora_dir}/{temp_name}")
|
| 254 |
+
final_name = temp_name
|
| 255 |
+
else:
|
| 256 |
+
final_name = file_name
|
| 257 |
+
source_path = f"{lora_dir}/{final_name}"
|
| 258 |
+
file_size = get_file_size(source_path)
|
| 259 |
+
# 如果path定义了则进行移动
|
| 260 |
+
if item.get('path') and item['path'] != "":
|
| 261 |
+
target_path = item['path']
|
| 262 |
+
move_model(source_path, target_path)
|
| 263 |
+
item['path'] = target_path
|
| 264 |
+
print(file_name + '已下载,重命名为:' + final_name)
|
| 265 |
+
print('移动--', final_name, f'到{target_path}')
|
| 266 |
+
# 如果为checkpoint则进行移动
|
| 267 |
+
elif file_size and 'GB' in file_size:
|
| 268 |
+
move_model(source_path, check_dir)
|
| 269 |
+
item['path'] = check_dir
|
| 270 |
+
print(file_name + '已下载,重命名为:' + final_name)
|
| 271 |
+
print('移动checkpoint--', final_name, f'到{check_dir}文件夹')
|
| 272 |
+
# 如果为lora则直接调用source_path
|
| 273 |
+
else:
|
| 274 |
+
item['path'] = lora_dir
|
| 275 |
+
print(file_name + '已下载,重命名为:' + final_name)
|
| 276 |
+
# 改变item['name']用于最小化更新的重命名判断
|
| 277 |
+
item['name'] = final_name
|
| 278 |
+
oldCo.append(item)
|
| 279 |
+
else:
|
| 280 |
+
print(f"{item['name']}下载失败,请检查{item['downloadLink']}")
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
# download_file(item)
|
| 284 |
+
# 使用线程池进行下载
|
| 285 |
+
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 286 |
+
for item in content:
|
| 287 |
+
executor.submit(download_file, item)
|
main3.py
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
import subprocess
|
| 5 |
+
import sys
|
| 6 |
+
wget_path = '/usr/bin/wget'
|
| 7 |
+
|
| 8 |
+
params = {}
|
| 9 |
+
for arg in sys.argv[1:]:
|
| 10 |
+
if arg.startswith('--'):
|
| 11 |
+
key_value = arg[len('--'):].split('=')
|
| 12 |
+
if len(key_value) == 2:
|
| 13 |
+
key, value = key_value
|
| 14 |
+
params[key] = value
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def find_model_json(root_path, mod_json_name):
|
| 18 |
+
for file_name in os.listdir(root_path):
|
| 19 |
+
file_path = os.path.join(root_path, file_name)
|
| 20 |
+
if os.path.isdir(file_path):
|
| 21 |
+
res = find_model_json(file_path, mod_json_name)
|
| 22 |
+
if res is not None:
|
| 23 |
+
return res
|
| 24 |
+
elif file_name == mod_json_name:
|
| 25 |
+
print("find model.json文件:", file_path)
|
| 26 |
+
return file_path
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# 自动查找config.json文件
|
| 30 |
+
def find_config_json(root_path, config_json_name):
|
| 31 |
+
for file_name in os.listdir(root_path):
|
| 32 |
+
file_path = os.path.join(root_path, file_name)
|
| 33 |
+
if os.path.isdir(file_path):
|
| 34 |
+
res = find_model_json(file_path, config_json_name)
|
| 35 |
+
if res is not None:
|
| 36 |
+
return res
|
| 37 |
+
elif file_name == config_json_name:
|
| 38 |
+
print("find config.json文件:", file_path)
|
| 39 |
+
return file_path
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def rec_config_json():
|
| 43 |
+
json_content = {
|
| 44 |
+
"samples_save": True,
|
| 45 |
+
"samples_format": "png",
|
| 46 |
+
"samples_filename_pattern": "",
|
| 47 |
+
"save_images_add_number": True,
|
| 48 |
+
"grid_save": True,
|
| 49 |
+
"grid_format": "png",
|
| 50 |
+
"grid_extended_filename": False,
|
| 51 |
+
"grid_only_if_multiple": True,
|
| 52 |
+
"grid_prevent_empty_spots": False,
|
| 53 |
+
"n_rows": -1,
|
| 54 |
+
"enable_pnginfo": True,
|
| 55 |
+
"save_txt": False,
|
| 56 |
+
"save_images_before_face_restoration": False,
|
| 57 |
+
"save_images_before_highres_fix": False,
|
| 58 |
+
"save_images_before_color_correction": False,
|
| 59 |
+
"jpeg_quality": 80,
|
| 60 |
+
"export_for_4chan": True,
|
| 61 |
+
"use_original_name_batch": True,
|
| 62 |
+
"use_upscaler_name_as_suffix": False,
|
| 63 |
+
"save_selected_only": True,
|
| 64 |
+
"do_not_add_watermark": False,
|
| 65 |
+
"temp_dir": "",
|
| 66 |
+
"clean_temp_dir_at_start": False,
|
| 67 |
+
"outdir_samples": "",
|
| 68 |
+
"outdir_txt2img_samples": "outputs/txt2img-images",
|
| 69 |
+
"outdir_img2img_samples": "outputs/img2img-images",
|
| 70 |
+
"outdir_extras_samples": "outputs/extras-images",
|
| 71 |
+
"outdir_grids": "",
|
| 72 |
+
"outdir_txt2img_grids": "outputs/txt2img-grids",
|
| 73 |
+
"outdir_img2img_grids": "outputs/img2img-grids",
|
| 74 |
+
"outdir_save": "log/images",
|
| 75 |
+
"save_to_dirs": False,
|
| 76 |
+
"grid_save_to_dirs": False,
|
| 77 |
+
"use_save_to_dirs_for_ui": False,
|
| 78 |
+
"directories_filename_pattern": "[date]",
|
| 79 |
+
"directories_max_prompt_words": 8,
|
| 80 |
+
"ESRGAN_tile": 192,
|
| 81 |
+
"ESRGAN_tile_overlap": 8,
|
| 82 |
+
"realesrgan_enabled_models": [
|
| 83 |
+
"R-ESRGAN 4x+",
|
| 84 |
+
"R-ESRGAN 4x+ Anime6B"
|
| 85 |
+
],
|
| 86 |
+
"upscaler_for_img2img": None,
|
| 87 |
+
"face_restoration_model": None,
|
| 88 |
+
"code_former_weight": 0.5,
|
| 89 |
+
"face_restoration_unload": False,
|
| 90 |
+
"show_warnings": False,
|
| 91 |
+
"memmon_poll_rate": 8,
|
| 92 |
+
"samples_log_stdout": False,
|
| 93 |
+
"multiple_tqdm": True,
|
| 94 |
+
"print_hypernet_extra": False,
|
| 95 |
+
"unload_models_when_training": True,
|
| 96 |
+
"pin_memory": False,
|
| 97 |
+
"save_optimizer_state": False,
|
| 98 |
+
"save_training_settings_to_txt": True,
|
| 99 |
+
"dataset_filename_word_regex": "",
|
| 100 |
+
"dataset_filename_join_string": " ",
|
| 101 |
+
"training_image_repeats_per_epoch": 1,
|
| 102 |
+
"training_write_csv_every": 500,
|
| 103 |
+
"training_xattention_optimizations": False,
|
| 104 |
+
"training_enable_tensorboard": False,
|
| 105 |
+
"training_tensorboard_save_images": False,
|
| 106 |
+
"training_tensorboard_flush_every": 120,
|
| 107 |
+
"sd_model_checkpoint": "",
|
| 108 |
+
"sd_checkpoint_cache": 0,
|
| 109 |
+
"sd_vae_checkpoint_cache": 0,
|
| 110 |
+
"sd_vae": "Automatic",
|
| 111 |
+
"sd_vae_as_default": False,
|
| 112 |
+
"inpainting_mask_weight": 1.0,
|
| 113 |
+
"initial_noise_multiplier": 1.0,
|
| 114 |
+
"img2img_color_correction": False,
|
| 115 |
+
"img2img_fix_steps": False,
|
| 116 |
+
"img2img_background_color": "#ffffff",
|
| 117 |
+
"enable_quantization": False,
|
| 118 |
+
"enable_emphasis": True,
|
| 119 |
+
"enable_batch_seeds": True,
|
| 120 |
+
"comma_padding_backtrack": 20,
|
| 121 |
+
"CLIP_stop_at_last_layers": 2,
|
| 122 |
+
"upcast_attn": False,
|
| 123 |
+
"use_old_emphasis_implementation": False,
|
| 124 |
+
"use_old_karras_scheduler_sigmas": False,
|
| 125 |
+
"use_old_hires_fix_width_height": False,
|
| 126 |
+
"interrogate_keep_models_in_memory": False,
|
| 127 |
+
"interrogate_return_ranks": False,
|
| 128 |
+
"interrogate_clip_num_beams": 1,
|
| 129 |
+
"interrogate_clip_min_length": 24,
|
| 130 |
+
"interrogate_clip_max_length": 48,
|
| 131 |
+
"interrogate_clip_dict_limit": 1500,
|
| 132 |
+
"interrogate_clip_skip_categories": [],
|
| 133 |
+
"interrogate_deepbooru_score_threshold": 0.7,
|
| 134 |
+
"deepbooru_sort_alpha": False,
|
| 135 |
+
"deepbooru_use_spaces": False,
|
| 136 |
+
"deepbooru_escape": True,
|
| 137 |
+
"deepbooru_filter_tags": "",
|
| 138 |
+
"extra_networks_default_view": "thumbs",
|
| 139 |
+
"extra_networks_default_multiplier": 1.0,
|
| 140 |
+
"sd_hypernetwork": "None",
|
| 141 |
+
"return_grid": False,
|
| 142 |
+
"do_not_show_images": False,
|
| 143 |
+
"add_model_hash_to_info": True,
|
| 144 |
+
"add_model_name_to_info": True,
|
| 145 |
+
"disable_weights_auto_swap": True,
|
| 146 |
+
"send_seed": True,
|
| 147 |
+
"send_size": True,
|
| 148 |
+
"font": "",
|
| 149 |
+
"js_modal_lightbox": True,
|
| 150 |
+
"js_modal_lightbox_initially_zoomed": True,
|
| 151 |
+
"show_progress_in_title": True,
|
| 152 |
+
"samplers_in_dropdown": False,
|
| 153 |
+
"dimensions_and_batch_together": True,
|
| 154 |
+
"keyedit_precision_attention": 0.1,
|
| 155 |
+
"keyedit_precision_extra": 0.05,
|
| 156 |
+
"quicksettings": "sd_model_checkpoint, sd_vae, CLIP_stop_at_last_layers, use_old_karras_scheduler_sigmas, always_discard_next_to_last_sigma",
|
| 157 |
+
"ui_reorder": "inpaint, sampler, checkboxes, hires_fix, dimensions, cfg, seed, batch, override_settings, scripts",
|
| 158 |
+
"ui_extra_networks_tab_reorder": "",
|
| 159 |
+
"localization": "zh_CN",
|
| 160 |
+
"show_progressbar": True,
|
| 161 |
+
"live_previews_enable": True,
|
| 162 |
+
"show_progress_grid": True,
|
| 163 |
+
"show_progress_every_n_steps": 20,
|
| 164 |
+
"show_progress_type": "Approx NN",
|
| 165 |
+
"live_preview_content": "Prompt",
|
| 166 |
+
"live_preview_refresh_period": 1000,
|
| 167 |
+
"hide_samplers": [],
|
| 168 |
+
"eta_ddim": 0.0,
|
| 169 |
+
"eta_ancestral": 1.0,
|
| 170 |
+
"ddim_discretize": "uniform",
|
| 171 |
+
"s_churn": 0.0,
|
| 172 |
+
"s_tmin": 0.0,
|
| 173 |
+
"s_noise": 1.0,
|
| 174 |
+
"eta_noise_seed_delta": 31337,
|
| 175 |
+
"always_discard_next_to_last_sigma": False,
|
| 176 |
+
"postprocessing_enable_in_main_ui": [],
|
| 177 |
+
"postprocessing_operation_order": [],
|
| 178 |
+
"upscaling_max_images_in_cache": 5,
|
| 179 |
+
"disabled_extensions": [],
|
| 180 |
+
"sd_checkpoint_hash": "7af57400eb7303877ec35e5b9e03fc29802c44066828165dc3a20b973c439428",
|
| 181 |
+
"ldsr_steps": 100,
|
| 182 |
+
"ldsr_cached": False,
|
| 183 |
+
"SWIN_tile": 192,
|
| 184 |
+
"SWIN_tile_overlap": 8,
|
| 185 |
+
"sd_lora": "None",
|
| 186 |
+
"lora_apply_to_outputs": False,
|
| 187 |
+
"tac_tagFile": "danbooru.csv",
|
| 188 |
+
"tac_active": True,
|
| 189 |
+
"tac_activeIn.txt2img": True,
|
| 190 |
+
"tac_activeIn.img2img": True,
|
| 191 |
+
"tac_activeIn.negativePrompts": True,
|
| 192 |
+
"tac_activeIn.thirdParty": True,
|
| 193 |
+
"tac_activeIn.modelList": "",
|
| 194 |
+
"tac_activeIn.modelListMode": "Blacklist",
|
| 195 |
+
"tac_maxResults": 15.0,
|
| 196 |
+
"tac_showAllResults": False,
|
| 197 |
+
"tac_resultStepLength": 100.0,
|
| 198 |
+
"tac_delayTime": 100.0,
|
| 199 |
+
"tac_useWildcards": True,
|
| 200 |
+
"tac_useEmbeddings": True,
|
| 201 |
+
"tac_useHypernetworks": True,
|
| 202 |
+
"tac_useLoras": True,
|
| 203 |
+
"tac_showWikiLinks": False,
|
| 204 |
+
"tac_replaceUnderscores": True,
|
| 205 |
+
"tac_escapeParentheses": True,
|
| 206 |
+
"tac_appendComma": True,
|
| 207 |
+
"tac_alias.searchByAlias": True,
|
| 208 |
+
"tac_alias.onlyShowAlias": False,
|
| 209 |
+
"tac_translation.translationFile": "CN.csv",
|
| 210 |
+
"tac_translation.oldFormat": True,
|
| 211 |
+
"tac_translation.searchByTranslation": True,
|
| 212 |
+
"tac_extra.extraFile": "extra-quality-tags.csv",
|
| 213 |
+
"tac_extra.addMode": "Insert before",
|
| 214 |
+
"additional_networks_extra_lora_path": "",
|
| 215 |
+
"additional_networks_sort_models_by": "name",
|
| 216 |
+
"additional_networks_reverse_sort_order": False,
|
| 217 |
+
"additional_networks_model_name_filter": "",
|
| 218 |
+
"additional_networks_xy_grid_model_metadata": "",
|
| 219 |
+
"additional_networks_hash_thread_count": 1.0,
|
| 220 |
+
"additional_networks_back_up_model_when_saving": True,
|
| 221 |
+
"additional_networks_show_only_safetensors": False,
|
| 222 |
+
"additional_networks_show_only_models_with_metadata": "disabled",
|
| 223 |
+
"additional_networks_max_top_tags": 20.0,
|
| 224 |
+
"additional_networks_max_dataset_folders": 20.0,
|
| 225 |
+
"images_history_preload": False,
|
| 226 |
+
"images_record_paths": True,
|
| 227 |
+
"images_delete_message": True,
|
| 228 |
+
"images_history_page_columns": 6.0,
|
| 229 |
+
"images_history_page_rows": 6.0,
|
| 230 |
+
"images_history_pages_perload": 20.0,
|
| 231 |
+
"img_downscale_threshold": 4.0,
|
| 232 |
+
"target_side_length": 4000.0,
|
| 233 |
+
"no_dpmpp_sde_batch_determinism": False,
|
| 234 |
+
"control_net_max_models_num": 3,
|
| 235 |
+
}
|
| 236 |
+
if params["image"] == "True":
|
| 237 |
+
json_content["outdir_txt2img_samples"] = "/content/drive/MyDrive/sd-generate/t2i"
|
| 238 |
+
json_content["outdir_img2img_samples"] = "/content/drive/MyDrive/sd-generate/i2i"
|
| 239 |
+
json_content["outdir_extras_samples"] = "/content/drive/MyDrive/sd-generate/extras"
|
| 240 |
+
json_content["outdir_txt2img_grids"] = "/content/drive/MyDrive/sd-generate/t2i-grids"
|
| 241 |
+
json_content["outdir_img2img_grids"] = "/content/drive/MyDrive/sd-generate/i2i-grids"
|
| 242 |
+
with open(f'{params["dir"]}/config.json', 'w') as configFile:
|
| 243 |
+
json.dump(json_content, configFile, ensure_ascii=False, indent=4)
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
if params["config"]== "False":
|
| 247 |
+
rec_config_json()
|
| 248 |
+
else:
|
| 249 |
+
res_config_json = find_config_json('/content/drive/', 'config.json')
|
| 250 |
+
print(res_config_json)
|
| 251 |
+
subprocess.run(f'cp {res_config_json} {params["dir"]}/config.json', shell=True)
|
| 252 |
+
|
| 253 |
+
# 图片自动下载脚本
|
| 254 |
+
if params["download"]== "True":
|
| 255 |
+
subprocess.run(
|
| 256 |
+
f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/datasets/daasd/sd_backup/resolve/main/png_auto_download.js -d {params["dir"]}/javascript -o pad.js',
|
| 257 |
+
shell=True)
|
| 258 |
+
else:
|
| 259 |
+
if os.path.exists(f'{params["dir"]}/javascript/png_auto_downloadjs'):
|
| 260 |
+
os.remove(f'{params["dir"]}/javascript/png_auto_download.js')
|
| 261 |
+
|
| 262 |
+
# anapnoe版本
|
| 263 |
+
# if params["ui"] == "anapnoe手机端完美适配":
|
| 264 |
+
# # png_info脚本
|
| 265 |
+
# subprocess.run([wget_path, '-O', f'{params["dir"]}/javascript/PNG_info_web.js',
|
| 266 |
+
# 'https://github.com/s4afa451dgf415f/colab_stable_diffusion/raw/main/PNG_info_web.js'],
|
| 267 |
+
# check=True)
|
| 268 |
+
# with open(f'{params["dir"]}/modules/ui.py', 'r') as readFile:
|
| 269 |
+
# content = readFile.read()
|
| 270 |
+
# content = content.replace(''' for tabname, button in buttons.items():
|
| 271 |
+
# parameters_copypaste.register_paste_params_button(parameters_copypaste.ParamBinding(
|
| 272 |
+
# paste_button=button, tabname=tabname, source_text_component=generation_info, source_image_component=image,
|
| 273 |
+
# ))
|
| 274 |
+
#
|
| 275 |
+
# image.change(
|
| 276 |
+
# fn=wrap_gradio_call(modules.extras.run_pnginfo),
|
| 277 |
+
# inputs=[image],
|
| 278 |
+
# outputs=[html, generation_info, html2],
|
| 279 |
+
# )''', '')
|
| 280 |
+
#
|
| 281 |
+
# with open(f'{params["dir"]}/modules/ui.py', 'w') as writeFile:
|
| 282 |
+
# writeFile.write(content)
|
| 283 |
+
#
|
| 284 |
+
# # png_info脚本兼容
|
| 285 |
+
# with open(f'{params["dir"]}/javascript/PNG_info_web.js', 'r') as readFile:
|
| 286 |
+
# content = readFile.read()
|
| 287 |
+
# content = content.replace('''querySelector("#tab_pnginfo > div > div > div:nth-child(2) > div:nth-child(3)")''',
|
| 288 |
+
# 'querySelector("#tab_pnginfo > div > div>div>div:nth-child(4)")')
|
| 289 |
+
# with open(f'{params["dir"]}/javascript/PNG_info_web.js', 'w') as writeFile:
|
| 290 |
+
# writeFile.write(content)
|
| 291 |
+
|
| 292 |
+
# automatic111版本
|
| 293 |
+
# else:
|
| 294 |
+
# 手机平板
|
| 295 |
+
css_content = '''
|
| 296 |
+
@media screen and (max-width: 600px) {
|
| 297 |
+
.gradio-slider input[type="range"]{
|
| 298 |
+
display: none;
|
| 299 |
+
}
|
| 300 |
+
.gradio-slider input[type="number"]{
|
| 301 |
+
width: 18em;
|
| 302 |
+
}
|
| 303 |
+
}
|
| 304 |
+
'''
|
| 305 |
+
with open(f'{params["dir"]}/style.css', 'a') as cssFile:
|
| 306 |
+
cssFile.write(css_content)
|
| 307 |
+
# png_info脚本
|
| 308 |
+
subprocess.run([wget_path, '-O', f'{params["dir"]}/javascript/piw.js',
|
| 309 |
+
'https://huggingface.co/datasets/daasd/sd_backup/resolve/main/PNG_info_web.js'],
|
| 310 |
+
check=True)
|
| 311 |
+
# # 拦截png_info
|
| 312 |
+
with open(f'{params["dir"]}/modules/ui.py', 'r') as readFile:
|
| 313 |
+
content = readFile.read()
|
| 314 |
+
content = content.replace(''' for tabname, button in buttons.items():
|
| 315 |
+
parameters_copypaste.register_paste_params_button(parameters_copypaste.ParamBinding(
|
| 316 |
+
paste_button=button, tabname=tabname, source_text_component=generation_info, source_image_component=image,
|
| 317 |
+
))
|
| 318 |
+
|
| 319 |
+
image.change(
|
| 320 |
+
fn=wrap_gradio_call(modules.extras.run_pnginfo),
|
| 321 |
+
inputs=[image],
|
| 322 |
+
outputs=[html, generation_info, html2],
|
| 323 |
+
)''', '')
|
| 324 |
+
with open(f'{params["dir"]}/modules/ui.py', 'w') as writeFile:
|
| 325 |
+
writeFile.write(content)
|
main4.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
params = {}
|
| 6 |
+
params2={}
|
| 7 |
+
paramArr=[]
|
| 8 |
+
for arg in sys.argv[1:]:
|
| 9 |
+
if arg.startswith('--'):
|
| 10 |
+
key_value = arg[len('--'):].split('=')
|
| 11 |
+
if len(key_value) == 2:
|
| 12 |
+
key, value = key_value
|
| 13 |
+
params[key] = value
|
| 14 |
+
elif len(key_value) == 1:
|
| 15 |
+
paramArr += key_value
|
| 16 |
+
|
| 17 |
+
params2=params.copy()
|
| 18 |
+
del params2["full"],params2["dark"],params2["token"],params2["dir"]
|
| 19 |
+
|
| 20 |
+
os.chdir(f'{params["dir"]}')
|
| 21 |
+
full_precision_str = "--share --lowram --disable-safe-unpickle --disable-console-progressbars --xformers --enable-insecure-extension-access --precision full --no-half --no-half-vae --opt-sub-quad-attention --opt-channelslast --api"
|
| 22 |
+
half_precision_str = "--share --lowram --disable-safe-unpickle --disable-console-progressbars --xformers --enable-insecure-extension-access --opt-sub-quad-attention --opt-channelslast --api"
|
| 23 |
+
#自定义的参数
|
| 24 |
+
for key, value in params2.items():
|
| 25 |
+
full_precision_str += " --{}={}".format(key, value)
|
| 26 |
+
half_precision_str += " --{}={}".format(key, value)
|
| 27 |
+
for item in paramArr:
|
| 28 |
+
full_precision_str += f" --{item}"
|
| 29 |
+
half_precision_str += f" --{item}"
|
| 30 |
+
if params["dark"] == "True":
|
| 31 |
+
full_precision_str += " --theme='dark'"
|
| 32 |
+
half_precision_str += " --theme='dark'"
|
| 33 |
+
else:
|
| 34 |
+
full_precision_str += " --theme='light'"
|
| 35 |
+
half_precision_str += " --theme='light'"
|
| 36 |
+
if params["token"]:
|
| 37 |
+
full_precision_str += f' --ngrok={params["token"]} --ngrok-region="auto"'
|
| 38 |
+
half_precision_str += f' --ngrok={params["token"]} --ngrok-region="auto"'
|
| 39 |
+
if params["full"] == "True":
|
| 40 |
+
subprocess.run(f"python launch.py {full_precision_str}", shell=True) # (解决精度不足但速度不够)
|
| 41 |
+
else:
|
| 42 |
+
subprocess.run(f"python launch.py {half_precision_str}", shell=True) # 半精度(速度提升1倍以上,但可能出现精度不足问题)
|