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

API Implementation working with Gradio

Open ArcticFaded opened this issue 3 years ago • 1 comments
trafficstars

This is a working prototype of an API service running with Gradio - besides the regular Gradio calls there is now "/sdapi/v1/txt2img" which can be called as a POST request to invoke txt2img. Using queue_lock even when multiple users are active, regardless of interaction in the UI or API, all calls are queued.

This prototype also includes a standalone model if you only wanted to run the API and not Gradio. This implementation benefits from being flexible with the API contract - StableDiffusionProcessingAPI is built at runtime based off the definition of the class StableDiffusionProcessingTxt2Img, so if the method contract of StableDiffusionProcessingTxt2Img changes, the API would follow.

ArcticFaded avatar Oct 18 '22 07:10 ArcticFaded

Looks like this addresses the queue lock issue raised in my branch before I got the chance to do it. I'd end up doing the exact same thing here. So glad to see this getting further along :)

simcop2387 avatar Oct 18 '22 08:10 simcop2387

should this have a wiki page explaining what we can do with the API, with get and post examples?

VictorJulianiR avatar Oct 19 '22 13:10 VictorJulianiR

@ArcticFaded is img2img , interrogate features to be done? also is there any documentation on API?

htkg avatar Oct 19 '22 14:10 htkg

I'm also interested in polling for the latest in-progress image to view the denoising process, is that possible yet?

Keavon avatar Oct 19 '22 16:10 Keavon

Is there a discussion thread on the features and design for the API? I'm open to contributing stuff like getting & setting config options, which is necessary for me to migrate my plugin to the new API.

If possible, such a discussion thread should be open to coordinate work on features that aren't implemented yet.

Interpause avatar Oct 20 '22 00:10 Interpause

Yay, this is a big step in the right direction. Some comments for anybody else having problems:

Currently --api is ignored if launch.py is used due to how it hits webui() directly. A work around in webui.py is

def webui():
    launch_api = cmd_opts.api  #add this

I got errors with list in api.py due to using a pre 3.9 version python. fixed it with adding "from typing import List" and changing "List[str]" to "list[str]"

@VictorJulianiR, it's not much but here is a basic API example call that might be useful for testing. ]

For a full list of parms, check http://127.0.0.1:7860/docs#/default/text2imgapi_sdapi_v1_txt2img_post (change ip to wherever your server is running)

SethRobinson avatar Oct 20 '22 00:10 SethRobinson

Hello. May i ask why sampler_index is in API_NOT_ALLOWED in modules/api/processing.py ?

coldeny avatar Oct 20 '22 16:10 coldeny

sampler_index is re-added again at the API level - we are exposing it to the client as a name even though internally its just a number

ArcticFaded avatar Oct 20 '22 17:10 ArcticFaded

@ArcticFaded is img2img , interrogate features to be done? also is there any documentation on API?

Yeah those are coming in separate PRs

ArcticFaded avatar Oct 20 '22 17:10 ArcticFaded

@ArcticFaded could you perhaps create a tracking issue to coordinate progress of the features to be added? That could help the community build out those features with you instead of making you do it all yourself.

Keavon avatar Oct 21 '22 18:10 Keavon

Thank you for the great work. can you please post a step by step example on how to start the server with API enabled and test it with curl? The following command gets an empty response: curl -X POST http://127.0.0.1:7860/sdapi/v1/txt2img -H "Content-Type: application/json" -d '{}'

loopstring avatar Oct 23 '22 19:10 loopstring

@loopstring i run (launch) with this cmd (bash/shell) python stable-diffusion-webui_AUTO/webui.py --api in another tab(cmd window) following curl cmd, produces result

curl -X POST -H 'Content-Type: application/json' -i 'http://127.0.0.1:7860/sdapi/v1/txt2img' --data '{
           "prompt": "cute dinosaur sticker with polka dots",
        "steps": 50,
        "sampler_index": "DDIM"
}'

lalamax3d avatar Oct 25 '22 16:10 lalamax3d

@loopstring You can also see this thread for a link to the API docs (works for txt2img and img2img as long as api mode is enabled), and another example curl: https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/3381#issuecomment-1287894516

For the type of instructions that you could give users, here's what I just wrote up yesterday (ignore anything about the Blender-specific stuff, after step 6): https://github.com/benrugg/AI-Render/wiki/Local-Installation

benrugg avatar Oct 25 '22 16:10 benrugg

Anyone up to trying to write an API for calling scripts? At a quick glance, the code for using scripts is nearly indecipherable, so good job to whoever contributed that. I think pull requests should also be reviewed for quality... I am going to figure it out anyways for my plugin.

Inspecting the script.run() method if aiming to do it dynamically might be necessary: https://docs.python.org/3/library/inspect.html#inspect.signature

But it isn't sufficient to determine the input type or other constraints. Right now I am resorting to write a simple adapter to translate Gradio UI elements to Qt...

Interpause avatar Oct 26 '22 00:10 Interpause

https://github.com/Interpause/auto-sd-krita/commit/ee4cc324efc80a4a283506466bd5a18a3757fdef

Am able to dynamically generate the script GUI in the krita plugin, and then call the script via my API. Lots of intermediate conversion was needed. Whoever decides to eventually work on a pull request for the script API, you may take reference from this.

Interpause avatar Oct 26 '22 09:10 Interpause

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

Woot got it working, ref this comment if you are in need of python code to do img2img in automatic1111 using the API

https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/3381#issuecomment-1310773727

RandomInternetPreson avatar Nov 10 '22 19:11 RandomInternetPreson