I’ve been going through Generative AI for Beginners course on GitHub over the last couple of days.
In general, I found it quite useful not only for beginners, but also for those who have some experience with AI and ML. It’s a great way to get started with the latest developments in the field. I also found it useful to get a better understanding of the Azure OpenAI platform and its capabilities.
However, I found that some of the Python modules were written with an older API schema in mind. More specifically, the openai library is now at v1.0.6, but the code in the course is written for v0.26. This means that some of the code will not work as expected.
In this post, I’ll show you how to generate images using DALL-E in Azure OpenAI with Python using the latest API schema.
Here’s the full code I wrote to generate an image using DALL-E in Azure OpenAI with Python using a custom prompt:
The code is pretty straightforward. I will explain it in more detail in the sections below. I used a combination of code from the MS Learn website and the Generative AI For Beginners course. I also modified it a little bit to include a user prompt instead of a hardcoded value. This Python script uses the openai library to generate an image using DALL-E model in Azure OpenAI.
Note
You would need an instance of Azure OpenAI Service with a DALL-E model deployed. At the time of writing, it was available in Sweden Central Azure region.
Python Packages
Let’s have a look at Python packages that are required to run this code:
- openai - This is the main package that is used to generate an image using DALL-E in Azure OpenAI with Python.
- os - This package is used to get the current working directory.
- json - This package is used to parse the JSON response from the API.
- requests - This package is used to make HTTP requests to the API.
- dotenv - This package is used to load environment variables from an
.env
file in your working directory. - PIL - This package is used to save the image to a file.
Environment Variables
You will need to set the following environment variables in your .env
file:
AZURE_OPENAI_ENDPOINT='[value]'
AZURE_OPENAI_KEY='[value]'
AZURE_OPENAI_DEPLOYMENT='[value]'
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT='[value]'
Note
If you are using conda to manage your Python environments, some environment variables tend to be sticky. I had to add the following code to workaround it: load_dotenv(find_dotenv(), override=True)
, which seems to have fixed the issue.
Authenticate to Azure OpenAI Service
The following code is used to authenticate to Azure OpenAI Service:
|
|
Make sure to use the current API version, which is 2023-12-01-preview
at the time of writing.
Generate an Image
The following code is used to generate an image using DALL-E in Azure OpenAI with Python:
|
|
You can also hardcode the prompt value instead of using the input()
function.
Parse the Response
The following code is used to parse the response from the Azure OpenaAI API:
|
|
Get the Image
The following code is used to get the image from the response:
|
|
Save the Image
The following code is used to save the image to a file:
|
|
Display the Image
The following code is used to display the image:
|
|
Conclusion
That’s it! You can now generate images using DALL-E in Azure OpenAI with Python using the current API schema.
Happy coding with Python and the Azure OpenAI Service!
🐳