How to Use Google Colab to Run Web GUI to Draw AI Images

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

Following sections is mostly coming from this blog post: https://ivonblog.com/en-us/posts/google-colab-stable-diffusion-webui/

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

The steps in this section is only for the first time. After you have completed the first time running, you wont need some steps since we have downloaded required models. 
  1. Go to Google Colab, click "New notebook". Or from File menu's drop down list, select New notebook.

  2. In the new notebook, copy & paste following example code. 

  3. 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.

1 2 3 
from google.colab import drive drive.mount('/content/drive') !mkdir /content/drive/MyDrive/sd-webui-files 
  1. Install dependencies

1 2 3 
!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 
  1. Clone SD WebUI directly from ATOMATIC1111’s repo

1 
!git clone --depth=1 https://github.com/AUTOMATIC1111/stable-diffusion-webui.git /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui
  1. 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.

1 
!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
  1. There are some issues of switching branches after launching SD WebUI. Thus, add these lines to fix them.

1 2 3 4 
%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 
  1. 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 getting AssertionError: extension access disabed.

1 
!python launch.py --share --xformers --enable-insecure-extension-access --theme light
  1. Here is the full code

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
# 掛載雲端硬碟 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模型 !wget -nc -P /content/drive/MyDrive/sd-webui-files/stable-diffusion-webui/models/Lora https://civitai.com/api/download/models/13739 -O Korean_Doll_Likenesss.safetensors  # 以git clone安裝擴充功能:中文化 !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  # 防止其他儲存庫造成錯誤 %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  # 啟動WebUI。直接從launch.py傳遞引數 !python launch.py --share --xformers --enable-insecure-extension-access --theme light

  1. Click Edit → Notebook Settings →Change Hardware accelerator type to use GPU as Hardware accelerator. Click Save.

  2. If you have not enabled GPU, the running result will show you torch can not use GPU error. The process will be stopped from there. You will have to enable GPU and re-run the notebook codes again. 

  3. Click Run button beside the code to run the cell, wait for 5 minutes. 

  4.  Click generated Gradio links (expired after 72 hours).

  5. Now we are ready to use Stable Diffusion WebUI.

The total process for first time running will take about 10 minutes. 

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 is about 4GB data stored on your Google Drive folders. 

Because we had downloaded all required files, replace the code in the cell with these lines:

1 2 3 4 5 6 7 
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 

This will install requirements while running SD WebUI and start SD WebUI from our Google Drive. The Gradio link should be ready in 2 minutes. You might got the following warning message about permitting notebook to access Google Drive Files. 

References

版权声明:
作者:Alex
链接:https://www.techfm.club/p/41410.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>