colab / run.py
gmk123's picture
Update run.py
c1acb8c verified
Raw
History Blame Contribute Delete
19.8 kB
import argparse #line:1
import binascii #line:2
import ipywidgets as widgets #line:3
def str_to_bool (OO0O0O0000OO0O000 ):#line:7
if OO0O0O0000OO0O000 .lower ()in ('yes','true','t','y','1'):#line:8
return True #line:9
elif OO0O0O0000OO0O000 .lower ()in ('no','false','f','n','0'):#line:10
return False #line:11
else :#line:12
raise argparse .ArgumentTypeError ('Invalid boolean value: {}'.format (OO0O0O0000OO0O000 ))#line:13
parser =argparse .ArgumentParser ()#line:16
parser .add_argument ('--Animatediff',type =str_to_bool )#line:18
parser .add_argument ('--ControlNet',type =str )#line:19
parser .add_argument ('--Roop',type =str_to_bool )#line:20
parser .add_argument ('--Drive_Map',type =str_to_bool )#line:21
parser .add_argument ('--Key_words',type =str_to_bool )#line:22
parser .add_argument ('--SadTalker',type =str_to_bool )#line:23
parser .add_argument ('--Segment_Anything',type =str_to_bool )#line:24
args =parser .parse_args ()#line:27
Animatediff =args .Animatediff #line:29
Roop =args .Roop #line:30
ControlNet =args .ControlNet #line:31
Drive_Map =args .Drive_Map #line:32
Key_words =args .Key_words #line:33
Segment_Anything =args .Segment_Anything #line:34
SadTalker =args .SadTalker #line:35
import sys #line:39
import os #line:40
import json #line:41
import base64 #line:42
import importlib .util #line:43
from IPython import get_ipython #line:44
from IPython .utils import capture #line:45
from IPython .display import clear_output #line:46
from google .colab import drive #line:47
import tensorflow as tf #line:48
print ("TensorFlow version:",tf .__version__ )#line:51
if tf .test .gpu_device_name ():#line:52
drive .mount ('/content/drive')#line:53
else :#line:54
raise Exception ("\n请在《代码执行程序》-《更改运行时类型》-设置为GPU~")#line:55
w =binascii .unhexlify ("7765627569").decode ('ascii')#line:57
sdw =binascii .unhexlify ("737461626c652d646966667573696f6e2d7765627569").decode ('ascii')#line:58
wb =f'/root/main'#line:59
sai =binascii .unhexlify ("737461626c652d646966667573696f6e2d73746162696c6974792d6169").decode ('ascii')#line:60
sd =binascii .unhexlify ("537461626c652d646966667573696f6e").decode ('ascii')#line:61
gwb =f'/content/drive/MyDrive/SD_DATA'#line:62
def cloudDriveSync (OO0OO0OO00OO00OOO ,O0OOOOOO000OO0OOO ='',O00O0000OOO0O00OO =False ):#line:66
if not os .path .exists (OO0OO0OO00OO00OOO ):#line:68
get_ipython ().system (f'mkdir {OO0OO0OO00OO00OOO}')#line:70
if not O00O0000OOO0O00OO :#line:73
return #line:74
get_ipython ().system (f'rm -rf {O0OOOOOO000OO0OOO}')#line:77
get_ipython ().system (f'ln -s {OO0OO0OO00OO00OOO} {O0OOOOOO000OO0OOO}')#line:79
def initCloudDrive ():#line:82
cloudDriveSync (f'{gwb}')#line:83
cloudDriveSync (f'{gwb}/Config')#line:84
cloudDriveSync (f'{gwb}/Models',f'{wb}/models/{sd}',Drive_Map )#line:85
cloudDriveSync (f'{gwb}/Lora',f'{wb}/models/Lora',Drive_Map )#line:86
cloudDriveSync (f'{gwb}/Vae',f'{wb}/models/VAE',Drive_Map )#line:87
cloudDriveSync (f'{gwb}/Outputs',f'{wb}/outputs',Drive_Map )#line:88
cloudDriveSync (f'{gwb}/embeddings',f'{wb}/embeddings',Drive_Map )#line:89
cloudDriveSync (f'{gwb}/hypernetworks',f'{wb}/models/hypernetworks',Drive_Map )#line:90
if not os .path .exists (f'{gwb}/Config/config.json'):#line:94
get_ipython ().system (f'wget -O {gwb}/Config/config.json "https://huggingface.co/gmk123/colab/raw/main/config.json"')#line:95
if not os .path .exists (f'{gwb}/styles.csv'):#line:97
get_ipython ().system (f'wget -O {gwb}/styles.csv "https://huggingface.co/gmk123/colab/raw/main/styles.csv"')#line:99
def gitDownload (OOOOO0OOO000OOOO0 ,O0OO0OO0OO0O0000O ):#line:102
if os .path .exists (O0OO0OO0OO0O0000O ):#line:103
return #line:104
get_ipython ().system (f'git clone {OOOOO0OOO000OOOO0} {O0OO0OO0OO0O0000O}')#line:106
def installAdditional ():#line:109
O0O00O000O000O0O0 =[f'https://github.com/AlUlkesh/{sdw}-images-browser',f'https://github.com/etherealxx/batchlinks-{w}',f'https://github.com/camenduru/sd-civitai-browser',f'https://github.com/AUTOMATIC1111/{sdw}-rembg',f'https://github.com/thomasasfk/sd-{w}-aspect-ratio-helper',f'https://github.com/hanamizuki-ai/{sdw}-localization-zh_Hans',f'https://github.com/kohya-ss/sd-{w}-additional-networks',f'https://github.com/fkunn1326/openpose-editor',f'https://github.com/nonnonstop/sd-{w}-3d-open-pose-editor',f'https://github.com/IDEA-Research/DWPose',f'https://github.com/Physton/sd-{w}-prompt-all-in-one',f'https://github.com/adieyal/sd-dynamic-prompts',f'https://github.com/Bing-su/adetailer',f'https://github.com/Tencent/LightDiffusionFlow',f'https://github.com/picobyte/{sdw}-wd14-tagger',f'https://github.com/pkuliyi2015/multidiffusion-upscaler-for-automatic1111',]#line:148
for OO0OO0O00OOO00OO0 in O0O00O000O000O0O0 :#line:149
O00OO00OO0OO0O000 =OO0OO0O00OOO00OO0 .split ('/')[-1 ]#line:151
if 'github'in OO0OO0O00OOO00OO0 :#line:153
get_ipython ().system (f'git clone {OO0OO0O00OOO00OO0} {wb}/extensions/{O00OO00OO0OO0O000}')#line:154
cloudDriveSync (f'{gwb}/prompts',f'{wb}/extensions/sd-dynamic-prompts/wildcards',Drive_Map )#line:156
get_ipython ().system (f'wget https://raw.githubusercontent.com/camenduru/{sdw}-scripts/main/run_n_times.py -O {wb}/scripts/run_n_times.py')#line:157
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d {wb}/models/ESRGAN -o 4x-UltraSharp.pth')#line:158
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth -d {wb}/models/ESRGAN -o RealESRGAN_x4plus_anime_6B.pth')#line:159
gitDownload (f'https://huggingface.co/embed/negative',f'{wb}/embeddings/negative')#line:163
get_ipython ().system (f'rm -rf {wb}/embeddings/negative/.git')#line:164
get_ipython ().system (f'rm {wb}/embeddings/negative/.gitattributes')#line:165
gitDownload (f'https://huggingface.co/embed/lora',f'{wb}/models/Lora/positive')#line:167
get_ipython ().system (f'rm -rf {wb}/models/Lora/positive/.git')#line:168
get_ipython ().system (f'rm {wb}/models/Lora/positive/.gitattributes')#line:169
gitDownload (f'https://github.com/DominikDoom/a1111-sd-{w}-tagcomplete',f'{wb}/extensions/a1111-sd-{w}-tagcomplete')#line:172
get_ipython ().system (f'rm -f {wb}/extensions/a1111-sd-{w}-tagcomplete/tags/danbooru.csv')#line:173
get_ipython ().system (f'wget https://huggingface.co/gmk123/colab/raw/main/danbooru.csv -O {wb}/extensions/a1111-sd-{w}-tagcomplete/tags/danbooru.csv')#line:174
if Roop :#line:177
gitDownload (f'https://github.com/s9roll7/ebsynth_utility',f'{wb}/extensions/ebsynth_utility')#line:179
gitDownload (f'https://github.com/aigc-apps/sd-{w}-EasyPhoto',f'{wb}/extensions/sd-{w}-EasyPhoto')#line:180
get_ipython ().system (f'pip install transparent-background color-matcher')#line:181
get_ipython ().system (f'pip install modelscope')#line:182
gitDownload (f'https://github.com/s0md3v/sd-{w}-roop',f'{wb}/extensions/sd-{w}-roop')#line:184
get_ipython ().system (f'pip install insightface==0.7.3')#line:185
get_ipython ().system (f'mkdir -p {wb}/models/roop')#line:186
get_ipython ().system (f'wget https://huggingface.co/ezioruan/inswapper_128.onnx/resolve/main/inswapper_128.onnx -O {wb}/models/roop/inswapper_128.onnx')#line:187
print ('Roop换脸启用')#line:188
else :#line:189
print ('Roop换脸不启用')#line:190
OO0OO0OO0O0O00O0O =['control_v11e_sd15_ip2p.pth','control_v11e_sd15_shuffle.pth','control_v11f1e_sd15_tile.pth','control_v11f1p_sd15_depth.pth','control_v11p_sd15_canny.pth','control_v11p_sd15_inpaint.pth','control_v11p_sd15_lineart.pth','control_v11p_sd15_mlsd.pth','control_v11p_sd15_normalbae.pth','control_v11p_sd15_openpose.pth','control_v11p_sd15_scribble.pth','control_v11p_sd15_seg.pth','control_v11p_sd15_softedge.pth','control_v11p_sd15s2_lineart_anime.pth',]#line:208
get_ipython ().system (f'rm -rf {wb}/extensions/sd-{w}-controlnet')#line:209
if ControlNet =="Colab":#line:211
gitDownload (f'https://github.com/Mikubill/sd-{w}-controlnet',f'{wb}/extensions/sd-{w}-controlnet')#line:212
for O000O000OO0OO0O00 in OO0OO0OO0O0O00O0O :#line:213
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/{O000O000OO0OO0O00} -d {wb}/extensions/sd-{w}-controlnet/models -o {O000O000OO0OO0O00}')#line:214
elif ControlNet =="Google_Drive":#line:217
cloudDriveSync (f'{gwb}/CntModels')#line:218
gitDownload (f'https://github.com/Mikubill/sd-{w}-controlnet',f'{wb}/extensions/sd-{w}-controlnet')#line:219
for O000O000OO0OO0O00 in OO0OO0OO0O0O00O0O :#line:220
if not os .path .exists (f'{gwb}/CntModels/{O000O000OO0OO0O00}'):#line:221
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/{O000O000OO0OO0O00} -d {gwb}/CntModels -o {O000O000OO0OO0O00}')#line:222
print ("创建扩展",f'{O000O000OO0OO0O00}')#line:223
else :#line:224
print ("扩展存在",f'{O000O000OO0OO0O00}')#line:225
for O000O000OO0OO0O00 in os .listdir (f'{gwb}/CntModels'):#line:227
get_ipython ().system (f'ln -s {gwb}/CntModels/{O000O000OO0OO0O00} {wb}/extensions/sd-{w}-controlnet/models')#line:229
elif ControlNet =="No":#line:231
print ("不使用 ControlNet")#line:232
if Animatediff :#line:235
gitDownload (f'https://github.com/continue-revolution/sd-{w}-animatediff',f'{wb}/extensions/sd-{w}-animatediff')#line:236
get_ipython ().system (f'pip install pyav==11.4.1')#line:237
gitDownload (f'https://github.com/deforum-art/sd-{w}-deforum',f'{wb}/extensions/sd-{w}-deforum')#line:239
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/conrevo/AnimateDiff-A1111/resolve/main/lora/mm_sd15_v3_adapter.safetensors -d {wb}/models/Lora -o mm_sd15_v3_adapter.safetensors')#line:241
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/conrevo/AnimateDiff-A1111/resolve/main/motion_module/mm_sd15_v2.safetensors -d {wb}/extensions/sd-{w}-animatediff/model -o mm_sd15_v2.safetensors')#line:242
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/conrevo/AnimateDiff-A1111/resolve/main/motion_module/mm_sd15_v3.safetensors -d {wb}/extensions/sd-{w}-animatediff/model -o mm_sd15_v3.safetensors')#line:243
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/conrevo/AnimateDiff-A1111/resolve/main/motion_module/mm_sdxl_v10_beta.safetensors -d {wb}/extensions/sd-{w}-animatediff/model -o mm_sdxl_v10_beta.safetensors')#line:244
print ("Animatediff动画制作插件启用")#line:245
else :#line:246
get_ipython ().system (f'rm -rf {wb}/extensions/sd-{w}-animatediff')#line:247
print ("Animatediff动画制作插件不启用")#line:248
if Key_words :#line:251
cloudDriveSync (f'{gwb}/Storage',f'{wb}/extensions/sd-{w}-prompt-all-in-one/storage',Drive_Map )#line:252
print ("关键词插件启用")#line:253
else :#line:254
get_ipython ().system (f'rm -rf {wb}/extensions/sd-{w}-prompt-all-in-one')#line:255
print ("关键词插件不启用")#line:256
if SadTalker :#line:259
get_ipython ().system (f'git clone https://github.com/numz/sd-wav2lip-uhq {wb}/extensions/sd-wav2lip-uhq')#line:265
get_ipython ().system (f'pip install bark')#line:266
get_ipython ().system (f'wget https://huggingface.co/gmk123/wav2lip/resolve/main/wav2lip.pth -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/checkpoints/wav2lip.pth')#line:267
get_ipython ().system (f'wget https://huggingface.co/gmk123/wav2lip/resolve/main/wav2lip_gan.pth -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/checkpoints/wav2lip_gan.pth')#line:268
get_ipython ().system (f'wget https://huggingface.co/gmk123/wav2lip/resolve/main/s3fd-619a316812.pth -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/face_detection/detection/sfd/s3fd-619a316812.pth')#line:269
get_ipython ().system (f'wget https://huggingface.co/gmk123/wav2lip/resolve/main/shape_predictor_68_face_landmarks.dat -O {wb}/extensions/sd-wav2lip-uhq/scripts/wav2lip/predicator/shape_predictor_68_face_landmarks.dat')#line:270
get_ipython ().system (f'wget https://huggingface.co/ezioruan/inswapper_128.onnx/resolve/main/inswapper_128.onnx -O {wb}/extensions/sd-wav2lip-uhq/scripts/faceswap/model/inswapper_128.onnx')#line:271
print ("数字人插件启用")#line:273
else :#line:274
get_ipython ().system (f'rm -rf {wb}/extensions/sd-wav2lip-uhq')#line:275
print ("数字人插件不启用")#line:276
if Segment_Anything :#line:279
get_ipython ().system (f'git clone https://github.com/continue-revolution/sd-{w}-segment-anything {wb}/extensions/sd-{w}-segment-anything')#line:280
get_ipython ().system (f'wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth -O {wb}/extensions/sd-{w}-segment-anything/models/sam/sam_vit_l_0b3195.pth')#line:283
get_ipython ().system (f'wget https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth -O {wb}/extensions/sd-{w}-segment-anything/models/grounding-dino/groundingdino_swinb_cogcoor.pth')#line:284
print ("智能抠图插件启用")#line:286
else :#line:287
get_ipython ().system (f'rm -rf {wb}/extensions/sd-{w}-segment-anything')#line:288
print ("智能抠图插件不启用")#line:289
def initLocal ():#line:292
get_ipython ().system ('apt -y install -qq aria2 git')#line:294
get_ipython ().system (f'pip install lmdb')#line:296
get_ipython ().system (f'pip install torch==2.1.0+cu121 torchvision==0.16.0+cu121 torchaudio==2.1.0 torchtext==0.16.0+cpu torchdata==0.7.0 --index-url https://download.pytorch.org/whl/cu121')#line:297
get_ipython ().system (f'pip install xformers==0.0.23.post1')#line:298
get_ipython ().run_line_magic ('cd','/content')#line:299
with capture .capture_output ()as O000000OO0O00O0O0 :#line:301
def O00OO00OO00O000OO (O0000OO0000OOO000 ,O000O0000OO00O0OO ,OOOO0OOO00O0O0O0O ):OO00O0OO000O000OO =widgets .Button (description =O0000OO0000OOO000 ,disabled =True ,button_style =O000O0000OO00O0OO ,layout =widgets .Layout (min_width =OOOO0OOO00O0O0O0O ));display (OO00O0OO000O000OO )#line:302
get_ipython ().system (f'git clone -q --branch master https://github.com/AUTOMATIC1111/{sdw} /root/main')#line:304
with capture .capture_output ()as O000000OO0O00O0O0 :#line:306
get_ipython ().run_line_magic ('cd','/content')#line:307
get_ipython ().system (f'mkdir -p {wb}/repositories/{sai}')#line:309
get_ipython ().system ('wget -q -i https://huggingface.co/gmk123/run/raw/main/A1111.txt')#line:310
get_ipython ().system ('dpkg -i *.deb')#line:311
get_ipython ().system ('tar -C /content/ --zstd -xf sd_mrep.tar.zst')#line:312
get_ipython ().system ('tar -C / --zstd -xf gcolabdeps.tar.zst')#line:313
get_ipython ().system ('rm *.deb | rm *.zst | rm *.txt')#line:314
get_ipython ().system (f'mv -n /content/sd/stablediffusion/* {wb}/repositories/{sai} && rm -rf /content/sd')#line:315
os .environ ['TF_CPP_MIN_LOG_LEVEL']='3'#line:316
os .environ ['PYTHONWARNINGS']='ignore'#line:317
get_ipython ().system ('sed -i \'s@text = _formatwarnmsg(msg)@text =\"\"@g\' /usr/lib/python3.10/warnings.py')#line:318
get_ipython ().system ('apt --fix-broken install')#line:320
get_ipython ().system (f'pip install --force-reinstall charset-normalizer==3.1.0')#line:322
get_ipython ().system (f'pip install -qq translators chardet openai boto3 aliyun-python-sdk-core aliyun-python-sdk-alimt python-dotenv image-reward pyfunctional')#line:323
get_ipython ().system (f'pip install -U dynamicprompts[attentiongrabber,magicprompt]')#line:325
get_ipython ().system (f'pip install fake_useragent')#line:326
get_ipython ().system (f'git -C {wb}/repositories/{sai} reset --hard')#line:328
initCloudDrive ()#line:333
installAdditional ()#line:336
clear_output ()#line:338
get_ipython ().system (f'rm -rf {wb}/config.json')#line:340
get_ipython ().system (f'ln -s {gwb}/Config/config.json {wb}/config.json')#line:341
if os .path .exists (f"{gwb}/styles.csv"):#line:344
get_ipython ().system (f'ln -s {gwb}/styles.csv {wb}/styles.csv')#line:345
if os .path .exists (f"{wb}/extensions/sd-{w}-additional-networks"):#line:348
cloudDriveSync (f'{gwb}/Lora',f'{wb}/extensions/sd-{w}-additional-networks/models/lora',Drive_Map )#line:349
if len (os .listdir (f"{gwb}/Models"))==0 :#line:352
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/chilloutmix/resolve/main/chilloutmix_NiPrunedFp32Fix.safetensors -d {wb}/models/{sd} -o chilloutmix_NiPrunedFp32Fix.safetensors')#line:354
if len (os .listdir (f"{gwb}/Vae"))==0 :#line:357
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -d {wb}/models/VAE -o vae-ft-mse-840000-ema-pruned.safetensors')#line:359
get_ipython ().system (f'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d {wb}/models/ESRGAN -o 4x-UltraSharp.pth')#line:362
get_ipython ().run_line_magic ('cd',f'{wb}/modules')#line:366
get_ipython ().system ('wget -q -O extras.py https://huggingface.co/gmk123/run/raw/main/extras.py')#line:367
get_ipython ().system ('wget -q -O sd_models.py https://huggingface.co/gmk123/run/raw/main/sd_models.py')#line:368
get_ipython ().system ('wget -q -O /usr/local/lib/python3.10/dist-packages/gradio/blocks.py https://huggingface.co/gmk123/run/raw/main/blocks.py')#line:369
get_ipython ().run_line_magic ('cd',f'{wb}')#line:371
get_ipython ().system (f'sed -i \'s@print(\\"No module.*@@\' {wb}/repositories/{sai}/ldm/modules/diffusionmodules/model.py')#line:372
get_ipython ().system (f'sed -i \'s@\\["sd_model_checkpoint"\\]@\\["sd_model_checkpoint", "sd_vae", "CLIP_stop_at_last_layers", "inpainting_mask_weight", "initial_noise_multiplier"\\]@g\' {wb}/modules/shared.py')#line:373
get_ipython ().system (f'sed -i \'s@possible_sd_paths =.*@possible_sd_paths = [\"{wb}/repositories/{sai}\"]@\' {wb}/modules/paths.py')#line:375
get_ipython ().system (f'sed -i \'s@\\.\\.\\/@src/@g\' {wb}/modules/paths.py')#line:376
get_ipython ().system (f'sed -i \'s@src/generative-models@generative-models@g\' {wb}/modules/paths.py')#line:377
wise ="--share --api --disable-safe-unpickle --enable-insecure-extension-access --xformers --disable-console-progressbars --theme dark --upcast-sampling --cors-allow-origins=*"#line:380
def run (OO000OO000OOO00OO ):#line:390
clear_output ()#line:391
get_ipython ().run_line_magic ('cd',f'{wb}')#line:394
get_ipython ().system (f'python {OO000OO000OOO00OO} {wise}')#line:395
if os .path .exists (f'{wb}'):#line:398
run (f'{w}.py')#line:399
else :#line:400
initLocal ()#line:402
run (f'{w}.py')#line:405