If you have a browser, you will be able to use Google Colab to run Stable Diffusion Web UI created by ATOMATIC1111, which has github project: https://github.com/AUTOMATIC1111/stable-diffusion-webui
In this post, I am going to show how to write your own notebook and customize the code to have more functions to meet your presonal customization requirements.
Stable Diffusion Web UI Introduction
A browser interface based on Gradio library for Stable Diffusion.
Github: https://github.com/AUTOMATIC1111/stable-diffusion-webui
Steps to Run Stable Diffusion Web UI at Colab
Note: Following steps are mostly coming from this blog post: https://ivonblog.com/en-us/posts/google-colab-stable-diffusion-webui/
-
Go to Google Colab, click “New notebook”. Or from File menu’s drop down list, select New notebook.
-
In the new notebook, copy & paste following example code.
-
You will need to log into your Google Account. Your Google Drive will have at least 10GB free space for downloading reqired components. During running, it will take about 10GB space from your Google Drive.
-
First we mount the Drive to
/content/drive
. And create a new folder (sd-webui-files) for storing files on your google drive’s root folder.
|
|
- Install dependencies
|
|
- Clone SD WebUI related files directly from ATOMATIC1111’s repo
|
|
- Download checkpoint models from Hugging Face. I prefer to use Anything model for generating anime art. Or you can use original Stable Diffusion model for generating realistic arts.
|
!wget -nc -P /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Stable-diffusion https://huggingface.co/andite/anything-v4.0/resolve/main/anything-v4.5-pruned.safetensors
|
- There are some issues of switching branches after launching SD WebUI. Thus, add these lines to fix them.
|
|
- Finally, we will have python to launch the WebUI. We use
--xformers
to decrease the consumption of VRAM. By adding--enable-insecure-extension-access
we can install extensions from URL in WebUI without gettingAssertionError: extension access disabed
.
|
|
- Here is the full code
|
|
-
Click Edit → Notebook Settings →Change Hardware accelerator type to use GPU as Hardware accelerator. Click Save.
-
If you have not enabled GPU, the running result will show you an error message regardng torch can not use GPU. The whole process will be stopped from there. You will have to enable GPU and re-run the notebook codes again. Error message: “AssertionError: Torch is not able to use GPU; add –skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check”
-
Click Run button beside the code to run the cell, wait for around 11-12 minutes (first time running usually take longer. Second time using shorter code will only take 5 minutes to complete).
-
Click generated Gradio links (expired after 72 hours).
-
Now we are ready to use Stable Diffusion WebUI.
Run Simplified Code After First Time Run
Now we have installed SD WebUI on Google drive. Even we closed our instance. Don’t worry, your data and progress has been stored on your Google Drive. It has about 10.35GB data downloaded and stored on your Google Drive folders under folder “stable-diffusion-webui”.
Because we have downloaded all required files, no need to run downloading code again. So you can replace the code in the cell with only following seven lines:
|
|
This will install requirements while running SD WebUI and start SD WebUI from our Google Drive. The Gradio link should be ready in 7 minutes. You might get a pop up window to connect to your google drive and the following warning message about permitting notebook to access Google Drive Files.
LoRA models
LoRA (Low-Rank Adaptation) models are small Stable Diffusion models that apply tiny changes to standard checkpoint models. They are usually 10 to 100 times smaller than checkpoint models. That makes them very attractive to people having an extensive collection of models.
Adding more checkpoints and models
Important folders:
There are some important folders under your Google Drive’s stable-diffusion-webui folder:
extensions
: As the name suggested, upload any extensions folder to this folder (Click Code button on Github repository of extesnions and download zip). Or you can install extensions by clicking Extensions → Install from URL in WebUI.models/Stable-duffusion/
: Upload new checkpoint models to this foldermodels/Lora
: Upload LoRa models to this folder
All changes would be applied after restarting SD WebUI.
Lora models:
- https://civitai.com/models/17998/hongkongdolllikeness – hongkongdoll – https://civitai.com/api/download/models/17998
- https://civitai.com/models/11722/iu -iu v3.5 – https://civitai.com/api/download/models/18576
- https://civitai.com/models/11619/korean-doll-likenesss – korean doll likenesss – https://civitai.com/api/download/models/13739
Command:
- !wget -nc -P /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Lora -O hongkongdolllikeness.safetensors https://civitai.com/api/download/models/18576
Chilloutmix Checkpoint file:
- https://civitai.com/models/23302/graffiti-art-chilloutmix –
Command:
- !wget -nc -P /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Stable-diffusion -O chilloutmix.safetensors https://civitai.com/api/download/models/27828
- !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
Final codes
To sum up, the final full codes for first time run:
# 掛載雲端硬碟
from google.colab import drive
drive.mount('/content/drive')
!mkdir /content/drive/MyDrive/sd-webui-files
# 安裝CUDA、xformers、Triton依賴
!pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116 -U
!pip install -q xformers==0.0.16
!pip install -q triton==2.0.0
# 複製SD WebUI上游的儲存庫
!git clone --depth=1 https://github.com/AUTOMATIC1111/stable-diffusion-webui.git /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui
# 下載存檔點模型至models資料夾,網址為在模型網站按右鍵取得
!wget -nc -P /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Stable-diffusion https://huggingface.co/andite/anything-v4.0/resolve/main/anything-v4.5-pruned.safetensors
# 下載LoRA模型 - Korean Doll
!mkdir /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Lora
!wget -nc https://civitai.com/api/download/models/13739 -O /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Lora/Korean_Doll_Likenesss.safetensors
# 以git clone安裝擴充功能:中文化 - Optional
# !git clone --depth=1 https://github.com/benlisquare/stable-diffusion-webui-localization-zh_TW.git /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/extensions/stable-diffusion-webui-localization-zh_TW
# 以git clone安裝擴充功能:ControlNet,以及Scribbles模型
!git clone --depth=1 https://github.com/Mikubill/sd-webui-controlnet.git /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/extensions/sd-webui-controlnet
!wget -nc -P /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/extensions/sd-webui-controlnet/models https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_scribble.pth
# Add new checkpoint and LoRA model (Hongkong Doll)
!wget -nc -O /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Lora/hongkongdolllikeness.safetensors https://civitai.com/api/download/models/17998
!wget -nc -O /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Stable-diffusion/chilloutmix.safetensors https://civitai.com/api/download/models/27828
# Fix change branch issues
%cd /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/
!git reset --hard
!git pull
!sed -i -e 's/checkout {commithash}/checkout --force {commithash}/g' launch.py
# Launch WebUI
!python launch.py --share --xformers --enable-insecure-extension-access --theme light
After first time run, you can run following codes instead:
from google.colab import drive
drive.mount('/content/drive')
!pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116 -U
!pip install -q xformers==0.0.16
!pip install -q triton==2.0.0
%cd /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/
!python launch.py --share --xformers --enable-insecure-extension-access --theme light
Note about Google Colab:
There are no official references for ‘Idle’ and ‘Maximum Lifetime’ durations, but testing done by Korakot Chaovavanich shows that:
- The ‘maximum lifetime’ of a running notebook is 12 hours (browser open)
- An ‘Idle’ notebook instance cuts-off after 90 minutes
- You can have a maximum of 2 notebooks running concurrently
- If you close the notebook window and open it while the instance is still running, the cell outputs and variables will still persist. However if the notebook instance has been recycled, your cell outputs and variables will no longer be available.
A sneaky workaround you can try is to have the Colabs instance open in your mobile browser in order to prevent the instance from being considered “Idle”.