stable-diffusion-webui
stable-diffusion-webui copied to clipboard
[API][Feature] - Add img2img API endpoint
Validated with curl, a postman client (insomnia), and a frontend that I built.
https://user-images.githubusercontent.com/3484403/197305320-f59c73d6-e926-40d0-bad4-d2de1a77a810.mp4
https://user-images.githubusercontent.com/3484403/197305328-fd8fa479-6759-4aba-b7c7-b6d659c4a6a5.mp4
@sphuff Yes! This is what many of us have been waiting for. Please let me know if there is anything I can do to contribute. Also, docs for the API would be really helpful.
this needs to be verified to work with mask argument, because it most likely will not
@AUTOMATIC1111 working on that now 👍
@benrugg Thanks! I should be able to wrap this up this afternoon, but I'll let you know if I hit any roadblocks. You'll be able to check out the API docs by going to YOUR_GRADIO_URL/docs#/default/text2imgapi_sdapi_v1_txt2img_post for example
@AUTOMATIC1111 verified that mask is now working
image:
mask:
output:

video of insomnia call:
https://user-images.githubusercontent.com/3484403/197363078-954a72a5-9779-4561-8756-2b2de1f72dac.mp4
im wondering - can we also allow the posting of a URL to download as an image via the API? We can check beforehand whether its base64 or starts with http/https
@ArcticFaded yeah, definitely would be nice on the API consumer side, and also easy to implement. My only concern is on the security side - do we want to hit whatever link gets sent? Or maybe we limit it to URLs that end in .png/jpeg/whatever
We could verify by both expecting .png/.jpeg AND checking the mime/type in the headers to be extra safe
Your api code breaks with python 3.7.15 which colab runs on.
Traceback (most recent call last):
File "launch.py", line 206, in
The fix is using List from typing.
from typing import List
class TextToImageResponse(BaseModel): images: List[str] parameters: Json info: Json
also maybe consider sending the info as object so you can get the last seed without extracting it from a string

also you send the initimage and mask back, which unnecessarily increases the amount of data sent
@ryukra thanks for the info/suggestions! opened up a PR to fix
@sphuff Just wanted to say thank you for implementing this! This is soooo helpful - it should help a lot of other apps like mine avoid breaking changes in the future. And it was 10x easier to implement than hacking together a gradio api, like I did last week.
@benrugg of course, glad you're getting some use out of it! great work on AI-Render btw, love the project
Would it be possible to get the in-progress images during the generation process as part of the API? My application shows the denoising process while the user waits, and I'd like to switch from the unstable Gradio list-based "API".
Is there something wrong with the img2img api? I can get the txt2img code working here
http://127.0.0.1:7860/docs#/default/text2imgapi_sdapi_v1_txt2img_post
SethRobinson's post here was very useful: https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/3040#issuecomment-1284746589
I can convert the output Base64 txt to an image and everything works.
But when I put Base64 txt in substitution for the word "string" in the img2img code
"init_images":[ "string" ]
http://127.0.0.1:7860/docs#/default/img2imgapi_sdapi_v1_img2img_post
I get an "Internal Server Error" in the response body and
"content-length: 21 content-type: text/plain; charset=utf-8 date: Thu,10 Nov 2022 05:23:33 GMT server: uvicorn "
error in the response headers
@AaronAlai I can't speak for the contributors on this project, but as a user, it's working well for me. I'd suggest two possible things to check:
- prefix your base64 txt with
data:image/png;base64, - if even the docs link isn't working for you, then something else is going wrong
@benrugg Thank you for the response! After a late night and a lot of thinking I managed to get everything to work in Spyder5. I still couldn't get it to work in the ad api window http://127.0.0.1:7860/docs#/default/img2imgapi_sdapi_v1_img2img_post
But that doesn't matter, I didn't intend on using it through those means anyway I really wanted code I could do other stuff with.
Using help from this post here: https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/3040#issuecomment-1284746589
and some head scratching/luck I've got functioning python code
I've included a pdf of the code to this comment incase someone in the future needs it.
@AaronAlai , thanks, i just wanna ask a small thing, what is best way to query / check progress of request. i.e when request sent from webui(web) it shows prgress in %. if i am creating a flask app, which is sending request via api, what is best way to get progress. huge thanks in advance.
@lalamax3d I think you want to use the "eta": 1 command? Not sure exactly though, I look at the cmd window to get an idea of amount done. If you have your automatic1111 program up you can click on this link: http://127.0.0.1:7860/docs#/default/img2imgapi_sdapi_v1_img2img_post
And it will open up a window in your browser showing you all the things you can get a from the api, if you look at that and my code you'll see the structure and how to add new stuff.
Does anyone else have a problem with the endpoint returning the same images sent into init_images?
@benrugg Thank you for the response! After a late night and a lot of thinking I managed to get everything to work in Spyder5. I still couldn't get it to work in the ad api window http://127.0.0.1:7860/docs#/default/img2imgapi_sdapi_v1_img2img_post
But that doesn't matter, I didn't intend on using it through those means anyway I really wanted code I could do other stuff with.
Using help from this post here: #3040 (comment)
and some head scratching/luck I've got functioning python code
I've included a pdf of the code to this comment incase someone in the future needs it.
Very helpful and it works, thank you very much!