经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » 人工智能基础 » 查看文章
Stable-diffusion WebUI API调用方法
来源:cnblogs  作者:小白开水66  时间:2023/10/17 11:02:32  对本文有异议
写这篇文章的主要原因是工作中需要写一个用训练好的模型批量生图的脚本,开始是想用python直接加载模型,但后来发现webui的界面中有不少好用的插件和参数,所以最终改成调用WebUI接口的方式来批量生图。

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

相关文档

官方文档:https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API

运行方式

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

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

  1. ## -- api调用示例代码 -- ##
  2. import json
  3. import base64
  4. import requests
  5. # 发送请求
  6. def submit_post(url: str, data: dict):
  7. return requests.post(url, data=json.dumps(data))
  8. # 解码并保存图片
  9. def save_encoded_image(b64_image: str, output_path: str):
  10. with open(output_path, 'wb') as image_file:
  11. image_file.write(base64.b64decode(b64_image))
  12. if __name__ == '__main__':
  13. txt2img_url = r'http://127.0.0.1:8085/sdapi/v1/txt2img'
  14. data = {'prompt': 'a dog wearing a hat',
  15. 'negative_prompt': '',
  16. 'sampler_index': 'DPM++ SDE',
  17. 'seed': 1234,
  18. 'steps': 20,
  19. 'width': 512,
  20. 'height': 512,
  21. 'cfg_scale': 8}
  22. response = submit_post(txt2img_url, data)
  23. save_image_path = r'./result/tmp.png'
  24. save_encoded_image(response.json()['images'][0], save_image_path)

/sdapi/v1/txt2img接口scripts参数列表,以xyz plot为例

  1. {
  2. "enable_hr": false,
  3. "denoising_strength": 0,
  4. "firstphase_width": 0,
  5. "firstphase_height": 0,
  6. "hr_scale": 2,
  7. "hr_upscaler": "",
  8. "hr_second_pass_steps": 0,
  9. "hr_resize_x": 0,
  10. "hr_resize_y": 0,
  11. "hr_sampler_name": "",
  12. "hr_prompt": "",
  13. "hr_negative_prompt": "",
  14. "prompt": "cute girl with short brown hair in black t-shirt in animation style",
  15. "styles": [
  16. ""
  17. ],
  18. "seed": -1,
  19. "subseed": -1,
  20. "subseed_strength": 0,
  21. "seed_resize_from_h": -1,
  22. "seed_resize_from_w": -1,
  23. "sampler_name": "Euler a",
  24. "batch_size": 1,
  25. "n_iter": 1,
  26. "steps": 50,
  27. "cfg_scale": 7,
  28. "width": 512,
  29. "height": 512,
  30. "restore_faces": false,
  31. "tiling": false,
  32. "do_not_save_samples": false,
  33. "do_not_save_grid": false,
  34. "negative_prompt": "",
  35. "eta": 0,
  36. "s_min_uncond": 0,
  37. "s_churn": 0,
  38. "s_tmax": 0,
  39. "s_tmin": 0,
  40. "s_noise": 1,
  41. "override_settings": {},
  42. "override_settings_restore_afterwards": true,
  43. "script_args": [4,"20,30",[],9,"Euler a, LMS",[],0,"",[],"True","False","False","False",0], # xyz plot参数
  44. "sampler_index": "Euler",
  45. "script_name": "X/Y/Z Plot",
  46. "send_images": true,
  47. "save_images": false,
  48. "alwayson_scripts": {}
  49. }

第三方开源库(推荐)

https://github.com/mix1009/sdwebuiapi

这个开源库是webui官方推荐的,将大多数api的使用方法都集成到一起了,而且还提供了scripts参数的使用方式。虽然这个库已经很久没有更新了,很多issue也没有解决,但不妨碍我们参考它的函数使用方式。我们在使用的使用可以直接import webuiapi,也可以参照他们的实现方式来直接调用官方接口。

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

原文链接:https://www.cnblogs.com/gaojie22/p/17768218.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号