Automatic now has an API!
— StableDiffusion — 2 min read
Unless you are watching the Automatic1111 repo like a hawk, you are probably unaware that Automatic now has an API. Both the txt2img and img2img endpoints are now implemented.
This is a big deal for the project, and I'm excited to share it with you.
If you're a developer, it's great news because making your own Stable Diffusion frontend just became a lot easier. If you're not, then you're about to see an explosion in the number of frontend options.
Here's why:
Up until now, if you wanted to build your own Stable Diffusion frontend, you needed to work with the base stable diffusion code, which involved a cumbersome process of writing your own server/wrapper logic around the original text2image/image2image scripts.
The first thing you'll notice when you start doing that is how much slower/more memory intensive the base 1.4 codebase is. Running the scripts directly can easily take 30+ seconds, even on a beefy GPU.
In the past month, there has been an explosion in the number of applications and optimizations on the original codebase. By writing your own frontend, you were necessarily losing out on those unless you implemented them yourself.
Now, it'll be possible to take advantage of those features and optimizations while writing a custom frontend.
Put another way, the Automatic1111 project is positioning itself to become a more optimized wrapper around stable diffusion that can be used to build out whatever app/webapp you can dream up. They are also making it possible for smaller teams keep up with the breakneck pace of developments.
It's really exciting news, and I can't wait to see what people build with it.
Getting set up
Linux users
All you need to do is:
- Pull the newest version of the repo. You can either do this with a
git pull
, or download the zip of from the Github repo. - Run the launch script with the
--api
flag
Windows users
All you need to do is:
- Pull the newest version of the repo. You can either do this with a
git pull
, or download the zip of from the Github repo. - Run
webui.bat
in the command line with the--api
flag (thanks to u/rmngtnspz for the correction)
Hello World
So you've got the API up and running. Now we'll need to make our first API call. Something exciting, something to comemorate this historic moment.
Might I suggest:
1curl --request POST --url 'http://localhost:7860/sdapi/v1/txt2img' --data '{"prompt": "Astronaut planting a flag on the moon"}' --header 'Content-Type: application/json'
You'll get back a large json response, the important part of which is the image. It'll look like this:
1{2 "images": "yourBase64Image",3 "parameters": {4 ...parameter list5 }6}
Img2Img
Hitting the img2img endpoint is going to be very similar. The main difference is that you're going to want to pass a base64 encoded image array in the request. You can also pass an optional base64 mask.
Here's an example (edited for brevity):
1curl --request POST \2 --url 'https://819e2173ef18d780.gradio.app/sdapi/v1/img2img?key=AIzaSyCH2-dsIEoNEYo3D4xxtE2k6oMqA7Fh8aY&photo_reference=AeJbb3fIZTaOZrUQyUY6lTA9Jd5pPXW6bj15Em8ntgI6DjURn0uk_h0POh34Cnj8nHRfOEl5yPEDOuLKoV-8q_dEvZ0B9C3U4BdMRcVno6239qEBIVgFPsGrmumRiXvEYTJGa33eqiN-OJZzcCJYQ6SW6NHyeqKR9SMFRM4H7yj0WIpUa3-7&maxwidth=400' \3 --header 'Content-Type: application/json' \4 --data '{5 "prompt": "Astronaut standing on a barren planet",6 "mask": "data:image/png;base64,iVBORw0K...",7 "init_images": ["data:image/png;base64,iVBORw0K..."]8}'
With the same json response as above:
1{2 "images": "yourBase64Image",3 "parameters": {4 ...parameter list5 }6}
Documentation
You can also check out the docs (if your instance is running locally) here. If you're using a share link, it'll be at your gradio link + /docs#/default/text2imgapi_sdapi_v1_txt2img_post
.
Thanks for reading!