Spaces:
Running
Running
Commit ·
c1fb62e
1
Parent(s): 66e0a94
Update scripts/mainrunpodA1111.py
Browse files- scripts/mainrunpodA1111.py +254 -589
scripts/mainrunpodA1111.py
CHANGED
|
@@ -10,13 +10,12 @@ from torch.hub import download_url_to_file
|
|
| 10 |
from urllib.parse import urlparse, parse_qs, unquote
|
| 11 |
import re
|
| 12 |
import six
|
| 13 |
-
import gdown
|
| 14 |
|
| 15 |
from urllib.request import urlopen, Request
|
| 16 |
import tempfile
|
| 17 |
-
from tqdm import tqdm
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
|
| 22 |
def Deps(force_reinstall):
|
|
@@ -58,19 +57,15 @@ def dwn(url, dst, msg):
|
|
| 58 |
req = Request(url, headers={"User-Agent": "torch.hub"})
|
| 59 |
u = urlopen(req)
|
| 60 |
meta = u.info()
|
| 61 |
-
if hasattr(meta,
|
| 62 |
content_length = meta.getheaders("Content-Length")
|
| 63 |
else:
|
| 64 |
content_length = meta.get_all("Content-Length")
|
| 65 |
if content_length is not None and len(content_length) > 0:
|
| 66 |
file_size = int(content_length[0])
|
| 67 |
|
| 68 |
-
with tqdm(
|
| 69 |
-
|
| 70 |
-
disable=False,
|
| 71 |
-
mininterval=0.5,
|
| 72 |
-
bar_format=msg + " |{bar:20}| {percentage:3.0f}%",
|
| 73 |
-
) as pbar:
|
| 74 |
with open(dst, "wb") as f:
|
| 75 |
while True:
|
| 76 |
buffer = u.read(8192)
|
|
@@ -83,572 +78,279 @@ def dwn(url, dst, msg):
|
|
| 83 |
|
| 84 |
def ntbks():
|
| 85 |
|
| 86 |
-
os.chdir(
|
| 87 |
-
if not os.path.exists(
|
| 88 |
-
call(
|
| 89 |
else:
|
| 90 |
-
call(
|
| 91 |
-
call(
|
| 92 |
-
os.chdir(
|
| 93 |
-
call(
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
-
call("rm Notebooks.txt", shell=True)
|
| 98 |
-
os.chdir("/workspace")
|
| 99 |
|
| 100 |
|
| 101 |
def repo(Huggingface_token_optional):
|
| 102 |
|
| 103 |
from slugify import slugify
|
| 104 |
from huggingface_hub import HfApi, CommitOperationAdd, create_repo
|
| 105 |
-
|
| 106 |
-
os.chdir(
|
| 107 |
-
if Huggingface_token_optional
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
call("rm sd_mrep.tar.zst", shell=True)
|
| 129 |
-
os.chdir("/workspace/sd")
|
| 130 |
-
if not os.path.exists("stable-diffusion-webui"):
|
| 131 |
-
call(
|
| 132 |
-
"git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui",
|
| 133 |
-
shell=True,
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
else:
|
| 137 |
-
print(
|
| 138 |
-
os.chdir(
|
| 139 |
-
if not os.path.exists(
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
print("[1;32m")
|
| 157 |
-
call("git pull", shell=True)
|
| 158 |
-
os.chdir("/workspace")
|
| 159 |
clear_output()
|
| 160 |
done()
|
| 161 |
|
| 162 |
|
|
|
|
| 163 |
def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK):
|
| 164 |
|
| 165 |
import gdown
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
if not os.path.exists("/workspace/sd/stable-diffusion-webui/models/VAE"):
|
| 170 |
-
call("mkdir -p /workspace/sd/stable-diffusion-webui/models/VAE", shell=True)
|
| 171 |
-
|
| 172 |
-
# Получаем список файлов в директории /workspace/auto-VAE
|
| 173 |
-
files = os.listdir("/workspace/auto-VAE")
|
| 174 |
-
|
| 175 |
-
for file in files:
|
| 176 |
-
source_path = os.path.join("/workspace/auto-VAE", file)
|
| 177 |
-
target_path = os.path.join(
|
| 178 |
-
"/workspace/sd/stable-diffusion-webui/models/VAE", file
|
| 179 |
-
)
|
| 180 |
-
# Проверяем, существует ли уже символическая ссылка
|
| 181 |
-
if not os.path.exists(target_path):
|
| 182 |
-
call(f"ln -s {source_path} {target_path}", shell=True)
|
| 183 |
-
|
| 184 |
-
# if os.path.exists('/workspace/sd/stable-diffusion-webui/models/VAE'):
|
| 185 |
-
# call('ln -s /workspace/auto-VAE/* /workspace/sd/stable-diffusion-webui/models/VAE', shell=True)
|
| 186 |
-
# else:
|
| 187 |
-
# call('mkdir -p /workspace/sd/stable-diffusion-webui/models/VAE', shell=True)
|
| 188 |
-
# call('ln -s /workspace/auto-VAE/* /workspace/sd/stable-diffusion-webui/models/VAE', shell=True)
|
| 189 |
-
|
| 190 |
-
if not os.path.exists(
|
| 191 |
-
"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv1-5.ckpt"
|
| 192 |
-
):
|
| 193 |
-
call(
|
| 194 |
-
"ln -s /workspace/auto-models/* /workspace/sd/stable-diffusion-webui/models/Stable-diffusion",
|
| 195 |
-
shell=True,
|
| 196 |
-
)
|
| 197 |
-
|
| 198 |
-
if not os.path.exists("/workspace/sd/stable-diffusion-webui/models/Lora"):
|
| 199 |
-
call("mkdir -p /workspace/sd/stable-diffusion-webui/models/Lora", shell=True)
|
| 200 |
-
call(
|
| 201 |
-
"ln -s /workspace/auto-lora/* /workspace/sd/stable-diffusion-webui/models/Lora",
|
| 202 |
-
shell=True,
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
if Path_to_MODEL != "":
|
| 206 |
-
if os.path.exists(str(Path_to_MODEL)):
|
| 207 |
-
print("[1;32mUsing the custom model")
|
| 208 |
-
model = Path_to_MODEL
|
| 209 |
-
else:
|
| 210 |
-
print("[1;31mWrong path, check that the path to the model is correct")
|
| 211 |
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
|
| 215 |
-
modelname = get_name(MODEL_LINK, False)
|
| 216 |
-
model = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
|
| 217 |
-
if not os.path.exists(model):
|
| 218 |
-
dwn(MODEL_LINK, model, "Downloading the custom model")
|
| 219 |
-
clear_output()
|
| 220 |
-
else:
|
| 221 |
-
print("[1;33mModel already exists")
|
| 222 |
-
elif src == "gdrive":
|
| 223 |
-
modelname = get_name(MODEL_LINK, True)
|
| 224 |
-
model = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
|
| 225 |
-
if not os.path.exists(model):
|
| 226 |
-
gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
|
| 227 |
-
clear_output()
|
| 228 |
-
else:
|
| 229 |
-
print("[1;33mModel already exists")
|
| 230 |
-
else:
|
| 231 |
-
modelname = os.path.basename(MODEL_LINK)
|
| 232 |
-
model = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
|
| 233 |
-
if not os.path.exists(model):
|
| 234 |
-
gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
|
| 235 |
-
clear_output()
|
| 236 |
-
else:
|
| 237 |
-
print("[1;33mModel already exists")
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
else:
|
| 251 |
if Original_Model_Version == "v1.5":
|
| 252 |
-
|
| 253 |
-
|
| 254 |
elif Original_Model_Version == "v2-512":
|
| 255 |
-
model
|
| 256 |
-
if not os.path.exists(
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
"gdown -O "
|
| 262 |
-
+ model
|
| 263 |
-
+ " https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-nonema-pruned.ckpt",
|
| 264 |
-
shell=True,
|
| 265 |
-
)
|
| 266 |
-
clear_output()
|
| 267 |
-
print("[1;32mUsing the original V2-512 model")
|
| 268 |
elif Original_Model_Version == "v2-768":
|
| 269 |
-
|
| 270 |
-
|
| 271 |
elif Original_Model_Version == "SDXL":
|
| 272 |
-
model
|
| 273 |
-
print(
|
| 274 |
|
| 275 |
else:
|
| 276 |
-
model
|
| 277 |
-
print(
|
| 278 |
try:
|
| 279 |
model
|
| 280 |
except:
|
| 281 |
-
model
|
| 282 |
|
| 283 |
return model
|
| 284 |
|
| 285 |
|
| 286 |
-
def modeldwn(model_LINK):
|
| 287 |
-
|
| 288 |
-
if model_LINK == "":
|
| 289 |
-
print("[1;33mNothing to do")
|
| 290 |
-
else:
|
| 291 |
-
os.makedirs(
|
| 292 |
-
"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion",
|
| 293 |
-
exist_ok=True,
|
| 294 |
-
)
|
| 295 |
-
|
| 296 |
-
src = getsrc(model_LINK)
|
| 297 |
-
|
| 298 |
-
if src == "civitai":
|
| 299 |
-
modelname = get_name(model_LINK, False)
|
| 300 |
-
loramodel = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
|
| 301 |
-
if not os.path.exists(loramodel):
|
| 302 |
-
dwn(model_LINK, loramodel, "Downloading the LoRA model")
|
| 303 |
-
clear_output()
|
| 304 |
-
else:
|
| 305 |
-
print("[1;33mModel already exists")
|
| 306 |
-
elif src == "gdrive":
|
| 307 |
-
modelname = get_true_name(model_LINK)
|
| 308 |
-
loramodel = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
|
| 309 |
-
if not os.path.exists(loramodel):
|
| 310 |
-
gdown.download(
|
| 311 |
-
url=model_LINK.replace("/file/d/", "/uc?id=").replace("/view", ""),
|
| 312 |
-
output=loramodel,
|
| 313 |
-
quiet=False,
|
| 314 |
-
)
|
| 315 |
-
clear_output()
|
| 316 |
-
else:
|
| 317 |
-
print("[1;33mModel already exists")
|
| 318 |
-
else:
|
| 319 |
-
modelname = os.path.basename(model_LINK)
|
| 320 |
-
loramodel = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
|
| 321 |
-
if not os.path.exists(loramodel):
|
| 322 |
-
gdown.download(
|
| 323 |
-
url=model_LINK, output=loramodel, quiet=False, fuzzy=True
|
| 324 |
-
)
|
| 325 |
-
clear_output()
|
| 326 |
-
else:
|
| 327 |
-
print("[1;33mModel already exists")
|
| 328 |
-
|
| 329 |
-
if os.path.exists(loramodel):
|
| 330 |
-
print("[1;32mCheckpoints downloaded")
|
| 331 |
-
else:
|
| 332 |
-
print("[1;31mWrong link, check that the link is valid")
|
| 333 |
-
|
| 334 |
-
|
| 335 |
def loradwn(LoRA_LINK):
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
|
|
|
|
|
|
| 339 |
else:
|
| 340 |
-
os.makedirs(
|
| 341 |
|
| 342 |
-
src
|
| 343 |
|
| 344 |
-
if src
|
| 345 |
-
modelname
|
| 346 |
-
loramodel
|
| 347 |
if not os.path.exists(loramodel):
|
| 348 |
-
|
| 349 |
-
|
| 350 |
else:
|
| 351 |
-
|
| 352 |
-
elif src
|
| 353 |
-
modelname
|
| 354 |
-
loramodel
|
| 355 |
if not os.path.exists(loramodel):
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
output=loramodel,
|
| 359 |
-
quiet=False,
|
| 360 |
-
)
|
| 361 |
-
clear_output()
|
| 362 |
else:
|
| 363 |
-
|
| 364 |
else:
|
| 365 |
-
modelname
|
| 366 |
-
loramodel
|
| 367 |
if not os.path.exists(loramodel):
|
| 368 |
-
|
| 369 |
-
|
| 370 |
else:
|
| 371 |
-
|
| 372 |
|
| 373 |
-
if os.path.exists(loramodel):
|
| 374 |
-
|
| 375 |
else:
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
def download_and_install_config():
|
| 380 |
-
target_directory = "/workspace/sd/stable-diffusion-webui/"
|
| 381 |
-
config_url = (
|
| 382 |
-
"https://huggingface.co/spaces/stlaurentjr/RNPD/raw/main/config/config.json"
|
| 383 |
-
)
|
| 384 |
-
config_file_path = os.path.join(target_directory, "config.json")
|
| 385 |
-
|
| 386 |
-
# Check if the config file already exists
|
| 387 |
-
if not os.path.exists(config_file_path):
|
| 388 |
-
# Change the directory
|
| 389 |
-
os.chdir(target_directory)
|
| 390 |
-
# Download the file using curl command
|
| 391 |
-
call(f"curl -o config.json {config_url}", shell=True)
|
| 392 |
-
print(f"Config file downloaded successfully.")
|
| 393 |
-
else:
|
| 394 |
-
print("Config file already exists, download skipped.")
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
def InstallDeforum():
|
| 398 |
-
# Переход в директорию расширений
|
| 399 |
-
os.chdir("/workspace/sd/stable-diffusion-webui/extensions")
|
| 400 |
-
|
| 401 |
-
# Проверка наличия директории с расширением
|
| 402 |
-
if not os.path.exists("deforum-for-automatic1111-webui"):
|
| 403 |
-
# Клонирование репозитория, если он не существует
|
| 404 |
-
call(
|
| 405 |
-
"git clone https://github.com/deforum-art/sd-webui-deforum.git", shell=True
|
| 406 |
-
)
|
| 407 |
-
# Возврат в корневую директорию
|
| 408 |
-
os.chdir("/workspace")
|
| 409 |
-
else:
|
| 410 |
-
# Если директория существует, переходим в нее
|
| 411 |
-
# os.chdir('deforum-for-automatic1111-webui')
|
| 412 |
-
# Сброс изменений и обновление репозитория
|
| 413 |
-
# call('git reset --hard', shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
|
| 414 |
-
# call('git pull', shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
|
| 415 |
-
# Возврат в корневую директорию
|
| 416 |
-
os.chdir("/workspace")
|
| 417 |
-
|
| 418 |
-
def InstallReActor():
|
| 419 |
-
# Переход в директорию расширений
|
| 420 |
-
os.chdir("/workspace/sd/stable-diffusion-webui/extensions")
|
| 421 |
-
# Проверка наличия директории с расширением
|
| 422 |
-
if not os.path.exists("sd-webui-reactor"):
|
| 423 |
-
# Установка библиотек
|
| 424 |
-
from subprocess import call, DEVNULL
|
| 425 |
-
call(
|
| 426 |
-
'pip install "onnxruntime-gpu>=1.16.1"',
|
| 427 |
-
shell=True
|
| 428 |
-
)
|
| 429 |
-
# Клонирование репозитория, если он не существует
|
| 430 |
-
call(
|
| 431 |
-
"git clone https://github.com/Gourieff/sd-webui-reactor.git",
|
| 432 |
-
shell=True,
|
| 433 |
-
)
|
| 434 |
-
# Создание папки под модели
|
| 435 |
-
call(
|
| 436 |
-
"mkdir /workspace/sd/stable-diffusion-webui/models/insightface",
|
| 437 |
-
shell=True
|
| 438 |
-
)
|
| 439 |
-
os.chdir("/workspace/sd/stable-diffusion-webui/models/insightface")
|
| 440 |
-
# Скачивание модели для reActor
|
| 441 |
-
call(
|
| 442 |
-
"wget https://github.com/facefusion/facefusion-assets/releases/download/models/inswapper_128.onnx",
|
| 443 |
-
shell=True,
|
| 444 |
-
stdout=DEVNULL,
|
| 445 |
-
stderr=DEVNULL
|
| 446 |
-
)
|
| 447 |
-
# Вовзрат в корневую директорию
|
| 448 |
-
clear_output()
|
| 449 |
-
os.chdir("/workspace")
|
| 450 |
-
else:
|
| 451 |
-
#Возврат в корневую директорию
|
| 452 |
-
os.chdir("/workspace")
|
| 453 |
-
|
| 454 |
-
def InstallAnimateDiff():
|
| 455 |
-
# Переход в директорию расширений
|
| 456 |
-
os.chdir("/workspace/sd/stable-diffusion-webui/extensions")
|
| 457 |
-
|
| 458 |
-
# Проверка наличия директории с расширением
|
| 459 |
-
if not os.path.exists("sd-webui-animatediff"):
|
| 460 |
-
from subprocess import call, DEVNULL
|
| 461 |
-
# Клонирование репозитория, если он не существует
|
| 462 |
-
call(
|
| 463 |
-
"git clone https://github.com/continue-revolution/sd-webui-animatediff.git",
|
| 464 |
-
shell=True,
|
| 465 |
-
)
|
| 466 |
-
# Переход в папку с моделями
|
| 467 |
-
os.chdir(
|
| 468 |
-
"/workspace/sd/stable-diffusion-webui/extensions/sd-webui-animatediff/model"
|
| 469 |
-
)
|
| 470 |
-
# Скачивание моделей для animatediff
|
| 471 |
-
call(
|
| 472 |
-
"wget https://huggingface.co/guoyww/animatediff/resolve/refs%2Fpr%2F3/mm_sd_v15_v2.ckpt",
|
| 473 |
-
shell=True,
|
| 474 |
-
stdout=DEVNULL,
|
| 475 |
-
stderr=DEVNULL
|
| 476 |
-
)
|
| 477 |
-
# call(
|
| 478 |
-
# "wget https://huggingface.co/manshoety/AD_Stabilized_Motion/resolve/main/mm-Stabilized_high.pth",
|
| 479 |
-
# shell=True,
|
| 480 |
-
# stdout=DEVNULL,
|
| 481 |
-
# stderr=DEVNULL
|
| 482 |
-
# )
|
| 483 |
-
# Вовзрат в корневую директорию
|
| 484 |
-
clear_output()
|
| 485 |
-
os.chdir("/workspace")
|
| 486 |
-
else:
|
| 487 |
-
# Возврат в корневую директорию
|
| 488 |
-
os.chdir("/workspace")
|
| 489 |
-
|
| 490 |
-
def InstallWildCards():
|
| 491 |
-
# Путь к целевой директории
|
| 492 |
-
target_dir = "/workspace/sd/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards/"
|
| 493 |
-
|
| 494 |
-
# Перемещаемся в директорию расширений
|
| 495 |
-
os.chdir("/workspace/sd/stable-diffusion-webui/extensions")
|
| 496 |
-
|
| 497 |
-
if not os.path.exists("stable-diffusion-webui-wildcards"):
|
| 498 |
-
# Клонирование репозитория, если он ещё не существует
|
| 499 |
-
call(
|
| 500 |
-
"git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-wildcards.git",
|
| 501 |
-
shell=True,
|
| 502 |
-
)
|
| 503 |
|
| 504 |
-
# Перемещаемся в клонированный репозиторий
|
| 505 |
-
os.chdir("stable-diffusion-webui-wildcards")
|
| 506 |
-
|
| 507 |
-
# Скачиваем файл по ID
|
| 508 |
-
gdown.download(
|
| 509 |
-
id="1sY9Yv29cCYZuxBvszkmLVgw--aRdwT1P", output="wildcards.zip", quiet=False
|
| 510 |
-
)
|
| 511 |
-
|
| 512 |
-
# Создаем целевую директорию, если она ещё не существует
|
| 513 |
-
os.makedirs(target_dir, exist_ok=True)
|
| 514 |
-
|
| 515 |
-
# Распаковываем архив
|
| 516 |
-
with zipfile.ZipFile("wildcards.zip", "r") as zip_ref:
|
| 517 |
-
zip_ref.extractall(target_dir)
|
| 518 |
-
else:
|
| 519 |
-
# Если репозиторий уже существует, обновляем его
|
| 520 |
-
os.chdir("stable-diffusion-webui-wildcards")
|
| 521 |
-
call(
|
| 522 |
-
"git reset --hard",
|
| 523 |
-
shell=True,
|
| 524 |
-
stdout=open(os.devnull, "w"),
|
| 525 |
-
stderr=open(os.devnull, "w"),
|
| 526 |
-
)
|
| 527 |
-
call(
|
| 528 |
-
"git pull",
|
| 529 |
-
shell=True,
|
| 530 |
-
stdout=open(os.devnull, "w"),
|
| 531 |
-
stderr=open(os.devnull, "w"),
|
| 532 |
-
)
|
| 533 |
-
|
| 534 |
-
# Возвращаемся в исходную рабочую директорию
|
| 535 |
-
os.chdir("/workspace")
|
| 536 |
|
| 537 |
|
| 538 |
def CNet(ControlNet_Model, ControlNet_XL_Model):
|
|
|
|
| 539 |
def download(url, model_dir):
|
| 540 |
-
try:
|
| 541 |
-
filename = os.path.basename(urlparse(url).path)
|
| 542 |
-
pth = os.path.abspath(os.path.join(model_dir, filename))
|
| 543 |
-
if not os.path.exists(pth):
|
| 544 |
-
print("Downloading: " + os.path.basename(url))
|
| 545 |
-
download_url_to_file(url, pth, hash_prefix=None, progress=True)
|
| 546 |
-
else:
|
| 547 |
-
print(f"[1;32mThe model {filename} already exists[0m")
|
| 548 |
-
except Exception as e:
|
| 549 |
-
print(f"Ошибка при скачивании {url}: {e}")
|
| 550 |
|
| 551 |
-
|
| 552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
if not os.path.exists("sd-webui-controlnet"):
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
)
|
| 557 |
-
os.chdir("/workspace")
|
| 558 |
else:
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
stdout=open("/dev/null", "w"),
|
| 564 |
-
stderr=open("/dev/null", "w"),
|
| 565 |
-
)
|
| 566 |
-
call(
|
| 567 |
-
"git pull",
|
| 568 |
-
shell=True,
|
| 569 |
-
stdout=open("/dev/null", "w"),
|
| 570 |
-
stderr=open("/dev/null", "w"),
|
| 571 |
-
)
|
| 572 |
-
os.chdir("/workspace")
|
| 573 |
-
|
| 574 |
-
mdldir = (
|
| 575 |
-
"/workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models"
|
| 576 |
-
)
|
| 577 |
-
call(
|
| 578 |
-
"ln -s /workspace/auto-controlnet-models/* /workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models",
|
| 579 |
-
shell=True,
|
| 580 |
-
)
|
| 581 |
|
|
|
|
| 582 |
for filename in os.listdir(mdldir):
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
|
| 587 |
-
call(
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
)
|
| 591 |
-
call(
|
| 592 |
-
"wget -q -O CN_models_XL.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_XL.txt",
|
| 593 |
-
shell=True,
|
| 594 |
-
)
|
| 595 |
-
|
| 596 |
-
with open("CN_models.txt", "r") as f:
|
| 597 |
mdllnk = f.read().splitlines()
|
| 598 |
-
with open("CN_models_XL.txt",
|
| 599 |
mdllnk_XL = d.read().splitlines()
|
| 600 |
-
call(
|
| 601 |
-
|
| 602 |
-
os.chdir(
|
| 603 |
|
| 604 |
-
if ControlNet_Model == "All" or ControlNet_Model == "all":
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
|
|
|
| 614 |
|
| 615 |
-
elif (
|
| 616 |
-
ControlNet_Model.isdigit()
|
| 617 |
-
and int(ControlNet_Model) - 1 < 16
|
| 618 |
-
and int(ControlNet_Model) > 0
|
| 619 |
-
):
|
| 620 |
-
download(mdllnk[int(ControlNet_Model) - 1], mdldir)
|
| 621 |
-
clear_output()
|
| 622 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
elif ControlNet_Model == "none":
|
| 624 |
-
|
| 625 |
-
|
| 626 |
|
| 627 |
else:
|
| 628 |
-
|
| 629 |
-
|
| 630 |
|
| 631 |
-
if ControlNet_XL_Model == "All" or ControlNet_XL_Model == "all":
|
| 632 |
-
for lnk_XL in mdllnk_XL:
|
| 633 |
-
download(lnk_XL, mdldir)
|
| 634 |
-
if not wrngv1:
|
| 635 |
-
clear_output()
|
| 636 |
-
done()
|
| 637 |
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
|
|
|
| 643 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
elif ControlNet_XL_Model == "none":
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
|
| 650 |
else:
|
| 651 |
-
|
|
|
|
| 652 |
|
| 653 |
|
| 654 |
def sd(User, Password, model):
|
|
@@ -697,95 +399,71 @@ def sd(User, Password, model):
|
|
| 697 |
else:
|
| 698 |
mdlpth="--ckpt-dir "+model
|
| 699 |
|
| 700 |
-
vae_path = "--vae-path /workspace/sd/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors"
|
| 701 |
configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --no-download-sd-model --opt-sdp-attention --enable-insecure-extension-access --skip-version-check --listen --port 3000 "+auth+" "+mdlpth
|
| 702 |
|
| 703 |
return configf
|
| 704 |
|
| 705 |
|
| 706 |
-
def save(Huggingface_Write_token):
|
| 707 |
|
|
|
|
|
|
|
| 708 |
from slugify import slugify
|
| 709 |
from huggingface_hub import HfApi, CommitOperationAdd, create_repo
|
| 710 |
-
|
| 711 |
-
if Huggingface_Write_token
|
| 712 |
-
print(
|
| 713 |
-
|
| 714 |
else:
|
| 715 |
-
os.chdir(
|
| 716 |
-
|
| 717 |
-
if os.path.exists(
|
| 718 |
-
|
| 719 |
-
call(
|
| 720 |
-
'tar --exclude="stable-diffusion-webui/models/*/*" --exclude="sd-webui-controlnet/models/*" --zstd -cf sd_backup_rnpd.tar.zst sd',
|
| 721 |
-
shell=True,
|
| 722 |
-
)
|
| 723 |
api = HfApi()
|
| 724 |
username = api.whoami(token=Huggingface_Write_token)["name"]
|
| 725 |
|
| 726 |
repo_id = f"{username}/{slugify('fast-stable-diffusion')}"
|
| 727 |
|
| 728 |
-
print("[1;32mBacking up...")
|
| 729 |
-
|
| 730 |
-
operations = [
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
path_or_fileobj="/workspace/sd_backup_rnpd.tar.zst",
|
| 734 |
-
)
|
| 735 |
-
]
|
| 736 |
-
|
| 737 |
-
create_repo(
|
| 738 |
-
repo_id,
|
| 739 |
-
private=True,
|
| 740 |
-
token=Huggingface_Write_token,
|
| 741 |
-
exist_ok=True,
|
| 742 |
-
repo_type="dataset",
|
| 743 |
-
)
|
| 744 |
|
| 745 |
api.create_commit(
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
)
|
| 752 |
|
| 753 |
-
call(
|
| 754 |
clear_output()
|
| 755 |
|
| 756 |
done()
|
| 757 |
-
|
| 758 |
else:
|
| 759 |
-
print(
|
|
|
|
|
|
|
| 760 |
|
| 761 |
|
| 762 |
def getsrc(url):
|
| 763 |
|
| 764 |
parsed_url = urlparse(url)
|
| 765 |
-
|
| 766 |
-
if parsed_url.netloc ==
|
| 767 |
-
src
|
| 768 |
-
elif parsed_url.netloc ==
|
| 769 |
-
src
|
| 770 |
-
elif parsed_url.netloc ==
|
| 771 |
-
src
|
| 772 |
else:
|
| 773 |
-
src
|
| 774 |
return src
|
| 775 |
|
| 776 |
|
| 777 |
-
def get_true_name(url):
|
| 778 |
-
response = requests.get(url)
|
| 779 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
| 780 |
-
title_tag = soup.find("title")
|
| 781 |
-
if title_tag:
|
| 782 |
-
title_text = title_tag.text
|
| 783 |
-
# Извлечение имени файла из тега title (предполагая, что имя файла указано в теге title)
|
| 784 |
-
file_name = title_text.split(" - ")[0]
|
| 785 |
-
return file_name
|
| 786 |
-
else:
|
| 787 |
-
raise RuntimeError("Could not find the title tag in the HTML")
|
| 788 |
-
|
| 789 |
|
| 790 |
def get_name(url, gdrive):
|
| 791 |
|
|
@@ -800,39 +478,26 @@ def get_name(url, gdrive):
|
|
| 800 |
disp_val = quer["response-content-disposition"][0].split(";")
|
| 801 |
for vals in disp_val:
|
| 802 |
if vals.strip().startswith("filename="):
|
| 803 |
-
filenm
|
| 804 |
-
return filenm.replace(
|
| 805 |
else:
|
| 806 |
-
headers = {
|
| 807 |
-
|
| 808 |
-
}
|
| 809 |
-
lnk = "https://drive.google.com/uc?id={id}&export=download".format(
|
| 810 |
-
id=url[url.find("/d/") + 3 : url.find("/view")]
|
| 811 |
-
)
|
| 812 |
res = requests.session().get(lnk, headers=headers, stream=True, verify=True)
|
| 813 |
-
res = requests.session().get(
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
content_disposition = six.moves.urllib_parse.unquote(
|
| 820 |
-
res.headers["Content-Disposition"]
|
| 821 |
-
)
|
| 822 |
-
filenm = (
|
| 823 |
-
re.search(r"filename\*=UTF-8''(.*)", content_disposition)
|
| 824 |
-
.groups()[0]
|
| 825 |
-
.replace(os.path.sep, "_")
|
| 826 |
-
)
|
| 827 |
-
return filenm
|
| 828 |
|
| 829 |
|
| 830 |
def done():
|
| 831 |
done = widgets.Button(
|
| 832 |
-
description=
|
| 833 |
disabled=True,
|
| 834 |
-
button_style=
|
| 835 |
-
tooltip=
|
| 836 |
-
icon=
|
| 837 |
)
|
| 838 |
-
display(done)
|
|
|
|
| 10 |
from urllib.parse import urlparse, parse_qs, unquote
|
| 11 |
import re
|
| 12 |
import six
|
|
|
|
| 13 |
|
| 14 |
from urllib.request import urlopen, Request
|
| 15 |
import tempfile
|
| 16 |
+
from tqdm import tqdm
|
| 17 |
+
|
| 18 |
+
|
| 19 |
|
| 20 |
|
| 21 |
def Deps(force_reinstall):
|
|
|
|
| 57 |
req = Request(url, headers={"User-Agent": "torch.hub"})
|
| 58 |
u = urlopen(req)
|
| 59 |
meta = u.info()
|
| 60 |
+
if hasattr(meta, 'getheaders'):
|
| 61 |
content_length = meta.getheaders("Content-Length")
|
| 62 |
else:
|
| 63 |
content_length = meta.get_all("Content-Length")
|
| 64 |
if content_length is not None and len(content_length) > 0:
|
| 65 |
file_size = int(content_length[0])
|
| 66 |
|
| 67 |
+
with tqdm(total=file_size, disable=False, mininterval=0.5,
|
| 68 |
+
bar_format=msg+' |{bar:20}| {percentage:3.0f}%') as pbar:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
with open(dst, "wb") as f:
|
| 70 |
while True:
|
| 71 |
buffer = u.read(8192)
|
|
|
|
| 78 |
|
| 79 |
def ntbks():
|
| 80 |
|
| 81 |
+
os.chdir('/workspace')
|
| 82 |
+
if not os.path.exists('Latest_Notebooks'):
|
| 83 |
+
call('mkdir Latest_Notebooks', shell=True)
|
| 84 |
else:
|
| 85 |
+
call('rm -r Latest_Notebooks', shell=True)
|
| 86 |
+
call('mkdir Latest_Notebooks', shell=True)
|
| 87 |
+
os.chdir('/workspace/Latest_Notebooks')
|
| 88 |
+
call('wget -q -i https://huggingface.co/datasets/TheLastBen/RNPD/raw/main/Notebooks.txt', shell=True)
|
| 89 |
+
call('rm Notebooks.txt', shell=True)
|
| 90 |
+
os.chdir('/workspace')
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
def repo(Huggingface_token_optional):
|
| 94 |
|
| 95 |
from slugify import slugify
|
| 96 |
from huggingface_hub import HfApi, CommitOperationAdd, create_repo
|
| 97 |
+
|
| 98 |
+
os.chdir('/workspace')
|
| 99 |
+
if Huggingface_token_optional!="":
|
| 100 |
+
username = HfApi().whoami(Huggingface_token_optional)["name"]
|
| 101 |
+
backup=f"https://huggingface.co/datasets/{username}/fast-stable-diffusion/resolve/main/sd_backup_rnpd.tar.zst"
|
| 102 |
+
headers = {"Authorization": f"Bearer {Huggingface_token_optional}"}
|
| 103 |
+
response = requests.head(backup, headers=headers)
|
| 104 |
+
if response.status_code == 302:
|
| 105 |
+
print('[1;33mRestoring the SD folder...')
|
| 106 |
+
open('/workspace/sd_backup_rnpd.tar.zst', 'wb').write(requests.get(backup, headers=headers).content)
|
| 107 |
+
call('tar --zstd -xf sd_backup_rnpd.tar.zst', shell=True)
|
| 108 |
+
call('rm sd_backup_rnpd.tar.zst', shell=True)
|
| 109 |
+
else:
|
| 110 |
+
print('[1;33mBackup not found, using a fresh/existing repo...')
|
| 111 |
+
time.sleep(2)
|
| 112 |
+
if not os.path.exists('/workspace/sd/stablediffusiond'): #reset later
|
| 113 |
+
call('wget -q -O sd_mrep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_mrep.tar.zst', shell=True)
|
| 114 |
+
call('tar --zstd -xf sd_mrep.tar.zst', shell=True)
|
| 115 |
+
call('rm sd_mrep.tar.zst', shell=True)
|
| 116 |
+
os.chdir('/workspace/sd')
|
| 117 |
+
if not os.path.exists('stable-diffusion-webui'):
|
| 118 |
+
call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
|
| 119 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
+
print('[1;33mInstalling/Updating the repo...')
|
| 122 |
+
os.chdir('/workspace')
|
| 123 |
+
if not os.path.exists('/workspace/sd/stablediffusiond'): #reset later
|
| 124 |
+
call('wget -q -O sd_mrep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_mrep.tar.zst', shell=True)
|
| 125 |
+
call('tar --zstd -xf sd_mrep.tar.zst', shell=True)
|
| 126 |
+
call('rm sd_mrep.tar.zst', shell=True)
|
| 127 |
+
|
| 128 |
+
os.chdir('/workspace/sd')
|
| 129 |
+
if not os.path.exists('stable-diffusion-webui'):
|
| 130 |
+
call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
os.chdir('/workspace/sd/stable-diffusion-webui/')
|
| 134 |
+
call('git reset --hard', shell=True)
|
| 135 |
+
print('[1;32m')
|
| 136 |
+
call('git pull', shell=True)
|
| 137 |
+
os.makedirs('/workspace/sd/stable-diffusion-webui/repositories', exist_ok=True)
|
| 138 |
+
call('git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-assets /workspace/sd/stable-diffusion-webui/repositories/stable-diffusion-webui-assets', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
|
| 139 |
+
os.chdir('/workspace')
|
|
|
|
|
|
|
|
|
|
| 140 |
clear_output()
|
| 141 |
done()
|
| 142 |
|
| 143 |
|
| 144 |
+
|
| 145 |
def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK):
|
| 146 |
|
| 147 |
import gdown
|
| 148 |
+
|
| 149 |
+
src=getsrc(MODEL_LINK)
|
| 150 |
|
| 151 |
+
if not os.path.exists('/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv1-5.ckpt'):
|
| 152 |
+
call('ln -s /workspace/auto-models/* /workspace/sd/stable-diffusion-webui/models/Stable-diffusion', shell=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
if Path_to_MODEL !='':
|
| 155 |
+
if os.path.exists(str(Path_to_MODEL)):
|
| 156 |
+
print('[1;32mUsing the custom model')
|
| 157 |
+
model=Path_to_MODEL
|
| 158 |
+
else:
|
| 159 |
+
print('[1;31mWrong path, check that the path to the model is correct')
|
| 160 |
|
| 161 |
+
elif MODEL_LINK !="":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
if src=='civitai':
|
| 164 |
+
modelname=get_name(MODEL_LINK, False)
|
| 165 |
+
model=f'/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
|
| 166 |
+
if not os.path.exists(model):
|
| 167 |
+
dwn(MODEL_LINK, model, 'Downloading the custom model')
|
| 168 |
+
clear_output()
|
| 169 |
+
else:
|
| 170 |
+
print('[1;33mModel already exists')
|
| 171 |
+
elif src=='gdrive':
|
| 172 |
+
modelname=get_name(MODEL_LINK, True)
|
| 173 |
+
model=f'/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
|
| 174 |
+
if not os.path.exists(model):
|
| 175 |
+
gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
|
| 176 |
+
clear_output()
|
| 177 |
+
else:
|
| 178 |
+
print('[1;33mModel already exists')
|
| 179 |
+
else:
|
| 180 |
+
modelname=os.path.basename(MODEL_LINK)
|
| 181 |
+
model=f'/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
|
| 182 |
+
if not os.path.exists(model):
|
| 183 |
+
gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
|
| 184 |
+
clear_output()
|
| 185 |
+
else:
|
| 186 |
+
print('[1;33mModel already exists')
|
| 187 |
+
|
| 188 |
+
if os.path.exists(model) and os.path.getsize(model) > 1810671599:
|
| 189 |
+
print('[1;32mModel downloaded, using the custom model.')
|
| 190 |
+
else:
|
| 191 |
+
call('rm '+model, shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
|
| 192 |
+
print('[1;31mWrong link, check that the link is valid')
|
| 193 |
|
| 194 |
else:
|
| 195 |
if Original_Model_Version == "v1.5":
|
| 196 |
+
model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv1-5.ckpt"
|
| 197 |
+
print('[1;32mUsing the original V1.5 model')
|
| 198 |
elif Original_Model_Version == "v2-512":
|
| 199 |
+
model='/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt'
|
| 200 |
+
if not os.path.exists('/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt'):
|
| 201 |
+
print('[1;33mDownloading the V2-512 model...')
|
| 202 |
+
call('gdown -O '+model+' https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-nonema-pruned.ckpt', shell=True)
|
| 203 |
+
clear_output()
|
| 204 |
+
print('[1;32mUsing the original V2-512 model')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
elif Original_Model_Version == "v2-768":
|
| 206 |
+
model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-768.ckpt"
|
| 207 |
+
print('[1;32mUsing the original V2-768 model')
|
| 208 |
elif Original_Model_Version == "SDXL":
|
| 209 |
+
model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/sd_xl_base_1.0.safetensors"
|
| 210 |
+
print('[1;32mUsing the original SDXL model')
|
| 211 |
|
| 212 |
else:
|
| 213 |
+
model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion"
|
| 214 |
+
print('[1;31mWrong model version, try again')
|
| 215 |
try:
|
| 216 |
model
|
| 217 |
except:
|
| 218 |
+
model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion"
|
| 219 |
|
| 220 |
return model
|
| 221 |
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
def loradwn(LoRA_LINK):
|
| 224 |
|
| 225 |
+
import gdown
|
| 226 |
+
|
| 227 |
+
if LoRA_LINK=='':
|
| 228 |
+
print('[1;33mNothing to do')
|
| 229 |
else:
|
| 230 |
+
os.makedirs('/workspace/sd/stable-diffusion-webui/models/Lora', exist_ok=True)
|
| 231 |
|
| 232 |
+
src=getsrc(LoRA_LINK)
|
| 233 |
|
| 234 |
+
if src=='civitai':
|
| 235 |
+
modelname=get_name(LoRA_LINK, False)
|
| 236 |
+
loramodel=f'/workspace/sd/stable-diffusion-webui/models/Lora/{modelname}'
|
| 237 |
if not os.path.exists(loramodel):
|
| 238 |
+
dwn(LoRA_LINK, loramodel, 'Downloading the LoRA model')
|
| 239 |
+
clear_output()
|
| 240 |
else:
|
| 241 |
+
print('[1;33mModel already exists')
|
| 242 |
+
elif src=='gdrive':
|
| 243 |
+
modelname=get_name(LoRA_LINK, True)
|
| 244 |
+
loramodel=f'/workspace/sd/stable-diffusion-webui/models/Lora/{modelname}'
|
| 245 |
if not os.path.exists(loramodel):
|
| 246 |
+
gdown.download(url=LoRA_LINK, output=loramodel, quiet=False, fuzzy=True)
|
| 247 |
+
clear_output()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
else:
|
| 249 |
+
print('[1;33mModel already exists')
|
| 250 |
else:
|
| 251 |
+
modelname=os.path.basename(LoRA_LINK)
|
| 252 |
+
loramodel=f'/workspace/sd/stable-diffusion-webui/models/Lora/{modelname}'
|
| 253 |
if not os.path.exists(loramodel):
|
| 254 |
+
gdown.download(url=LoRA_LINK, output=loramodel, quiet=False, fuzzy=True)
|
| 255 |
+
clear_output()
|
| 256 |
else:
|
| 257 |
+
print('[1;33mModel already exists')
|
| 258 |
|
| 259 |
+
if os.path.exists(loramodel) :
|
| 260 |
+
print('[1;32mLoRA downloaded')
|
| 261 |
else:
|
| 262 |
+
print('[1;31mWrong link, check that the link is valid')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
|
| 266 |
def CNet(ControlNet_Model, ControlNet_XL_Model):
|
| 267 |
+
|
| 268 |
def download(url, model_dir):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
+
filename = os.path.basename(urlparse(url).path)
|
| 271 |
+
pth = os.path.abspath(os.path.join(model_dir, filename))
|
| 272 |
+
if not os.path.exists(pth):
|
| 273 |
+
print('Downloading: '+os.path.basename(url))
|
| 274 |
+
download_url_to_file(url, pth, hash_prefix=None, progress=True)
|
| 275 |
+
else:
|
| 276 |
+
print(f"[1;32mThe model {filename} already exists[0m")
|
| 277 |
+
|
| 278 |
+
wrngv1=False
|
| 279 |
+
os.chdir('/workspace/sd/stable-diffusion-webui/extensions')
|
| 280 |
if not os.path.exists("sd-webui-controlnet"):
|
| 281 |
+
call('git clone https://github.com/Mikubill/sd-webui-controlnet.git', shell=True)
|
| 282 |
+
os.chdir('/workspace')
|
|
|
|
|
|
|
| 283 |
else:
|
| 284 |
+
os.chdir('sd-webui-controlnet')
|
| 285 |
+
call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
|
| 286 |
+
call('git pull', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
|
| 287 |
+
os.chdir('/workspace')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
+
mdldir="/workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models"
|
| 290 |
for filename in os.listdir(mdldir):
|
| 291 |
+
if "_sd14v1" in filename:
|
| 292 |
+
renamed = re.sub("_sd14v1", "-fp16", filename)
|
| 293 |
+
os.rename(os.path.join(mdldir, filename), os.path.join(mdldir, renamed))
|
| 294 |
|
| 295 |
+
call('wget -q -O CN_models.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models.txt', shell=True)
|
| 296 |
+
call('wget -q -O CN_models_XL.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_XL.txt', shell=True)
|
| 297 |
+
|
| 298 |
+
with open("CN_models.txt", 'r') as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
mdllnk = f.read().splitlines()
|
| 300 |
+
with open("CN_models_XL.txt", 'r') as d:
|
| 301 |
mdllnk_XL = d.read().splitlines()
|
| 302 |
+
call('rm CN_models.txt CN_models_XL.txt', shell=True)
|
| 303 |
+
|
| 304 |
+
os.chdir('/workspace')
|
| 305 |
|
| 306 |
+
if ControlNet_Model == "All" or ControlNet_Model == "all" :
|
| 307 |
+
for lnk in mdllnk:
|
| 308 |
+
download(lnk, mdldir)
|
| 309 |
+
clear_output()
|
| 310 |
|
| 311 |
+
|
| 312 |
+
elif ControlNet_Model == "15":
|
| 313 |
+
mdllnk=list(filter(lambda x: 't2i' in x, mdllnk))
|
| 314 |
+
for lnk in mdllnk:
|
| 315 |
+
download(lnk, mdldir)
|
| 316 |
+
clear_output()
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
elif ControlNet_Model.isdigit() and int(ControlNet_Model)-1<14 and int(ControlNet_Model)>0:
|
| 320 |
+
download(mdllnk[int(ControlNet_Model)-1], mdldir)
|
| 321 |
+
clear_output()
|
| 322 |
+
|
| 323 |
elif ControlNet_Model == "none":
|
| 324 |
+
pass
|
| 325 |
+
clear_output()
|
| 326 |
|
| 327 |
else:
|
| 328 |
+
print('[1;31mWrong ControlNet V1 choice, try again')
|
| 329 |
+
wrngv1=True
|
| 330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
+
if ControlNet_XL_Model == "All" or ControlNet_XL_Model == "all" :
|
| 333 |
+
for lnk_XL in mdllnk_XL:
|
| 334 |
+
download(lnk_XL, mdldir)
|
| 335 |
+
if not wrngv1:
|
| 336 |
+
clear_output()
|
| 337 |
+
done()
|
| 338 |
|
| 339 |
+
elif ControlNet_XL_Model.isdigit() and int(ControlNet_XL_Model)-1<5:
|
| 340 |
+
download(mdllnk_XL[int(ControlNet_XL_Model)-1], mdldir)
|
| 341 |
+
if not wrngv1:
|
| 342 |
+
clear_output()
|
| 343 |
+
done()
|
| 344 |
+
|
| 345 |
elif ControlNet_XL_Model == "none":
|
| 346 |
+
pass
|
| 347 |
+
if not wrngv1:
|
| 348 |
+
clear_output()
|
| 349 |
+
done()
|
| 350 |
|
| 351 |
else:
|
| 352 |
+
print('[1;31mWrong ControlNet XL choice, try again')
|
| 353 |
+
|
| 354 |
|
| 355 |
|
| 356 |
def sd(User, Password, model):
|
|
|
|
| 399 |
else:
|
| 400 |
mdlpth="--ckpt-dir "+model
|
| 401 |
|
|
|
|
| 402 |
configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --no-download-sd-model --opt-sdp-attention --enable-insecure-extension-access --skip-version-check --listen --port 3000 "+auth+" "+mdlpth
|
| 403 |
|
| 404 |
return configf
|
| 405 |
|
| 406 |
|
|
|
|
| 407 |
|
| 408 |
+
def save(Huggingface_Write_token):
|
| 409 |
+
|
| 410 |
from slugify import slugify
|
| 411 |
from huggingface_hub import HfApi, CommitOperationAdd, create_repo
|
| 412 |
+
|
| 413 |
+
if Huggingface_Write_token=="":
|
| 414 |
+
print('[1;31mA huggingface write token is required')
|
| 415 |
+
|
| 416 |
else:
|
| 417 |
+
os.chdir('/workspace')
|
| 418 |
+
|
| 419 |
+
if os.path.exists('sd'):
|
| 420 |
+
|
| 421 |
+
call('tar --exclude="stable-diffusion-webui/models/*/*" --exclude="sd-webui-controlnet/models/*" --zstd -cf sd_backup_rnpd.tar.zst sd', shell=True)
|
|
|
|
|
|
|
|
|
|
| 422 |
api = HfApi()
|
| 423 |
username = api.whoami(token=Huggingface_Write_token)["name"]
|
| 424 |
|
| 425 |
repo_id = f"{username}/{slugify('fast-stable-diffusion')}"
|
| 426 |
|
| 427 |
+
print("[1;32mBacking up...")
|
| 428 |
+
|
| 429 |
+
operations = [CommitOperationAdd(path_in_repo="sd_backup_rnpd.tar.zst", path_or_fileobj="/workspace/sd_backup_rnpd.tar.zst")]
|
| 430 |
+
|
| 431 |
+
create_repo(repo_id,private=True, token=Huggingface_Write_token, exist_ok=True, repo_type="dataset")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
api.create_commit(
|
| 434 |
+
repo_id=repo_id,
|
| 435 |
+
repo_type="dataset",
|
| 436 |
+
operations=operations,
|
| 437 |
+
commit_message="SD folder Backup",
|
| 438 |
+
token=Huggingface_Write_token
|
| 439 |
)
|
| 440 |
|
| 441 |
+
call('rm sd_backup_rnpd.tar.zst', shell=True)
|
| 442 |
clear_output()
|
| 443 |
|
| 444 |
done()
|
| 445 |
+
|
| 446 |
else:
|
| 447 |
+
print('[1;33mNothing to backup')
|
| 448 |
+
|
| 449 |
+
|
| 450 |
|
| 451 |
|
| 452 |
def getsrc(url):
|
| 453 |
|
| 454 |
parsed_url = urlparse(url)
|
| 455 |
+
|
| 456 |
+
if parsed_url.netloc == 'civitai.com':
|
| 457 |
+
src='civitai'
|
| 458 |
+
elif parsed_url.netloc == 'drive.google.com':
|
| 459 |
+
src='gdrive'
|
| 460 |
+
elif parsed_url.netloc == 'huggingface.co':
|
| 461 |
+
src='huggingface'
|
| 462 |
else:
|
| 463 |
+
src='others'
|
| 464 |
return src
|
| 465 |
|
| 466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
def get_name(url, gdrive):
|
| 469 |
|
|
|
|
| 478 |
disp_val = quer["response-content-disposition"][0].split(";")
|
| 479 |
for vals in disp_val:
|
| 480 |
if vals.strip().startswith("filename="):
|
| 481 |
+
filenm=unquote(vals.split("=", 1)[1].strip())
|
| 482 |
+
return filenm.replace("\"","")
|
| 483 |
else:
|
| 484 |
+
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"}
|
| 485 |
+
lnk="https://drive.google.com/uc?id={id}&export=download".format(id=url[url.find("/d/")+3:url.find("/view")])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
res = requests.session().get(lnk, headers=headers, stream=True, verify=True)
|
| 487 |
+
res = requests.session().get(get_url_from_gdrive_confirmation(res.text), headers=headers, stream=True, verify=True)
|
| 488 |
+
content_disposition = six.moves.urllib_parse.unquote(res.headers["Content-Disposition"])
|
| 489 |
+
filenm = re.search('attachment; filename="(.*?)"', content_disposition).groups()[0]
|
| 490 |
+
return filenm
|
| 491 |
+
|
| 492 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
|
| 495 |
def done():
|
| 496 |
done = widgets.Button(
|
| 497 |
+
description='Done!',
|
| 498 |
disabled=True,
|
| 499 |
+
button_style='success',
|
| 500 |
+
tooltip='',
|
| 501 |
+
icon='check'
|
| 502 |
)
|
| 503 |
+
display(done)
|