File size: 28,504 Bytes
d335a9c
 
a3d898e
 
 
 
d335a9c
a3d898e
 
 
 
 
 
 
 
 
 
fdc7ba2
a3d898e
626caba
a3d898e
 
 
 
 
 
fdc7ba2
a3d898e
fdc7ba2
a3d898e
 
 
 
938adde
 
 
 
 
 
 
 
 
 
a3d898e
 
 
 
ea78eb4
 
626caba
a3d898e
01898cf
a3d898e
458d020
10cbe5d
a3d898e
 
 
 
 
e35929b
 
 
938adde
e35929b
14c3887
e35929b
938adde
 
a3d898e
0232a5d
a3d898e
ad85866
a3d898e
 
 
 
 
7228f88
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dbaba7
d335a9c
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65431eb
 
f5c6bae
65431eb
 
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7228f88
 
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c3887
 
 
 
a3d898e
ae1df0b
 
 
 
a3d898e
 
 
 
 
4b1bf55
 
6014396
4b1bf55
6014396
 
 
65431eb
 
 
c5da664
a639f03
6014396
 
 
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc270c8
ccb5633
 
a3d898e
d567df4
 
6255f4e
d567df4
a3d898e
 
 
d567df4
a3d898e
 
 
d567df4
a3d898e
 
d567df4
a3d898e
 
 
d567df4
 
 
 
 
a3d898e
 
 
d567df4
a3d898e
 
 
 
 
ccb5633
a3d898e
ccb5633
 
 
 
 
 
 
 
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af89335
 
 
 
 
6d0fd71
 
af89335
 
a3d898e
af89335
83e7fa0
a3d898e
95a9ce2
a3d898e
 
 
bc4d620
 
a3d898e
 
 
 
 
 
 
 
af89335
a3d898e
6d0fd71
a3d898e
 
 
 
 
 
3d3c758
 
 
a3d898e
5e0a2f8
 
 
 
 
a3d898e
 
 
 
 
af89335
 
 
 
a3d898e
 
 
 
 
265fefa
a3d898e
265fefa
 
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc4d620
 
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc4d620
 
 
 
d335a9c
 
 
 
 
 
 
 
 
 
ccb5633
d335a9c
bc4d620
 
420c949
bc4d620
420c949
bc4d620
af89335
bc4d620
d335a9c
 
 
bc4d620
 
 
 
 
 
 
 
 
 
 
d335a9c
af89335
 
a3d898e
 
af89335
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74481b2
a3d898e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c3887
d335a9c
 
 
f331469
 
d335a9c
 
 
 
 
 
 
 
 
14c3887
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
##~   DOWNLOADING CODE | BY: ANXETY   ~##

import os
import re
import time
import json
import requests
import subprocess
from datetime import timedelta
from subprocess import getoutput
from urllib.parse import unquote
from IPython.utils import capture
from IPython.display import clear_output


#  ================= DETECT ENV =================
def detect_environment():
    free_plan = (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / (1024. ** 3) <= 20)
    environments = {
        'COLAB_GPU': ('Google Colab', "/root" if free_plan else "/content"),
        'KAGGLE_URL_BASE': ('Kaggle', "/kaggle/working/content"),
        'SAGEMAKER_INTERNAL_IMAGE_URI': ('SageMaker Studio Lab', "/home/studio-lab-user/content")
    }

    for env_var, (environment, path) in environments.items():
        if env_var in os.environ:
            return environment, path, free_plan

env, root_path, free_plan = detect_environment()
webui_path = f"{root_path}/sdw"
#  ----------------------------------------------


# === ONLY SAGEMAKER ===
if env == "SageMaker Studio Lab":
    print("Обновление зависимостей, может занять некоторое время...")
    get_ipython().system('pip install -q --upgrade torchsde')
    get_ipython().system('pip install -q --upgrade pip')
    get_ipython().system('pip install -q --upgrade psutil')

    clear_output()


# ================ LIBRARIES ================
flag_file = f"{root_path}/libraries_installed.txt"

if not os.path.exists(flag_file):
    # automatic1111 update webui to 1.8.0
    xformers = "xformers==0.0.23.post1 triton==2.1.0"
    torch = "torch==2.1.2+cu121 torchvision==0.16.2+cu121 torchaudio==2.1.2 --extra-index-url https://download.pytorch.org/whl/cu121"

    print("💿 Установка библиотек, это займет какое-то время...", end='')
    with capture.capture_output() as cap:
        get_ipython().system('pip install -U gdown')
        get_ipython().system('apt-get update && apt -y install aria2')
        get_ipython().system('npm install -g localtunnel &> /dev/null')
        get_ipython().system('curl -s -OL https://github.com/DEX-1101/sd-webui-notebook/raw/main/res/new_tunnel --output-dir {root_path}')
        get_ipython().system('curl -s -Lo /usr/bin/cl https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 && chmod +x /usr/bin/cl')
        get_ipython().system('pip install insightface')

        if env == "SageMaker Studio Lab":
            get_ipython().run_line_magic('pip', 'install -q opencv-python-headless huggingface-hub')
            get_ipython().run_line_magic('conda', 'update -q -n base conda')
            get_ipython().run_line_magic('conda', 'install -q -y aria2')
            get_ipython().run_line_magic('conda', 'install -q -y glib')
            get_ipython().system('pip install tensorflow')

            get_ipython().system('wget -P /home/studio-lab-user https://huggingface.co/NagisaNao/fast_repo/resolve/main/sagemaker/FULL_DELETED_NOTEBOOK.ipynb')

        if env == "Google Colab":
            get_ipython().system('pip install -q pip install xformers==0.0.26.dev778 -U')
        else:
            get_ipython().system('pip install -q {torch} -U')
            get_ipython().system('pip install -q {xformers} -U')

        with open(flag_file, "w") as f:
            f.write("hey ;3")
    del cap
    print("\r🍪 Библиотеки установлены!" + " "*35)
    time.sleep(2)
    clear_output()


# ================= loading settings V4 =================
def load_settings(path):
    if os.path.exists(path):
        with open(path, 'r') as file:
            return json.load(file)
    return {}

settings = load_settings(f'{root_path}/settings.json')

variables = [
    'Model', 'Model_Num', 'Inpainting_Model',
    'Vae', 'Vae_Num',
    'latest_webui', 'latest_exstensions', 'detailed_download',
    'controlnet', 'controlnet_Num', 'commit_hash', 'optional_huggingface_token',
    'ngrok_token', 'commandline_arguments',
    'Model_url', 'Vae_url', 'LoRA_url', 'Embedding_url', 'Extensions_url', 'custom_file_urls'
]

locals().update({key: settings.get(key) for key in variables})


# ================= OTHER =================
try:
    start_colab
except:
    start_colab = int(time.time())-5

# CONFIG DIR
models_dir = f"{webui_path}/models/Stable-diffusion"
vaes_dir = f"{webui_path}/models/VAE"
embeddings_dir = f"{webui_path}/embeddings"
loras_dir = f"{webui_path}/models/Lora"
extensions_dir = f"{webui_path}/extensions"
control_dir = f"{webui_path}/models/ControlNet"


# ================= MAIN CODE =================
# --- Obsolescence warning ---
if env == "SageMaker Studio Lab":
    print("Вы используете среду 'SageMaker' - эта среда устарела, поэтому многие ошибки не будут исправлены, а ее функциональность будет урезана. Для экономии памяти и/или во избежание ошибок.\n\n")


if not os.path.exists(webui_path):
    start_install = int(time.time())
    print("⌚ Распоковка Stable Diffusion...", end='')
    with capture.capture_output() as cap:
        get_ipython().system('aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/NagisaNao/fast_repo/resolve/main/FULL_REPO.zip -o repo.zip')
        get_ipython().system('unzip -q -o repo.zip -d {webui_path}')
        get_ipython().system('rm -rf repo.zip')

        get_ipython().run_line_magic('cd', '{root_path}')
        os.environ["SAFETENSORS_FAST_GPU"]='1'
        os.environ["CUDA_MODULE_LOADING"]="LAZY"
        os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
        os.environ["PYTHONWARNINGS"] = "ignore"

        get_ipython().system('echo -n {start_colab} > {webui_path}/static/colabTimer.txt')
    del cap
    install_time = timedelta(seconds=time.time()-start_install)
    print("\r🚀 Распаковка Завершена! За","%02d:%02d:%02d ⚡\n" % (install_time.seconds / 3600, (install_time.seconds / 60) % 60, install_time.seconds % 60), end='', flush=True)
else:
    if env == "SageMaker Studio Lab":
        get_ipython().system('echo -n {start_colab} > {webui_path}/static/colabTimer.txt')
    print("🚀 Все распакованно... Пропуск. ⚡")
    start_colab = float(open(f'{webui_path}/static/colabTimer.txt', 'r').read())
    time_since_start = str(timedelta(seconds=time.time()-start_colab)).split('.')[0]
    print(f"⌚️ Вы проводите эту сессию в течение - \033[33m{time_since_start}\033[0m")


## Changes extensions and WebUi
if latest_webui or latest_exstensions:
    action = "Обновление WebUI и Расширений" if latest_webui and latest_exstensions else ("Обновление WebUI" if latest_webui else "Обновление Расширений")
    print(f"⌚️ {action}...", end='', flush=True)
    with capture.capture_output() as cap:
        get_ipython().system('git config --global user.email "[email protected]"')
        get_ipython().system('git config --global user.name "Your Name"')

        ## Update Webui
        if latest_webui:
            get_ipython().run_line_magic('cd', '{webui_path}')
            get_ipython().system('git restore .')
            get_ipython().system('git pull -X theirs --rebase --autostash')

        ## Update extensions
        if latest_exstensions:
            if env != "SageMaker Studio Lab":
                get_ipython().system('{\'for dir in \' + webui_path + \'/extensions/*/; do cd \\"$dir\\" && git reset --hard && git pull; done\'}')
            else:
                get_ipython().system('{\'for dir in /home/studio-lab-user/content/sdw/extensions/*/; do cd \\"$dir\\" && git fetch origin && git pull; done\'}')

        # My Chinese friend, you broke the images again in the latest update... >W<'
        get_ipython().run_line_magic('cd', '{webui_path}/extensions/Encrypt-Image')
        get_ipython().system('git reset --hard 376358d8854472b9ea50e9fc8800367d1ca51137 # stable commit :3')

        get_ipython().system('cd {webui_path}/repositories/stable-diffusion-stability-ai && git restore .')
    del cap
    print(f"\r✨ {action} Завершено!")


# === FIXING ERRORS ===
# --- Encrypt-Image ---
get_ipython().system("sed -i '9,37d' {webui_path}/extensions/Encrypt-Image/javascript/encrypt_images_info.js")

# --- SageMaker ---
if env == "SageMaker Studio Lab":
    with capture.capture_output() as cap:
        # --- SuperMerger Remove ---
        if os.path.exists(f"{webui_path}/extensions/supermerger"):
            get_ipython().system('rm -rf {webui_path}/extensions/supermerger')
        # --- Launch (Style) ---
        get_ipython().system('wget -O {webui_path}/modules/styles.py https://huggingface.co/NagisaNao/fast_repo/resolve/main/sagemaker/fixing/webui/styles.py')
    del cap


## Version switching
if commit_hash:
    print('⏳ Активация машины времени...', end="", flush=True)
    with capture.capture_output() as cap:
        get_ipython().run_line_magic('cd', '{webui_path}')
        get_ipython().system('git config --global user.email "[email protected]"')
        get_ipython().system('git config --global user.name "Your Name"')
        get_ipython().system('git reset --hard {commit_hash}')
    del cap
    print(f"\r⌛️ Машина времени активированна! Текущий коммит: \033[34m{commit_hash}\033[0m")


## Downloading model and stuff | oh yeah~ I'm starting to misunderstand my own code ( almost my own ;3 )
print("📦 Скачивание моделей и прочего...", end='')
model_list = {
    "1.Anime (by XpucT) + INP": [
        {"url": "https://huggingface.co/XpucT/Anime/resolve/main/Anime_v2.safetensors", "name": "Anime_v2.safetensors"},
        {"url": "https://huggingface.co/XpucT/Anime/resolve/main/Anime_v2-inpainting.safetensors", "name": "Anime_v2-inpainting.safetensors"}
    ],
    "2.BluMix [Anime] [V7]": [
        {"url": "https://civitai.com/api/download/models/361779", "name": "BluMix_v7.safetensors"}
    ],
    "3.Cetus-Mix [Anime] [V4] + INP": [
        {"url": "https://civitai.com/api/download/models/130298", "name": "CetusMix_V4.safetensors"},
        {"url": "https://civitai.com/api/download/models/139882", "name": "CetusMix_V4-inpainting.safetensors"}
    ],
    "4.Counterfeit [Anime] [V3] + INP": [
        {"url": "https://civitai.com/api/download/models/125050", "name": "Counterfeit_V3.safetensors"},
        {"url": "https://civitai.com/api/download/models/137911", "name": "Counterfeit_V3-inpainting.safetensors"}
    ],
    "5.CuteColor [Anime] [V3]": [
        {"url": "https://civitai.com/api/download/models/138754", "name": "CuteColor_V3.safetensors"}
    ],
    "6.Dark-Sushi-Mix [Anime]": [
        {"url": "https://civitai.com/api/download/models/101640", "name": "DarkSushiMix_2_5D.safetensors"},
        {"url": "https://civitai.com/api/download/models/56071", "name": "DarkSushiMix_colorful.safetensors"}
    ],
    "7.Deliberate [Realism] [V6] + INP": [
        {"url": "https://huggingface.co/XpucT/Deliberate/resolve/main/Deliberate_v6.safetensors", "name": "Deliberate_v6.safetensors"},
        {"url": "https://huggingface.co/XpucT/Deliberate/resolve/main/Deliberate_v6-inpainting.safetensors", "name": "Deliberate_v6-inpainting.safetensors"}
    ],
    "8.Meina-Mix [Anime] [V11] + INP": [
        {"url": "https://civitai.com/api/download/models/119057", "name": "MeinaMix_V11.safetensors"},
        {"url": "https://civitai.com/api/download/models/120702", "name": "MeinaMix_V11-inpainting.safetensors"}
    ],
    "9.Mix-Pro [Anime] [V4] + INP": [
        {"url": "https://civitai.com/api/download/models/125668", "name": "MixPro_V4.safetensors"},
        {"url": "https://civitai.com/api/download/models/139878", "name": "MixPro_V4-inpainting.safetensors"}
    ]
}

# 1-4 (fp16/cleaned)
vae_list = {
    "1.Anime.vae": [
        {"url": "https://civitai.com/api/download/models/131654", "name": "Anime.vae.safetensors"},
        {"url": "https://civitai.com/api/download/models/131658", "name": "vae-ft-mse.vae.safetensors"}
    ],
    "2.Anything.vae": [{"url": "https://civitai.com/api/download/models/131656", "name": "Anything.vae.safetensors"}],
    "3.Blessed2.vae": [{"url": "https://civitai.com/api/download/models/142467", "name": "Blessed2.vae.safetensors"}],
    "4.ClearVae.vae": [{"url": "https://civitai.com/api/download/models/133362", "name": "ClearVae_23.vae.safetensors"}],
    "5.WD.vae": [{"url": "https://huggingface.co/NoCrypt/resources/resolve/main/VAE/wd.vae.safetensors", "name": "WD.vae.safetensors"}]
}

controlnet_list = {
    "1.canny": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny_fp16.safetensors", "name": "control_v11p_sd15_canny_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_canny_fp16.yaml", "name": "control_v11p_sd15_canny_fp16.yaml"}
    ],
    "2.openpose": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose_fp16.safetensors", "name": "control_v11p_sd15_openpose_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_openpose_fp16.yaml", "name": "control_v11p_sd15_openpose_fp16.yaml"}
    ],
    "3.depth": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth_fp16.safetensors", "name": "control_v11f1p_sd15_depth_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11f1p_sd15_depth_fp16.yaml", "name": "control_v11f1p_sd15_depth_fp16.yaml"},
        {"url": "https://huggingface.co/NagisaNao/models/resolve/main/ControlNet_v11/control_v11p_sd15_depth_anything_fp16.safetensors", "name": "control_v11p_sd15_depth_anything_fp16.safetensors"}
    ],
    "4.normal_map": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_normalbae_fp16.safetensors", "name": "control_v11p_sd15_normalbae_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_normalbae_fp16.yaml", "name": "control_v11p_sd15_normalbae_fp16.yaml"}
    ],
    "5.mlsd": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_mlsd_fp16.safetensors", "name": "control_v11p_sd15_mlsd_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_mlsd_fp16.yaml", "name": "control_v11p_sd15_mlsd_fp16.yaml"}
    ],
    "6.lineart": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart_fp16.safetensors", "name": "control_v11p_sd15_lineart_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15s2_lineart_anime_fp16.safetensors", "name": "control_v11p_sd15s2_lineart_anime_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_lineart_fp16.yaml", "name": "control_v11p_sd15_lineart_fp16.yaml"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15s2_lineart_anime_fp16.yaml", "name": "control_v11p_sd15s2_lineart_anime_fp16.yaml"}
    ],
    "7.soft_edge": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_softedge_fp16.safetensors", "name": "control_v11p_sd15_softedge_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_softedge_fp16.yaml", "name": "control_v11p_sd15_softedge_fp16.yaml"}
    ],
    "8.scribble": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_scribble_fp16.safetensors", "name": "control_v11p_sd15_scribble_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_scribble_fp16.yaml", "name": "control_v11p_sd15_scribble_fp16.yaml"}
    ],
    "9.segmentation": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_seg_fp16.safetensors", "name": "control_v11p_sd15_seg_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_seg_fp16.yaml", "name": "control_v11p_sd15_seg_fp16.yaml"}
    ],
    "10.shuffle": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle_fp16.safetensors", "name": "control_v11e_sd15_shuffle_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11e_sd15_shuffle_fp16.yaml", "name": "control_v11e_sd15_shuffle_fp16.yaml"}
    ],
    "11.tile": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile_fp16.safetensors", "name": "control_v11f1e_sd15_tile_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11f1e_sd15_tile_fp16.yaml", "name": "control_v11f1e_sd15_tile_fp16.yaml"}
    ],
    "12.inpaint": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_inpaint_fp16.safetensors", "name": "control_v11p_sd15_inpaint_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11p_sd15_inpaint_fp16.yaml", "name": "control_v11p_sd15_inpaint_fp16.yaml"}
    ],
    "13.instruct_p2p": [
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors", "name": "control_v11e_sd15_ip2p_fp16.safetensors"},
        {"url": "https://huggingface.co/ckpt/ControlNet-v1-1/raw/main/control_v11e_sd15_ip2p_fp16.yaml", "name": "control_v11e_sd15_ip2p_fp16.yaml"}
    ]
}

extension_repo = []
prefixes = {
    # It's complicated, but supplementing has become easier
    "model": [models_dir, "Model_url"],
    "vae": [vaes_dir, "Vae_url"],
    "lora": [loras_dir, "LoRA_url"],
    "embed": [embeddings_dir, "Embedding_url"],
    "extension": [extensions_dir, "Extensions_url"],
    "control": [control_dir, "ControlNet_url"]
}

get_ipython().system('mkdir -p {models_dir} {vaes_dir} {loras_dir} {embeddings_dir} {extensions_dir} {control_dir}')

url = ""
ControlNet_url = ""
hf_token = optional_huggingface_token if optional_huggingface_token else "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"
user_header = f"\"Authorization: Bearer {hf_token}\""

''' main download code '''

def handle_manual(url):
    original_url = url
    url = url.split(':', 1)[1]
    file_name = re.search(r'\[(.*?)\]', url)
    file_name = file_name.group(1) if file_name else None
    if file_name:
        url = re.sub(r'\[.*?\]', '', url)

    for prefix, (dir, _) in prefixes.items():
        if original_url.startswith(f"{prefix}:"):
            if prefix != "extension":
                manual_download(url, dir, file_name=file_name)
            else:
                extension_repo.append((url, file_name))

def manual_download(url, dst_dir, file_name):
    basename = url.split("/")[-1] if file_name is None else file_name
    header_option = f"--header={user_header}"

    print("\033[32m---"*45 + f"\n\033[33mURL: \033[34m{url}\n\033[33mSAVE DIR: \033[34m{dst_dir}\n\033[33mFILE NAME: \033[34m{file_name}\033[32m\n~~~\033[0m")

    # I do it at my own risk..... Fucking CivitAi >:(
    civitai_token = "62c0c5956b2f9defbd844d754000180b"
    if 'civitai' in url and civitai_token:
        url = f"{url}?token={civitai_token}"

    # -- GDrive --
    if 'drive.google' in url:
        if 'folders' in url:
            get_ipython().system('gdown --folder "{url}" -O {dst_dir} --fuzzy -c')
        else:
            if file_name:
               get_ipython().system('gdown "{url}" -O {dst_dir}/{file_name} --fuzzy -c')
            else:
               get_ipython().system('gdown "{url}" -O {dst_dir} --fuzzy -c')
    # -- Huggin Face --
    elif 'huggingface' in url:
        if '/blob/' in url:
            url = url.replace('/blob/', '/resolve/')
        if file_name:
            get_ipython().system('aria2c {header_option} --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -c -j5 -x16 -s16 -k1M -d {dst_dir} -o {basename} {url}')
        else:
            parsed_link = f'\n{url}\n\tout={unquote(url.split("/")[-1])}'
            get_ipython().system('echo -e "{parsed_link}" | aria2c {header_option} --console-log-level=error --summary-interval=10 -i- -j5 -x16 -s16 -k1M -c -d "{dst_dir}" -o {basename}')
    # -- Other --
    elif 'http' in url or 'magnet' in url:
        if file_name:
            get_ipython().system('aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -j5 -x16 -s16 -k1M -c -d {dst_dir} -o {file_name} {url}')
        else:
            parsed_link = '"{}"'.format(url)
            get_ipython().system('aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -j5 -x16 -s16 -k1M -c -d {dst_dir} -Z {parsed_link}')

def download(url):
    links_and_paths = url.split(',')

    for link_or_path in links_and_paths:
        link_or_path = link_or_path.strip()
        if not link_or_path:
            continue
        if any(link_or_path.startswith(prefix.lower()) for prefix in prefixes):
            handle_manual(link_or_path)
            continue

        url, dst_dir, file_name = link_or_path.split()
        manual_download(url, dst_dir, file_name)

''' submodels - added urls '''

submodels = []

def add_submodels(selection, num_selection, model_dict, dst_dir):
    if selection == "none":
        return []
    if selection == "ALL":
        all_models = []
        for models in model_dict.values():
            all_models.extend(models)
        selected_models = all_models
    else:
        selected_models = model_dict[selection]
        selected_nums = map(int, num_selection.replace(',', '').split())

        for num in selected_nums:
            if 1 <= num <= len(model_dict):
                name = list(model_dict)[num - 1]
                selected_models.extend(model_dict[name])

    unique_models = list({model['name']: model for model in selected_models}.values())

    for model in unique_models:
        model['dst_dir'] = dst_dir

    return unique_models

submodels += add_submodels(Model, Model_Num, model_list, models_dir)                                                   # model
submodels += add_submodels(Vae, Vae_Num, vae_list, vaes_dir)                                                           # vae
submodels += add_submodels(controlnet, "" if controlnet == "ALL" else controlnet_Num, controlnet_list, control_dir)    # controlnet

for submodel in submodels:
    if not Inpainting_Model and "inpainting" in submodel['name']:
        continue
    url += f"{submodel['url']} {submodel['dst_dir']} {submodel['name']}, "

''' file.txt - added urls '''

unique_urls = []

def process_file_download(file_url):
    if file_url.startswith("http"):
        if "blob" in file_url:
            file_url = file_url.replace("blob", "raw")
        response = requests.get(file_url)
        lines = response.text.split('\n')
    else:
        with open(file_url, 'r') as file:
            lines = file.readlines()

    current_tag = None
    for line in lines:
        if any(f'# {tag}' in line.lower() for tag in prefixes):
            current_tag = next((tag for tag in prefixes if tag in line.lower()))

        urls = [url.strip() for url in line.split(',')]
        for url in urls:
            if url.startswith("http") and url not in unique_urls:
                globals()[prefixes[current_tag][1]] += ", " + url
                unique_urls.append(url)

# fix all possible errors/options and function call
if custom_file_urls:
    for custom_file_url in custom_file_urls.replace(',', '').split():
        if not custom_file_url.endswith('.txt'):
            custom_file_url += '.txt'
        if not custom_file_url.startswith('http'):
            if not custom_file_url.startswith(root_path):
                custom_file_url = f'{root_path}/{custom_file_url}'

        try:
            process_file_download(custom_file_url)
        except FileNotFoundError:
            pass

# url prefixing
urls = [globals()[urls[1]] for urls in prefixes.values()]
for i, prefix in enumerate(prefixes):
    if urls[i]:
        prefixed_urls = [f"{prefix}:{url}" for url in urls[i].replace(',', '').split()]
        if prefixed_urls:
            url += ", ".join(prefixed_urls) + ", "

if detailed_download == "on":
    print("\n\n\033[33m# ====== Подробная Загрузка ====== #\n\033[0m")
    download(url)
    print("\n\033[33m# =============================== #\n\033[0m")
else:
    with capture.capture_output() as cap:
        download(url)
    del cap

print("\r🏁 Скачивание Завершено!" + " "*15)


# Cleaning shit after downloading...
get_ipython().system('find {webui_path} \\( -type d \\( -name ".ipynb_checkpoints" -o -name ".aria2" \\) -o -type f -name "*.aria2" \\) -exec rm -r {{}} \\; >/dev/null 2>&1')


## Install of Custom extensions
if len(extension_repo) > 0:
    print("✨ Устанвока кастомных расширений...", end='', flush=True)
    with capture.capture_output() as cap:
        for repo, repo_name in extension_repo:
            if not repo_name:
                repo_name = repo.split('/')[-1]
            get_ipython().system('cd {extensions_dir}                  && git clone {repo} {repo_name}                  && cd {repo_name}                  && git fetch')
    del cap
    print(f"\r📦 Установлено '{len(extension_repo)}', Кастомных расширений!")


## List Models and stuff
if detailed_download == "off":
    print("\n\n\033[33mЕсли вы не видете каких-то скаченных файлов, включите в виджетах функцию 'Подробная Загрузка'.")

if any(not file.endswith('.txt') for file in os.listdir(models_dir)):
    print("\n\033[33m➤ Models\033[0m")
    get_ipython().system("find {models_dir}/ -mindepth 1 ! -name '*.txt' -printf '%f\\n'")
if any(not file.endswith('.txt') for file in os.listdir(vaes_dir)):
    print("\n\033[33m➤ VAEs\033[0m")
    get_ipython().system("find {vaes_dir}/ -mindepth 1 ! -name '*.txt' -printf '%f\\n'")
if any(not file.endswith('.txt') and not os.path.isdir(os.path.join(embeddings_dir, file)) for file in os.listdir(embeddings_dir)):
    print("\n\033[33m➤ Embeddings\033[0m")
    get_ipython().system("find {embeddings_dir}/ -mindepth 1 -maxdepth 1 \\( -name '*.pt' -or -name '*.safetensors' \\) -printf '%f\\n'")
if any(not file.endswith('.txt') for file in os.listdir(loras_dir)):
    print("\n\033[33m➤ LoRAs\033[0m")
    get_ipython().system("find {loras_dir}/ -mindepth 1 ! -name '*.keep' -printf '%f\\n'")
print(f"\n\033[33m➤ Extensions\033[0m")
get_ipython().system("find {extensions_dir}/ -mindepth 1 -maxdepth 1 ! -name '*.txt' -printf '%f\\n'")
if any(not file.endswith(('.txt', '.yaml')) for file in os.listdir(control_dir)):
    print("\n\033[33m➤ ControlNet\033[0m")
    get_ipython().system("find {control_dir}/ -mindepth 1 ! -name '*.yaml' -printf '%f\\n' | sed 's/^[^_]*_[^_]*_[^_]*_\\(.*\\)_fp16\\.safetensors$/\\1/'")


# === OTHER ===
# Downlaod discord tags UmiWildcards
files_umi = [
    "https://huggingface.co/NagisaNao/fast_repo/resolve/main/extensions/UmiWildacrd/discord/200_pan_gen.txt",
    "https://huggingface.co/NagisaNao/fast_repo/resolve/main/extensions/UmiWildacrd/discord/150_bra_gen.txt"
]
save_dir_path = f"{webui_path}/extensions/Umi-AI-Wildcards/wildcards/discord"

with capture.capture_output() as cap:
    for file in files_umi:
        get_ipython().system('aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -j5 -x16 -s16 -k1M -c -d {save_dir_path} {file}')
del cap


# === ONLY SAGEMAKER ===
if env == "SageMaker Studio Lab":
    with capture.capture_output() as cap:
        get_ipython().system('rm -rf /home/studio-lab-user/.conda/envs/studiolab-safemode')
        get_ipython().system('rm -rf /home/studio-lab-user/.conda/envs/sagemaker-distribution')
        get_ipython().system('rm -rf /home/studio-lab-user/.conda/pkgs/cache')
        get_ipython().system('pip cache purge')
        get_ipython().system('rm -rf ~/.cache')
    del cap