From d0f3a0920c6528adc2c92596456da66232c2908a Mon Sep 17 00:00:00 2001 From: ClashSAN <98228077+ClashSAN@users.noreply.github.com> Date: Thu, 27 Oct 2022 05:26:51 +0000 Subject: [PATCH] Updated API (markdown) --- API.md | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/API.md b/API.md index 733f003..2c93b16 100644 --- a/API.md +++ b/API.md @@ -1,17 +1,19 @@ ## API guide by [@Kilvoctu](https://github.com/Kilvoctu) --First, of course, is to run web ui with `--api` commandline argument --This enables the api which can be reviewed at http://127.0.0.1:7860/docs (or whever the URL is + /docs) +- First, of course, is to run web ui with `--api` commandline argument +- This enables the api which can be reviewed at http://127.0.0.1:7860/docs (or whever the URL is + /docs) The basic ones I'm interested in are these two. Let's just focus only on ` /sdapi/v1/txt2img` + ![image](https://user-images.githubusercontent.com/2993060/198171114-ed1c5edd-76ce-4c34-ad73-04e388423162.png) --When you expand that tab, it gives an example of a payload to send to the API. I used this often as reference. +- When you expand that tab, it gives an example of a payload to send to the API. I used this often as reference. + ![image](https://user-images.githubusercontent.com/2993060/198171454-5b826ded-5e73-4249-9c0c-a97b32c42569.png) ------ --So that's the backend. The API basically says what's available, what it's asking for, and where to send it. Now moving onto the frontend, I'll start with constructing a payload with the values I want. An example can be: +- So that's the backend. The API basically says what's available, what it's asking for, and where to send it. Now moving onto the frontend, I'll start with constructing a payload with the values I want. An example can be: ``` payload = { "prompt": "maltese puppy", @@ -20,7 +22,7 @@ payload = { ``` I can put in as few or as many values as I want in the payload. The API will use the defaults for anything I don't set. --After that, I dump the json, whatever that means, then I can send it to the API +- After that, I dump the json, whatever that means, then I can send it to the API ``` payload_json = json.dumps(payload) response = requests.post(url=f'http://127.0.0.1:7860/sdapi/v1/txt2img', data=payload_json).json() @@ -30,13 +32,13 @@ If we execute this code, the web ui will generate an image based on the payload. ------ --After the backend does its thing, the API sends the response back in a variable that was assigned above: `response`. The response contains three entries; "images", "parameters", and "info", and I have to find some way to get the information from these entries. --"images" is the generated image, which is what I want mostly. There's no link or anything; it's a giant string of random characters, apparently we have to decode it. This is how I do it: +- After the backend does its thing, the API sends the response back in a variable that was assigned above: `response`. The response contains three entries; "images", "parameters", and "info", and I have to find some way to get the information from these entries. +- "images" is the generated image, which is what I want mostly. There's no link or anything; it's a giant string of random characters, apparently we have to decode it. This is how I do it: ``` for i in response['images']: image = Image.open(io.BytesIO(base64.b64decode(i))) ``` --With that, we have an image in the `image` variable that we can work with, for example saving it with `image.save('output.png')`. +- With that, we have an image in the `image` variable that we can work with, for example saving it with `image.save('output.png')`. -"parameters" shows what was sent to the API, which could be useful, but what I want in this case is "info". I use it to insert metadata into the image, so I can drop it into web ui PNG Info. For that I simply reference it with `response['info']` ------ @@ -65,12 +67,12 @@ for i in response['images']: pnginfo.add_text("parameters", str(response['info'])) image.save('output.png', pnginfo=pnginfo) ``` --Import the things I need --define the url and the payload to send --send said payload to said url through the API --when we get the response, grab "images" and decode it --define a plugin to add png info, then add "info" into it --at the end here, save the image with the png info +- Import the things I need +- define the url and the payload to send +- send said payload to said url through the API +- when we get the response, grab "images" and decode it +- define a plugin to add png info, then add "info" into it +- at the end here, save the image with the png info For a more complete implementation of a frontend, my Discord bot is [here](https://github.com/Kilvoctu/aiyabot) if anyone wants to look at it as an example. Most of the action happens in stablecog.py. There are many comments explaining what each code does. Also a reference can be https://github.com/Kilvoctu/aiyabot/pull/11, my PR where I added API support.