{"cells":[{"cell_type":"markdown","metadata":{"id":"UDSpxrglM4Kd"},"source":["# 环境安装及运行(打开执行我)\n"]},{"cell_type":"markdown","metadata":{"id":"IV5EHYh5gmz5"},"source":["## **1. demo安装和运行**"]},{"cell_type":"markdown","metadata":{"id":"bINItiYBu1o8"},"source":["说明:\n","
第一次执行: 1 -> 2 -> 3, 小标签2.1, 2.2按自己需求执行\n","
云盘启动(已缓存): 1 -> 3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ValVO7P_k5Fb"},"outputs":[],"source":["#@title ## 1、加载谷歌云盘并初始化环境(每次都必须执行)\n","from IPython.display import clear_output\n","import ipywidgets as widgets\n","def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)\n","#@markdown ####是否允许加载谷歌云盘(禁止将无法保存模型):##\n","is_load_pan = \"\\u5141\\u8BB8\" #@param [\"允许\",\"禁止\"]\n","import os\n","import concurrent.futures\n","run_state = False\n","mainpth = 'MyDrive'\n","def init_pan():\n"," if is_load_pan==\"允许\":\n"," from google.colab import drive\n"," drive.mount('/content/drive')\n"," run_state = True\n"," else:\n"," print('已禁止加载云盘')\n"," run_state = False\n","def glabdep_install():\n"," if not os.path.exists(\"/content/drive\"):\n"," print('\u001b[1;31mGdrive not connected, using temporary colab storage ...')\n"," time.sleep(4)\n"," mainpth=\"MyDrive\"\n"," !mkdir -p /content/drive/$mainpth\n"," Shared_Drive=\"\"\n","\n"," if Shared_Drive!=\"\" and not os.path.exists(\"/content/drive/Shareddrives\"):\n"," print('\u001b[1;31mShared drive not detected, using default MyDrive')\n"," mainpth=\"MyDrive\"\n","\n"," %mkdir -p /content/drive/$mainpth/sd\n","\n"," %cd /content/\n"," !wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt\n"," !dpkg -i *.deb\n"," if not os.path.exists('/content/drive/'+mainpth+'/sd/stablediffusiond'): #restore later\n"," !tar -C /content/drive/$mainpth --zstd -xf sd_mrep.tar.zst\n"," !tar -C / --zstd -xf gcolabdeps.tar.zst\n"," !rm *.deb | rm *.zst | rm *.txt\n"," if not os.path.exists('drive/'+mainpth+'/sd/libtcmalloc/libtcmalloc_minimal.so.4'):\n"," %env CXXFLAGS=-std=c++14\n"," !wget -q https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz && tar zxf gperftools-2.5.tar.gz && mv gperftools-2.5 gperftools\n"," !wget -q https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/Patch\n"," %cd /content/gperftools\n"," !patch -p1 < /content/Patch\n"," !./configure --enable-minimal --enable-libunwind --enable-frame-pointers --enable-dynamic-sized-delete-support --enable-sized-delete --enable-emergency-malloc; make -j4\n"," !mkdir -p /content/drive/$mainpth/sd/libtcmalloc && cp .libs/libtcmalloc*.so* /content/drive/$mainpth/sd/libtcmalloc\n"," %env LD_PRELOAD=/content/drive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4\n"," %cd /content\n"," !rm *.tar.gz Patch && rm -r /content/gperftools\n"," else:\n"," %env LD_PRELOAD=/content/drive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4\n","\n"," os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n"," os.environ['PYTHONWARNINGS'] = 'ignore'\n"," !sed -i 's@text = _formatwarnmsg(msg)@text =\\\"\\\"@g' /usr/lib/python3.10/warnings.py\n","def aira2_install():\n"," !apt -y update -qq\n"," # !wget https://github.com/camenduru/gperftools/releases/download/v1.0/libtcmalloc_minimal.so.4 -O /content/libtcmalloc_minimal.so.4\n"," # %env LD_PRELOAD=/content/libtcmalloc_minimal.so.4\n"," !apt-get -y install -qq aria2 libcairo2-dev pkg-config python3-dev\n","executor = concurrent.futures.ThreadPoolExecutor(max_workers=2)\n","init_pan()\n","# task1 = executor.submit(init_pan)\n","task2 = executor.submit(glabdep_install)\n","task3 = executor.submit(aira2_install)\n","# concurrent.futures.wait([task1,task2,task3])\n","concurrent.futures.wait([task2,task3])\n","\n","clear_output()\n","# if run_state: print(\"云盘加载成功\")\n","# else: print('已禁止加载云盘')\n","inf('\\u2714 Done','success', '50px')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7ybaLXXzlASJ"},"outputs":[],"source":["#@title ## 2.0、下载各种插件(如果是云盘启动,执行第一次就可以)\n","from IPython.utils import capture\n","from IPython.display import clear_output\n","import ipywidgets as widgets\n","def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)\n","\n","sd0=\"s\"+\"t\"+\"a\"+\"b\"+\"l\"+\"e\"+\"-\"+\"d\"+\"i\"+\"f\"+\"f\"+\"u\"+\"s\"+\"i\"+\"o\"+\"n\"+\"-\"+\"w\"+\"e\"+\"b\"+\"u\"\n","sd=sd0+'i'\n","server0=\"s\"+\"d\"+\"-\"+\"v\"+\"1\"+\".\"+\"5\"+\".\"+\"1\"+\"-\"+\"s\"+\"e\"+\"r\"+\"v\"+\"e\"\n","server = server0+\"r\"\n","wi='w'+'e'+'b'+'u'+'i'\n","import os\n","#@markdown ####是否把SD服务和初始资源下载到网盘:##\n","saveInDrive = True # @param {type:'boolean'}\n","#@markdown ####初始化的大模型:##\n","first_checkpoint = \"protogenV22Anime_22.safetensors\" #@param [\"Dark_sushi_mix.safetensors\", \"AnythingV5V3_v5PrtRE.safetensors\", \"chilloutmix_NiPrunedFp16Fix.safetensors\", \"rpg_V4.safetensors\", \"protogenV22Anime_22.safetensors\",\"none\"]\n","#@markdown ####stable diffusion的ui页面:##\n","sd_wi = \"\\u5B9A\\u5236\\u7248UI-Demo\\u7248\\u672C(base webui v1.6.0)\" #@param [\"定制版UI-Demo版本(base webui v1.6.0)\",\"定制版UI-Demo版本(base webui v1.5.1)\"]\n","#@markdown ####按定制版UI-Demo所需版本进行资源下载:##\n","is_customized_version = True # @param {type:'boolean'}\n","#@markdown ####是否加载云盘里的extensions、VAE、embeddings、lora、checkpoint?##\n","is_pan_extensions = True # @param {type:'boolean'}\n","default_gdrive_path = \"/content/drive/MyDrive\" #@param {type: \"string\"}\n","base_dir = \"\"\n","if saveInDrive :\n"," os.makedirs(default_gdrive_path, exist_ok=True)\n"," os.makedirs(default_gdrive_path + \"/sdwi\", exist_ok=True)\n"," base_dir = default_gdrive_path + \"/sdwi\"\n","else:\n"," %cd /content/\n"," !mkdir /content/sdwi\n"," base_dir = \"/content/sdwi\"\n","\n","with capture.capture_output() as cap:\n"," if sd_wi==\"定制版UI-Demo版本(base webui v1.5.1)\":\n"," # !git clone https://github.com/NullFullme/{server} --recursive {base_dir}\n"," %cd /content/\n"," !wget -q -i https://raw.githubusercontent.com/NullFullme/sd-v1.5.1-server/main/Dependencies/Custom-sd.txt\n"," if not os.path.exists(base_dir+'/repositories'):\n"," !tar -xvf \"sd-v1.5.1-server.tar\" --recursive-unlink -C {base_dir}\n"," # !dpkg -i *.deb\n"," # !tar -C / --zstd -xf gcolabdeps.tar.zst\n"," !rm *.deb | rm *.zst | rm *.txt\n"," !sed -i 's@text = _formatwarnmsg(msg)@text =\\\"\\\"@g' /usr/lib/python3.10/warnings.py\n"," # elif sd_wi==\"AUTOMATIC1111原版v1.3.0(稳定)\":\n"," # !git clone -b v1.3.0 --single-branch https://github.com/AUTOMATIC1111/{sd} {base_dir}\n"," # else:\n"," # !git clone -b v1.5.1 --single-branch https://github.com/AUTOMATIC1111/{sd} {base_dir}\n"," elif sd_wi==\"定制版UI-Demo版本(base webui v1.6.0)\":\n"," %cd /content/\n"," !wget https://huggingface.co/riruA/dependencies/resolve/main/sd-v1.6.0-server.tar\n"," if not os.path.exists(base_dir+'/repositories'):\n"," !tar -xvf \"sd-v1.6.0-server.tar\" --recursive-unlink -C {base_dir}\n"," !rm *.deb | rm *.zst | rm *.txt\n"," !sed -i 's@text = _formatwarnmsg(msg)@text =\\\"\\\"@g' /usr/lib/python3.10/warnings.py\n","\n","!mkdir -p base_dir/cache/\n","os.environ['TRANSFORMERS_CACHE']=os.path.join(base_dir, \"cache\")\n","os.environ['TORCH_HOME'] = os.path.join(base_dir, \"cache\")\n","\n","import concurrent.futures\n","import time\n","import subprocess\n","\n","checkpoint_url = {\n"," \"Dark_sushi_mix.safetensors\" : \"https://huggingface.co/mdl-mirror/dark-sushi-mix/resolve/main/darkSushiMixMix_brighter.safetensors\",\n"," \"AnythingV5V3_v5PrtRE.safetensors\" : \"https://huggingface.co/ckpt/anything-v5.0/resolve/main/AnythingV5V3_v5PrtRE.safetensors\",\n"," \"chilloutmix_NiPrunedFp16Fix.safetensors\" : \"https://huggingface.co/naonovn/chilloutmix_NiPrunedFp32Fix/resolve/main/chilloutmix_NiPrunedFp32Fix.safetensors\",\n"," \"rpg_V4.safetensors\" : \"https://huggingface.co/Anashel/rpg/resolve/main/RPG-V4-Model-Download/RPG-v4.safetensors\",\n"," \"ProtoGen_X5.8-pruned-fp16.safetensors\" : \"https://huggingface.co/darkstorm2150/Protogen_x5.8_Official_Release/resolve/main/ProtoGen_X5.8-pruned-fp16.safetensors\",\n"," \"none\" : \"\",\n","}\n","\n","def run_git_download():\n"," start_time = time.time()\n"," !git clone https://github.com/Physton/sd-{wi}-prompt-all-in-one {base_dir}/extensions/sd-{wi}-prompt-all-in-one\n"," !git clone https://github.com/Mikubill/sd-{wi}-controlnet {base_dir}/extensions/sd-{wi}-controlnet\n"," !git -C {base_dir}/extensions/sd-{wi}-controlnet reset --hard 784eadbb\n"," !git clone https://github.com/fkunn1326/openpose-editor {base_dir}/extensions/openpose-editor\n"," !git clone https://github.com/DominikDoom/a1111-sd-{wi}-tagcomplete {base_dir}/extensions/a1111-sd-{wi}-tagcomplete\n"," !git clone https://github.com/Coyote-A/ultimate-upscale-for-automatic1111 {base_dir}/extensions/ultimate-upscale\n"," !git clone https://github.com/hako-mikan/sd-{wi}-lora-block-weight {base_dir}/extensions/sd-{wi}-lora-block-weight\n"," #xformers加速\n"," end_time = time.time()\n"," print(\"已克隆git耗时:\", end_time-start_time, \"秒\")\n","\n","def run_aria2c_download():\n"," start_time = time.time()\n"," cmd=f\"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {checkpoint_url[first_checkpoint]} -d {base_dir}/models/Stable-diffusion -o {first_checkpoint}\"\n"," subprocess.run(cmd, shell=True)\n"," if is_customized_version:\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth -d {base_dir}/models/ControlNet -o control_v11p_sd15_canny.pth\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_inpaint.pth -d {base_dir}/models/ControlNet -o control_v11p_sd15_inpaint.pth\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart.pth -d {base_dir}/models/ControlNet -o control_v11p_sd15_lineart.pth\n"," else:\n"," # 半精ControlNet\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11e_sd15_ip2p.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11f1p_sd15_depth_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11f1p_sd15_depth.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_canny_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_canny.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_inpaint_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_inpaint.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_lineart_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11f1p_sd15_depth.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_mlsd_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_mlsd.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_normalbae_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_normalbae.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_openpose_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_openpose.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_scribble_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_scribble.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_seg_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_seg.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_softedge_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15_softedge.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15s2_lineart_anime_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11p_sd15s2_lineart_anime.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11f1e_sd15_tile_fp16.safetensors -d {base_dir}/models/ControlNet -o control_v11f1e_sd15_tile_fp16.safetensors\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/lokCX/4x-Ultrasharp/resolve/main/4x-UltraSharp.pth -d {base_dir}/models/ESRGAN/ -o 4x-UltraSharp.pth\n"," !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/datasets/daasd/CN.csv/resolve/main/CN.csv -d {base_dir}/extensions/a1111-sd-{wi}-tagcomplete/tags -o CN.csv\n"," end_time = time.time()\n"," print(\"aria2c完成下载耗时:\", end_time-start_time, \"秒\")\n","\n","def curl_download():\n"," start_time = time.time()\n"," !curl -Lo \"{base_dir}/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors\" https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors\n"," !curl -Lo \"{base_dir}/models/VAE/kl-f8-anime2.ckpt\" https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/4c4f05104055c029ad577c18ac176462f0d1d7c1/vae/kl-f8-anime2.ckpt\n"," !curl -Lo \"{base_dir}/models/VAE/animevae.pt\" https://huggingface.co/swl-models/animvae/resolve/main/animevae.pt\n"," end_time = time.time()\n"," print(\"curl完成下载耗时:\", end_time-start_time,\"秒\")\n","\n","def wget_download():\n"," start_time = time.time()\n"," !apt install libunwind8-dev -yqq\n"," os.environ[\"LD_PRELOAD\"] = \"libtcmalloc.so.4\"\n"," os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\"\n"," !sudo apt-get install sox ffmpeg libcairo2 libcairo2-dev\n"," end_time = time.time()\n"," print(\"wget完成下载耗时:\", end_time-start_time,\"秒\")\n","\n","def pip_download():\n"," start_time = time.time()\n"," !pip install xformers xformers==0.0.20\n"," end_time = time.time()\n"," print(\"pip完成下载耗时:\", end_time-start_time,\"秒\")\n","\n","with capture.capture_output() as cap:\n"," executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)\n"," task1 = executor.submit(run_git_download)\n"," task2 = executor.submit(run_aria2c_download)\n"," task3 = executor.submit(curl_download)\n"," task4 = executor.submit(wget_download)\n"," task5 = executor.submit(pip_download)\n"," # concurrent.futures.wait([task1,task2,task3,task4,task5])\n"," concurrent.futures.wait([task1,task2,task3,task5])\n"," # concurrent.futures.wait([task5])\n","\n","\n","\n","# with capture.capture_output() as cap:\n","# %cd {base_dir}\n","# !git config --unset core.hooksPath\n","# import shutil\n","# if os.path.exists(f'{base_dir}/embeddings'):\n","# shutil.rmtree(f'{base_dir}/embeddings')\n","# !git clone https://huggingface.co/nolanaatama/embeddings\n","\n","#@markdown ####存放的路径如下(需要自己在网盘手动创建文件夹):##\n","#@markdown #####extensions: default_gdrive_path/extensions##\n","#@markdown #####VAE: default_gdrive_path/VAE##\n","#@markdown #####embeddings: default_gdrive_path/embeddings##\n","#@markdown #####lora: default_gdrive_path/lora##\n","#@markdown #####checkpoint: default_gdrive_path/checkpoint##\n","\n","#个人插件从云盘的extensions文件夹与VAE文件夹加载\n","clear_output()\n","if is_pan_extensions:\n"," if os.path.exists(default_gdrive_path+\"/extensions\"):\n"," !rsync -a {default_gdrive_path}/extensions/* {base_dir}/extensions\n"," print('已加载云盘里的插件')\n"," if os.path.exists(default_gdrive_path+\"/VAE\"):\n"," !rsync -a {default_gdrive_path}/VAE/* {base_dir}/models/VAE\n"," print('已加载云盘里的VAE')\n"," if os.path.exists(default_gdrive_path+\"/embeddings\"):\n"," !rsync -a {default_gdrive_path}/embeddings/* {base_dir}/embeddings\n"," print('已加载云盘里的embeddings')\n"," if os.path.exists(default_gdrive_path+\"/lora\"):\n"," !mkdir -p {base_dir}/models/Lora\n"," !rsync -a {default_gdrive_path}/lora/* {base_dir}/models/Lora\n"," print('已加载云盘里的lora')\n"," if os.path.exists(default_gdrive_path+\"/checkpoint\"):\n"," !rsync -a {default_gdrive_path}/checkpoint/* {base_dir}/models/Stable-diffusion\n"," print('已加载云盘里的Stable-diffusion')\n","\n","\n","inf('\\u2714 Done','success', '50px')"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"L0HL67EQs8cg"},"outputs":[],"source":["#@title ## 2.1、下载列表管理/增删改查模型或其他文件\n","#@markdown ####选择mod集合(如果云盘没找到集合则在根目录创建):##\n","mod_json_name = \"model.json\" #@param {type: \"string\"}\n","# 创建并写入文本到文件\n","mod_json_dir=''\n","\n","#递归目录查找mod集合\n","def find_model_json(root_path,mod_json_name):\n"," for file_name in os.listdir(root_path):\n"," file_path = os.path.join(root_path, file_name)\n"," if os.path.isdir(file_path):\n"," res=find_model_json(file_path,mod_json_name)\n"," if res is not None:\n"," return res\n"," elif file_name == mod_json_name:\n"," print(\"找到model.json文件:\", file_path)\n"," return file_path\n","\n","if 'is_load_pan' in globals() and is_load_pan==\"允许\":\n"," mod_json_dir=find_model_json('/content/drive/',mod_json_name)\n"," if mod_json_dir==None:\n"," with open(f'/content/drive/MyDrive/{mod_json_name}', 'w') as f:\n"," f.write('[]')\n"," mod_json_dir=f'/content/drive/MyDrive/{mod_json_name}'\n","else:\n"," mod_json_dir=f'/content/{mod_json_name}'\n","\n","import json\n","import pandas as pd\n","# drive.mount('/content/drive')\n","from IPython.display import display, clear_output, HTML\n","import ipywidgets as widgets\n","from datetime import datetime\n","import time\n","\n","custom_css = \"\"\"\n","\n","\"\"\"\n","display(HTML(custom_css))\n","\n","# 加载JSON数据为Python对象\n","try:\n"," # 尝试打开你的文件\n"," with open(mod_json_dir, 'r') as f:\n"," try:\n"," # 尝试使用json模块的load()函数,将文件内容转换为JSON\n"," data = json.load(f)\n"," except json.JSONDecodeError:\n"," # 如果出现 JSONDecodeError 异常,设置 data 为一个空字典\n"," data = {}\n","except FileNotFoundError:\n"," # 如果出现 FileNotFoundError 异常,设置 data 为一个空字典\n"," data = {}\n","\n","print(mod_json_dir)\n","print(data)\n","# 将JSON数据转换为Pandas DataFrame\n","df = pd.DataFrame(data)\n","\n","# 显示表格\n","def show_table():\n"," clear_output()\n"," # 新字段兼容\n"," if not df.columns.empty and 'path' not in df.columns:\n"," df['path'] = ''\n"," styled_df = df.style.set_properties(**{'text-align': 'center'})\n","\n"," # 设置列宽度和居中对齐\n"," styled_df.set_table_styles([{'selector': 'th', 'props': [('max-width', '150px'), ('text-align', 'center')]}])\n","\n"," # 输出DataFrame\n"," display(styled_df)\n"," display(widgets.HBox([input_name, input_type, input_download_link, input_size, input_path]))\n"," display(widgets.HBox([input_index, read_button, update_button, delete_button, add_button]))\n"," display(widgets.HBox([save_button]))\n","\n","# 添加一行数据\n","def add_row(button):\n"," try:\n"," if not input_download_link.value:\n"," display(HTML(f\"

下载链接为必填项

\"))\n"," return\n","\n"," new_row = {\n"," \"name\": input_name.value.strip() or '未命名',\n"," \"type\": '未定义' if input_type.value == '不选则根据下载内容决定' else input_type.value,\n"," \"downloadLink\": input_download_link.value.strip(), # 修改这里的键名\n"," \"size\": input_size.value,\n"," \"path\": input_path.value,\n"," \"lastChangeDate\": datetime.now().strftime('%Y-%m-%d')\n"," }\n"," global df\n"," df = df.append(new_row, ignore_index=True)\n"," lock_buttons()\n"," show_table()\n"," clear_input()\n"," display(HTML(\"

新增{}成功

\".format(new_row)))\n"," time.sleep(1)\n"," display(HTML(\"\"))\n"," except Exception as e:\n"," display(HTML(f\"

新增出错:{e}

\"))\n","\n","# 删除一行数据\n","def delete_row(button):\n"," try:\n"," index = int(input_index.value)\n"," global df\n"," df = df.drop(index)\n"," lock_buttons()\n"," show_table()\n"," display(HTML(\"

删除{}成功

\".format(index)))\n"," time.sleep(1)\n"," display(HTML(\"\"))\n"," except Exception as e:\n"," display(HTML(f\"

删除出错:{e}

\"))\n","\n","#读取一行数据\n","def read_row(button):\n"," try:\n"," index = int(input_index.value)\n"," input_name.value = df.at[index, 'name']\n"," input_type.value ='不选则根据下载内容决定' if df.at[index, 'type']=='未定义' else df.at[index, 'type']\n"," input_download_link.value = df.at[index, 'downloadLink']\n"," input_size.value = df.at[index, 'size']\n"," input_path.value = df.at[index, 'path']\n"," display(HTML(f\"

读取{index}成功

\"))\n"," time.sleep(1)\n"," display(HTML(\"\"))\n"," except Exception as e:\n"," display(HTML(f\"

读取出错:{e}

\"))\n","# 修改一行数据\n","def update_row(button):\n"," try:\n"," index = int(input_index.value)\n"," if input_download_link.value:\n"," df.at[index, 'downloadLink'] = input_download_link.value.strip()\n"," else:\n"," display(HTML(f\"

下载链接为必填项{e}

\"))\n"," return\n"," df.at[index, 'name'] = input_name.value.strip() or '未命名'\n"," df.at[index, 'type'] = '未定义' if input_type.value == '不选则根据下载内容决定' else input_type.value\n"," df.at[index, 'size'] = input_size.value\n"," df.at[index, 'path'] = input_path.value or ''\n"," df.at[index, 'lastChangeDate'] = datetime.now().strftime('%Y-%m-%d')\n"," lock_buttons()\n"," clear_input()\n"," show_table()\n"," display(HTML(\"

修改{}成功

\".format(index)))\n"," time.sleep(1)\n"," display(HTML(\"\"))\n"," except Exception as e:\n"," display(HTML(f\"

修改出错:{e}

\"))\n","\n","#清空输入框\n","def clear_input():\n"," try:\n"," input_name.value = ''\n"," input_type.value = ''\n"," input_download_link.value = ''\n"," input_size.value = ''\n"," input_path.value = ''\n"," except Exception as e:\n"," display(HTML(f\"

重置输入框出错:{e}

\"))\n","\n","#保存\n","def save_row(button):\n"," global df\n"," try:\n"," with open(mod_json_dir, 'w') as Wfile:\n"," json=df.to_json(orient='records')\n"," Wfile.write(json)\n"," display(HTML(\"

保存成功

\"))\n"," time.sleep(1)\n"," display(HTML(\"\"))\n"," except Exception as e:\n"," display(HTML(f\"

保存出错:{e}

\"))\n","\n","\n","\n","# 定义输入框和按钮\n","input_name = widgets.Text(description='名字',placeholder='不填则根据下载内容决定')\n","input_type = widgets.Text(description='类型',placeholder='选填')\n","input_download_link = widgets.Text(description='下载链接',placeholder='必填:C站或者huggingface') # 修改这里的描述\n","input_size = widgets.Text(description='大小',placeholder='选填')\n","input_path = widgets.Text(description='下载路径',placeholder='除了lora和checkpoint其余必填')\n","input_index = widgets.Text(description='mod编号',placeholder='与新增无关,用于读改删mod',layout=widgets.Layout(margin='10px'))\n","add_button = widgets.Button(description='新增mod', layout=widgets.Layout(margin='10px 180px'), button_style='info')\n","delete_button = widgets.Button(description='删除mod', layout=widgets.Layout(margin='10px 10px'), button_style='danger')\n","update_button = widgets.Button(description='更新mod', layout=widgets.Layout(margin='10px 30px 10px 0'), button_style='warning')\n","save_button = widgets.Button(description='保存', layout=widgets.Layout(margin='20px auto'), button_style='success')\n","read_button = widgets.Button(description='读取mod', layout=widgets.Layout(margin='10px 0px 10px 30px'), button_style='primary')\n","\n","\n","#节流\n","def lock_buttons():\n"," # add_button.disabled = True\n"," delete_button.disabled = True\n"," update_button.disabled = True\n","\n","def unlock_buttons():\n"," # add_button.disabled = False\n"," delete_button.disabled = False\n"," update_button.disabled = False\n","\n","def on_input_change(change):\n"," if change.new:\n"," # len(change.new)\n"," if change.new==change.owner.value:\n"," if len(change.new)==1:\n"," lock_buttons()\n"," time.sleep(0.5)\n"," unlock_buttons()\n"," else:\n"," lock_buttons()\n","\n","\n","# 为输入框添加输入事件\n","# input_name.observe(on_input_change, names='value')\n","# input_type.observe(on_input_change, names='value')\n","# input_download_link.observe(on_input_change, names='value')\n","# input_size.observe(on_input_change, names='value')\n","input_index.observe(on_input_change, names='value')\n","\n","add_button.on_click(add_row)\n","delete_button.on_click(delete_row)\n","update_button.on_click(update_row)\n","save_button.on_click(save_row)\n","read_button.on_click(read_row)\n","\n","# 显示表格和交互式按钮\n","lock_buttons()\n","show_table()"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"12jXj6MutXiy"},"outputs":[],"source":["#@title ## 2.2、按照2.1的列表开始下载\n","#@markdown ####是否下载到网盘:##\n","saveInDrive = True # @param {type:'boolean'}\n","default_gdrive_path = \"/content/drive/MyDrive\" #@param {type: \"string\"}\n","base_dir = \"\"\n","if saveInDrive :\n"," base_dir = default_gdrive_path\n","else:\n"," base_dir = \"/content/sdwi\"\n","#@markdown ####(可选)只进行部分下载就填(复制名字按“与“字分割):##\n","mod_name = \"\" #@param {type: \"string\"}\n","sd0=\"s\"+\"t\"+\"a\"+\"b\"+\"l\"+\"e\"+\"-\"+\"d\"+\"i\"+\"f\"+\"f\"+\"u\"+\"s\"+\"i\"+\"o\"+\"n\"+\"-\"+\"w\"+\"e\"+\"b\"+\"u\"\n","sd=sd0+'i'\n","check_dir=\"\"\n","lora_dir=\"\"\n","if saveInDrive:\n"," check_dir=f(base_dir+\"/checkpoint\")\n"," lora_dir=f(base_dir+\"/lora\")\n","else:\n"," check_dir=f(base_dir+\"/models/Stable-diffusion\")\n"," lora_dir=f(base_dir+\"/models/Lora\")\n","\n","import json\n","import re\n","import subprocess\n","import shutil\n","import os\n","from concurrent.futures import ThreadPoolExecutor\n","\n","def swap(arr,l,r):\n"," temp=arr[l]\n"," arr[l]=arr[r]\n"," arr[r]=temp\n","\n","def checkRelateMod(oldMod, newMod):\n"," try:\n"," return oldMod and newMod and oldMod['downloadLink'] == newMod['downloadLink']\n"," except Exception as e:\n"," print(e)\n","\n","\n","def sanitize_filename(filename):\n"," # 定义不兼容字符的正则表达式模式\n"," pattern = r'[\\\\/:*?\"<>|]'\n"," # 使用下划线替换不兼容字符\n"," sanitized_filename = re.sub(pattern, '_', filename)\n"," return sanitized_filename\n","\n","def dealRelateMod(oldRenameMod, newRenameMod):\n"," old_file_path = f\"{oldRenameMod['path']}/{oldRenameMod['name']}\"\n"," #新模型的定义不带后缀名,所以需要加上处理,且在预处理情况下不知道路径那么只能沿用老模型的路径和名字\n"," new_file_dir= (\n"," newRenameMod['path']\n"," if newRenameMod.get('path') and newRenameMod['path'] != \"\"\n"," else oldRenameMod['path']\n"," )\n"," new_file_name=(\n"," sanitize_filename(newRenameMod['name'])+os.path.splitext(oldRenameMod['name'])[1]\n"," if newRenameMod['name'] and newRenameMod['name'] !=\"未命名\"\n"," else oldRenameMod['name']\n"," )\n"," new_file_path = f\"{new_file_dir}/{new_file_name}\"\n"," if old_file_path != new_file_path:\n"," try:\n"," if os.path.exists(old_file_path):\n"," os.rename(old_file_path,new_file_path)\n"," oldRenameMod['path']=new_file_dir\n"," oldRenameMod['name']=new_file_name\n"," print(f\"{old_file_path}更改为{new_file_path}成功\")\n"," else:\n"," print(f\"{old_file_path}不存在,重命名失败\")\n"," except OSError as e:\n"," print(f\"更改{old_file_path}时发生错误: {e}\")\n"," else:\n"," print(f\"{newRenameMod['name']}已存在且无任何改变,将忽视\")\n","\n","def deleteMod(oldDeMod):\n"," file_path = f\"{oldDeMod['path']}/{oldDeMod['name']}\"\n"," try:\n"," if os.path.exists(file_path):\n"," os.remove(file_path)\n"," #oldCo的行也要进行同步删除\n"," print(f\"{oldDeMod['name']}删除成功\")\n"," del oldDeMod\n"," else:\n"," print(f\"{oldDeMod['name']}不存在,删除失败\")\n"," except OSError as e:\n"," print(f\"删除{oldDeMod['name']}时发生错误: {e}\")\n","\n","def LCS(oldCo, newCo):\n"," oldStartIdx = 0 # 旧前指针\n"," newStartIdx = 0 # 新前指针\n"," oldEndIdx = len(oldCo) - 1 # 旧后指针\n"," newEndIdx = len(newCo) - 1 # 新后指针\n"," oldStartMod = oldCo[oldStartIdx] # 旧前Mod\n"," oldEndMod = oldCo[oldEndIdx] # 旧后Mod\n"," newStartMod = newCo[newStartIdx] # 新前Mod\n"," newEndMod = newCo[newEndIdx] # 新后Mod\n"," linkMap = {}\n"," while oldStartIdx <= oldEndIdx and newStartIdx <= newEndIdx:\n"," # 新前旧前\n"," if checkRelateMod(oldStartMod, newStartMod):\n"," dealRelateMod(oldStartMod, newStartMod)\n"," oldStartIdx += 1\n"," newStartIdx += 1\n"," if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:\n"," break\n"," oldStartMod = oldCo[oldStartIdx]\n"," newStartMod = newCo[newStartIdx]\n"," # 新后旧后\n"," elif checkRelateMod(oldEndMod, newEndMod):\n"," dealRelateMod(oldEndMod, newEndMod)\n"," oldEndIdx -= 1\n"," newEndIdx -= 1\n"," if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:\n"," break\n"," oldEndMod = oldCo[oldEndIdx]\n"," newEndMod = newCo[newEndIdx]\n"," # 新后旧前\n"," elif checkRelateMod(oldStartMod, newEndMod):\n"," dealRelateMod(oldStartMod, newEndMod)\n"," oldStartIdx += 1\n"," newEndIdx -= 1\n"," if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:\n"," break\n"," oldStartMod = oldCo[oldStartIdx]\n"," newEndMod = newCo[newEndIdx]\n"," # 新前旧后\n"," elif checkRelateMod(oldEndMod, newStartMod):\n"," dealRelateMod(oldEndMod, newStartMod)\n"," oldEndIdx -= 1\n"," newStartIdx += 1\n"," if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:\n"," break\n"," oldEndMod = oldCo[oldEndIdx]\n"," newStartMod = newCo[newStartIdx]\n"," # 四种均未找到\n"," else:\n"," # if not linkMap:\n"," linkMap = {}\n"," # 从 oldStartIdx 开始,到oldEndIdx结束,创建linkMap映射对象\n"," for i in range(oldStartIdx, oldEndIdx+1):\n"," downloadLink = oldCo[i]['downloadLink']\n"," if downloadLink is not None:\n"," linkMap[downloadLink] = i\n"," idxInOld = linkMap.get(newStartMod['downloadLink'], None)\n"," if idxInOld is None:\n"," print(f\"{newStartMod['name']}不存在于旧mod,{newStartMod['downloadLink']}将添加到下载任务队列\")\n"," content.append(newStartMod)\n","\n"," else:\n"," dealRelateMod(oldCo[idxInOld], newStartMod)\n"," swap(oldCo,idxInOld,oldEndIdx)\n"," oldEndIdx-=1\n"," # 指针下移,移动新的头\n"," newStartIdx += 1\n"," if newStartIdx > newEndIdx:\n"," break\n"," newStartMod = newCo[newStartIdx]\n"," oldEndMod=oldCo[oldEndIdx]\n"," # oldStartIdx += 1\n"," print('new',newStartIdx,newEndIdx)\n"," print('old',oldStartIdx,oldEndIdx)\n"," # new这里还有剩余Mod没有处理\n"," if newStartIdx <= newEndIdx:\n"," for i in range(newStartIdx, newEndIdx + 1):\n"," print(f\"{newCo[i]['name']}为新且不重复mod,将添加到下载任务队列\")\n"," content.append(newCo[i])\n","\n"," elif oldStartIdx <= oldEndIdx:\n"," for i in range(oldStartIdx, oldEndIdx + 1):\n"," if oldCo[i]:\n"," print(f\"{oldCo[i]['name']}为旧不重复mod,将删除\")\n"," deleteMod(oldCo[i])\n","\n","# LCS(oldArr,oldArr)\n","with open(mod_json_dir, 'r') as modelFile:\n"," preContent=json.loads(modelFile.read())\n","\n","#更新阶段\n","if 'oldCo' in globals() and len(oldCo)>0:\n"," content=[]\n"," print(oldCo)\n"," print(preContent)\n"," LCS(oldCo, preContent)\n"," print(content)\n","#初始化阶段\n","else:\n"," oldCo=[]\n"," content=preContent\n","\n","\n","#部分下载的情况\n","selected_mods = [x.strip() for x in mod_name.split('与') if x.strip()]\n","\n","\n","\n","\n","def get_file_size(file_path):\n"," try:\n"," file_size_bytes = os.path.getsize(file_path)\n"," for unit in ['B', 'KB', 'MB', 'GB', 'TB']:\n"," if file_size_bytes < 1024.0:\n"," break\n"," file_size_bytes /= 1024.0\n"," return f\"{file_size_bytes:.2f} {unit}\"\n"," except OSError as e:\n"," print(f\"Error: {e}\")\n"," return None\n","\n","def get_civitai_file(str):\n"," match = re.search(r'[^/]+$', str.decode())\n"," if match:\n"," return match.group(0).split('\\n')[0]\n"," else:\n"," print(\"无法从输出中提取文件名\")\n","\n","def move_model(source_path, target_path):\n"," try:\n"," shutil.move(source_path, target_path)\n"," except OSError as e:\n"," print(f\"Error: {e}\")\n","\n","def download_file(item):\n"," # for item in content:\n"," if selected_mods and item['name'] not in selected_mods:\n"," return\n"," download_url=item['downloadLink']\n"," #huggingface\n"," match = re.search(r'/([^/]*)$', download_url)\n"," file_name = match.group(1)\n"," #开始下载\n"," cmd = f\"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {lora_dir}\" if '.' not in match.group(1) else f\"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {lora_dir} -o {file_name}\"\n"," result = subprocess.run(cmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)\n"," if result.returncode == 0:\n"," # 判断是civitai还是hugging face还是自定义\n"," #civitai\n"," if '.' not in file_name:\n"," file_name =get_civitai_file(result.stdout)\n"," #如果定义了名字则重命名\n"," if item['name']!='未命名':\n"," #需要进行文件名的兼容处理\n"," temp_name=sanitize_filename(item['name'])+os.path.splitext(file_name)[1]\n"," os.rename(f\"{lora_dir}/{file_name}\",f\"{lora_dir}/{temp_name}\")\n"," final_name =temp_name\n"," else:\n"," final_name =file_name\n"," source_path = f\"{lora_dir}/{final_name}\"\n"," file_size = get_file_size(source_path)\n"," #如果path定义了则进行移动\n"," if item.get('path') and item['path'] != \"\":\n"," target_path = item['path']\n"," move_model(source_path, target_path)\n"," item['path']=target_path\n"," print(file_name+'已下载,重命名为:'+final_name)\n"," print('移动--',final_name,f'到{target_path}')\n"," #如果为checkpoint则进行移动\n"," elif file_size and 'GB'in file_size:\n"," move_model(source_path, check_dir)\n"," item['path']=check_dir\n"," print(file_name+'已下载,重命名为:'+final_name)\n"," print('移动checkpoint--',final_name,f'到{check_dir}文件夹')\n"," #如果为lora则直接调用source_path\n"," else:\n"," item['path']=lora_dir\n"," print(file_name+'已下载,重命名为:'+final_name)\n"," #改变item['name']用于最小化更新的重命名判断\n"," item['name']=final_name\n"," oldCo.append(item)\n"," else:\n"," print(f\"{item['name']}下载失败,请检查{item['downloadLink']}\")\n","\n","# download_file(item)\n","# 使用线程池进行下载\n","with ThreadPoolExecutor(max_workers=5) as executor:\n"," for item in content:\n"," executor.submit(download_file, item)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2E9ZDP8OvYsf"},"outputs":[],"source":["#@title # **3、运行/重启**\n","from IPython.utils import capture\n","from IPython.display import clear_output\n","import os\n","sd0=\"s\"+\"t\"+\"a\"+\"b\"+\"l\"+\"e\"+\"-\"+\"d\"+\"i\"+\"f\"+\"f\"+\"u\"+\"s\"+\"i\"+\"o\"+\"n\"+\"-\"+\"w\"+\"e\"+\"b\"+\"u\"\n","sd=sd0+'i'\n","#@markdown ####从云盘启动:##\n","startInDrive = True # @param {type:'boolean'}\n","default_gdrive_path = \"/content/drive/MyDrive\" #@param {type: \"string\"}\n","base_dir = \"\"\n","if startInDrive :\n"," base_dir = default_gdrive_path + \"/sdwi\"\n","else:\n"," base_dir = \"/content/sdwi\"\n","%cd {base_dir}\n","# outputs_dir = base_dir + \"/outputs\"\n","# if os.path.exists(outputs_dir):\n","# pass\n","# else:\n","# !mkdir outputs\n","# print(\"创建outputs目录\")\n","os.environ['TRANSFORMERS_CACHE']=os.path.join(base_dir, \"cache\")\n","os.environ['TORCH_HOME'] = os.path.join(base_dir, \"cache\")\n","\n","# with capture.capture_output() as cap:\n","# !git reset --hard\n","# !git checkout main\n","# time.sleep(1)\n","# !rm webui.sh\n","# !git pull\n","# clear_output()\n","#@markdown ####启动定制版Demo:##\n","is_Demo = True # @param {type:'boolean'}\n","#@markdown ####全精度/半精度启动:##\n","is_full_precision = True # @param {type:'boolean'}\n","#@markdown #(重要!! **ngrok** 必须获取,第一次使用先注册,然后邮件验证帐号再获取token)\n","#@markdown #### 验证后填入一次即可\n","#@markdown ####(获取[ngrok](https://dashboard.ngrok.com/get-started/your-authtoken)的token进行免费网络加速:##\n","ngrok_auth=\"2V9HDEfPlGGfJTb2b8s334hhOc2_oA8jroor1ZN6Sw9FM6Fu\" #@param {type:\"string\"}\n","\n","# !git -C base_dir/repositories/k-diffusion reset --hard HEAD\n","\n","!pip uninstall torch -y\n","!pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118\n","\n","if ngrok_auth == \"\":\n"," print(\"请获取token后重新运行\")\n","else:\n"," if is_Demo:\n"," full_precision_str=\"--nowebui --port 7789 --share --lowram --disable-safe-unpickle --disable-console-progressbars --xformers --enable-insecure-extension-access --precision full --no-half --no-half-vae --opt-sub-quad-attention --opt-channelslast --api\"\n"," half_precision_str=\"--nowebui --port 7789 --share --lowram --disable-safe-unpickle --disable-console-progressbars --xformers --enable-insecure-extension-access --opt-sub-quad-attention --opt-channelslast --api\"\n","\n"," if ngrok_auth:\n"," full_precision_str+=f\" --ngrok={ngrok_auth} --ngrok-region='auto'\"\n"," half_precision_str+=f\" --ngrok={ngrok_auth} --ngrok-region='auto'\"\n"," if is_full_precision:\n"," !python launch.py {full_precision_str} #(解决精度足但速度不够)\n"," else:\n"," !pyt\n"," else:\n"," full_precision_str=\"--share --lowram --disable-safe-unpickle --disable-console-progressbars --xformers --enable-insecure-extension-access --precision full --no-half --no-half-vae --opt-sub-quad-attention --opt-channelslast --api\"\n"," half_precision_str=\"--share --lowram --disable-safe-unpickle --disable-console-progressbars --xformers --enable-insecure-extension-access --opt-sub-quad-attention --opt-channelslast --api\"\n","\n"," if ngrok_auth:\n"," full_precision_str+=f\" --ngrok={ngrok_auth} --ngrok-region='auto'\"\n"," half_precision_str+=f\" --ngrok={ngrok_auth} --ngrok-region='auto'\"\n"," if is_full_precision:\n"," !python launch.py {full_precision_str} #(解决精度足但速度不够)\n"," else:\n"," !python launch.py {half_precision_str} #半精度(速度提升1倍以上,但可能出现精度不足问题)"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"oDLTyKVoxKXa"},"outputs":[],"source":["#@title ## 其他自定义下载\n","#@markdown ####下载地址:##\n","download_url = \"\" #@param {type: \"string\"}\n","#@markdown ####存放目录:##\n","download_dir = \"\" #@param {type: \"string\"}\n","#@markdown ####文件名:##\n","download_name = \"\" #@param {type: \"string\"}\n","\n","!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {download_dir} -o {download_name}\n"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"ZWVzwUYWxFO0"},"outputs":[],"source":["#@title ## 删除临时硬盘的SD,重新安装和部署(别乱点)(用于临时部署模式非网盘模式)\n","sd0=\"s\"+\"t\"+\"a\"+\"b\"+\"l\"+\"e\"+\"-\"+\"d\"+\"i\"+\"f\"+\"f\"+\"u\"+\"s\"+\"i\"+\"o\"+\"n\"+\"-\"+\"w\"+\"e\"+\"b\"+\"u\"\n","sd=sd0+\"i\"\n","import os\n","import shutil\n","import ipywidgets as widgets\n","import time\n","from IPython.display import display\n","from google.colab import output\n","\n","def delete_lora_folder(b):\n"," folder_path = f'/content/sdwi'\n"," if os.path.exists(folder_path):\n"," shutil.rmtree(folder_path)\n"," print(\"已成功删除,请重新安装\")\n"," time.sleep(5)\n"," output.clear()\n"," else:\n"," print(\"已取消删除\")\n"," time.sleep(5)\n"," output.clear()\n","\n","def on_button_click(button):\n"," delete_lora_folder(button)\n","\n","button = widgets.Button(description=\"确定要删除吗?\")\n","button.on_click(on_button_click)\n","display(button)\n"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"Zc6h5mN_Xdda"},"outputs":[],"source":["\n","#@title 查看GPU和VRAM可用信息\n","\n","!nvidia-smi --query-gpu=name,memory.total,memory.free --format=csv,noheader"]},{"cell_type":"markdown","metadata":{"id":"wjvS2oV3eozH"},"source":["# 模型下载"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"kiuK1rJ5aeOl"},"outputs":[],"source":["#@title Checkpoints/safetensors等大模型下载\n","a=\"stable\"\n","b=\"Stable\"\n","url = \"https://civitai.com/api/download/models/128713\" #@param {type:\"string\"}\n","#@ markdown - _Remote source url._\n","destination_folder = \"/content/drive/MyDrive/checkpoint\" #@param {type:\"string\"}\n","#@ markdown - _Find folder in gdrive on the left and right-click to copy the path._\n","filename = \"DreamShaper.safetensors\" #@param {type:\"string\"}\n","#@ markdown - _Filename with extension._\n","\n","target = f'{destination_folder}/{filename}'\n","\n","!gdown -O $target \"$url\"\n"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"Jvnx3qoaa9gb"},"outputs":[],"source":["#@title Lora模型下载\n","a=\"stable\"\n","url = \"https://civitai.com/api/download/models/150273\" #@param {type:\"string\"}\n","#@ markdown - _Remote source url._\n","destination_folder = \"/content/drive/MyDrive/lora\" #@param {type:\"string\"}\n","#@ markdown - _Find folder in gdrive on the left and right-click to copy the path._\n","filename = \"Genshin.safetensors\" #@param {type:\"string\"}\n","#@ markdown - _Filename with extension._\n","\n","target = f'{destination_folder}/{filename}'\n","\n","!gdown --fuzzy -O $target \"$url\""]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"h_gQJGj8bLAr"},"outputs":[],"source":["#@title embeddings模型下载\n","a=\"stable\"\n","url = \"https://huggingface.co/datasets/gsdf/EasyNegative/resolve/main/EasyNegative.safetensors\" #@param {type:\"string\"}\n","#@ markdown - _Remote source url._\n","destination_folder = \"/content/drive/MyDrive/embeddings\" #@param {type:\"string\"}\n","#@ markdown - _Find folder in gdrive on the left and right-click to copy the path._\n","filename = \"EasyNegative.safetensors\" #@param {type:\"string\"}\n","#@ markdown - _Filename with extension._\n","\n","target = f'{destination_folder}/{filename}'\n","\n","!gdown --fuzzy -O $target \"$url\""]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"Q9pSXyDYbVMn"},"outputs":[],"source":["\n","#@title VAE下载\n","a=\"stable\"\n","url = \"https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors\" #@param {type:\"string\"}\n","#@ markdown - _Remote source url._\n","destination_folder = \"/content/drive/MyDrive/VAE\" #@param {type:\"string\"}\n","#@ markdown - _Find folder in gdrive on the left and right-click to copy the path._\n","filename = \"vae-ft-mse-840000-ema-pruned.safetensors\" #@param {type:\"string\"}\n","#@ markdown - _Filename with extension._\n","\n","target = f'{destination_folder}/{filename}'\n","\n","!gdown --fuzzy -O $target \"$url\""]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"xxITLG1PbpCD"},"outputs":[],"source":["#@title 自定义下载\n","url = \"https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors\" #@param {type:\"string\"}\n","#@markdown - _下载链接._\n","destination_folder = \"/content/drive/MyDrive/models\" #@param {type:\"string\"}\n","#@markdown - _在google云盘中找到想要下载的目标路径右键复制地址._\n","filename = \"vae-ft-mse-840000-ema-pruned.safetensors\" #@param {type:\"string\"}\n","#@markdown - _保存的文件名._\n","\n","target = f'{destination}/{filename}'\n","\n","!gdown --fuzzy -O $target \"$url\""]},{"cell_type":"markdown","metadata":{"id":"pBpE1IiZhgei"},"source":["# 生成图片下载到本地"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"m1mmDY3FdJY_"},"outputs":[],"source":["#@title 从云盘下载生成图片到本地\n","from google.colab import files\n","%ls\n","a=\"stable\"\n","!zip -r /content/outputs.zip /content/drive/MyDrive/sdwi/outputs\n","files.download('/content/outputs.zip')"]},{"cell_type":"markdown","metadata":{"id":"RhSQKDA0or9E"},"source":["# **安装调试服务环境**"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"SUwLzrZMoow7"},"outputs":[],"source":["#@title ## 1、调试环境\n","import random, string, urllib.request, json, getpass\n","\n","#Generate root password\n","password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))\n","\n","#Download ngrok\n","! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n","! unzip -qq -n ngrok-stable-linux-amd64.zip\n","\n","#Setup sshd\n","! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null\n","\n","#Set root password\n","! echo root:$password | chpasswd\n","! mkdir -p /var/run/sshd\n","! echo \"PermitRootLogin yes\" >> /etc/ssh/sshd_config\n","! echo \"PasswordAuthentication yes\" >> /etc/ssh/sshd_config\n","! echo \"LD_LIBRARY_PATH=/usr/lib64-nvidia\" >> /root/.bashrc\n","! echo \"export LD_LIBRARY_PATH\" >> /root/.bashrc\n","\n","#Run sshd\n","get_ipython().system_raw('/usr/sbin/sshd -D &')\n","\n","#Ask token\n","print(\"Copy authtoken from https://dashboard.ngrok.com/auth\")\n","authtoken = getpass.getpass()\n","\n","#Create tunnel\n","get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')\n","\n","#Get public address and print connect command\n","with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:\n"," data = json.loads(response.read().decode())\n"," (host, port) = data['tunnels'][0]['public_url'][6:].split(':')\n"," print(f'SSH command: ssh -p{port} root@{host}')\n","\n","#Print root password\n","print(f'Root password: {password}')\n"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"DFzGN70drB0j"},"outputs":[],"source":["#@title ## 2、安装SSH服务\n","! apt-get install openssh-server\n","! mkdir /var/run/sshd\n","! echo \"root:root\" | chpasswd\n","! sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config\n","! service ssh restart"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"Km8yBU7orG_C"},"outputs":[],"source":["#@title ## 3、获取链接信息\n","import socket\n","print(socket.gethostbyname(socket.gethostname()))"]}],"metadata":{"accelerator":"GPU","colab":{"collapsed_sections":["wjvS2oV3eozH","pBpE1IiZhgei"],"gpuType":"T4","private_outputs":true,"provenance":[],"machine_shape":"hm"},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.8"},"vscode":{"interpreter":{"hash":"e593ac106456af50ce7af38f9671c411b49d6cd90f9b885e167f0f594e09038c"}}},"nbformat":4,"nbformat_minor":0}