自动化搭建专属 AI 绘图服务

通义万相AIGC技术已经比较成熟,结合阿里云的计算和存储产品可以方便的搭建自己专属的 AI 绘图服务。例如《创意加速器:AI 绘画创作》这个解决方案,利用阿里自研的通义万相AIGC技术在 Web 服务中实现先进的图像生成。

AI 绘画服务搭建步骤

从架构图中可以看到,要想搭建这样一套服务,大概分为 2 个步骤

  1. 创建基础云产品资源
  2. 部署 AI 绘画服务

手动去各个控制台去创建资源非常繁琐,而登录到服务器上去部署绘画服务也需要一些技术基础,所以该方案提供了一键部署链接,方便用户快速部署。

选择完参数等待创建完成之后在输出中点击体验地址就可以开始体验 AI 绘图了。

一键部署原理

一键部署页面是如何把云产品资源的创建和应用程序的部署一起完成的呢?其背后是使用的资源编排 ROS 产品,ROS 基于 IaC 的设计理念,根据模板就能自动完成资源的创建和应用的部署过程。点击一键部署页面的“上一步”按钮就可以看到模板的全部内容。

ROSTemplateFormatVersion: '2015-09-01'
Description:
  zh-cn: 通义万相的文本绘图与人像美化之旅。
  en: Tongyi Wanxiang’s journey of text drawing and portrait beautification.
Parameters:
  BucketName:
    ConstraintDescription:
      zh-cn: 3 到 63 个字符,不以连字符 (-) 开头和结尾,可以包含小写字母、数字和连字符 (-)
      en: 3 to 63 characters, not beginning and ending with a hyphen (-), can contain lowercase letters, Numbers and hyphens (-)
    Description:
      zh-cn: 3到63个字符,不以连字符(-)开头和结尾,可以包含小写字母、数字和连字符(-);<br>备注:<font color='blue'><b>需要保证整个网络的唯一性,已经存在的无法创建</b></font>
      en: '3 to 63 characters, not beginning and ending with a hyphen (-), can contain lowercase letters, Numbers and hyphens (-);<br>Note: <font color=''blue''><b>need whole network uniqueness, already existing can not be created.</b></font>'
    MinLength: 3
    Label:
      zh-cn: 存储空间名称
      en: Bucket Name
    AllowedPattern: ^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$
    MaxLength: 63
    Type: String
    AssociationProperty: AutoCompleteInput
    AssociationPropertyMetadata:
      Length: 5
      Prefix: drawing-with-tongyi-wanxiang-
      CharacterClasses:
        - Class: lowercase
          min: 1
  DashScopeApiKey:
    Type: String
    AssociationProperty: ALIYUN::DashScope::ApiKey
    Label:
      en: DashScope API-KEY
      zh-cn: DashScope API-KEY
    Description:
      zh-cn: 开通灵积模型服务,并获得 API-KEY。请参考: <a href="https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key"  target="_blank">开通DashScope并创建API-KEY</a>。
      en: 'Activate DashScope and obtain the API-KEY. Please refer to:  <a href="https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key"  target="_blank">Activate DashScope and create API-KEY</a>.'
  ZoneId:
    Type: String
    Default: Null
    Required: true
    Label:
      en: Availability Zone
      zh-cn: 可用区
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    AssociationPropertyMetadata:
      AutoSelectFirst: true
  InstanceType:
    Type: String
    Default: Null
    Required: true
    Label:
      en: Instance Type
      zh-cn: 实例类型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceChargeType: PostPaid
      SystemDiskCategory: cloud_essd
  InstancePassword:
    Type: String
    NoEcho: true
    Default: Null
    Description:
      en: Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: 长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)
    AssociationProperty: ALIYUN::ECS::Instance::Password
  CommonName:
    Type: String
    Default: wanxiang
Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName:
        Fn::Sub: ${CommonName}-vpc
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VpcId:
        Ref: Vpc
      CidrBlock: 192.168.0.0/24
      ZoneId:
        Ref: ZoneId
      VSwitchName:
        Fn::Sub: ${CommonName}-vsw
  SecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupName:
        Fn::Sub: ${CommonName}-sg
      SecurityGroupIngress:
        - PortRange: 22/22
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: 80/80
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: '-1/-1'
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: icmp
  EcsInstance:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId
      VSwitchId:
        Ref: VSwitch
      SecurityGroupId:
        Ref: SecurityGroup
      ImageId: aliyun_3_9_x64_20G_alibase_
      InstanceName:
        Fn::Sub: ${CommonName}-ecs
      InstanceType:
        Ref: InstanceType
      SystemDiskCategory: cloud_essd
      MaxAmount: 1
      InternetMaxBandwidthOut: 5
      Password:
        Ref: InstancePassword
  Bucket:
    Type: ALIYUN::OSS::Bucket
    Properties:
      BucketName:
        Ref: BucketName
      DeletionForce: true
  User:
    Type: ALIYUN::RAM::User
    Properties:
      UserName:
        Fn::Sub: create_by_solution-${ALIYUN::StackId}
      PolicyAttachments:
        Custom:
          - Ref: CustomPolicy
  AccessKey:
    Type: ALIYUN::RAM::AccessKey
    Properties:
      UserName:
        Ref: User
  CustomPolicy:
    Type: ALIYUN::RAM::ManagedPolicy
    Properties:
      PolicyName:
        Fn::Sub: create_by_solution-${ALIYUN::StackId}
      PolicyDocument:
        Version: '1'
        Statement:
          - Effect: Allow
            Action:
              - oss:GetObject
              - oss:PutObject
            Resource:
              - Fn::Sub: acs:oss:${ALIYUN::Region}:${ALIYUN::TenantId}:${BucketName}/*
  ModuleInstallJava:
    Version: default
    Type: MODULE::ACS::OOS::Extension
    Properties:
      EcsInstanceIds:
        - Ref: EcsInstance
      PackageName: ACS-Extension-java-1853370294850618
  InstallApp:
    Type: ALIYUN::ECS::RunCommand
    DependsOn: ModuleInstallJava
    Properties:
      InstanceIds:
        - Ref: EcsInstance
      Type: RunShellScript
      Sync: true
      Timeout: 3600
      CommandContent:
        Fn::Sub: |-
          #!/bin/bash

          cat << EOF >> ~/.bash_profile
          export DASHSCOPE_API_KEY=${DashScopeApiKey}
          export OSS_ACCESS_KEY_ID=${AccessKey.AccessKeyId}
          export OSS_ACCESS_KEY_SECRET=${AccessKey.AccessKeySecret}
          export WANX_DEMO_OSS_BUCKET=${BucketName}
          export WANX_DEMO_OSS_ENDPOINT=https://oss-${ALIYUN::Region}.aliyuncs.com
          EOF

          source ~/.bash_profile 
          wget https://help-static-aliyun-doc.aliyuncs.com/demos/wanx-demo-0.0.1-SNAPSHOT.jar
          nohup java -jar wanx-demo-0.0.1-SNAPSHOT.jar > wanx-demo.log 2>&1 &
Outputs:
  EcsLoginAddress:
    Description:
      en: Ecs login address.
      zh-cn: ECS登录地址。
    Value:
      Fn::Sub: https://ecs-workbench.aliyun.com/?from=EcsConsole&instanceType=ecs&regionId=${ALIYUN::Region}&instanceId=${EcsInstance}
  ExperienceAddress:
    Description:
      en: Experience address.
      zh-cn: 体验地址。
    Value:
      Fn::Sub:
        - http://${PublicIp}/wanx-demo
        - PublicIp:
            Fn::Select:
              - 0
              - Fn::GetAtt:
                  - EcsInstance
                  - PublicIps
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - DashScopeApiKey
          - BucketName
          - ZoneId
          - InstanceType
          - InstancePassword
    Hidden:
      - CommonName
    TemplateTags:
      - acs:technical-solution:ai:通义万相的文本绘图与人像美化之旅-tech_solu_109

模板解析

可以看出模板是一个 yaml 文件,意味着我们不需要熟悉编程语言也能完成复杂架构的一键部署。

这里简单地介绍下模板中定义的内容,看看模板是如何做到云资源的创建和 AI 绘画服务的部署的。

Resources 模块

我们是在模板的Resources 模块中声明要创建的云资源的,拿 VPC 举例子,Type: ALIYUN::ECS::VPC声明了要创建的是 VPC 实例,而在Properties中声明了 VPC 的属性信息。

Resources:
  Vpc:
    # ALIYUN::ECS::VPC 表示 VPC 实例
    Type: ALIYUN::ECS::VPC
    Properties:
      # 表示 VPC 的网段是 192.168.0.0/16
      CidrBlock: 192.168.0.0/16
      # 表示 VPC 的名称,这里用了 Fn::Sub 函数,${CommonName} 表示Parameters模块的 CommonName的值
      VpcName:
        Fn::Sub: ${CommonName}-vpc

其他的 VSwitch、安全组和 ECS 实例资源也都类似。值得一提的是,Java 环境的安装我们使用的是MODULE::ACS::OOS::Extension类似的资源,而 AI 绘画服务的部署则是用的ALIYUN::ECS::RunCommand资源。

Parameters 模块

我们在模板的Parameters模块中声明了参数,参数是可以让用户选择的,一键部署页面上配置模板参数表单就是根据Parameters的定义来渲染出来的。AI 服务中主要定义了AI 绘画需要的灵积模型服务需要的 API-KEY 、OSS Bucket 的名称、影响库存和价格的 ECS 的可用区和规格。

Outputs 模块

我们在Outputs模块中声明了输出信息,即 AI 绘画服务的体验地址。用户在创建完成之后,点击到输出页签就可以点击体验了。

Outputs:
  ExperienceAddress:
    Description:
      en: Experience address.
      zh-cn: 体验地址。
    # Value 中定义了输出的值,在这里可以使用内置函数获取到创建的资源属性
    Value:
      Fn::Sub:
        - http://${PublicIp}/wanx-demo
        - PublicIp:
            Fn::Select:
              - 0
              - Fn::GetAtt:
                  - EcsInstance
                  - PublicIps

注:详细的模板语法可以参考 ROS 的模板结构说明文档。

ROS 编排引擎

ROS 的编排引擎能解析模板内容,自动分析参数引用和资源的依赖关系,并发地完成模板资源中声明的内容的部署。ROS 把这样一组资源放到资源栈中管理,用户不用关心其中单个资源的状态,当资源栈部署完成时,表示所有的资源都部署完成。

一键部署自己的应用

通过 ROS 自动创建云资源、自动部署应用非常方便,其他的应用也希望一键部署要如何操作呢?

编写 ROS 模板

因为 ROS 是基于模板来自动化部署,第一步自然是编写 ROS 模板了,模板看起来比较复杂,但是对于在 ECS 上部署应用的场景,确实非常固定,基本遵循三个步骤。

建立 OnECS 模板框架

这部分我们主要是创建包括 VPC、VSwitch、安全组和 ECS 的云资源,我们可以直接复用如下模板。

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  # 可用区会影响库存信息,提取到参数中会动态获取支持售卖、有库存的可用区
  ZoneId:
    Type: String
    Label:
      en: Availability Zone
      zh-cn: 可用区
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    AssociationPropertyMetadata:
      AutoSelectFirst: true
  # 实例规格也受库存因素影响,需要提取到参数中动态查询
  InstanceType:
    Type: String
    Default: null
    Required: true
    Label:
      en: Instance Type
      zh-cn: 实例类型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      # 定义实例规格的筛选器,建议使用后付费的实例,部署体验完成之后可以随时释放
      ZoneId: ${ZoneId}
      InstanceChargeType: PostPaid
      SystemDiskCategory: cloud_essd
  InstancePassword:
    Type: String
    NoEcho: true
    Default: null
    Description:
      en: >-
        Server login password, Length 8-30, must contain three(Capital letters,
        lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
        symbol in)
      zh-cn: >-
        服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
        中的特殊符号)
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: >-
        Length 8-30, must contain three(Capital letters, lowercase letters,
        numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
    AssociationProperty: 'ALIYUN::ECS::Instance::Password'
  CommonName:
    Type: String
    Default: app
Resources:
  Vpc:
    Type: 'ALIYUN::ECS::VPC'
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName: 
        Fn::Sub: ${CommonName}-vpc
  VSwitch:
    Type: 'ALIYUN::ECS::VSwitch'
    Properties:
      VpcId:
        Ref: Vpc
      CidrBlock: 192.168.0.0/24
      ZoneId:
        Ref: ZoneId
      VSwitchName:
        Fn::Sub: ${CommonName}-vsw
  SecurityGroup:
    Type: 'ALIYUN::ECS::SecurityGroup'
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupName:
        Fn::Sub: ${CommonName}-sg
      # 根据实际应用选择安全组开放的端口
      SecurityGroupIngress:
        - PortRange: 22/22
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: 80/80
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: '-1/-1'
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: icmp
  # ECS资源建议把 ZoneId 和 InstanceType 提取到参数中做为动态查询,其他属性建议直接写到模板中。
  EcsInstance:
    Type: 'ALIYUN::ECS::InstanceGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId
      VSwitchId:
        Ref: VSwitch
      SecurityGroupId:
        Ref: SecurityGroup
      # 镜像可以不精确填写,后续镜像更新之后模板不用改。
      ImageId: aliyun_3_9_x64_20G_alibase_
      InstanceName:
        Fn::Sub: ${CommonName}-ecs
      InstanceType:
        Ref: InstanceType
      SystemDiskCategory: cloud_essd
      MaxAmount: 1
      InternetMaxBandwidthOut: 5
      Password:
        Ref: InstancePassword
Outputs:
  # 建议将 ECS 的登录地址放到输出中,方便用户登录 ECS 实例
  EcsLoginAddress:
    Description:
      en: Ecs login address.
      zh-cn: ECS登录地址。
    Value:
      Fn::Sub: https://ecs-workbench.aliyun.com/?from=EcsConsole&instanceType=ecs&regionId=${ALIYUN::Region}&instanceId=${EcsInstance}
Metadata:
  ALIYUN::ROS::Interface:
    # 设置参数填写顺序
    ParameterGroups:
      - Parameters:
          - ZoneId
          - InstanceType
          - InstancePassword
    Hidden:
      - CommonName
搭建应用运行环境(可选)

大多数应用的运行都需要 Java、Python、Docker 或 LNMP 等前置的运行环境,在 ROS 模板中我们可以借助OOS 的公共插件标准化的安装所需要的基础运行环境。在 ROS 模板中的具体使用方式如下所示。

Resources:
  # 该模块可以在指定的 ECS 实例中安装 OOS 公共插件
  ModuleInstallDocker:
    Version: default
    Type: 'MODULE::ACS::OOS::Extension'
    Properties:
      # 定义在哪个 ECS 实例上安装环境,示例是基础框架模板中的 ECS 实例
      EcsInstanceIds:
        Fn::GetAtt:
          - EcsInstance
          - InstanceIds
      # 公共插件名称,在 OOS 控制台如下截图位置获取,示例是部署Docker环境
      PackageName: ACS-Extension-DockerCE-1853370294850618

利用 Shell 脚本完成应用的部署

我们可以利用 ALIYUN::ECS::RunCommand非常方便地在 ECS 上执行部署应用的脚本,具体用法如下

Resources:
  InstallApp:
    Type: ALIYUN::ECS::RunCommand
    Properties:
      # 通过 InstanceIds 指定需要执行脚本的 ECS 实例,这里使用之前创建的实例
      InstanceIds:
        - Ref: EcsInstance
      # 定义了脚本类型,这里声明的是 Shell 脚本
      Type: RunShellScript
      Sync: true
      Timeout: 3600
      # CommandContent 中就是具体运行的 Shell 脚本,我们可以利用 Fn::Sub 内置函数还引用参数或资源的输出信息
      CommandContent:
        Fn::Sub: |-
          #!/bin/bash

          cat << EOF >> ~/.bash_profile
          export DASHSCOPE_API_KEY=${DashScopeApiKey}
          export OSS_ACCESS_KEY_ID=${AccessKey.AccessKeyId}
          export OSS_ACCESS_KEY_SECRET=${AccessKey.AccessKeySecret}
          export WANX_DEMO_OSS_BUCKET=${BucketName}
          export WANX_DEMO_OSS_ENDPOINT=https://oss-${ALIYUN::Region}.aliyuncs.com
          EOF

          source ~/.bash_profile 
          wget https://help-static-aliyun-doc.aliyuncs.com/demos/wanx-demo-0.0.1-SNAPSHOT.jar
          nohup java -jar wanx-demo-0.0.1-SNAPSHOT.jar > wanx-demo.log 2>&1 &

定制专属应用页面

写好模板之后,我们可以很方便地为自己的应用定制专属页面。用户只需要选择参数就可以一键部署应用了。

我们先来分析一下 AI 绘画的一键部署链接地址

https://ros.console.aliyun.com/region/stacks/create
?templateUrl=https://ros-public-templates.oss-cn-hangzhou.aliyuncs.com/service_template/technical-solution/drawing_with_tongyi_wanxiang.yml
&hideStepRow=true
&hideStackConfig=true
&pageTitle=通义万相的文本绘图与人像美化之旅
&disableRollback=false
&isSimplified=true
&productNavBar=disabled
&disableNavigation=true

我们可以直接通过 URL 中的参数来控制页面的内容,需要注意的是模板信息我们是通过templateUrl来设置的,所以我们需要把模板先上传到支持跨域访问的地址上,这里推荐上传到 OSS 中,生成一个模板链接。

pageTitle改成我们自己的标题,其他参数保持不变即可。

总结

虽然 AI 绘画服务涉及多个云产品,但借助 ROS 资源编排服务就能让用户在几分钟之内拥有自己的 AI 绘画服务。利用 ROS 自动化部署优势,可以让用户方便快捷地体验到我们的应用。相比于手动部署或者通过 API、SDK 的部署方式,ROS 有着高效、稳定等诸多优势,也是服务上云的最佳实践。

相关推荐

  1. FunASR 框架使用;api服务

    2024-06-08 10:28:06       16 阅读
  2. 使用python自己的AI智能问答服务

    2024-06-08 10:28:06       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 10:28:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 10:28:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 10:28:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 10:28:06       20 阅读

热门阅读

  1. kali-vulhub(持续更新)

    2024-06-08 10:28:06       8 阅读
  2. 每日一题36:数据分组之科目种类数量

    2024-06-08 10:28:06       9 阅读
  3. kubernetes之安装ingress-nginx(k8s1.19版本之后)

    2024-06-08 10:28:06       12 阅读
  4. azure cli的安装和使用

    2024-06-08 10:28:06       8 阅读
  5. go语言打印出变量的类型

    2024-06-08 10:28:06       6 阅读
  6. 【自动驾驶】点与向量从ego系转odometry系

    2024-06-08 10:28:06       10 阅读
  7. Github 2024-06-08 开源项目日报Top10

    2024-06-08 10:28:06       8 阅读
  8. 长轮询之websocket

    2024-06-08 10:28:06       9 阅读
  9. langchian_aws模块学习

    2024-06-08 10:28:06       11 阅读