---
title: "如何在 Google Colab 上部署 Stable Diffusion Web UI Colab"
date: 2023-02-19T00:17:06.000Z
author: Z.SHINCHVEN
tags: [Stable Diffusion, Stable Diffusion Web UI, Stable Diffusion Web UI Colab, Google Colab, AIGC]
canonical: https://atlassc.net/2023/02/19/stable-diffusion-webui-colab-guide-for-beginners
---
## 太长不读

点击下面这个按钮 `马上` 进入 Google Colab 部署 Stable Diffusion Web UI Colab。

[![从Colab打开](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-webui-colab/blob/main/stable/chillout_mix_webui_colab.ipynb)

## 什么是 Stable Diffusion Web UI Colab

[Stable Diffusion](https://stablediffusionweb.com/) 是 Stability AI 推出的一个基于深度学习技术的`文字生成图片`AI模型。

[Stable Diffusion Web UI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) 是一个强大好用的 Stable Diffusion Web 应用，你可以使用它来操作 Stable Diffusion 生成图片。一般用它 GitHub 账号中的 `A1111` 代称。

要运行 Stable Diffusion，你的电脑需要有一块强大的GPU和较大的运行内存。但如果你没有这样的硬件，你仍然可以充点小钱上 Google Colab 运行 Stable Diffusion Web UI Colab。

[Stable Diffusion Web UI Colab](https://github.com/camenduru/stable-diffusion-webui-colab) 是一个可以在 Google Colab 上部署 Stable Diffusion Web UI 的 fork 项目。在它的 GitHub 仓库的 README 中已经准备好了不同模型的`.ipynb`文件，你只需要点击其中一个`Open in Colab`按钮，就可以在 Google Colab 上一键部署 Stable Diffusion Web UI Colab。

## 什么是 Google Colab

Colaboratory 简称 [Google Colab](https://colab.research.google.com/)，是 Google Research 团队开发的一款产品。 在 Colab 中，任何人都可以通过浏览器编写和执行任意 Python 代码。 它尤其适合机器学习、数据分析和教育目的。 从技术上来说，Colab 是一种托管式 Jupyter 笔记本服务。它的出现让用户不需要在本地安装任何环境，只需要打开浏览器就可以直接开始编写代码，非常方便。

[Colab 提供免费版、按量付费版和专业版](https://colab.research.google.com/signup/pricing)，免费版提供的资源（compute units）是有限的，但对于大多数人来说已经足够了。如果你需要更多的资源，可以考虑订阅专业版，快乐地使用 A100 GPU 来进行 `AIGC`。

根据 Colab 团队主管最新的[推文](https://twitter.com/thechrisperry/status/1649189902079381505?s=20)，由于使用 Colab 生图的人实在是太多了，现在只允许付费用户在 Colab 中使用 Stable Diffusion。

## 什么是 .ipynb

Google Colab 上执行的文件类型是 `.ipynb`。 .ipynb 是一种可以进行计算的的 Jupyter Notebook 笔记文件格式。你可以在 Colab 中编写代码，也可以在 Colab 中打开 GitHub 仓库中的`.ipynb`文件，直接在 Colab 上运行。

## 基本用法

[Stable Diffusion Web UI Colab V2.0](https://github.com/camenduru/stable-diffusion-webui-colab/tree/v2.0) 目前已经可以使用，相比主分支的版本，它能更好的使用 ControlNet 和 LoRA 等插件。我建议直接从 `2.0` 开始使用。

Stable Diffusion Web UI Colab 的 GitHub 主仓库的提供一系列可以在 Google Colab 上执行 `.ipynb`，每个 `.ipynb` 文件对应一个模型，你可以点击 README 的其中一个 `Open in Colab` 按钮，跳转到 Google Colab 上一键部署 Stable Diffusion Web UI Colab。

当你已经通过 GitHub 上的 .ipynb 文件进入 Google Colab 时，离完成部署只有一键之遥，点击代码块中的 `▶️` 运行按钮，就可以开始部署 Stable Diffusion Web UI Colab 了。

部署可能需要几分钟时间，主要取决于你选择的模型的大小。

当部署完成之后，会在输出日志的最后显示一个 Gradio 的链接（类似于：https://xxxxxxxxxx.gradio.live），点击它就可以打开 Stable Diffusion Web UI Colab 的界面了。

## 我的自定义笔记

Stable Diffusion Web UI Colab 提供的 `.ipynb` 笔记已经是很完善的一键脚本，但是我想根据自己的使用需求载入多个模型，所以我在它的基础上做了一些调整。

Google Colab 只能同时运行一个代码块，如果你需要通过执行笔记来安装额外的模型或者插件，你需要先停止正在运行的代码块，才能执行新的代码块。所以我将代码块分为了 `安装`、`下载模型`、`启动` 三个部分，另外还增加了一个 `保存产物` 的部分，以在完成操作以后保存成果。

如果你在运行自己创建的新笔记时遇到了无法找到 GPU 的问题，请在 Runtime 设置中切换成 GPU 类型的运行环境。

### 安装

Stable Diffusion Web UI Colab 只需要安装一次，而可能要停止、重启多次，重启时就没必要重新安装和下载模型了。所以我参照官方的脚本，把安装和下载模型的代码块注释掉，并拷贝到单独的代码块中。

安装的代码块一般只需要执行一次，建议排序到笔记本的最下面。

```ipynb
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
```

### 下载模型

Stable Diffusion Web UI 有内置的扩展来下载 [Civitai](https://civitai.com/) 上的模型，但是你也可以使用 `wget` 或者 `aria2` 命令在 notebook 中下载其他来源的模型。

这里我为各位中文用户准备了亚洲美女模型合集，供你们一键打包使用。

```ipynb
# 下载主模型，请根据自己的需求下载
!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

# 下载 Lora 模型
!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
```

### 启动 SD Web UI

当你需要 `启动` 或者 `重启` Web UI 时，可以运行下面的代码来启动。

```ipynb
# 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"
```

### 保存产物

Colab 有自带的文件浏览器，但是你无法从中批量下载文件。

所以我选择使用 `zip` 来打包产物，并保存到 Google Drive。

挂载 Google Drive 时会弹出授权窗口，然后允许授权账号即可。

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