Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Implementation working with Gradio #3040

Merged
merged 16 commits into from
Oct 19, 2022
Merged

API Implementation working with Gradio #3040

merged 16 commits into from
Oct 19, 2022

Conversation

ArcticFaded
Copy link
Collaborator

@ArcticFaded ArcticFaded commented Oct 18, 2022

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.

@simcop2387
Copy link
Contributor

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 simcop2387 mentioned this pull request Oct 18, 2022
6 tasks
@VictorJulianiR
Copy link

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

@htkg
Copy link

htkg commented Oct 19, 2022

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

@Keavon
Copy link
Contributor

Keavon commented Oct 19, 2022

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

@Interpause
Copy link

Interpause commented Oct 20, 2022

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.

@SethRobinson
Copy link

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)

@coldeny
Copy link

coldeny commented Oct 20, 2022

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

@ArcticFaded
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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

Yeah those are coming in separate PRs

@Keavon
Copy link
Contributor

Keavon commented Oct 21, 2022

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

@loopstring
Copy link

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 '{}'

@lalamax3d
Copy link

@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"
}'

@benrugg
Copy link

benrugg commented Oct 25, 2022

@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: #3381 (comment)

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

@Interpause
Copy link

Interpause commented Oct 26, 2022

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
Copy link

Interpause commented Oct 26, 2022

Interpause@ee4cc32

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.

@RandomInternetPreson
Copy link

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: #3040 (comment)

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
Copy link

RandomInternetPreson commented Nov 10, 2022

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

#3381 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.