The neat thing about Stable Diffusion being open source is that you can modify it and build things with it. Photoshop plugins, bots, animations, fixing human foibles, all sorts.
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.
We maintain an open-source Python client for the API. Install it with pip:
There's also a community-maintained Node.js/JavaScript library. See replicate-js on GitHub.
Sign up for Replicate, then you can find your API token on your account page. Set your token in an environment variable in your terminal shell:
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:
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:
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.
init_image
to do image to image generationJoin us in Discord to show us what you've made, or if you need any help. We're looking forward to seeing your creations. ✨