教程|腾讯云高性能应用服务(HAI)搭建Stable Diffusion 文生图API

本次我们使用 腾讯云高性能应用服务 HAI 体验快速搭建并使用 AI 模型 StableDiffusion ,实现思路如下:

  • 提前通过高性能应用服务 HAI 部署成功 StableDiffusion 应用。
  • 基于部署好的应用,利用体验 JupyterLab 进行 StableDiffusion API 的部署。

前提

在部署 API 服务之前,请确保您已成功部署 StableDiffusion 应用。详细步骤可参见 快速使用 Stable Diffusion 文生图应用

部署 API 服务

1. 进入 jupyter_lab 控制台操作界面。

1.1 在实例列表中选择更多 > JupyterLab 并进入该实例的详情页。

1.2 初步认识并操作 JupyterLab。

1.3 选择使用终端命令行操作。

输入代码:

cd stable-diffusion-webui

python launch.py --nowebui --xformers --opt-split-attention --listen --port 7862

命令参数描述如下图:

命令

描述

--nowebui

以 API 模式启动。

--xformers

改善内存消耗和速度。

--opt-split-attention

Cross attention layer optimization 优化显着减少了内存使用。

--listen

默认启动绑定的 IP 是 127.0.0.1。

--port

默认端口是7860,可以配置并修改该参数,例如:--port 7862。

--gradio-auth username:password

如果希望给 WebUI 设置登录密码,可以配置该参数,例如:--gradio-auth GitLqr:123456。

操作截图如下图所示:

1.4 添加高性能应用服务 HAI 的端口配置,使外部网络能够顺利地访问该服务器提供的 API 服务。

1.4.1 在算力管理页面。单击实例空白进入详情设置页。

1.4.2 在端口配置弹窗中,单击编辑规则。

1.4.3 在安全组规则页面中,在入站规则页签单击添加规则。

配置参考如下:

来源:0.0.0.0/0

协议端口:TCP:7862 (根据您配置的端口填写)

2. 启动 StableDiffusion API 接口使用指南

2.1 配置完成后,在浏览器地址栏输入服务器 IP 地址:端口号/docs 可查看相关的 API 接口使用指南。

官方提供的常用 API 如下:

/sdapi/v1/txt2img文字生图 POST

/sdapi/v1/img2img图片生图 POST

/sdapi/v1/options获取设置 GET | 更新设置 POST(可用来更新远端的模型)

/sdapi/v1/sd-models获取所有的模型 GET

2.2 查看相关接口示例 (/sdapi/v1/txt2img) 。

常用输入如下:

{

"denoising_strength": 0,

"prompt": "puppy dogs",

"negative_prompt": "",

"seed": -1,

"batch_size": 2,

"n_iter": 1,

"steps": 50,

"cfg_scale": 7,

"width": 512,

"height": 512,

"restore_faces": false,

"tiling": false,

"sampler_index": "Euler"

}

可复制以上参数到 Request body 中。

名称

说明

prompt

提示词

negative_prompt

反向提示词

seed

种子,随机数

batch_size

每次张数

n_iter

生成批次

steps

生成步数

cfg_scale

关键词相关性

width

宽度

height

高度

restore_faces

脸部修复

tiling

可平铺

sampler_index

采样方法

请求 API 接口成功截图如下:

返回的格式如下:

{

"images": [...],// 这里是一个base64格式的字符串数组,根据请求的图片数量而定

"parameters": { ... },//此处为输入的body

"info": "{...}"// 返回的图片的信息

}

当看到类似上图的消息时,说明已经成功与远端的服务器进行连接!如果希望验证结果的图片的实际展示效果,可以复制 images 中的其中一张图片的 base64 格式的字符串,到相关的网站下转换为 jpg 格式。

3. 使用 Python 向高性能应用服务 HAI 提供的 StableDiffusionAPI 发送请求。

以下演示如何使用 Python 向 StableDiffusion API 发出请求。向应用程序的 txt2img(即文本到图像)API 发送 POST 请求以简单地生成图像。

我们将使用 requests 包,如果您还没有安装,请使用安装脚本:

pip install requests

我们可以发送一个包含提示的请求作为一个简单的字符串。服务器将返回一个图像作为 base64 编码的 PNG 文件,我们需要对其进行解码。 要解码 base64 图像,我们只需使用 base64.b64decode(b64_image)。 以下使用 Python 作为脚本代码测试:

import json

import base64

import requests

your_ip = '0.0.0.0' # HAI服务器IP地址

your_port =7862 # SD api 监听的端口

def submit_post(url: str,data: dict):

"""

Submit a POST request to the given URL withthe given data.

"""

return requests.post(url,data=json.dumps(data))

def save_encoded_image(b64_image: str,output_path: str):

"""

Save the given image to the given outputpath.

"""

with open(output_path,"wb") asimage_file:

image_file.write(base64.b64decode(b64_image))

if __name__ == '__main__':

#/sdapi/v1/txt2img

txt2img_url = f'http://{your_ip}:{your_port}/sdapi/v1/txt2img'

data = {

'prompt': 'a pretty cat,cyberpunk art,kerem beyit,verycute robot zen,Playful,Independent,beeple |',

'negative_prompt':'(deformed,distorted,disfigured:1.0),poorlydrawn,bad anatomy,wrong anatomy,extra limb,missing limb,floating limbs,(mutatedhands and fingers:1.5),disconnectedlimbs,mutation,mutated,ugly,disgusting,blurry,amputation,flowers,human,man,woman',

'Steps':50,

'Seed':1791574510

}

response = submit_post(txt2img_url,data)

save_encoded_image(response.json()['images'][0],'cat.png')

请记住,您的结果会与上述示例有所不同。 如果遇到问题,请仔细检查运行 StableDiffusionAPI 应用程序的终端的输出。 如果您遇到404 Not Found 的问题,请仔细检查 URL 是否输入正确并指向正确的地址(例如 127.0.0.1)。

服务端可查看每一次接口调用详情:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值