Table of contents
Learn how to push a Diffusers image generation model to Replicate as a scalable API.
Diffusers is an open-source Python library that provides a consistent interface for using diffusion models for image generation, the most popular of which is Stable Diffusion.
There are lots of ways to use Stable Diffusion: You can run it from your browser on Replicate's website, or run it from your code using Replicate's API. You can even run it on your own machine.
But what if you want to customize the model? There are a lot of things you might want to change:
In this guide we'll walk you through the steps to create your own model, so you can make these kinds of changes and push them to Replicate as your own public or private model with a stable API.
To follow this guide, you'll need:
docker info
in your terminal.First, create a model on Replicate at replicate.com/create. If you haven't used Replicate before, you'll need to sign in with your GitHub account. You can configure the model to be private so that only you can use it, or public so anyone can use it.
Replicate maintains an open-source Stable Diffusion repo on GitHub at replicate/cog-stable-diffusion.
Create your own fork of the repo on GitHub, or clone the repo directly:
This repo is fairly small, and contains two noteworthy files:
cog.yaml
- Defines the CUDA and Python versions, and dependencies for the model. This file tells Cog how to package the model.predict.py
- Defines the inputs and outputs of the model, and the code to run the model. The Stable Diffusion model itself is imported through the Python diffusers
library.Cog is an open source tool that makes it easy to put a machine learning model in a Docker container. Run the following commands to install it and set the correct permissions:
Confirm that Cog is installed by running cog --version
:
The repo you cloned is configured to build Stable Diffusion 2.1 by default, but you can customize it to use any diffusers-compatible model, including trained models like OpenJourney, Analog Diffusion, and Realistic Vision.
To use a different model, open predict.py
and change the value of MODEL_ID
to a shorthand {owner}/{repo}
string that corresponds to a diffusers-compatible model on Hugging Face:
If you just want to use a different pretrained model, this is the only change that's needed.
If you want to make other changes to your model's behavior, take a look at predict.py
, which is the Python interface that defines the inputs and outputs for your model.
Now that you've configured your model, it's time to download the weights:
This process will take a few minutes but you'll only need to run it once, as it will cache the downloaded dependencies on disk. Get up and stretch, grab yourself a snack, or use this opportunity to add metadata to the model page you created on Replicate in Step 1 by adding a title, README, GitHub repository URL, etc.
Now that you've downloaded the weights, you can run the model locally with Cog:
This will run the model locally and return the output image. You can change the prompt, width, and height to see how the model responds to different inputs. Note that larger dimensions require more VRAM, so 512x512 is a sensible default.
Now that you've customized your model, it's time to push it to Replicate.
First you'll need to authenticate:
Then push your model using the name you specified in Step 1:
Your model is now live! 🚀
You can run the model from the website by clicking the "Demo" tab on the model page, or you can use the HTTP API to run the model from your own code.
Click the "API" tab on your model page to see example code for running the model:
Now that you have your own model, see what else you can do with it.
If you need inspiration or guidance, jump into our Discord.