Do you want to create AI art? With the release of the Dall-E 2 API, it’s now possible to create stunning AI-generated images with the help of the GPT-3 API. In this tutorial, we’ll show you how to use Python to combine these two powerful tools and create an AI art generator.
We’ll also provide a simple code example to help you get started. So what are you waiting for? Let’s get started!
Read more, or watch the YouTube video (Recommended):
AI Art Generator with GPT-3 and Dall-E 2 API in Python
On November 3rd OpenAI released the news that Dall-E 2 is now available as an API. So I really wanted to try to combine GPT-3 with the new Image API.
In this tutorial I tried to make the steps as simple as possible, and I really recommend you watch the YouTube video for this.
My idea was to create an AI Art generator that could take input text and transform this into prompts for the Dall-E 2 API.
I found this to be very simple to use, and I guess this can be upgraded to create some really cool stuff.
With Python the GPT-3 and the Dall-E 2 API works very well together, and the inference time is very low. This again is just a great example of an amazing use case for Generative AI.
So far I have been really impressed with how smooth this is working, but i don’t know yet if this can compete with the free and open source Stable Diffusion. But at least it is very user friendly as this tutorial will show you.
How can you use the Dall-E 2 API?
The Dall-E 2 API provides three methods for interacting with images:
1. Creating images from scratch based on a text prompt
2. Creating edits of an existing image based on a new text prompt
3. Creating variations of an existing image.
To generate an image, you provide a text prompt to the API, specifying the size of the image you want (256×256, 512×512, or 1024×1024 pixels) and the number of images you want to generate (1-10).
The API will then generate an original image based on your prompt.
Content moderation is built into the API, so prompts and images are automatically filtered based on OpenAI’s content policy.
If a prompt or image is flagged, an error will be returned.
Price of Dall-E 2 API
The price of the Dall-E 2 Image API is:
Build DALL·E directly into your apps to generate and edit novel images and art. Our image models offer three tiers of resolution for flexibility.
|1024×1024||$0.020 / image|
|512×512||$0.018 / image|
|256×256||$0.016 / image|
Price of GPT-3 API
The price of the DaVinci-002 model is:
$0.0200 / 1K tokens
Prices are per 1,000 tokens. You can think of tokens as pieces of words, where 1,000 tokens is about 750 words.
Simple Python Script with GPT-3 and Dall-E 2 API
The Python script I wrote in this tutorial was designed to be as simple as possible. I will write a step by step list on how i proceeded creating this GPT-3 + Dall-E 2 API script in Python:
- Imported openai, requests and os modules
- Created a openfile and savefile function
- Added my OpenAI API Key
- Wrote my prompt in a text file named prompt.txt
- Used the GPT-3 API to generate a prompt for Dall-E 2
- Saved the output to a text file
- Opened that text file as the input prompt for the Dall-E 2 API
- Ran the Dall-E 2 API
- Used requests.get(url) to download the image to my folder
- Go to folder and open the imagefile
This setup worked out very well. I also did a bit more advanced script that is described in the YouTube video.
Overall, the Dall-E 2 API and GPT-3 API work well together and are relatively easy to use. With a bit of Python scripting, you can create stunning AI-generated images.
In this tutorial, we showed you how to use these tools to create an AI art generator.
Now it’s your turn to experiment and see what you can create!
Feel free to contact me if you have any questions.