stable-diffusion-webui icon indicating copy to clipboard operation
stable-diffusion-webui copied to clipboard

[API][Feature] - Add img2img API endpoint

Open sphuff opened this issue 3 years ago • 2 comments

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 avatar Oct 21 '22 23:10 sphuff

@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.

benrugg avatar Oct 22 '22 14:10 benrugg

this needs to be verified to work with mask argument, because it most likely will not

AUTOMATIC1111 avatar Oct 22 '22 17:10 AUTOMATIC1111

@AUTOMATIC1111 working on that now 👍

sphuff avatar Oct 22 '22 19:10 sphuff

@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

sphuff avatar Oct 22 '22 19:10 sphuff

@AUTOMATIC1111 verified that mask is now working

image: tmpk72qrcuk mask: mask output: out

video of insomnia call:

https://user-images.githubusercontent.com/3484403/197363078-954a72a5-9779-4561-8756-2b2de1f72dac.mp4

sphuff avatar Oct 22 '22 21:10 sphuff

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 avatar Oct 23 '22 01:10 ArcticFaded

@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

sphuff avatar Oct 23 '22 02:10 sphuff

We could verify by both expecting .png/.jpeg AND checking the mime/type in the headers to be extra safe

ArcticFaded avatar Oct 23 '22 04:10 ArcticFaded

Your api code breaks with python 3.7.15 which colab runs on.

Traceback (most recent call last): File "launch.py", line 206, in start_webui() File "launch.py", line 201, in start_webui webui.webui() File "/content/stable-diffusion-webui/webui.py", line 149, in webui create_api(app) File "/content/stable-diffusion-webui/webui.py", line 104, in create_api from modules.api.api import Api File "/content/stable-diffusion-webui/modules/api/api.py", line 17, in class TextToImageResponse(BaseModel): File "/content/stable-diffusion-webui/modules/api/api.py", line 18, in TextToImageResponse images: list[str] TypeError: 'type' object is not subscriptable

The fix is using List from typing.

from typing import List

class TextToImageResponse(BaseModel): images: List[str] parameters: Json info: Json

kantsche avatar Oct 24 '22 10:10 kantsche

also maybe consider sending the info as object so you can get the last seed without extracting it from a string Adobe_UXP_Developer_Tool_NSZwXtfURM

kantsche avatar Oct 24 '22 12:10 kantsche

also you send the initimage and mask back, which unnecessarily increases the amount of data sent

kantsche avatar Oct 24 '22 13:10 kantsche

@ryukra thanks for the info/suggestions! opened up a PR to fix

sphuff avatar Oct 24 '22 16:10 sphuff

@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 avatar Oct 24 '22 22:10 benrugg

@benrugg of course, glad you're getting some use out of it! great work on AI-Render btw, love the project

sphuff avatar Oct 25 '22 15:10 sphuff

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".

Keavon avatar Oct 25 '22 19:10 Keavon

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

RandomInternetPreson avatar Nov 10 '22 05:11 RandomInternetPreson

@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 avatar Nov 10 '22 16:11 benrugg

@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.

img2imgv6.pdf

RandomInternetPreson avatar Nov 10 '22 19:11 RandomInternetPreson

@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 avatar Nov 11 '22 11:11 lalamax3d

@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.

RandomInternetPreson avatar Nov 11 '22 21:11 RandomInternetPreson

Does anyone else have a problem with the endpoint returning the same images sent into init_images?

ngokevin avatar Nov 19 '22 00:11 ngokevin

@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.

img2imgv6.pdf

Very helpful and it works, thank you very much!

orlop avatar Jan 10 '23 15:01 orlop