avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Introduction to Stable Diffusion Web UI Colab v2.0 for Beginners

Stable Diffusion Web UI Colab is a fork project to deploy AUTOMATIC1111's Stable Diffusion web UI on Google Colab.

Colab provides virtual machine with GPU, so you don't have to own a powerful GPU to enjoy AI computing. You can pay as you go with Google Colab.

And stable diffusion webui colab v2.0 is now available!

Here are some notes on how to use it.

Basic Usage

Stable Diffusion Web UI Colab can be deployed easily using the given .ipynb file, which is a Jupyter Notebook, and you can run it on Google Colab to deploy the Stable Diffusion web UI.

  • First, you simply just need to click one of Open in Colab button in the repository to open the notebook in Colab. Each button configures for a specific model.
  • Then, you can run the notebook to deploy A1111 by clicking the run button on top left corner of the notebook.
  • After the deployment is finished, a gradio URL will show up in the output like https://xxxxxxxxx.gradio.live for you to open the web UI in your browser.

Open In Colab

The notebook you run will be something like this:

  • Be sure you have selected GPU as the runtime type in Colab, otherwise the deployment will fail.
!apt-get -y install -qq aria2
!pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.16/xformers-0.0.16+814314d.d20230118-cp38-cp38-linux_x86_64.whl
!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
%cd /content/stable-diffusion-webui

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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/webui/ControlNet-modules-safetensors/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

!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

I just commented out the model download part and launch part, so I can download models manually before launching the web UI.

Download Models

There is a built-in extension to download models from Civitai in web UI, but you can also download models from other sources using wget command in notebook.

Google Colab can only run 1 piece of code at a time, to download models manually, you must first stop the running code by clicking the stop button on top left corner of the notebook, then run the following code to download models.

# Download models
!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/DucHaiten/DucHaitenAIart/resolve/main/DucHaitenAIart_v2.0.safetensors -O /content/stable-diffusion-webui/models/DucHaitenAIart_v2.0.safetensors

!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
!wget https://civitai.com/api/download/models/9070 -O /content/stable-diffusion-webui/models/Lora/taiwanDollLikeness_v10.safetensors
!wget https://civitai.com/api/download/models/11195 -O /content/stable-diffusion-webui/models/Lora/LORAChineseDoll_chinesedolllikeness1.safetensors
!wget https://civitai.com/api/download/models/13397 -O /content/stable-diffusion-webui/models/Lora/asiangirllikeness_v20.safetensors
# !wget https://civitai.com/api/download/models/7974 -O /content/stable-diffusion-webui/models/Lora/arknightsTexasThe_v10.safetensors
# !wget https://civitai.com/api/download/models/7840 -O /content/stable-diffusion-webui/models/Lora/stLouisLuxuriousWheels_v1.safetensors
# !wget https://civitai.com/api/download/models/11520 -O /content/stable-diffusion-webui/models/Lora/ganyuGenshinImpact_v10.safetensors
# !wget https://civitai.com/api/download/models/5835 -O /content/stable-diffusion-webui/models/Lora/wlopStyleLora_30Epochs.safetensors
# !wget https://civitai.com/api/download/models/6370 -O /content/stable-diffusion-webui/models/Lora/lucyCyberpunk_35Epochs.safetensors
# !wget https://civitai.com/api/download/models/8174 -O /content/stable-diffusion-webui/models/Lora/harunoSakura_v11.safetensors
# !wget https://civitai.com/api/download/models/8347 -O /content/stable-diffusion-webui/models/Lora/blatap3_v10.safetensors
# !wget https://civitai.com/api/download/models/7460 -O /content/stable-diffusion-webui/models/Lora/TsumasakyCCCodeGeass_v1.safetensors
# !wget https://civitai.com/api/download/models/6031 -O /content/stable-diffusion-webui/models/Lora/yorha_noDOT_2_type_b.safetensors
# !wget https://civitai.com/api/download/models/11689 -O /content/stable-diffusion-webui/models/Lora/keqingGenshinImpact_v10.safetensors
# !wget https://civitai.com/api/download/models/11523 -O /content/stable-diffusion-webui/models/Lora/yaeMikoRealistic_yaemikoMixed.safetensors

Launch Web UI

When you need to launch or restart the web UI, you can run the following code to launch it.

If you run into some error that says can't attach to GPU, make sure you have selected GPU as the runtime type in Colab.

# Launch
%cd /content/stable-diffusion-webui
!python launch.py --share --xformers --enable-insecure-extension-access --gradio-queue

Copy Generated Images to Google Drive

There is a file explorer in Colab, but you can't download batch of files from it.

The best way to download generated images I can think of is to zip them and save them to Google Drive.

Here's a piece of code to do so:

# 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!