关于云计算:只需10分钟基于Amazon-EC2-快速部署-Stable-Diffusion-WebUI-博思云为云技术分享

5次阅读

共计 4217 个字符,预计需要花费 11 分钟才能阅读完成。

前言

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
正文完
 0