File size: 33,170 Bytes
f95b529
 
 
 
 
 
 
 
 
 
 
 
c5578ec
f95b529
 
 
9d08232
083660e
abac7d4
f95b529
 
 
 
9d08232
 
 
f95b529
9d08232
 
 
f95b529
9d08232
 
 
 
 
f95b529
9d08232
f95b529
9d08232
f95b529
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
9d08232
 
 
 
 
 
 
f95b529
 
 
 
 
 
 
 
 
 
9d08232
f95b529
 
 
 
 
 
9d08232
 
 
 
 
 
f95b529
 
 
 
 
 
 
 
 
 
 
 
9d08232
 
 
f95b529
9d08232
 
 
 
 
 
 
 
 
f95b529
 
 
 
 
 
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e73d616
f95b529
 
 
 
 
 
9d08232
 
 
 
 
 
a3030da
9d08232
 
a3030da
9d08232
 
 
 
a3030da
 
9d08232
a3030da
9d08232
a3030da
9d08232
a3030da
 
07f8044
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
9d08232
 
 
 
 
 
 
 
 
 
f95b529
 
 
9d08232
 
f95b529
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
9d08232
 
f95b529
9d08232
 
f95b529
 
9d08232
 
f95b529
 
 
9d08232
f95b529
 
9d08232
 
f95b529
c5578ec
9d08232
 
f95b529
9d08232
 
 
 
f95b529
9d08232
f95b529
9d08232
 
 
f95b529
9d08232
 
f95b529
9d08232
 
 
 
f95b529
9d08232
 
 
 
 
 
f95b529
9d08232
f95b529
9d08232
 
f95b529
9d08232
 
 
 
f95b529
9d08232
f95b529
9d08232
 
f95b529
9d08232
 
f95b529
 
c5578ec
9d08232
 
f95b529
9d08232
f95b529
9d08232
f95b529
9d08232
 
 
f95b529
9d08232
 
f95b529
9d08232
 
 
 
f95b529
9d08232
 
 
 
 
 
f95b529
9d08232
f95b529
9d08232
 
f95b529
9d08232
 
f95b529
9d08232
f95b529
9d08232
 
f95b529
9d08232
 
f95b529
e9537a0
9d08232
 
 
 
 
 
e9537a0
 
 
 
 
9d08232
 
e9537a0
9d08232
 
e9537a0
9d08232
 
 
 
 
1205ac7
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
76a1a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41211e8
76a1a21
 
07e3848
76a1a21
9d08232
 
 
 
 
 
 
76a1a21
9d08232
 
 
 
 
 
 
 
 
 
 
 
76a1a21
 
 
9d08232
76a1a21
 
 
 
 
 
9d08232
41211e8
9d08232
1205ac7
9d08232
 
 
 
1205ac7
9d08232
1205ac7
 
9d08232
f95b529
1205ac7
 
9d08232
 
 
 
 
1205ac7
9d08232
1205ac7
 
9d08232
 
 
1205ac7
 
 
 
 
9d08232
1205ac7
 
 
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
1205ac7
 
9d08232
 
f95b529
 
 
73a3bd8
 
 
 
 
 
 
 
 
 
f95b529
9d08232
 
f95b529
9d08232
 
 
 
f95b529
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3892aeb
f95b529
9d08232
 
 
 
 
bdcfb14
9d08232
 
 
 
 
 
 
 
f95b529
9d08232
f95b529
9d08232
f95b529
9d08232
 
 
 
 
 
f95b529
bdcfb14
9d08232
 
 
 
f95b529
9d08232
 
cc0733f
9d08232
 
 
 
f95b529
 
9d08232
 
f95b529
 
9d08232
 
f95b529
9d08232
 
 
 
 
 
f95b529
9d08232
 
 
 
 
f95b529
 
9d08232
 
 
 
f95b529
 
9d08232
f95b529
 
 
 
 
9d08232
f95b529
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
 
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
 
9d08232
f95b529
9d08232
dca07ad
9d08232
 
 
 
 
 
 
 
f95b529
 
 
 
 
9d08232
f95b529
 
9d08232
 
 
 
f95b529
9d08232
 
 
 
 
 
 
 
f95b529
 
 
 
 
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
 
9d08232
 
 
 
 
f95b529
 
9d08232
f95b529
 
 
 
9d08232
 
f95b529
 
 
 
 
9d08232
 
 
 
 
 
 
f95b529
9d08232
f95b529
 
9d08232
c5578ec
083660e
9d08232
 
083660e
 
 
9d08232
083660e
 
9d08232
 
f95b529
 
 
 
 
 
 
 
 
 
 
 
 
 
9d08232
 
f95b529
9d08232
 
 
 
 
 
f95b529
9d08232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95b529
 
 
 
9d08232
f95b529
9d08232
 
 
f95b529
9d08232
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
import os
from IPython.display import clear_output
from subprocess import call, getoutput, Popen, run
import time
import ipywidgets as widgets
import requests
import sys
import fileinput
from torch.hub import download_url_to_file
from urllib.parse import urlparse, parse_qs, unquote
import re
import six
import gdown

from urllib.request import urlopen, Request
import tempfile
from tqdm import tqdm
from bs4 import BeautifulSoup
import zipfile


def Deps(force_reinstall):

    if not force_reinstall and os.path.exists(
        "/usr/local/lib/python3.10/dist-packages/safetensors"
    ):
        ntbks()
        print("[1;32mModules and notebooks updated, dependencies already installed")
        os.environ["TORCH_HOME"] = "/workspace/cache/torch"
        os.environ["PYTHONWARNINGS"] = "ignore"
    else:
        call(
            "pip install --root-user-action=ignore --disable-pip-version-check --no-deps -qq gdown PyWavelets numpy==1.23.5 accelerate==0.12.0 --force-reinstall",
            shell=True,
            stdout=open("/dev/null", "w"),
        )
        ntbks()
        if os.path.exists("deps"):
            call("rm -r deps", shell=True)
        if os.path.exists("diffusers"):
            call("rm -r diffusers", shell=True)
        call("mkdir deps", shell=True)
        if not os.path.exists("cache"):
            call("mkdir cache", shell=True)
        os.chdir("deps")
        dwn(
            "https://huggingface.co/TheLastBen/dependencies/resolve/main/rnpddeps-t2.tar.zst",
            "/workspace/deps/rnpddeps-t2.tar.zst",
            "Installing dependencies",
        )
        call(
            "tar -C / --zstd -xf rnpddeps-t2.tar.zst",
            shell=True,
            stdout=open("/dev/null", "w"),
        )
        call(
            "sed -i 's@~/.cache@/workspace/cache@' /usr/local/lib/python3.10/dist-packages/transformers/utils/hub.py",
            shell=True,
        )
        os.chdir("/workspace")
        call(
            "git clone --depth 1 -q --branch main https://github.com/TheLastBen/diffusers",
            shell=True,
            stdout=open("/dev/null", "w"),
        )
        call(
            "pip install --root-user-action=ignore --disable-pip-version-check -qq gradio==3.41.2",
            shell=True,
            stdout=open("/dev/null", "w"),
        )
        call("rm -r deps", shell=True)
        os.chdir("/workspace")
        os.environ["TORCH_HOME"] = "/workspace/cache/torch"
        os.environ["PYTHONWARNINGS"] = "ignore"
        call(
            "sed -i 's@text = _formatwarnmsg(msg)@text =\"\"@g' /usr/lib/python3.10/warnings.py",
            shell=True,
        )
        clear_output()

        done()


def dwn(url, dst, msg):
    file_size = None
    req = Request(url, headers={"User-Agent": "torch.hub"})
    u = urlopen(req)
    meta = u.info()
    if hasattr(meta, "getheaders"):
        content_length = meta.getheaders("Content-Length")
    else:
        content_length = meta.get_all("Content-Length")
    if content_length is not None and len(content_length) > 0:
        file_size = int(content_length[0])

    with tqdm(
        total=file_size,
        disable=False,
        mininterval=0.5,
        bar_format=msg + " |{bar:20}| {percentage:3.0f}%",
    ) as pbar:
        with open(dst, "wb") as f:
            while True:
                buffer = u.read(8192)
                if len(buffer) == 0:
                    break
                f.write(buffer)
                pbar.update(len(buffer))
            f.close()


def ntbks():

    os.chdir("/workspace")
    if not os.path.exists("Latest_Notebooks"):
        call("mkdir Latest_Notebooks", shell=True)
    else:
        call("rm -r Latest_Notebooks", shell=True)
        call("mkdir Latest_Notebooks", shell=True)
    os.chdir("/workspace/Latest_Notebooks")
    call(
        "wget -q -i https://huggingface.co/datasets/TheLastBen/RNPD/raw/main/Notebooks.txt",
        shell=True,
    )
    call("rm Notebooks.txt", shell=True)
    os.chdir("/workspace")


def repo(Huggingface_token_optional):

    from slugify import slugify
    from huggingface_hub import HfApi, CommitOperationAdd, create_repo

    os.chdir("/workspace")
    if Huggingface_token_optional != "":
        username = HfApi().whoami(Huggingface_token_optional)["name"]
        backup = f"https://huggingface.co/datasets/{username}/fast-stable-diffusion/resolve/main/sd_backup_rnpd.tar.zst"
        headers = {"Authorization": f"Bearer {Huggingface_token_optional}"}
        response = requests.head(backup, headers=headers)
        if response.status_code == 302:
            print("[1;33mRestoring the SD folder...")
            open("/workspace/sd_backup_rnpd.tar.zst", "wb").write(
                requests.get(backup, headers=headers).content
            )
            call("tar --zstd -xf sd_backup_rnpd.tar.zst", shell=True)
            call("rm sd_backup_rnpd.tar.zst", shell=True)
        else:
            print("[1;33mBackup not found, using a fresh/existing repo...")
            time.sleep(2)
            if not os.path.exists("/workspace/sd/stablediffusiond"):  # reset later
                call(
                    "wget -q -O sd_mrep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_mrep.tar.zst",
                    shell=True,
                )
                call("tar --zstd -xf sd_mrep.tar.zst", shell=True)
                call("rm sd_mrep.tar.zst", shell=True)
            os.chdir("/workspace/sd")
            if not os.path.exists("stable-diffusion-webui"):
                call(
                    "git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui",
                    shell=True,
                )

    else:
        print("[1;33mInstalling/Updating the repo...")
        os.chdir("/workspace")
        if not os.path.exists("/workspace/sd/stablediffusiond"):  # reset later
            call(
                "wget -q -O sd_mrep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_mrep.tar.zst",
                shell=True,
            )
            call("tar --zstd -xf sd_mrep.tar.zst", shell=True)
            call("rm sd_mrep.tar.zst", shell=True)

        os.chdir("/workspace/sd")
        if not os.path.exists("stable-diffusion-webui"):
            call(
                "git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui",
                shell=True,
            )

    os.chdir("/workspace/sd/stable-diffusion-webui/")
    call("git reset --hard", shell=True)
    print("[1;32m")
    call("git pull", shell=True)
    os.chdir("/workspace")
    clear_output()
    done()


def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK):

    import gdown

    src = getsrc(MODEL_LINK)

    if not os.path.exists("/workspace/sd/stable-diffusion-webui/models/VAE"):
        call("mkdir -p /workspace/sd/stable-diffusion-webui/models/VAE", shell=True)

    # Получаем список файлов в директории /workspace/auto-VAE
    files = os.listdir("/workspace/auto-VAE")

    for file in files:
        source_path = os.path.join("/workspace/auto-VAE", file)
        target_path = os.path.join(
            "/workspace/sd/stable-diffusion-webui/models/VAE", file
        )
        # Проверяем, существует ли уже символическая ссылка
        if not os.path.exists(target_path):
            call(f"ln -s {source_path} {target_path}", shell=True)

    # if os.path.exists('/workspace/sd/stable-diffusion-webui/models/VAE'):
    #    call('ln -s /workspace/auto-VAE/* /workspace/sd/stable-diffusion-webui/models/VAE', shell=True)
    # else:
    #    call('mkdir -p /workspace/sd/stable-diffusion-webui/models/VAE', shell=True)
    #    call('ln -s /workspace/auto-VAE/* /workspace/sd/stable-diffusion-webui/models/VAE', shell=True)

    if not os.path.exists(
        "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv1-5.ckpt"
    ):
        call(
            "ln -s /workspace/auto-models/* /workspace/sd/stable-diffusion-webui/models/Stable-diffusion",
            shell=True,
        )

    if not os.path.exists("/workspace/sd/stable-diffusion-webui/models/Lora"):
        call("mkdir -p /workspace/sd/stable-diffusion-webui/models/Lora", shell=True)
        call(
            "ln -s /workspace/auto-lora/* /workspace/sd/stable-diffusion-webui/models/Lora",
            shell=True,
        )

    if Path_to_MODEL != "":
        if os.path.exists(str(Path_to_MODEL)):
            print("[1;32mUsing the custom model")
            model = Path_to_MODEL
        else:
            print("[1;31mWrong path, check that the path to the model is correct")

    elif MODEL_LINK != "":

        if src == "civitai":
            modelname = get_name(MODEL_LINK, False)
            model = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
            if not os.path.exists(model):
                dwn(MODEL_LINK, model, "Downloading the custom model")
                clear_output()
            else:
                print("[1;33mModel already exists")
        elif src == "gdrive":
            modelname = get_name(MODEL_LINK, True)
            model = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
            if not os.path.exists(model):
                gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
                clear_output()
            else:
                print("[1;33mModel already exists")
        else:
            modelname = os.path.basename(MODEL_LINK)
            model = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
            if not os.path.exists(model):
                gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
                clear_output()
            else:
                print("[1;33mModel already exists")

        if os.path.exists(model) and os.path.getsize(model) > 1810671599:
            print("[1;32mModel downloaded, using the custom model.")
        else:
            call(
                "rm " + model,
                shell=True,
                stdout=open("/dev/null", "w"),
                stderr=open("/dev/null", "w"),
            )
            print("[1;31mWrong link, check that the link is valid")

    else:
        if Original_Model_Version == "v1.5":
            model = "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv1-5.ckpt"
            print("[1;32mUsing the original V1.5 model")
        elif Original_Model_Version == "v2-512":
            model = "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt"
            if not os.path.exists(
                "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt"
            ):
                print("[1;33mDownloading the V2-512 model...")
                call(
                    "gdown -O "
                    + model
                    + " https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-nonema-pruned.ckpt",
                    shell=True,
                )
                clear_output()
            print("[1;32mUsing the original V2-512 model")
        elif Original_Model_Version == "v2-768":
            model = "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-768.ckpt"
            print("[1;32mUsing the original V2-768 model")
        elif Original_Model_Version == "SDXL":
            model = "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/sd_xl_base_1.0.safetensors"
            print("[1;32mUsing the original SDXL model")

        else:
            model = "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion"
            print("[1;31mWrong model version, try again")
    try:
        model
    except:
        model = "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion"

    return model


def modeldwn(model_LINK):

    if model_LINK == "":
        print("[1;33mNothing to do")
    else:
        os.makedirs(
            "/workspace/sd/stable-diffusion-webui/models/Stable-diffusion",
            exist_ok=True,
        )

        src = getsrc(model_LINK)

        if src == "civitai":
            modelname = get_name(model_LINK, False)
            loramodel = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
            if not os.path.exists(loramodel):
                dwn(model_LINK, loramodel, "Downloading the LoRA model")
                clear_output()
            else:
                print("[1;33mModel already exists")
        elif src == "gdrive":
            modelname = get_true_name(model_LINK)
            loramodel = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
            if not os.path.exists(loramodel):
                gdown.download(
                    url=model_LINK.replace("/file/d/", "/uc?id=").replace("/view", ""),
                    output=loramodel,
                    quiet=False,
                )
                clear_output()
            else:
                print("[1;33mModel already exists")
        else:
            modelname = os.path.basename(model_LINK)
            loramodel = f"/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}"
            if not os.path.exists(loramodel):
                gdown.download(
                    url=model_LINK, output=loramodel, quiet=False, fuzzy=True
                )
                clear_output()
            else:
                print("[1;33mModel already exists")

        if os.path.exists(loramodel):
            print("[1;32mCheckpoints downloaded")
        else:
            print("[1;31mWrong link, check that the link is valid")


def loradwn(LoRA_LINK):

    if LoRA_LINK == "":
        print("[1;33mNothing to do")
    else:
        os.makedirs("/workspace/sd/stable-diffusion-webui/models/Lora", exist_ok=True)

        src = getsrc(LoRA_LINK)

        if src == "civitai":
            modelname = get_name(LoRA_LINK, False)
            loramodel = f"/workspace/sd/stable-diffusion-webui/models/Lora/{modelname}"
            if not os.path.exists(loramodel):
                dwn(LoRA_LINK, loramodel, "Downloading the LoRA model")
                clear_output()
            else:
                print("[1;33mModel already exists")
        elif src == "gdrive":
            modelname = get_true_name(LoRA_LINK)
            loramodel = f"/workspace/sd/stable-diffusion-webui/models/Lora/{modelname}"
            if not os.path.exists(loramodel):
                gdown.download(
                    url=LoRA_LINK.replace("/file/d/", "/uc?id=").replace("/view", ""),
                    output=loramodel,
                    quiet=False,
                )
                clear_output()
            else:
                print("[1;33mModel already exists")
        else:
            modelname = os.path.basename(LoRA_LINK)
            loramodel = f"/workspace/sd/stable-diffusion-webui/models/Lora/{modelname}"
            if not os.path.exists(loramodel):
                gdown.download(url=LoRA_LINK, output=loramodel, quiet=False, fuzzy=True)
                clear_output()
            else:
                print("[1;33mModel already exists")

        if os.path.exists(loramodel):
            print("[1;32mLoRA downloaded")
        else:
            print("[1;31mWrong link, check that the link is valid")


def download_and_install_config():
    target_directory = "/workspace/sd/stable-diffusion-webui/"
    config_url = (
        "https://huggingface.co/spaces/stlaurentjr/RNPD/raw/main/config/config.json"
    )
    config_file_path = os.path.join(target_directory, "config.json")

    # Check if the config file already exists
    if not os.path.exists(config_file_path):
        # Change the directory
        os.chdir(target_directory)
        # Download the file using curl command
        call(f"curl -o config.json {config_url}", shell=True)
        print(f"Config file downloaded successfully.")
    else:
        print("Config file already exists, download skipped.")


def InstallDeforum():
    # Переход в директорию расширений
    os.chdir("/workspace/sd/stable-diffusion-webui/extensions")

    # Проверка наличия директории с расширением
    if not os.path.exists("deforum-for-automatic1111-webui"):
        # Клонирование репозитория, если он не существует
        call(
            "git clone https://github.com/deforum-art/sd-webui-deforum.git", shell=True
        )
        # Возврат в корневую директорию
        os.chdir("/workspace")
    else:
        # Если директория существует, переходим в нее
        # os.chdir('deforum-for-automatic1111-webui')
        # Сброс изменений и обновление репозитория
        # call('git reset --hard', shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
        # call('git pull', shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
        # Возврат в корневую директорию
        os.chdir("/workspace")
        
def InstallReActor():
    # Переход в директорию расширений
    os.chdir("/workspace/sd/stable-diffusion-webui/extensions")
    # Проверка наличия директории с расширением
    if not os.path.exists("sd-webui-reactor"):
        # Установка библиотек
        from subprocess import call, DEVNULL
        call(
            'pip install "onnxruntime-gpu>=1.16.1"', 
             shell=True
        )
        # Клонирование репозитория, если он не существует
        call(
            "git clone https://github.com/Gourieff/sd-webui-reactor.git",
            shell=True,
        )
        # Создание папки под модели
        call(
            "mkdir /workspace/sd/stable-diffusion-webui/models/insightface",
            shell=True
        )
        os.chdir("/workspace/sd/stable-diffusion-webui/models/insightface")
        # Скачивание модели для reActor
        call(
            "wget https://github.com/facefusion/facefusion-assets/releases/download/models/inswapper_128.onnx", 
            shell=True, 
            stdout=DEVNULL, 
            stderr=DEVNULL
        )
        # Вовзрат в корневую директорию
        clear_output()
        os.chdir("/workspace")
    else:
        #Возврат в корневую директорию
        os.chdir("/workspace")

def InstallAnimateDiff():
    # Переход в директорию расширений
    os.chdir("/workspace/sd/stable-diffusion-webui/extensions")

    # Проверка наличия директории с расширением
    if not os.path.exists("sd-webui-animatediff"):
        from subprocess import call, DEVNULL
        # Клонирование репозитория, если он не существует
        call(
            "git clone https://github.com/continue-revolution/sd-webui-animatediff.git",
            shell=True,
        )
        # Переход в папку с моделями
        os.chdir(
            "/workspace/sd/stable-diffusion-webui/extensions/sd-webui-animatediff/model"
        )
        # Скачивание моделей для animatediff
        call(
            "wget https://huggingface.co/guoyww/animatediff/resolve/refs%2Fpr%2F3/mm_sd_v15_v2.ckpt",
            shell=True, 
            stdout=DEVNULL, 
            stderr=DEVNULL
        )
      #  call(
      #      "wget https://huggingface.co/manshoety/AD_Stabilized_Motion/resolve/main/mm-Stabilized_high.pth",
      #      shell=True, 
      #      stdout=DEVNULL, 
      #      stderr=DEVNULL
      #  )
        # Вовзрат в корневую директорию
        clear_output()
        os.chdir("/workspace")
    else:
        # Возврат в корневую директорию
        os.chdir("/workspace")

def InstallWildCards():
    # Путь к целевой директории
    target_dir = "/workspace/sd/stable-diffusion-webui/extensions/stable-diffusion-webui-wildcards/wildcards/"

    # Перемещаемся в директорию расширений
    os.chdir("/workspace/sd/stable-diffusion-webui/extensions")

    if not os.path.exists("stable-diffusion-webui-wildcards"):
        # Клонирование репозитория, если он ещё не существует
        call(
            "git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-wildcards.git",
            shell=True,
        )

        # Перемещаемся в клонированный репозиторий
        os.chdir("stable-diffusion-webui-wildcards")

        # Скачиваем файл по ID
        gdown.download(
            id="1sY9Yv29cCYZuxBvszkmLVgw--aRdwT1P", output="wildcards.zip", quiet=False
        )

        # Создаем целевую директорию, если она ещё не существует
        os.makedirs(target_dir, exist_ok=True)

        # Распаковываем архив
        with zipfile.ZipFile("wildcards.zip", "r") as zip_ref:
            zip_ref.extractall(target_dir)
    else:
        # Если репозиторий уже существует, обновляем его
        os.chdir("stable-diffusion-webui-wildcards")
        call(
            "git reset --hard",
            shell=True,
            stdout=open(os.devnull, "w"),
            stderr=open(os.devnull, "w"),
        )
        call(
            "git pull",
            shell=True,
            stdout=open(os.devnull, "w"),
            stderr=open(os.devnull, "w"),
        )

    # Возвращаемся в исходную рабочую директорию
    os.chdir("/workspace")


def CNet(ControlNet_Model, ControlNet_XL_Model):
    def download(url, model_dir):
        try:
            filename = os.path.basename(urlparse(url).path)
            pth = os.path.abspath(os.path.join(model_dir, filename))
            if not os.path.exists(pth):
                print("Downloading: " + os.path.basename(url))
                download_url_to_file(url, pth, hash_prefix=None, progress=True)
            else:
                print(f"[1;32mThe model {filename} already exists[0m")
        except Exception as e:
            print(f"Ошибка при скачивании {url}: {e}")

    wrngv1 = False
    os.chdir("/workspace/sd/stable-diffusion-webui/extensions")
    if not os.path.exists("sd-webui-controlnet"):
        call(
            "git clone https://github.com/Mikubill/sd-webui-controlnet.git", shell=True
        )
        os.chdir("/workspace")
    else:
        os.chdir("sd-webui-controlnet")
        call(
            "git reset --hard",
            shell=True,
            stdout=open("/dev/null", "w"),
            stderr=open("/dev/null", "w"),
        )
        call(
            "git pull",
            shell=True,
            stdout=open("/dev/null", "w"),
            stderr=open("/dev/null", "w"),
        )
        os.chdir("/workspace")

    mdldir = (
        "/workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models"
    )
    call(
        "ln -s /workspace/auto-controlnet-models/* /workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models",
        shell=True,
    )

    for filename in os.listdir(mdldir):
        if "_sd14v1" in filename:
            renamed = re.sub("_sd14v1", "-fp16", filename)
            os.rename(os.path.join(mdldir, filename), os.path.join(mdldir, renamed))

    call(
        "wget -q -O CN_models.txt https://huggingface.co/spaces/stlaurentjr/RNPD/raw/main/config/CN_models.txt",
        shell=True,
    )
    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,
    )

    with open("CN_models.txt", "r") as f:
        mdllnk = f.read().splitlines()
    with open("CN_models_XL.txt", "r") as d:
        mdllnk_XL = d.read().splitlines()
    call("rm CN_models.txt CN_models_XL.txt", shell=True)

    os.chdir("/workspace")

    if ControlNet_Model == "All" or ControlNet_Model == "all":
        for lnk in mdllnk:
            download(lnk, mdldir)
        clear_output()

    elif ControlNet_Model == "17":
        mdllnk = list(filter(lambda x: "t2i" in x, mdllnk))
        for lnk in mdllnk:
            download(lnk, mdldir)
        clear_output()

    elif (
        ControlNet_Model.isdigit()
        and int(ControlNet_Model) - 1 < 16
        and int(ControlNet_Model) > 0
    ):
        download(mdllnk[int(ControlNet_Model) - 1], mdldir)
        clear_output()

    elif ControlNet_Model == "none":
        pass
        clear_output()

    else:
        print("[1;31mWrong ControlNet V1 choice, try again")
        wrngv1 = True

    if ControlNet_XL_Model == "All" or ControlNet_XL_Model == "all":
        for lnk_XL in mdllnk_XL:
            download(lnk_XL, mdldir)
        if not wrngv1:
            clear_output()
        done()

    elif ControlNet_XL_Model.isdigit() and int(ControlNet_XL_Model) - 1 < 5:
        download(mdllnk_XL[int(ControlNet_XL_Model) - 1], mdldir)
        if not wrngv1:
            clear_output()
        done()

    elif ControlNet_XL_Model == "none":
        pass
        if not wrngv1:
            clear_output()
        done()

    else:
        print("[1;31mWrong ControlNet XL choice, try again")


def sd(User, Password, model):

    import gradio

    gradio.close_all()

    auth = f"--gradio-auth {User}:{Password}"
    if User == "" or Password == "":
        auth = ""

    call(
        "wget -q -O /usr/local/lib/python3.10/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py",
        shell=True,
    )

    os.chdir("/workspace/sd/stable-diffusion-webui/modules")

    call(
        "sed -i 's@possible_sd_paths =.*@possible_sd_paths = [\"/workspace/sd/stablediffusion\"]@' /workspace/sd/stable-diffusion-webui/modules/paths.py",
        shell=True,
    )
    call(
        "sed -i 's@\.\.\/@src/@g' /workspace/sd/stable-diffusion-webui/modules/paths.py",
        shell=True,
    )
    call(
        "sed -i 's@src\/generative-models@generative-models@g' /workspace/sd/stable-diffusion-webui/modules/paths.py",
        shell=True,
    )

    call(
        'sed -i \'s@\["sd_model_checkpoint"\]@\["sd_model_checkpoint", "sd_vae", "CLIP_stop_at_last_layers", "inpainting_mask_weight", "initial_noise_multiplier"\]@g\' /workspace/sd/stable-diffusion-webui/modules/shared.py',
        shell=True,
    )

    call(
        'sed -i \'s@\["sd_model_checkpoint"\]@\["sd_model_checkpoint", "sd_vae", "CLIP_stop_at_last_layers", "inpainting_mask_weight", "initial_noise_multiplier"\]@g\' /workspace/sd/stable-diffusion-webui/modules/shared.py',
        shell=True,
    )

    call(
        'sed -i \'s/"CLIP_stop_at_last_layers": 1/"CLIP_stop_at_last_layers": 2/g\' /workspace/sd/stable-diffusion-webui/config.json',
        shell=True,
    )

    call(
        "sed -i 's@print(\"No module.*@@' /workspace/sd/stablediffusion/ldm/modules/diffusionmodules/model.py",
        shell=True,
    )

    os.chdir("/workspace/sd/stable-diffusion-webui")
    clear_output()

    podid = os.environ.get("RUNPOD_POD_ID")
    localurl = f"{podid}-3001.proxy.runpod.net"

    for line in fileinput.input(
        "/usr/local/lib/python3.10/dist-packages/gradio/blocks.py", inplace=True
    ):
        if line.strip().startswith("self.server_name ="):
            line = f'            self.server_name = "{localurl}"\n'
        if line.strip().startswith('self.protocol = "https"'):
            line = '            self.protocol = "https"\n'
        if line.strip().startswith(
            'if self.local_url.startswith("https") or self.is_colab'
        ):
            line = ""
        if line.strip().startswith('else "http"'):
            line = ""
        sys.stdout.write(line)

    if model == "":
        mdlpth = ""
    else:
        if os.path.isfile(model):
            mdlpth = "--ckpt " + model
        else:
            mdlpth = "--ckpt-dir " + model
    vae_path = "--vae-path /workspace/sd/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors"
    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
        + " "
        + vae_path
    )

    return configf


def save(Huggingface_Write_token):

    from slugify import slugify
    from huggingface_hub import HfApi, CommitOperationAdd, create_repo

    if Huggingface_Write_token == "":
        print("[1;31mA huggingface write token is required")

    else:
        os.chdir("/workspace")

        if os.path.exists("sd"):

            call(
                'tar --exclude="stable-diffusion-webui/models/*/*" --exclude="sd-webui-controlnet/models/*" --zstd -cf sd_backup_rnpd.tar.zst sd',
                shell=True,
            )
            api = HfApi()
            username = api.whoami(token=Huggingface_Write_token)["name"]

            repo_id = f"{username}/{slugify('fast-stable-diffusion')}"

            print("[1;32mBacking up...")

            operations = [
                CommitOperationAdd(
                    path_in_repo="sd_backup_rnpd.tar.zst",
                    path_or_fileobj="/workspace/sd_backup_rnpd.tar.zst",
                )
            ]

            create_repo(
                repo_id,
                private=True,
                token=Huggingface_Write_token,
                exist_ok=True,
                repo_type="dataset",
            )

            api.create_commit(
                repo_id=repo_id,
                repo_type="dataset",
                operations=operations,
                commit_message="SD folder Backup",
                token=Huggingface_Write_token,
            )

            call("rm sd_backup_rnpd.tar.zst", shell=True)
            clear_output()

            done()

        else:
            print("[1;33mNothing to backup")


def getsrc(url):

    parsed_url = urlparse(url)

    if parsed_url.netloc == "civitai.com":
        src = "civitai"
    elif parsed_url.netloc == "drive.google.com":
        src = "gdrive"
    elif parsed_url.netloc == "huggingface.co":
        src = "huggingface"
    else:
        src = "others"
    return src


def get_true_name(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    title_tag = soup.find("title")
    if title_tag:
        title_text = title_tag.text
        # Извлечение имени файла из тега title (предполагая, что имя файла указано в теге title)
        file_name = title_text.split(" - ")[0]
        return file_name
    else:
        raise RuntimeError("Could not find the title tag in the HTML")


def get_name(url, gdrive):

    from gdown.download import get_url_from_gdrive_confirmation

    if not gdrive:
        response = requests.get(url, allow_redirects=False)
        if "Location" in response.headers:
            redirected_url = response.headers["Location"]
            quer = parse_qs(urlparse(redirected_url).query)
            if "response-content-disposition" in quer:
                disp_val = quer["response-content-disposition"][0].split(";")
                for vals in disp_val:
                    if vals.strip().startswith("filename="):
                        filenm = unquote(vals.split("=", 1)[1].strip())
                        return filenm.replace('"', "")
    else:
        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"
        }
        lnk = "https://drive.google.com/uc?id={id}&export=download".format(
            id=url[url.find("/d/") + 3 : url.find("/view")]
        )
        res = requests.session().get(lnk, headers=headers, stream=True, verify=True)
        res = requests.session().get(
            get_url_from_gdrive_confirmation(res.text),
            headers=headers,
            stream=True,
            verify=True,
        )
        content_disposition = six.moves.urllib_parse.unquote(
            res.headers["Content-Disposition"]
        )
        filenm = (
            re.search(r"filename\*=UTF-8''(.*)", content_disposition)
            .groups()[0]
            .replace(os.path.sep, "_")
        )
        return filenm


def done():
    done = widgets.Button(
        description="Done!",
        disabled=True,
        button_style="success",
        tooltip="",
        icon="check",
    )
    display(done)