This is a guide on how to use DreamStudio API for Stable Diffusion Image generation AI in Python.
Stability AI finally released its Image generation AI, Stable Diffusion in Open Source. Along with the code, they also launched a new consumer-facing product called DreamStudio
DreamStudio is an OpenAI Dall-e like Image generation tool that you can use in a browser. They also released an API for the Dream Studio that can be used by developers in their scripts and applications via code.
This guide is oriented towards people who are programmers and coders. If you are looking for a way to use Stable Diffusion as an end user, check out guides for windows, Macs, Machines without GPU, etc
Table of Contents
Get a Stable Diffusion DreamStudio API Key
First, you need your own API key for the Stable Diffusion DreamStudio. Here’s how to get your own Stable Diffusion API key.
- Create an account on or log in to DreamStudio
- From your profile Icon on the top right corner, go to your membership area
- Click on API Key and copy your API key
Keep this safe and don’t share anyone as whosoever has access to this key can use it to exhaust your generation quota.
Using Stable Diffusion DreamStudio API in a Python Script
If you prefer calling the API in a Python script on your local machine or on a cloud VM server, here are the steps:
Note: Code credit to the Official Stability AI SDK Colab Notebook
Make sure you have Python 3 or above installed on your machine and you can run it from the command line.
Install Stable Diffusion Python SDK/module
pip install stability-sdk
apt-get install libmagic1
pip install python-libmagic
Create a new python file on your favorite IDE and paste the below code in the file
Change the prompt in the above code to get a different result. Also, make sure to add the API at the appropriate place.
import io import os import warnings from PIL import Image from stability_sdk import client import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
stability_api = client.StabilityInference( key='STABILITY_API_KEY', verbose=True, ) answers = stability_api.generate( prompt="houston, we are a 'go' for launch!" ) for resp in answers: for artifact in resp.artifacts: if artifact.finish_reason == generation.FILTER: warnings.warn( "Your request activated the API's safety filters and could not be processed." "Please modify the prompt and try again.") if artifact.type == generation.ARTIFACT_IMAGE: img = Image.open(io.BytesIO(artifact.binary)) display(img) ---------------------
Run the file by passing it to the python interpreter
Image-to Image generation using DreamStudio API
Above we saw how to generate text-to-image using DreamStudio API.
Now we will see how to use the image-to-image feature.
You don’t need to do the setup again and can use what is explained above.
You need to pass an additional parameter to the API generate function
here img is the variable that contains the image you want to feed to the API
Using Stable Diffusion DreamStudio API in Jupyter Notebook
- Open this official Google Colab Notebook.
- In the Google Colab Notebook, you just opened, select Runtime from the top menu bar, and click on Run all
- You will be asked to enter your API key for DreamStudio. Enter the API key you copied in the above section and press enter
- Once it is finished it would have your API key for Dream Studio to generate an image using Stable Diffusion AI.
- In order to get a different image, change the prompt in the last cell and click on the run button on the left side of the cell.
In this article, we saw how to use Stability AI’S DreamStudio API to generate images. We saw how to do this using Python on a local machine as well as in Google Colab Notebook.