只需10分钟,基于Amazon EC2 快速部署 Stable Diffusion WebUI | 博思云为云技术分享

前言

Stable Diffusion (SD) 已迅速成为 2023 年非常流行的文生图的(又称“AI 艺术生成”)模型之一。其成功的一个关键因素是它已作为开源软件提供。

这催生了一个充满活力的社区,该社区快速构建了工具,使任何对 SD 感兴趣的人都能更容易地使用 SD,无论他们的技术知识如何。

其中一个工具是由 Automatic1111 开发的简单但功能强大的 Web 界面 stable-diffusion-webui。

它允许我们无需任何编码即可使用 SD 的所有功能,而且它也是开源的,这意味着任何人都可以下载此 Web UI 以及 SD 模型并将其部署到任何他们想要的地方。

然而,挑战在于 SD 仍然需要足够强大的 GPU 能力才能非常快速的生成一张图像信息,而这需要我们自行采购GPU并部署配置

因此,在本教程中,我们借助于亚马逊云科技 G4dn 实例类型来部署 stable-diffusion-webui,帮助加速机器学习推理和图形密集型的工作场景,教程内容也可以在 GitLab 中找到。

我们只需使用一个命令,使用 AWS CloudFormation 模板来设置所有所需的基础设施即可完成此操作。

部署模板

Cloudformation模板如下所示:

AWSTemplateFormatVersion: '2010-09-09'
Description: A CloudFormation template to deploy the Stable Diffusion Web UI by Automatic1111
 
Parameters :
  InstanceType:
    Description : EC2 instance type
    Type : String
    Default : g4dn.xlarge
    AllowedValues :
           - g4dn.xlarge
 
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
    Type: AWS::EC2::KeyPair::KeyName
 
Resources:
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: sd-webui-sg
      GroupDescription: Security group for whisper-demo EC2 instance
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 7860
          ToPort: 7860
          CidrIp: 0.0.0.0/0
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      ImageId: ami-03f65b8614a860c29
      BlockDeviceMappings:
        - DeviceName: /dev/sda1
          Ebs:
            VolumeSize: 100
            VolumeType: gp3
      "Tags" : [
            {"Key" : "Name", "Value" : "sd-web-ui-cf"},
        ]
      SecurityGroups:
        - Ref: SecurityGroup
      UserData:
        'Fn::Base64': |
            #!/bin/bash
            cd /home/ubuntu
            git clone http://labs.bosicloud.com/bosi-samples/stable-diffusion-webui.git
            bash stable-diffusion-webui/setup.sh -y
  MyEIP:
    Type: AWS::EC2::EIP
  MyEIPAssociation:
    Type: AWS::EC2::EIPAssociation
    Properties:
      AllocationId: !GetAtt MyEIP.AllocationId
      InstanceId: !Ref EC2Instance

将此模板文件上传到 Cloudformation 中并部署:

脚本代码说明

如前所述,我们在 EC2 实例上运行一个 setup.sh 脚本,该脚本将执行命令来设置 Web UI 的所有内容,让我们一步步地看。

第一部分在安装软件包后禁用 Ubuntu 重新启动对话框,然后安装一些我们需要的软件包:

# disable the restart dialogue and install several packages
sudo sed -i "/#\$nrconf{restart} = 'i';/s/.*/\$nrconf{restart} = 'a';/" /etc/needrestart/needrestart.conf
sudo apt-get update
sudo apt install wget git python3 python3-venv build-essential net-tools -y

下一节将下载并安装 CUDA 驱动程序,以便我们可以访问机器的 GPU:

# install CUDA (from https://developer.nvidia.com/cuda-downloads)
wget https://developer.download.nvidia.com/compute/cuda/12.0.0/local_installers/cuda_12.0.0_525.60.13_linux.run
sudo sh cuda_12.0.0_525.60.13_linux.run --silent

之后我们需要安装 Git Large File Storage,因为我们将下载一个大约 5 GB 的稳定扩散模型:

# install git-lfs
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
sudo -u ubuntu git lfs install --skip-smudge

现在 Git LFS 已安装,我们可以从 Hugging Face 模型中心下载模型。请注意,我们启用了 “skip-smudge” 选项,它允许我们只下载我们需要的特定文件。

我们将下载 SD v2.1(512px 版本)并将其移动到 Web UI 期望模型所在的目录中。

# download the SD model v2.1 and move it to the SD model directory
sudo -u ubuntu git clone --depth 1 https://huggingface.co/stabilityai/stable-diffusion-2-1-base
cd stable-diffusion-2-1-base/
sudo -u ubuntu git lfs pull --include "v2-1_512-ema-pruned.ckpt"
sudo -u ubuntu git lfs install --force
cd ..
mv stable-diffusion-2-1-base/v2-1_512-ema-pruned.ckpt stable-diffusion-webui/models/Stable-diffusion/

请注意,您可以更改脚本以下载不同版本的 Stable Diffusion,例如版本 1.5。您还可以稍后将所需数量的模型添加到 UI 中,方法是将它们放入 odel 目录中。

除了模型之外,我们还需要一个由 WebUI 读取的配置文件。我们从 Stable Diffusion Github 存储库下载一个配置文件,并将其重命名以匹配模型名称,并将其也放入同一目录中:

# download the corresponding config file and move it also to the model directory (make sure the name matches the model name)
wget https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference.yaml
cp v2-inference.yaml stable-diffusion-webui/models/Stable-diffusion/v2-1_512-ema-pruned.yaml

最后,我们将 WebUI 的所有权更改为 ubuntu 用户,并以该用户身份启动服务器(因为不允许用户 root 启动应用程序):

# change ownership of the web UI so that a regular user can start the server
sudo chown -R ubuntu:ubuntu stable-diffusion-webui/
 
# start the server as user 'ubuntu'
sudo -u ubuntu nohup bash stable-diffusion-webui/webui.sh --listen > log.txt

测试Web UI

等待15-20 分钟完成部署。我们可以通过 Amazon console 来查看 EC2 实例的 IP 地址:

检索到 IP 地址后,我们可以通过在浏览器中导航到 :7860 来打开应用程序(如果请求超时,则安装尚未完成)。安装完成后我们可以看到应用程序已启动并运行。

关闭EC2实例并重新启动应用程序

我们在不使用的时候停止实例的运行,避免产生过多的费用。我们可以在AWS控制台中停止实例并在再次需要时重新启动它,而不会丢失任何已安装的应用程序。

重新启动 EC2 实例后,我们可以通过 SSH 登录它并使用以下命令重新启动应用程序:

nohup bash stable-diffusion-webui/webui.sh --listen > log.txt

你可能感兴趣的:(只需10分钟,基于Amazon EC2 快速部署 Stable Diffusion WebUI | 博思云为云技术分享)