Get Started With Stable Diffusion API For Free
Hello, we will be discussing how to get started with stable diffusion with Python
There are a couple of things you need to do before jumping into the project:
- Sign Up on GitHub. If you already have an account then skip it.
- Install Python in your system. You can download and install it from https://www.python.org/downloads/.
- This one is optional, but I will be using Visual Studio Code. If you wish to follow along you can install it from https://code.visualstudio.com/
Okay. You are good to go.
Installing Packages for Stable Diffusion:
As we have mentioned, we will be using stable diffusion. It is available at Replicate. So to use it we need to install the replicate package. Run the following command in your command prompt/terminal to install it.
pip install replicate
In case you’re having issues with pip install replicate, try pip3 install replicate. Make sure you’re running the command prompt as administrator.
Getting replicate API Key:
Now, we need to get our API key to generate art.
Step 1:
We first have to go to Replicate and click on Copy your API token.
Step 2:
Click Sign in with GitHub to proceed to the GitHub login page.
Step 3:
Log in to your GitHub Account with valid credentials.
Step 4:
Give the app permission to read your email addresses and proceed.
Step 5:
Voila! We got the API key. Copy and don’t share it with anyone or anywhere else.
Coding:
Here comes the most interesting part. Go to a directory and create a main.py file. And write lines of code to it.
Embedded Code:
Breaking Down the Code:
Firstly, we import the replicate package (duh!). Then we create an instance of the Client class, here we need to pass on the API key. So, the app now represents an object of the Client class.
The term variable represents the prompt. The run method has some arguments, first one here is the model, text2image in our case. The input argument takes a dictionary. It has the following parameters:
- prompt: Input prompt
Default value: a vision of paradise. unreal engine
- image_dimensions: Pixel dimensions of the output image. Allowed values:
512x512
,768x768
Default value: 768x768
- negative_prompt: Specify things to not see in the output
- num_outputs: Number of images to output.
Default value: 1
- num_inference_steps: Number of denoising steps
Default value: 150
- guidance_scale: Scale for classifier-free guidance
Default value: 7.5
- scheduler: Allowed values:
DDIM
,K_EULER
,
DPMSolverMultistep
, K_EULER_ANCESTRAL
, PNDM
, KLMS
Default value: DPMSolverMultistep
- seed: Random seed. Leave blank to randomize the seed
Output:
If you run the code, you will get to see something similar. Click any of the links generated from the script.
Our AI image generator is Ready!