Stable-diffusion WebUI API调用方法

写这篇文章的主要原因是工作中需要写一个用训练好的模型批量生图的脚本,开始是想用python直接加载模型,但后来发现webui的界面中有很多用起来比较方便的插件和参数,最终改成调用WebUI接口的方式来批量生图。

Stable-diffusion的webui界面使用比较方便,但是它的api文档比较简陋,很多功能需要去看源码,所以在这里记录下主要的调用方法

相关文档

官方文档:API · AUTOMATIC1111/stable-diffusion-webui Wiki · GitHub

运行方式

复制代码

|---|-------------------------------------------------------|
| | # 1. 首先需要在webui-user.bat中给COMMANDLINE_ARGS添加--api参数 |
| | # 2. 启动命令中需要添加nowebui |
| | python launch.py --nowebui |

然后使用http://ip:port/docs即可查看官方文档,需要替换ip:port为自己的地址才能看到,官方文档中有执行按钮可以执行example,能简单的看下返回效果

复制代码

|---|---------------------------------------------------------------------|
| | ## -- api调用示例代码 -- ## |
| | import json |
| | import base64 |
| | import requests |
| | |
| | # 发送请求 |
| | def submit_post(url: str, data: dict): |
| | return requests.post(url, data=json.dumps(data)) |
| | |
| | # 解码并保存图片 |
| | def save_encoded_image(b64_image: str, output_path: str): |
| | with open(output_path, 'wb') as image_file: |
| | image_file.write(base64.b64decode(b64_image)) |
| | |
| | |
| | if __name__ == '__main__': |
| | txt2img_url = r'http://127.0.0.1:8085/sdapi/v1/txt2img' |
| | |
| | data = {'prompt': 'a dog wearing a hat', |
| | 'negative_prompt': '', |
| | 'sampler_index': 'DPM++ SDE', |
| | 'seed': 1234, |
| | 'steps': 20, |
| | 'width': 512, |
| | 'height': 512, |
| | 'cfg_scale': 8} |
| | |
| | response = submit_post(txt2img_url, data) |
| | save_image_path = r'./result/tmp.png' |
| | save_encoded_image(response.json()['images'][0], save_image_path) |

/sdapi/v1/txt2img接口scripts参数列表,以xyz plot为例,可以直接复制到官方文档的测试窗口中查看一下效果

复制代码

|---|------------------------------------------------------------------------------------------------------------|
| | { |
| | "enable_hr": false, |
| | "denoising_strength": 0, |
| | "firstphase_width": 0, |
| | "firstphase_height": 0, |
| | "hr_scale": 2, |
| | "hr_upscaler": "", |
| | "hr_second_pass_steps": 0, |
| | "hr_resize_x": 0, |
| | "hr_resize_y": 0, |
| | "hr_sampler_name": "", |
| | "hr_prompt": "", |
| | "hr_negative_prompt": "", |
| | "prompt": "cute girl with short brown hair in black t-shirt in animation style", |
| | "styles": [ |
| | "" |
| | ], |
| | "seed": -1, |
| | "subseed": -1, |
| | "subseed_strength": 0, |
| | "seed_resize_from_h": -1, |
| | "seed_resize_from_w": -1, |
| | "sampler_name": "Euler a", |
| | "batch_size": 1, |
| | "n_iter": 1, |
| | "steps": 50, |
| | "cfg_scale": 7, |
| | "width": 512, |
| | "height": 512, |
| | "restore_faces": false, |
| | "tiling": false, |
| | "do_not_save_samples": false, |
| | "do_not_save_grid": false, |
| | "negative_prompt": "", |
| | "eta": 0, |
| | "s_min_uncond": 0, |
| | "s_churn": 0, |
| | "s_tmax": 0, |
| | "s_tmin": 0, |
| | "s_noise": 1, |
| | "override_settings": {}, |
| | "override_settings_restore_afterwards": true, |
| | "script_args": [4,"20,30",[],9,"Euler a, LMS",[],0,"",[],"True","False","False","False",0], # xyz plot参数 |
| | "sampler_index": "Euler", |
| | "script_name": "X/Y/Z Plot", |
| | "send_images": true, |
| | "save_images": false, |
| | "alwayson_scripts": {} |
| | } |

第三方开源库(推荐)

GitHub - mix1009/sdwebuiapi: Python API client for AUTOMATIC1111/stable-diffusion-webui

这个开源库是webui官方推荐的,就是简单的给官方的api套了个壳,而且还提供了scripts参数的使用方式。虽然这个库已经很久没有更新了,很多issue也没有解决,但不妨碍我们参考它的函数使用方式。我们在使用的时候可以直接import webuiapi,也可以参照他们的实现方式来直接调用官方接口。

复制代码

|---|---------------------------------------------------------------------------------|
| | import webuiapi |
| | from PIL import Image |
| | |
| | # create API client with custom host, port |
| | api = webuiapi.WebUIApi(host='127.0.0.1', port=8085) |
| | |
| | XYZPlotAvailableTxt2ImgScripts = [...] # 根据脚本参数自行增加调整xyz轴可选择的参数内容 |
| | |
| | # 省略部分参数定义 |
| | ... |
| | |
| | # 参数与官方文档的txt2img完全一致,参照上文参数文档 |
| | result = api.txt2img( |
| | prompt="cute girl with short brown hair in black t-shirt in animation style", |
| | seed=1003, |
| | script_name="X/Y/Z Plot", |
| | script_args=[ |
| | # index,对应xyz轴每个变量在滚动条中的索引数 |
| | XYZPlotAvailableTxt2ImgScripts.index(XAxisType), |
| | # 选择的对应坐标轴的变量值 |
| | XAxisValues, |
| | # 变量值下拉列表,webui库更新的1.16之后,新增的参数,必填,不然无法执行生图操作 |
| | [], |
| | XYZPlotAvailableTxt2ImgScripts.index(YAxisType), |
| | YAxisValues, |
| | ["model_6.safetensors","model_16.safetensors"], |
| | XYZPlotAvailableTxt2ImgScripts.index(ZAxisType), |
| | ZAxisValues, |
| | [], |
| | drawLegend, |
| | includeLoneImages, |
| | includeSubGrids, |
| | noFixedSeeds, |
| | marginSize, |
| | ] |
| | ) |
| | |
| | |
| | # save image with jpg format |
| | img = result.image |
| | img.save("./result/output2.jpg", quality=90) |

相关推荐
迈火3 天前
Facerestore CF (Code Former):ComfyUI人脸修复的卓越解决方案
人工智能·gpt·计算机视觉·stable diffusion·aigc·语音识别·midjourney
重启编程之路4 天前
Stable Diffusion 参数记录
stable diffusion
孤狼warrior7 天前
图像生成 Stable Diffusion模型架构介绍及使用代码 附数据集批量获取
人工智能·python·深度学习·stable diffusion·cnn·transformer·stablediffusion
love530love9 天前
【避坑指南】提示词“闹鬼”?Stable Diffusion 自动注入神秘词汇 xiao yi xian 排查全记录
人工智能·windows·stable diffusion·model keyword
世界尽头与你9 天前
Stable Diffusion web UI 未授权访问漏洞
安全·网络安全·stable diffusion·渗透测试
love530love9 天前
【故障解析】Stable Diffusion WebUI 更换主题后启动报 JSONDecodeError?可能是“主题加载”惹的祸
人工智能·windows·stable diffusion·大模型·json·stablediffusion·gradio 主题
ai_xiaogui14 天前
Stable Diffusion Web UI 绘世版 v4.6.1 整合包:一键极速部署,深度解决 AI 绘画环境配置与 CUDA 依赖难题
人工智能·stable diffusion·环境零配置·高性能内核优化·全功能插件集成·极速部署体验
微学AI15 天前
金仓数据库的新格局:以多模融合开创文档数据库
人工智能·stable diffusion
我的golang之路果然有问题15 天前
开源绘画大模型简单了解
人工智能·ai作画·stable diffusion·人工智能作画
我的golang之路果然有问题15 天前
comfyUI中的动作提取分享
人工智能·stable diffusion·ai绘画·人工智能作画·comfy