Info Security Memo
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org

Build Confidence

Focusing on Information Security 

Info Security Notes

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

4/9/2023

0 Comments

 
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












via Blogger http://blog.51sec.org/2023/04/how-to-use-google-colab-to-run-web-gui.html
April 09, 2023 at 02:24PM AI
0 Comments



Leave a Reply.

    Categories

    All
    Architecture
    Blog
    Checkpoint
    Cisco
    Cloud
    CyberArk
    F5
    Fortigate
    Guardium
    Juniper
    Linux
    Network
    Others
    Palo Alto
    Qualys
    Raspberry Pi
    Security
    SIEM
    Software
    Vmware
    VPN
    Wireless

    Archives

    March 2024
    February 2024
    January 2024
    December 2023
    November 2023
    October 2023
    September 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022
    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    July 2021
    June 2021
    May 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    July 2020
    October 2019
    September 2019
    June 2019
    July 2018
    May 2018
    December 2017
    August 2017
    April 2017
    March 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015
    August 2015
    July 2015
    June 2015
    May 2015
    April 2015
    March 2015

    Print Page:

    RSS Feed

    Email Subscribe
Powered by Create your own unique website with customizable templates.
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org