Lcat

Lcat

How to Deploy Stable Diffusion Web UI Colab on Google Colab

Too Long; Didn't Read#

Click the link below to immediately enter Google Colab to deploy Stable Diffusion Web UI Colab.
``https://colab.research.google.com/github/camenduru/stable-diffusion-webui-colab/blob/main/stable/chillout_mix_webui_colab.ipynb

1. What is Stable Diffusion Web UI Colab#

Stable Diffusion is a text-to-image AI model based on deep learning technology launched by Stability AI.

2. Stable Diffusion Web UI#

is a powerful and user-friendly Stable Diffusion web application that you can use to operate Stable Diffusion to generate images. It is generally referred to as A1111 in its GitHub account.

To run Stable Diffusion, your computer needs to have a powerful GPU and a large amount of RAM. However, if you do not have such hardware, you can still spend a little money to run Stable Diffusion Web UI Colab on Google Colab.

Stable Diffusion Web UI Colab is a fork project that can deploy Stable Diffusion Web UI on Google Colab. Different models' .ipynb files are already prepared in its GitHub repository's README, and you only need to click one of the "Open in Colab" buttons to deploy Stable Diffusion Web UI Colab on Google Colab with one click.

3. What is Google Colab#

Colaboratory, abbreviated as Google Colab, is a product developed by the Google Research team. In Colab, anyone can write and execute any Python code through a browser. It is especially suitable for machine learning, data analysis, and educational purposes. Technically, Colab is a hosted Jupyter notebook service. Its emergence allows users to start writing code directly in the browser without needing to install any environment locally, which is very convenient.

Colab offers a free version, a pay-as-you-go version, and a professional version. The resources (compute units) provided by the free version are limited, but they are sufficient for most people. If you need more resources, you can consider subscribing to the professional version and happily use the A100 GPU for AIGC.

According to the latest tweet from the Colab team leader, due to the overwhelming number of users using Colab for image generation, only paid users are now allowed to use Stable Diffusion in Colab.

4. What is .ipynb#

The file type executed on Google Colab is .ipynb. .ipynb is a Jupyter Notebook file format that can perform computations. You can write code in Colab or open .ipynb files from GitHub repositories directly in Colab to run them.

5. Basic Usage#

Stable Diffusion Web UI Colab V2.0 is now available for use, and compared to the main branch version, it can better utilize plugins like ControlNet and LoRA. I recommend starting directly from 2.0.

The main GitHub repository of Stable Diffusion Web UI Colab provides a series of .ipynb files that can be executed on Google Colab, each corresponding to a model. You can click one of the "Open in Colab" buttons in the README to jump to Google Colab and deploy Stable Diffusion Web UI Colab with one click.

When you have entered Google Colab through the .ipynb file on GitHub, you are just one click away from completing the deployment. Click the ▶️ run button in the code block to start deploying Stable Diffusion Web UI Colab.

The deployment may take a few minutes, mainly depending on the size of the model you choose.

Once the deployment is complete, a Gradio link (similar to: https://xxxxxxxxxx.gradio.live) will be displayed at the end of the output log. Click it to open the Stable Diffusion Web UI Colab interface.
My Custom Notes

The .ipynb notebook provided by Stable Diffusion Web UI Colab is already a very complete one-click script, but I want to load multiple models based on my usage needs, so I made some adjustments to it.

Google Colab can only run one code block at a time. If you need to install additional models or plugins by executing the notebook, you need to stop the currently running code block before executing a new one. Therefore, I divided the code blocks into three parts: installation, downloading models, and starting, and added a section for saving outputs to save results after completing the operations.

If you encounter a problem where the GPU cannot be found when running your newly created notebook, please switch to a GPU type runtime environment in the Runtime settings.
Installation

Stable Diffusion Web UI Colab only needs to be installed once, but it may need to be stopped and restarted multiple times. During restarts, there is no need to reinstall and download the models. So I referred to the official script, commented out the installation and model downloading code blocks, and copied them into separate code blocks.

The installation code block generally only needs to be executed once, and it is recommended to place it at the bottom of the notebook.

import os
from google.colab.output import eval_js
os.environ['colab_url'] = eval_js("google.colab.kernel.proxyPort(7860, {'cache': false})")

!apt -y update -qq
!wget http://launchpadlibrarian.net/367274644/libgoogle-perftools-dev_2.5-2.2ubuntu3_amd64.deb
!wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/google-perftools_2.5-2.2ubuntu3_all.deb
!wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libtcmalloc-minimal4_2.5-2.2ubuntu3_amd64.deb
!wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libgoogle-perftools4_2.5-2.2ubuntu3_amd64.deb
!apt install -qq libunwind8-dev
!dpkg -i *.deb
%env LD_PRELOAD=libtcmalloc.so
!rm *.deb

!apt -y install -qq aria2
!pip install -q --pre xformers
!pip install -q --pre triton

!git clone -b v2.0 https://github.com/camenduru/stable-diffusion-webui
!wget https://raw.githubusercontent.com/camenduru/stable-diffusion-webui-scripts/main/run_n_times.py -O /content/stable-diffusion-webui/scripts/run_n_times.py
!git clone https://github.com/deforum-art/deforum-for-automatic1111-webui /content/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui
!git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser /content/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser
!git clone https://github.com/camenduru/stable-diffusion-webui-huggingface /content/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface
!git clone -b v2.0 https://github.com/camenduru/sd-civitai-browser /content/stable-diffusion-webui/extensions/sd-civitai-browser
!git clone https://github.com/kohya-ss/sd-webui-additional-networks /content/stable-diffusion-webui/extensions/sd-webui-additional-networks
!git clone https://github.com/Mikubill/sd-webui-controlnet /content/stable-diffusion-webui/extensions/sd-webui-controlnet
!git clone https://github.com/camenduru/openpose-editor /content/stable-diffusion-webui/extensions/openpose-editor
!git clone https://github.com/jexom/sd-webui-depth-lib /content/stable-diffusion-webui/extensions/sd-webui-depth-lib
!git clone https://github.com/hnmr293/posex /content/stable-diffusion-webui/extensions/posex
!git clone https://github.com/camenduru/sd-webui-tunnels /content/stable-diffusion-webui/extensions/sd-webui-tunnels
!git clone https://github.com/etherealxx/batchlinks-webui /content/stable-diffusion-webui/extensions/batchlinks-webui
%cd /content/stable-diffusion-webui
!git reset --hard

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_canny-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_canny-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_depth-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_depth-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_hed-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_hed-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_mlsd-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_mlsd-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_normal-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_normal-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_openpose-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_openpose-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_scribble-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_scribble-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_seg-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_seg-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/hand_pose_model.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/openpose -o hand_pose_model.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/body_pose_model.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/openpose -o body_pose_model.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/dpt_hybrid-midas-501f0c75.pt -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/midas -o dpt_hybrid-midas-501f0c75.pt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/mlsd_large_512_fp32.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/mlsd -o mlsd_large_512_fp32.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/mlsd_tiny_512_fp32.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/mlsd -o mlsd_tiny_512_fp32.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/network-bsds500.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/hed -o network-bsds500.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/upernet_global_small.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/uniformer -o upernet_global_small.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_style_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_style_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_sketch_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_sketch_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_seg_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_seg_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_openpose_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_openpose_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_keypose_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_keypose_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_depth_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_depth_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_color_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_color_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_canny_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_canny_sd14v1.pth

# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/sd14/resolve/main/sd-v1-4.ckpt -d /content/stable-diffusion-webui/models/Stable-diffusion -o sd-v1-4.ckpt

!sed -i -e '''/    prepare_environment()/a\    os.system\(f\"""sed -i -e ''\"s/dict()))/dict())).cuda()/g\"'' /content/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/util.py""")''' /content/stable-diffusion-webui/launch.py

!mkdir /content/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/models

# !python launch.py --share --xformers --enable-insecure-extension-access --theme dark --gradio-queue --cloudflared

Download Models

Stable Diffusion Web UI has built-in extensions to download models from Civitai, but you can also use wget or aria2 commands to download models from other sources in the notebook.

Here I have prepared a collection of Asian beauty models for Chinese users, for you to use with one click.
(1) Download the main model

# Download the main model, please download according to your needs
!wget https://civitai.com/api/download/models/11745 -O /content/stable-diffusion-webui/models/Stable-diffusion/Chilloutmix-Ni-pruned-fp32-fix.safetensors
# !wget https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned.safetensors -O /content/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned.safetensors
# !wget https://huggingface.co/DucHaiten/DucHaitenAIart/resolve/main/DucHaitenAIart_v2.0.safetensors -O /content/stable-diffusion-webui/models/DucHaitenAIart_v2.0.safetensors

(2) Download Lora models

# Download Lora models
!mkdir -p /content/stable-diffusion-webui/models/Lora
!wget https://civitai.com/api/download/models/12050 -O /content/stable-diffusion-webui/models/Lora/japaneseDollLikeness_v10.safetensors
!wget https://civitai.com/api/download/models/8750 -O /content/stable-diffusion-webui/models/Lora/koreanDollLikeness_v10.safetensors

(3) Start SD Web UI

When you need to start or restart the Web UI, you can run the code below to launch it.

# Launch
%cd /content/stable-diffusion-webui
!python launch.py --share --xformers --enable-insecure-extension-access --gradio-queue --theme dark --gradio-queue --cloudflared
# !python launch.py --share --xformers --enable-insecure-extension-access --theme dark --gradio-queue --cloudflared --vae-path "/content/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors"

6. Save Outputs#

Colab has a built-in file browser, but you cannot download files in bulk from it.

So I choose to use zip to package the outputs and save them to Google Drive.

When mounting Google Drive, an authorization window will pop up, and then you can allow the authorized account.

# Mount Google Drive, authorization modal will show up
from google.colab import drive
drive.mount('/content/drive')

# Zip images and save to Google Drive
%cd /content/stable-diffusion-webui/outputs 
!zip -r /content/drive/MyDrive/images.zip txt2img-images

Have fun generating images with Stable Diffusion!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.