{"cells":[{"cell_type":"markdown","metadata":{"id":"UDSpxrglM4Kd"},"source":["# 环境安装及运行\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SaAJk33ppFw1"},"outputs":[],"source":["#@title 1、安装环境并运行\n","a=\"stable\"\n","b=\"Stable\"\n","c=\"sd\"\n","%cd /content\n","\n","%env TF_CPP_MIN_LOG_LEVEL=1\n","\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","\n","!apt -y install -qq aria2 libcairo2-dev pkg-config python3-dev\n","!pip install -q torch==2.0.1+cu118 torchvision==0.15.2+cu118 torchaudio==2.0.2+cu118 torchtext==0.15.2 torchdata==0.6.1 --extra-index-url https://download.pytorch.org/whl/cu118 -U\n","!pip install -q xformers==0.0.20 triton==2.0.0 gradio_client==0.2.7 -U\n","\n","# !git clone -b v2.5 https://github.com/camenduru/{a}-diffusion-webui\n","!git clone https://github.com/NullFullme/sd-v1.5.1-server.git\n","!git clone https://huggingface.co/embed/negative /content/sd-v1.5.1-server/embeddings/negative\n","!git clone https://huggingface.co/embed/lora /content/sd-v1.5.1-server/models/Lora/positive\n","!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d /content/sd-v1.5.1-server/models/ESRGAN -o 4x-UltraSharp.pth\n","!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 /content/sd-v1.5.1-server/models/ESRGAN -o RealESRGAN_x4plus_anime_6B.pth\n","!wget https://raw.githubusercontent.com/camenduru/{a}-diffusion-webui-scripts/main/run_n_times.py -O /content/sd-v1.5.1-server/scripts/run_n_times.py\n","!git clone https://github.com/deforum-art/deforum-for-automatic1111-webui /content/sd-v1.5.1-server/extensions/deforum-for-automatic1111-webui\n","!git clone https://github.com/butaixianran/{b}-Diffusion-Webui-Civitai-Helper /content/sd-v1.5.1-server/extensions/{b}-Diffusion-Webui-Civitai-Helper\n","!git clone https://github.com/Mikubill/{c}-webui-controlnet /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet\n","!git -C /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet reset --hard 784eadbb\n","# !git clone https://github.com/fkunn1326/openpose-editor /content/sd-v1.5.1-server/extensions/openpose-editor\n","!git clone https://github.com/jexom/{c}-webui-depth-lib /content/sd-v1.5.1-server/extensions/{c}-webui-depth-lib\n","!git clone https://github.com/camenduru/{a}-diffusion-webui-huggingface /content/sd-v1.5.1-server/extensions/{a}-diffusion-webui-huggingface\n","# !git clone https://github.com/kohya-ss/{c}-webui-additional-networks /content/sd-v1.5.1-server/extensions/{c}-webui-additional-networks\n","# !git clone https://github.com/hnmr293/posex /content/sd-v1.5.1-server/extensions/posex\n","# !git clone https://github.com/nonnonstop/{c}-webui-3d-open-pose-editor /content/sd-v1.5.1-server/extensions/{c}-webui-3d-open-pose-editor\n","!git clone https://github.com/camenduru/{c}-webui-tunnels /content/sd-v1.5.1-server/extensions/{c}-webui-tunnels\n","# !git clone https://github.com/etherealxx/batchlinks-webui /content/sd-v1.5.1-server/extensions/batchlinks-webui\n","# !git clone https://github.com/catppuccin/{a}-diffusion-webui /content/sd-v1.5.1-server/extensions/{a}-diffusion-webui-catppuccin\n","# !git clone https://github.com/AUTOMATIC1111/{a}-diffusion-webui-rembg /content/sd-v1.5.1-server/extensions/{a}-diffusion-webui-rembg\n","!git clone https://github.com/ashen-sensored/{a}-diffusion-webui-two-shot /content/sd-v1.5.1-server/extensions/{a}-diffusion-webui-two-shot\n","!git clone https://github.com/thomasasfk/{c}-webui-aspect-ratio-helper /content/sd-v1.5.1-server/extensions/{c}-webui-aspect-ratio-helper\n","# !git clone https://github.com/Bing-su/adetailer /content/sd-v1.5.1-server/extensions/adetailer\n","!git clone https://github.com/mcmonkeyprojects/{c}-dynamic-thresholding /content/sd-v1.5.1-server/extensions/{c}-dynamic-thresholding\n","!git clone https://github.com/adieyal/{c}-dynamic-prompts /content/sd-v1.5.1-server/extensions/{c}-dynamic-prompts\n","# !git clone https://github.com/AUTOMATIC1111/{a}-diffusion-webui-wildcards /content/sd-v1.5.1-server/extensions/{a}-diffusion-webui-wildcards\n","# !git clone https://github.com/continue-revolution/{c}-webui-segment-anything /content/sd-v1.5.1-server/extensions/{c}-webui-segment-anything\n","# !git clone https://gitcode.net/ranting8323/{a}-diffusion-webui-localization-zh_CN.git /content/sd-v1.5.1-server/extensions/{a}-diffusion-webui-localization-zh_CN\n","!git clone https://github.com/pkuliyi2015/multidiffusion-upscaler-for-automatic1111 /content/sd-v1.5.1-server/extensions/multidiffusion-upscaler-for-automatic1111\n","!git clone https://github.com/Physton/{c}-webui-prompt-all-in-one.git /content/sd-v1.5.1-server/extensions/{c}-webui-prompt-all-in-one.git\n","\n","\n","%cd /content/sd-v1.5.1-server\n","!git reset --hard\n","!git -C /content/sd-v1.5.1-server/repositories/{a}-diffusion-stability-ai reset --hard\n","\n","# ControlNet Model\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11e_sd15_ip2p.pth\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11e_sd15_shuffle.pth\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11f1e_sd15_tile.pth\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11f1p_sd15_depth.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_sd15s2_lineart_anime.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15s2_lineart_anime.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_canny.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -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 /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -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 /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_lineart.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_mlsd.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_mlsd.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_normalbae.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_normalbae.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_openpose.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_openpose.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_scribble.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_scribble.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_seg.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_seg.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_softedge.pth -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_softedge.pth\n","# ---yaml\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11e_sd15_ip2p.yaml\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11e_sd15_shuffle.yaml\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11f1e_sd15_tile.yaml\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11f1p_sd15_depth.yaml\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15s2_lineart_anime.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15s2_lineart_anime.yaml\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.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_canny.yaml\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.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_inpaint.yaml\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.yaml -d /content/sd-v1.5.1-server/extensions/{c}-webui-controlnet/models -o control_v11p_sd15_lineart.yaml\n","\n","!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/94640 -d /content/sd-v1.5.1-server/models/{b}-diffusion/ -o majicmixRealistic_v6.safetensors\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/46846 -d /content/sd-v1.5.1-server/models/{b}-diffusion/ -o revAnimated_v122.safetensors\n","!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/62833 -d /content/sd-v1.5.1-server/models/Lora/ -o add_detail.safetensors\n","# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/32988 -d /content/sd-v1.5.1-server/models/Lora/ -o blindbox_v1_mix.safetensors\n","\n","\n","\n","!sed -i -e '''/from modules import launch_utils/a\\import os''' /content/sd-v1.5.1-server/launch.py\n","!sed -i -e '''/ prepare_environment()/a\\ os.system\\(f\\\"\"\"sed -i -e ''\\\"s/dict()))/dict())).cuda()/g\\\"'' /content/sd-v1.5.1-server/repositories/{a}-diffusion-stability-ai/ldm/util.py\"\"\")''' /content/sd-v1.5.1-server/launch.py\n","!sed -i -e 's/\\\"sd_model_checkpoint\\\"\\,/\\\"sd_model_checkpoint\\,sd_vae\\,CLIP_stop_at_last_layers\\\"\\,/g' /content/sd-v1.5.1-server/modules/shared.py\n","\n","!python launch.py --xformers --opt-channelslast --api --api-log --autolaunch --listen --share --gradio-queue --enable-insecure-extension-access --multiple"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qzuCoKRvhLRj"},"outputs":[],"source":["#@title 2、开始运行\n","a=\"stable\"\n","\n","%cd /content/sd-v1.5.1-server/\n","\n","!python launch.py --xformers --opt-channelslast --api --api-log --autolaunch --listen --share --gradio-queue --enable-insecure-extension-access --multiple\n"]},{"cell_type":"code","execution_count":null,"metadata":{"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":["\n","#@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/\" + \"sd-v1.5.1-server/models/\" + b+\"-diffusion\" #@ 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/\" + \"sd-v1.5.1-server/models/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/\" + \"sd-v1.5.1-server/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/\" + \"sd-v1.5.1-server/models/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":{"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/sd-v1.5.1-server/models/VAE\" #@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":{"id":"m1mmDY3FdJY_"},"outputs":[],"source":["#@title 下载生成图片到本地\n","from google.colab import files\n","%ls\n","a=\"stable\"\n","!zip -r /content/outputs.zip /content/sd-v1.5.1-server/outputs\n","files.download('/content/outputs.zip')"]},{"cell_type":"markdown","metadata":{"id":"RhSQKDA0or9E"},"source":["# **安装调试服务环境**"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SUwLzrZMoow7"},"outputs":[],"source":["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":"markdown","metadata":{"id":"jnXg_u4spx_x"},"source":["安装SSH服务"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DFzGN70drB0j"},"outputs":[],"source":["! 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":{"id":"Km8yBU7orG_C"},"outputs":[],"source":["import socket\n","print(socket.gethostbyname(socket.gethostname()))"]}],"metadata":{"accelerator":"GPU","colab":{"collapsed_sections":["wjvS2oV3eozH","pBpE1IiZhgei"],"gpuType":"T4","private_outputs":true,"provenance":[{"file_id":"1h9ZpxTd63Ff9xCu79D9lyr533b7u4LBS","timestamp":1694188605046},{"file_id":"https://github.com/DeekshithSH/stable-diffusion-webui-colab/blob/main/stable/anything_5_webui_colab.ipynb","timestamp":1692451985697}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.8"},"vscode":{"interpreter":{"hash":"e593ac106456af50ce7af38f9671c411b49d6cd90f9b885e167f0f594e09038c"}}},"nbformat":4,"nbformat_minor":0}