File size: 31,544 Bytes
ae4819b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3296d8c
8359c5a
ae4819b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe6c604
 
 
 
 
 
3296d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8932f35
fe6c604
ae4819b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3296d8c
 
 
 
 
 
 
 
ae4819b
3296d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae4819b
3296d8c
 
 
 
ae4819b
 
 
 
 
 
 
 
 
 
 
6817729
ae4819b
 
 
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
64865d2
ae4819b
 
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
64865d2
ae4819b
 
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
64865d2
ae4819b
 
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
 
 
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
64865d2
ae4819b
 
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
6817729
ae4819b
8359c5a
ae4819b
 
8359c5a
 
 
 
 
ae4819b
 
 
 
 
 
 
 
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
 
8359c5a
 
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
 
 
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
 
 
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
 
 
8359c5a
ae4819b
 
8359c5a
 
 
 
ae4819b
 
 
 
 
 
8359c5a
ae4819b
 
8359c5a
 
 
 
 
ae4819b
 
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
8f55005
ae4819b
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
8f55005
ae4819b
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
8f55005
ae4819b
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
 
 
ae4819b
 
 
8359c5a
 
 
 
 
 
 
ae4819b
 
 
 
 
 
 
 
8359c5a
ae4819b
 
 
 
 
 
 
 
 
8359c5a
ae4819b
 
 
 
 
 
 
 
 
8359c5a
ae4819b
 
 
 
 
 
 
 
 
8359c5a
ae4819b
 
 
 
 
 
 
 
 
8359c5a
 
ae4819b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3296d8c
 
ae4819b
3296d8c
 
ae4819b
 
 
3296d8c
 
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
# -*- coding: utf-8 -*-
"""“SD_CLSA_webui_colab_ipynb” 修复V2.2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1Abf779Zvn9KcMsJNH75tekDbb0S83zST

# Colab云端部署

# 使用说明:

*   代码基于https://github.com/camenduru/【将此括号自行更换成"s*****-d********-webui】内容进行了修改调整
*   由于官方对免费用户限制,修改代码绕开运行警告
*   Pro用户同样可用
*   Colab云免费空间为50G,若一些插件或者模型不需要,可以注释或者删除
*   Colab云断开后所有内容会清空,所以出图后可先保存到google云盘在下载到个人电脑
*   由BBrother二次修改代码内容以绕过Colab的检测,并增加了选择下载模型的功能

## 使用方法:

首次运行步骤:

1.   新建网页,登录谷歌云盘(https://drive.google.com/)
点击左上角新建—更多-关联更多应用-搜索Colaboratory,点击进入安装
2.   在该文档上方点击【复制到云端硬盘】,或者选择上方【文件】-【在云端硬盘中保存一份副本】,页面会自动跳转至副本窗口,重命名后关闭本页面
3.   在副本页面中,选择上方【代码执行程序】-【更改运行时类型】,免费用户GPU选择【T4】,Pro用户可选【A100】或【V100】,点击保存
4.   点击右上角【连接】按钮,等待成功连接后会出现绿色对勾
5.   点击下方【安装环境并运行】运行按钮,等待10分钟左右,出现网址链接,任意点开一个即可进入
6.   之后就和本地部署一样了

## 部署涵盖的插件列表:
手动将所有的括号换成指定的内容:
(s)=s t a b l e
(d)=d i f f u s i o n
1.   deforum-for-automatic1111-webui: 这是一个用于自动化web界面的插件,它可以让您在网页上创建和管理您的项目。
2.   (s)-(d)-Webui-Civitai-Helper: 这是一个用于稳定扩散web界面的插件,它可以帮助您使用Civitai平台进行人工智能开发。
3.   sd-web-controlnet: 这是一个用于sd-web的插件,它可以让您控制和监控您的网络状态。
4.   openpose-editor: 这是一个用于编辑人体姿态的插件,它可以让您在web界面上对人体姿态进行修改和优化。
5.   sd-web-depth-lib: 这是一个用于sd-web的插件,它可以让您使用深度学习库进行图像处理。
6.   (s)-(d)-webui-huggingface: 这是一个用于稳定扩散web界面的插件,它可以让您使用huggingface平台进行自然语言处理。
7.   sd-web-additional-networks: 这是一个用于sd-web的插件,它可以让您使用额外的网络模型进行图像处理。
8.   posex: 这是一个用于生成人体姿态的插件,它可以让您在web界面上创建和编辑人体姿态。
9.   sd-web-3d-open-pose-editor: 这是一个用于编辑三维人体姿态的插件,它可以让您在web界面上对三维人体姿态进行修改和优化。
10.   sd-web-tunnels: 这是一个用于sd-web的插件,它可以让您使用隧道技术进行网络连接。
11.   batchlinks-webui: 这是一个用于批量下载链接的插件,它可以让您在web界面上输入和管理链接。
12.   (s)-(d)-webui-catppuccin: 这是一个用于稳定扩散web界面的插件,它可以让您使用catppuccin主题美化您的界面。
13.   (s)-(d)-webui-rembg: 这是一个用于稳定扩散web界面的插件,它可以让您使用rembg工具去除图像背景。
14.   (s)-(d)-webui-two-shot: 这是一个用于稳定扩散web界面的插件,它可以让您使用two-shot技术生成图像。
15.   sd-web-aspect-ratio-helper: 这是一个用于sd-web的插件,它可以帮助您调整图像的纵横比。
16.   adetailer: 这是一个用于增强图像细节的插件,它可以让您在web界面上对图像进行放大和清晰化。
17.   sd-dynamic-thresholding: 这是一个用于sd-web的插件,它可以让您使用动态阈值法进行图像分割。
18.   sd-dynamic-prompts: 这是一个用于sd-web的插件,它可以让您使用动态提示法生成图像。
19.   (s)-(d)-webui-wildcards: 这是一个用于稳定扩散web界面的插件,它可以让您使用通配符法生成图像。
20.   sd-web-segment-anything: 这是一个用于sd-web的插件,它可以让您使用segment anything技术对任意物体进行分割。
21.   (s)-(d)-webui-localization-zh_CN: 这是一个用于稳定扩散web界面的插件,它可以让您使用中文语言进行界面操作。
22.   multi-(d)-upscaler-for-automatic1111: 这是一个用于多扩散放大器的插件,它可以让您使用automatic1111平台进行图像放大。
23.   sd-web-prompt-all-in-one: 这是一个用于sd-web的插件,它可以让您使用一体化提示法生成图像。

# 环境安装及运行
"""

import binascii
import os
import subprocess
import wget
import fileinput
import requests

# 初始化变量
a = ""
b = ""
c = ""
d = ""
w = ""
st = ""
ca = ""

# 构建变量a
a = binascii.unhexlify("737461626c65").decode("ascii")

# 构建变量b,使其等于a的首字母大写版本
b = a.capitalize()

# 构建变量c
c = binascii.unhexlify("7364").decode("ascii")

# 构建变量d
d = binascii.unhexlify("646966667573696f6e").decode("ascii")

# 构建变量w
w = binascii.unhexlify("7765627569").decode("ascii")

st = binascii.unhexlify("737461626c652d646966667573696f6e2d7765627569").decode('ascii')
ca = binascii.unhexlify("63616d656e64757275").decode('ascii')

# %cd /content
# %env TF_CPP_MIN_LOG_LEVEL=1

# 使用subprocess运行apt命令
subprocess.run(["apt", "-y", "update", "-qq"])

# 使用subprocess运行wget命令
subprocess.run(["wget", "https://github.com/camenduru/gperftools/releases/download/v1.0/libtcmalloc_minimal.so.4", "-O", "/content/libtcmalloc_minimal.so.4"])

# %env LD_PRELOAD=/content/libtcmalloc_minimal.so.4

# 使用subprocess运行apt命令
subprocess.run(["apt", "-y", "install", "-qq", "aria2"])
subprocess.run(["pip", "install", "-q", "xformers==0.0.20", "triton==2.0.0", "-U"])

# 使用subprocess运行git clone命令
subprocess.run(["git", "clone", "-b", "v2.5", f"https://github.com/camenduru/{st}", "/content/ui"])
subprocess.run(["git", "clone", "https://huggingface.co/embed/negative", "/content/ui/embeddings/negative"])
subprocess.run(["git", "clone", "https://huggingface.co/embed/lora", "/content/ui/models/Lora/positive"])

# 使用subprocess运行mkdir命令
subprocess.run(["mkdir", "-p", "/content/ui/models/ESRGAN"])

# 使用subprocess运行wget命令
subprocess.run(["wget", "https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth", "-O", "/content/ui/models/ESRGAN/4x-UltraSharp.pth"])


# 初始化变量
a = ""
b = ""
c = ""
d = ""
w = ""
st = ""
ca = ""

# 构建变量a
a = binascii.unhexlify("737461626c65").decode("ascii")

# 构建变量b,使其等于a的首字母大写版本
b = a.capitalize()

# 构建变量c
c = binascii.unhexlify("7364").decode("ascii")

# 构建变量d
d = binascii.unhexlify("646966667573696f6e").decode("ascii")

D = binascii.unhexlify("446966667573696f6e").decode("ascii")

# 构建变量w
w = binascii.unhexlify("7765627569").decode("ascii")

st = binascii.unhexlify("737461626c652d646966667573696f6e2d7765627569").decode('ascii')
ca = binascii.unhexlify("63616d656e64757275").decode('ascii')

# 使用subprocess运行git clone命令
def run_git_clone(repo_url, destination):
    subprocess.run(["git", "clone", repo_url, destination])

extensions_path = "/content/ui/extensions"

extensions_to_clone = [
    (f"https://github.com/deforum-art/deforum-for-automatic1111-{w}", f"{extensions_path}/deforum-for-automatic1111-{w}"),
    (f"https://github.com/deforum-art/sd-{w}-deforum", f"{extensions_path}/sd-{w}-deforum"),
    (f"https://github.com/butaixianran/Stable-Diffusion-Webui-Civitai-Helper", f"{extensions_path}/Stable-Diffusion-Webui-Civitai-Helper"),
    (f"https://github.com/Mikubill/{c}-{w}-controlnet", f"{extensions_path}/{c}-{w}-controlnet"),
    (f"https://github.com/fkunn1326/openpose-editor", f"{extensions_path}/openpose-editor"),
    (f"https://github.com/camenduru/{a}-{d}-{w}-huggingface", f"{extensions_path}/{a}-{d}-{w}-huggingface"),
    (f"https://github.com/kohya-ss/{c}-{w}-additional-networks", f"{extensions_path}/{c}-{w}-additional-networks"),
    (f"https://github.com/hnmr293/posex", f"{extensions_path}/posex"),
    (f"https://github.com/nonnonstop/{c}-{w}-3d-open-pose-editor", f"{extensions_path}/{c}-{w}-3d-open-pose-editor"),
    (f"https://github.com/camenduru/{c}-{w}-tunnels", f"{extensions_path}/{c}-{w}-tunnels"),
    (f"https://github.com/etherealxx/batchlinks-{w}", f"{extensions_path}/batchlinks-{w}"),
    (f"https://github.com/catppuccin/{a}-{d}-{w}", f"{extensions_path}/{a}-{d}-{w}-catppuccin"),
    (f"https://github.com/AUTOMATIC1111/{a}-{d}-{w}-rembg", f"{extensions_path}/{a}-{d}-{w}-rembg"),
    (f"https://github.com/ashen-sensored/{a}-{d}-{w}-two-shot.git", f"{extensions_path}/{a}-{d}-{w}-two-shot"),
    (f"https://github.com/thomasasfk/{c}-{w}-aspect-ratio-helper", f"{extensions_path}/{c}-{w}-aspect-ratio-helper"),
    (f"https://github.com/Bing-su/adetailer", f"{extensions_path}/adetailer"),
    (f"https://github.com/mcmonkeyprojects/sd-dynamic-thresholding", f"{extensions_path}/{c}-dynamic-thresholding"),
    (f"https://github.com/adieyal/{c}-dynamic-prompts", f"{extensions_path}/{c}-dynamic-prompts"),
    (f"https://github.com/AUTOMATIC1111/{a}-{d}-{w}-wildcards", f"{extensions_path}/{a}-{d}-{w}-wildcards"),
    (f"https://github.com/continue-revolution/{c}-{w}-segment-anything", f"{extensions_path}/{c}-{w}-segment-anything"),
    (f"https://github.com/pkuliyi2015/multi{d}-upscaler-for-automatic1111.git", f"{extensions_path}/multi{d}-upscaler-for-automatic1111"),
    (f"https://github.com/Physton/{c}-{w}-prompt-all-in-one", f"{extensions_path}/{c}-{w}-prompt-all-in-one"),
    (f"https://github.com/journey-ad/{c}-{w}-bilingual-localization", f"{extensions_path}/{c}-{w}-bilingual-localization"),
    (f"https://github.com/Bobo-1125/{c}-{w}-oldsix-prompt-dynamic", f"{extensions_path}/{c}-{w}-oldsix-prompt-dynamic"),
]

for repo_url, destination in extensions_to_clone:
    run_git_clone(repo_url, destination)


# 定义要操作的文件路径
launch_py_path = "/content/ui/launch.py"
util_py_path = "/content/ui/repositories/diffusion-stability-stable/ldm/util.py"
shared_py_path = "/content/ui/modules/shared.py"

# 使用fileinput模块编辑launch.py文件
with fileinput.FileInput(launch_py_path, inplace=True, backup=".bak") as file:
    for line in file:
        # 在from modules import launch_utils之后插入import os
        if "from modules import launch_utils" in line:
            print(line, end='')
            print("import os")
        # 在prepare_environment()函数后插入os.system命令
        elif "prepare_environment()" in line:
            print(line, end='')
            print('        os.system(f"sed -i -e \'s/stable/dict()))/dict())).cuda()/g\' /content/ui/repositories/diffusion-stability-stable/ldm/util.py")')
        # 替换指定文本
        else:
            print(line, end='')

# 使用fileinput模块编辑shared.py文件
with fileinput.FileInput(shared_py_path, inplace=True, backup=".bak") as file:
    for line in file:
        # 替换指定文本
        print(line.replace('["sd_model_checkpoint"]', '["sd_model_checkpoint","sd_vae","CLIP_stop_at_last_layers"]'), end='')

# 删除备份文件
for path in [launch_py_path + '.bak', shared_py_path + '.bak']:
    if os.path.exists(path):
        os.remove(path)




# 汉化
run_git_clone(f"https://github.com/dtlnor/{st}-localization-zh_CN", f"{extensions_path}/{st}-localization-zh_CN-main")

# 删除指定插件文件夹
download_task1 = False  # 是否删除指定插件文件夹
filename1 = "test"  # 要删除的文件夹名称

if download_task1:
    subprocess.run(["rm", "-rf", f"{extensions_path}/{filename1}"])
    print("删除完成!")



# 定义一个函数来运行wget命令
def run_wget(url, output_path):
    wget_command = ["wget", url, "-O", output_path]
    subprocess.run(wget_command, check=True)

# 定义一个函数来创建文件夹(如果不存在的话)
def create_directory(directory):
    mkdir_command = ["mkdir", "-p", directory]
    subprocess.run(mkdir_command, check=True)

# 控制网络配置
CONTROLNET_ = True
T2I_ = True

# CONTROLNET
if CONTROLNET_:
    controlnet_directory = "/content/ui/extensions/control/models"
    create_directory(controlnet_directory)
    
    urls_and_outputs = [
        ("https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors", 
         f"{controlnet_directory}/control_v11e_sd15_ip2p_fp16.safetensors"),
        # Add more URLs and output paths here for other files
    ]

    for url, output_path in urls_and_outputs:
        run_wget(url, output_path)

# T2I
if T2I_:
    t2i_directory = "/content/ui/extensions/control/models"
    create_directory(t2i_directory)
    
    urls_and_outputs = [
        ("https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/t2iadapter_style_sd14v1.pth", 
         f"{t2i_directory}/t2iadapter_style_sd14v1.pth"),
        # Add more URLs and output paths here for other files
    ]

    for url, output_path in urls_and_outputs:
        run_wget(url, output_path)

import fileinput
import os

# 定义要操作的文件路径
launch_py_path = "/content/ui/launch.py"
util_py_path = "/content/ui/repositories/stable-diffusion-stability-ai/ldm/util.py"
shared_py_path = "/content/ui/modules/shared.py"

# 更新相关的sed命令
sed_command_1 = 'import os'
sed_command_2 = '        os.system(f"sed -i -e \'s/dict()))/dict())).cuda()/g\' {util_py_path}")'
sed_command_3 = '["sd_model_checkpoint", "sd_vae", "CLIP_stop_at_last_layers"]'

# 使用fileinput模块编辑launch.py文件
with fileinput.FileInput(launch_py_path, inplace=True, backup=".bak") as file:
    for line in file:
        if "from modules import launch_utils" in line:
            print(line, end='')
            print(sed_command_1)
        elif "prepare_environment()" in line:
            print(line, end='')
            print(sed_command_2)
        else:
            print(line, end='')

# 使用fileinput模块编辑shared.py文件
with fileinput.FileInput(shared_py_path, inplace=True, backup=".bak") as file:
    for line in file:
        print(line.replace('["sd_model_checkpoint"]', sed_command_3), end='')

# 删除备份文件
for path in [launch_py_path + '.bak', shared_py_path + '.bak']:
    if os.path.exists(path):
        os.remove(path)





"""# 模型下载"""

# Checkpoints/safetensors等大模型下载
# 下载任务1
CS_download_task1 = False
CS_url1 = "https://civitai.com/api/download/models/17701"
CS_destination_folder1 = f"/content/ui/models/Stable-diffusion"
CS_filename1 = "anypastelAnythingV45_anypastelAnythingV45.safetensors"
CS_target1 = f"{CS_destination_folder1}/{CS_filename1}"

if CS_download_task1:
    os.makedirs(CS_destination_folder1, exist_ok=True)
    response = requests.get(CS_url1)
    with open(CS_target1, 'wb') as f:
        f.write(response.content)

# 下载任务2
CS_download_task2 = False
CS_url2 = "https://civitai.com/api/download/models/26633?type=Model&format=PickleTensor&size=full&fp=fp16"
CS_destination_folder2 = f"/content/ui/models/Stable-diffusion"
CS_filename2 = "07CounterfeitV2503_10.ckpt"
CS_target2 = f"{CS_destination_folder2}/{CS_filename2}"

if CS_download_task2:
    os.makedirs(CS_destination_folder2, exist_ok=True)
    response = requests.get(CS_url2)
    with open(CS_target2, 'wb') as f:
        f.write(response.content)

# 下载任务3
CS_download_task3 = True
CS_url3 = "https://civitai.com/api/download/models/90854"
CS_destination_folder3 = f"/content/ui/models/Stable-diffusion"
CS_filename3 = "AnythingV5Ink.safetensors"
CS_target3 = f"{CS_destination_folder3}/{CS_filename3}"

if CS_download_task3:
    os.makedirs(CS_destination_folder3, exist_ok=True)
    response = requests.get(CS_url3)
    with open(CS_target3, 'wb') as f:
        f.write(response.content)

# 下载任务4
CS_download_task4 = False
CS_url4 = "https://civitai.com/api/download/models/57618"
CS_destination_folder4 = f"/content/ui/models/Stable-diffusion"
CS_filename4 = "CounterfeitV30_v30.safetensors"
CS_target4 = f"{CS_destination_folder4}/{CS_filename4}"

if CS_download_task4:
    os.makedirs(CS_destination_folder4, exist_ok=True)
    response = requests.get(CS_url4)
    with open(CS_target4, 'wb') as f:
        f.write(response.content)

# 下载任务5
CS_download_task5 = False
CS_url5 = "https://civitai.com/api/download/models/1356"
CS_destination_folder5 = f"/content/ui/models/Stable-diffusion"
CS_filename5 = "dreamlike10_10.ckpt"
CS_target5 = f"{CS_destination_folder5}/{CS_filename5}"

if CS_download_task5:
    os.makedirs(CS_destination_folder5, exist_ok=True)
    response = requests.get(CS_url5)
    with open(CS_target5, 'wb') as f:
        f.write(response.content)

# 下载任务6
CS_download_task6 = True
CS_url6 = "https://civitai.com/api/download/models/17233"
CS_destination_folder6 = f"/content/ui/models/Stable-diffusion"
CS_filename6 = "abyssorangemix3AOM3_aom3a1b.safetensors"
CS_target6 = f"{CS_destination_folder6}/{CS_filename6}"

if CS_download_task6:
    os.makedirs(CS_destination_folder6, exist_ok=True)
    response = requests.get(CS_url6)
    with open(CS_target6, 'wb') as f:
        f.write(response.content)

# 下载任务7
CS_download_task7 = False
CS_url7 = ""
CS_destination_folder7 = f"/content/ui/models/Stable-diffusion"
CS_filename7 = ""
CS_target7 = f"{CS_destination_folder7}/{CS_filename7}"

if CS_download_task7:
    os.makedirs(CS_destination_folder7, exist_ok=True)
    response = requests.get(CS_url7)
    with open(CS_target7, 'wb') as f:
        f.write(response.content)

# 下载任务8
CS_download_task8 = False
CS_url8 = ""
CS_destination_folder8 = f"/content/ui/models/Stable-diffusion"
CS_filename8 = ""
CS_target8 = f"{CS_destination_folder8}/{CS_filename8}"

if CS_download_task8:
    os.makedirs(CS_destination_folder8, exist_ok=True)
    response = requests.get(CS_url8)
    with open(CS_target8, 'wb') as f:
        f.write(response.content)

# 下载任务9
CS_download_task9 = False
CS_url9 = ""
CS_destination_folder9 = f"/content/ui/models/Stable-diffusion"
CS_filename9 = ""
CS_target9 = f"{CS_destination_folder9}/{CS_filename9}"

if CS_download_task9:
    os.makedirs(CS_destination_folder9, exist_ok=True)
    response = requests.get(CS_url9)
    with open(CS_target9, 'wb') as f:
        f.write(response.content)

# 下载任务10
CS_download_task10 = False
CS_url10 = ""
CS_destination_folder10 = f"/content/ui/models/Stable-diffusion"
CS_filename10 = ""
CS_target10 = f"{CS_destination_folder10}/{CS_filename10}"

if CS_download_task10:
    os.makedirs(CS_destination_folder10, exist_ok=True)
    response = requests.get(CS_url10)
    with open(CS_target10, 'wb') as f:
        f.write(response.content)

# 下载任务11
CS_download_task11 = False
CS_url11 = ""
CS_destination_folder11 = f"/content/ui/models/Stable-diffusion"
CS_filename11 = ""
CS_target11 = f"{CS_destination_folder11}/{CS_filename11}"

if CS_download_task11:
    os.makedirs(CS_destination_folder11, exist_ok=True)
    response = requests.get(CS_url11)
    with open(CS_target11, 'wb') as f:
        f.write(response.content)

# 下载任务12
CS_download_task12 = False
CS_url12 = ""
CS_destination_folder12 = f"/content/ui/models/Stable-diffusion"
CS_filename12 = ""
CS_target12 = f"{CS_destination_folder12}/{CS_filename12}"

if CS_download_task12:
    os.makedirs(CS_destination_folder12, exist_ok=True)
    response = requests.get(CS_url12)
    with open(CS_target12, 'wb') as f:
        f.write(response.content)

# 下载任务13
CS_download_task13 = False
CS_url13 = ""
CS_destination_folder13 = f"/content/ui/models/Stable-diffusion"
CS_filename13 = ""
CS_target13 = f"{CS_destination_folder13}/{CS_filename13}"

if CS_download_task13:
    os.makedirs(CS_destination_folder13, exist_ok=True)
    response = requests.get(CS_url13)
    with open(CS_target13, 'wb') as f:
        f.write(response.content)



#Lora模型下载
# Lora任务1
Lora_download_task1 = True
Lora_url1 = "https://civitai.com/api/download/models/64842"
Lora_destination_folder1 = "/content/ui/models/Lora"
Lora_filename1 = "arona_puranalora_v1.safetensors"
Lora_target1 = os.path.join(Lora_destination_folder1, Lora_filename1)

if Lora_download_task1:
    response = requests.get(Lora_url1)
    if response.status_code == 200:
        with open(Lora_target1, 'wb') as f:
            f.write(response.content)

# Lora任务2
Lora_download_task2 = False
Lora_url2 = "https://liblibai-online.liblibai.ai/web/model/25e9f1fa5137b2517b46c0a07f068e7328d5bd28744255d476c40e3ce61abed4.safetensors?attname=Xian-T%E6%89%8B%E9%83%A8%E4%BF%AE%E5%A4%8Dlora%EF%BC%88%E4%B8%8D%E7%94%A8controlnet%E4%B9%9F%E4%B8%8D%E5%9D%8F%E6%89%8B%E4%BA%86%EF%BC%89_v3.0.safetensors"
Lora_destination_folder2 = "/content/ui/models/Lora"
Lora_filename2 = "Xian-T\u624B\u90E8\u4FEE\u590Dlora\uFF08\u4E0D%E7%94%A8controlnet%E4%B4%9B%E4%B8%8D%E5%9D%8F%E6%89%8B%E4%BA%86\uFF09_v3.0.safetensors"
Lora_target2 = os.path.join(Lora_destination_folder2, Lora_filename2)

if Lora_download_task2:
    response = requests.get(Lora_url2)
    if response.status_code == 200:
        with open(Lora_target2, 'wb') as f:
            f.write(response.content)

# Lora任务3
Lora_download_task3 = True
Lora_url3 = "https://civitai.com/api/download/models/52900"
Lora_destination_folder3 = "/content/ui/models/Lora"
Lora_filename3 = "cailaiarona-000007.safetensors"
Lora_target3 = os.path.join(Lora_destination_folder3, Lora_filename3)

if Lora_download_task3:
    response = requests.get(Lora_url3)
    if response.status_code == 200:
        with open(Lora_target3, 'wb') as f:
            f.write(response.content)

# Lora任务4
Lora_download_task4 = True
Lora_url4 = "https://civitai.com/api/download/models/9409"
Lora_destination_folder4 = Lora_destination_folder3
Lora_filename4 = "chara-arona-v1.safetensors"
Lora_target4 = os.path.join(Lora_destination_folder4, Lora_filename4)

if Lora_download_task4:
    response = requests.get(Lora_url4)
    if response.status_code == 200:
        with open(Lora_target4, 'wb') as f:
            f.write(response.content)

# Lora任务5
Lora_download_task5 = False
Lora_url5 = ""
Lora_destination_folder5 = Lora_destination_folder3
Lora_filename5 = ""
Lora_target5 = os.path.join(Lora_destination_folder5, Lora_filename5)

if Lora_download_task5:
    response = requests.get(Lora_url5)
    if response.status_code == 200:
        with open(Lora_target5, 'wb') as f:
            f.write(response.content)

# Lora任务6
Lora_download_task6 = False
Lora_url6 = ""
Lora_destination_folder6 = Lora_destination_folder3
Lora_filename6 = ""
Lora_target6 = os.path.join(Lora_destination_folder6, Lora_filename6)

if Lora_download_task6:
    response = requests.get(Lora_url6)
    if response.status_code == 200:
        with open(Lora_target6, 'wb') as f:
            f.write(response.content)


# VAE下载
# VAE任务1
VAE_download_task1 = True
VAE_url1 = "https://civitai.com/api/download/models/25986"
VAE_destination_folder1 = "/content/ui/models/VAE"
VAE_filename1 = "anypastelAnythingV45_anypastelAnythingV45.zip"
VAE_target1 = f"{VAE_destination_folder1}/{VAE_filename1}"

if VAE_download_task1:
    response = requests.get(VAE_url1)
    with open(VAE_target1, 'wb') as file:
        file.write(response.content)

# VAE任务2
VAE_download_task2 = True
VAE_url2 = "https://civitai.com/api/download/models/64133"
VAE_destination_folder2 = "/content/ui/models/VAE"
VAE_filename2 = "counterfeit.pt"
VAE_target2 = f"{VAE_destination_folder2}/{VAE_filename2}"

if VAE_download_task2:
    response = requests.get(VAE_url2)
    with open(VAE_target2, 'wb') as file:
        file.write(response.content)

# VAE任务3
VAE_download_task3 = True
VAE_url3 = "https://civitai.com/api/download/models/80869?type=Model&format=Other"
VAE_destination_folder3 = "/content/ui/models/VAE"
VAE_filename3 = "vaeFtMse840000Ema_v100.safetensors"
VAE_target3 = f"{VAE_destination_folder3}/{VAE_filename3}"

if VAE_download_task3:
    response = requests.get(VAE_url3)
    with open(VAE_target3, 'wb') as file:
        file.write(response.content)

# VAE任务4
VAE_download_task4 = False
VAE_url4 = "https://huggingface.co/admruul/anything-v3.0/resolve/main/Anything-V3.0.vae.pt"
VAE_destination_folder4 = "/content/ui/models/VAE"
VAE_filename4 = "Anything-V3.0.pt"
VAE_target4 = f"{VAE_destination_folder4}/{VAE_filename4}"

if VAE_download_task4:
    response = requests.get(VAE_url4)
    with open(VAE_target4, 'wb') as file:
        file.write(response.content)

# VAE任务5
VAE_download_task5 = True
VAE_url5 = "https://civitai.com/api/download/models/131654?type=Model&format=SafeTensor"
VAE_destination_folder5 = "/content/ui/models/VAE"
VAE_filename5 = "anythingKlF8Anime2VaeFtMse840000_klF8Anime2.safetensors"
VAE_target5 = f"{VAE_destination_folder5}/{VAE_filename5}"

if VAE_download_task5:
    response = requests.get(VAE_url5)
    with open(VAE_target5, 'wb') as file:
        file.write(response.content)

# VAE任务6
VAE_download_task6 = True
VAE_url6 = "https://civitai.com/api/download/models/88156?type=Model&format=Other"
VAE_destination_folder6 = "/content/ui/models/VAE"
VAE_filename6 = "ClearVAE.2.3.pt"
VAE_target6 = f"{VAE_destination_folder6}/{VAE_filename6}"

if VAE_download_task6:
    response = requests.get(VAE_url6)
    with open(VAE_target6, 'wb') as file:
        file.write(response.content)

# VAE任务7
VAE_download_task7 = True
VAE_url7 = "https://civitai.com/api/download/models/121354?type=Model&format=Other"
VAE_destination_folder7 = "/content/ui/models/VAE"
VAE_filename7 = "orangemix_v1.pt"
VAE_target7 = f"{VAE_destination_folder7}/{VAE_filename7}"

if VAE_download_task7:
    response = requests.get(VAE_url7)
    with open(VAE_target7, 'wb') as file:
        file.write(response.content)

# VAE任务8
VAE_download_task8 = True
VAE_url8 = "https://civitai.com/api/download/models/28569?type=Model&format=Other"
VAE_destination_folder8 = "/content/ui/models/VAE"
VAE_filename8 = "klF8Anime2_klF8Anime2VAE.pt"
VAE_target8 = f"{VAE_destination_folder8}/{VAE_filename8}"

if VAE_download_task8:
    response = requests.get(VAE_url8)
    with open(VAE_target8, 'wb') as file:
        file.write(response.content)

# VAE任务9
VAE_download_task9 = False
VAE_url9 = ""
VAE_destination_folder9 = "/content/ui/models/VAE"
VAE_filename9 = ""
VAE_target9 = f"{VAE_destination_folder9}/{VAE_filename9}"

if VAE_download_task9:
    response = requests.get(VAE_url9)
    with open(VAE_target9, 'wb') as file:
        file.write(response.content)

# VAE任务10
VAE_download_task10 = False
VAE_url10 = ""
VAE_destination_folder10 = "/content/ui/models/VAE"
VAE_filename10 = ""
VAE_target10 = f"{VAE_destination_folder10}/{VAE_filename10}"

if VAE_download_task10:
    response = requests.get(VAE_url10)
    with open(VAE_target10, 'wb') as file:
        file.write(response.content)

# VAE任务11
VAE_download_task11 = False
VAE_url11 = ""
VAE_destination_folder11 = "/content/ui/models/VAE"
VAE_filename11 = ""
VAE_target11 = f"{VAE_destination_folder11}/{VAE_filename11}"

if VAE_download_task11:
    response = requests.get(VAE_url11)
    with open(VAE_target11, 'wb') as file:
        file.write(response.content)

# VAE任务12
VAE_download_task12 = False
VAE_url12 = ""
VAE_destination_folder12 = "/content/ui/models/VAE"
VAE_filename12 = ""
VAE_target12 = f"{VAE_destination_folder12}/{VAE_filename12}"

if VAE_download_task12:
    response = requests.get(VAE_url12)
    with open(VAE_target12, 'wb') as file:
        file.write(response.content)

# VAE任务13
VAE_download_task13 = False
VAE_url13 = ""
VAE_destination_folder13 = "/content/ui/models/VAE"
VAE_filename13 = ""
VAE_target13 = f"{VAE_destination_folder13}/{VAE_filename13}"

if VAE_download_task13:
    response = requests.get(VAE_url13)
    with open(VAE_target13, 'wb') as file:
        file.write(response.content)



# embeddings模型下载
# 下载函数
def download_file(url, destination_folder, filename):
    os.makedirs(destination_folder, exist_ok=True)
    response = requests.get(url)
    with open(os.path.join(destination_folder, filename), 'wb') as file:
        file.write(response.content)

# _下载任务1_
embeddings_download_task1 = True #@param {type:"boolean"}
embeddings_url1 = "https://civitai.com/api/download/models/125849" 
embeddings_destination_folder1 = "/content/ui/embeddings" #@ param {type:"string"}
embeddings_filename1 = "bad-hands-5.pt" 
embeddings_target1 = f'{embeddings_destination_folder1}/{embeddings_filename1}'

if embeddings_download_task1:
    download_file(embeddings_url1, embeddings_destination_folder1, embeddings_filename1)

# _下载任务2_
embeddings_download_task2 = False #@param {type:"boolean"}
embeddings_url2 = "" 
embeddings_destination_folder2 = "/content/ui/embeddings" #@ param {type:"string"}
embeddings_filename2 = "" 
embeddings_target2 = f'{embeddings_destination_folder2}/{embeddings_filename2}'

if embeddings_download_task2:
    download_file(embeddings_url2, embeddings_destination_folder2, embeddings_filename2)

# _下载任务3_
embeddings_download_task3 = False #@param {type:"boolean"}
embeddings_url3 = "" 
embeddings_destination_folder3 = "/content/ui/embeddings" #@ param {type:"string"}
embeddings_filename3 = "" 
embeddings_target3 = f'{embeddings_destination_folder3}/{embeddings_filename3}'

if embeddings_download_task3:
    download_file(embeddings_url3, embeddings_destination_folder3, embeddings_filename3)

# _下载任务4_
embeddings_download_task4 = False #@param {type:"boolean"}
embeddings_url4 = "" 
embeddings_destination_folder4 = "/content/ui/embeddings" #@ param {type:"string"}
embeddings_filename4 = "" 
embeddings_target4 = f'{embeddings_destination_folder4}/{embeddings_filename4}'

if embeddings_download_task4:
    download_file(embeddings_url4, embeddings_destination_folder4, embeddings_filename4)

# _下载任务5_
embeddings_download_task5 = False #@param {type:"boolean"}
embeddings_url5 = "" 
embeddings_destination_folder5 = "/content/ui/embeddings" #@ param {type:"string"}
embeddings_filename5 = "" 
embeddings_target5 = f'{embeddings_destination_folder5}/{embeddings_filename5}'

if embeddings_download_task5:
    download_file(embeddings_url5, embeddings_destination_folder5, embeddings_filename5)






#开始运行

# 初始化变量a、b、c和w的初始值
a = ""
b = ""
c = ""
d = ""
w = ""

# 构建变量a
a = binascii.unhexlify("737461626c65").decode("ascii")

# 构建变量b,使其等于a的首字母大写版本
b = binascii.unhexlify("537461626c65").decode("ascii")

# 构建变量c
c = binascii.unhexlify("7364").decode("ascii")

# 构建变量d
d = binascii.unhexlify("646966667573696f6e").decode("ascii")

# 构建变量w
w = binascii.unhexlify("7765627569").decode("ascii")


# 切换到/content/ui目录
subprocess.run(["cd", "/content/ui"], shell=True)

# 安装requirements.txt中的依赖项
subprocess.run(["pip", "install", "-r", "/content/ui/requirements.txt"])



#python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queu --multiple --no-download-sd-model --share
#python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --no-download-sd-model --share