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:

  1. Sign Up on GitHub. If you already have an account then skip it.
  2. Install Python in your system. You can download and install it from https://www.python.org/downloads/.
  3. 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
Installing Replicate Package
Installing Replicate Package

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.

 

Getting Replicate API Key
Getting Replicate API Key

 

Step 2:

Click Sign in with GitHub to proceed to the GitHub login page.

Signing into Replicate using Github
Signing into Replicate using Github

 

Step 3:

Log in to your GitHub Account with valid credentials.

Logging into GitHub
Logging into GitHub

Step 4:

Give the app permission to read your email addresses and proceed.

Give Permission to Authorize
Give Permission to Authorize

Step 5:

Voila! We got the API key. Copy and don’t share it with anyone or anywhere else.

Generating Replicate API
Generating Replicate API

 

Coding:

Here comes the most interesting part. Go to a directory and create a main.py file. And write lines of code to it.

Code to Generate Images
Code to Generate Images

 

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:512x512768x768

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:DDIMK_EULER,

 DPMSolverMultistepK_EULER_ANCESTRALPNDMKLMS

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.

Stable Diffusion Prompt Output
Stable Diffusion Prompt Output

 

Our AI image generator is Ready!

Stable Diffusion Output
Stable Diffusion Output

Leave a Reply