Stable-diffusion-WebUI 的API调用(内含文生图和图生图实例)

打印 上一主题 下一主题

主题 940|帖子 940|积分 2820

前情提要

在之前实验使用Diffusers库来举行stable-diffusion的接口调用以及各种插件功能实现,但发现diffusers库中各复杂功能的添加较为麻烦,而且难以实现对采样器的添加,safetensors格式模子的读取。在官网上找到了webui有专门的api接口,可以或许极大方便我们举行类似webui界面的api调用。


diffusers文档
webui项目官网
webui API阐明
webui项目部署

这种调用webui自带的api的方法必要先将webui运行起来,无论是自己从官网配置的webui,还是各类启动器一键启动的都是可以的。(我使用的为一键启动包,较为简单)
一键启动包教程
如果是自己配置的
使用
  1. bash webui.sh --nowebui
复制代码
大概
  1. python launch.py --xformers --api
复制代码
API接口调用

当我们把webui项目启动之后,我们可以看到运行的端口(默以为7860)
可以举行调用
1. 文生图(python示例):
  1. import json
  2. import requests
  3. import io
  4. import base64
  5. from PIL import Image
  6. url = "http://127.0.0.1:7860"
  7. prompt = "dog"
  8. negative_prompt = ""
  9. payload = {
  10.     # 模型设置
  11.     "override_settings":{
  12.           "sd_model_checkpoint": "v1-5-pruned.ckpt",
  13.           "sd_vae": "animevae.pt",
  14.           "CLIP_stop_at_last_layers": 2,
  15.     },
  16.     # 基本参数
  17.     "prompt": prompt,
  18.     "negative_prompt": negative_prompt,
  19.     "steps": 30,
  20.     "sampler_name": "Euler a",
  21.     "width": 512,
  22.     "height": 512,
  23.     "batch_size": 1,
  24.     "n_iter": 1,
  25.     "seed": 1,
  26.     "CLIP_stop_at_last_layers": 2,
  27.     # 面部修复 face fix
  28.     "restore_faces": False,
  29.     #高清修复 highres fix
  30.     # "enable_hr": True,
  31.     # "denoising_strength": 0.4,
  32.     # "hr_scale": 2,
  33.     # "hr_upscaler": "Latent",
  34. }
  35. response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
  36. r = response.json()
  37. image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
  38. image.show()
  39. image.save('output.png')
复制代码
2. 图生图(python 示例)
  1. import json
  2. import requests
  3. import io
  4. import base64
  5. from PIL import Image
  6. import cv2
  7. url = "http://127.0.0.1:7860"
  8. prompt = "cat"
  9. negative_prompt = ""
  10. # 此处为读取一张图片作为输入图像
  11. img = cv2.imread('image.jpg')
  12. # 编码图像
  13. retval, bytes = cv2.imencode('.png', img)
  14. encoded_image = base64.b64encode(bytes).decode('utf-8')
  15. payload = {
  16. #     # 模型设置
  17. #     "override_settings":{
  18. #           "sd_model_checkpoint": "v1-5-pruned.ckpt",
  19. #           "sd_vae": "animevae.pt",
  20. #           "CLIP_stop_at_last_layers": 2,
  21. #     },
  22.     # 基本参数
  23.     "prompt": prompt,
  24.     "negative_prompt": negative_prompt,
  25.     "steps": 30,
  26.     "sampler_name": "Euler a",
  27.     "width": 512,
  28.     "height": 512,
  29.     "batch_size": 1,
  30.     "n_iter": 1,
  31.     "seed": 1,
  32.     "cfg_scale": 7,
  33.     "CLIP_stop_at_last_layers": 2,
  34.    
  35.     "init_images": [encoded_image],
  36.     # 面部修复 face fix
  37.     "restore_faces": False,
  38.     #高清修复 highres fix
  39.     # "enable_hr": True,
  40.     # "denoising_strength": 0.4,
  41.     # "hr_scale": 2,
  42.     # "hr_upscaler": "Latent",
  43. }
  44. response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
  45. r = response.json()
  46. image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
  47. image.show()
  48. image.save('output.png')
复制代码
如要修改其他参数可参照官网文档举行修改。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

前进之路

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表