Run Stable Diffusion with an API
Posted August 29, 2022 by @zeke
But, if you want to integrate it into an app or a project, you need to set it up with GPUs and put an API in front of it. And the GPUs are expensive so you don't want to leave them switched on all the time.
Replicate lets you run machine learning models from your own code without having to set up any infrastructure. In this post, we'll show you how to use it to run Stable Diffusion.
Install the Python library
We maintain an open-source Python client for the API. Install it with pip:
pip install replicate
You'll be able to use the API for free for a bit, but eventually we'll ask you to enter your credit card. We only charge you by the second for the amount of time your requests are running, so it usually works out much cheaper than running your own GPUs.
Create a file called
dream.py and paste in the following content:
import replicate import webbrowser model = replicate.models.get("stability-ai/stable-diffusion") output_url = model.predict(prompt="electric sheep, neon, synthwave") print(output_url) webbrowser.open(output_url)
Then run the script from your terminal:
This will create a prediction using the API and open the generated image in your web browser. It might look something like this:
View your predictions
Whenever you run a model on Replicate, whether in the browser or with the API, the prediction is saved and associated with your user account. Visit your dashboard to view all your previous predictions.
- Learn how to build a Discord bot with Stable Diffusion
- See some more examples of what you can build
- Try setting
init_imageto do image to image generation
- Learn more about how the HTTP API works
- Generate animations using andreasjansson/stable-diffusion-animation
Join us in Discord to show us what you've made, or if you need any help. We're looking forward to seeing your creations. ✨