DALL-E 3 is a Generative AI model from OpenAI, which can produce images based on your textual input (prompt). You will find a Jupyter notebook in this repo, that utilises Azure OpenAI deployment of DALL-E 3 to generate caricature image of a famous Disney character.
To build this demo, I used the latest version of OpenAI Python SDK - v1.x. To upgrade your openai Python package, please use the following pip command:
pip install --upgrade openai
- To use API key authentication, assign the API endpoint name, version and key, along with the Azure OpenAI deployment name of DALL-E 3 model to OPENAI_API_BASE, OPENAI_API_VERSION, OPENAI_API_KEY and OPENAI_API_DEPLOY_DALLE environment variables respectively.
Note: If you want to use the Entra ID (former Azure Active Directory) authentication instead, you may find some implementation options here.
- Install the required Python packages, by using the pip command and the provided requirements.txt file.
pip install -r requirements.txt
- Connection with the backend Azure OpenAI service is established through the openai Python SDK v1. Current implementation passes the Azure OpenAI endpoint's API key as a parameter value of the AzureOpenAI class. If necessary, you can switch to the Entra ID authentication instead.
client = AzureOpenAI(
api_version = AOAI_API_VERSION,
api_key = AOAI_API_KEY,
azure_endpoint = AOAI_API_BASE,
)
- We define then a helper function, that would call our DALL-E 3 model in Azure OpenAI and return URL of generated image.
def image_generator(prompt):
# Generate image with DALL-E 3
response = client.images.generate(
model = AOAI_DEPLOYMENT,
prompt = prompt,
)
# Extract image URL
json_response = json.loads(response.model_dump_json())
image_url = json_response["data"][0]["url"]
return image_url
- We can then execute our helper function with an image prompt, e.g. "Caricature picture of Mickey Mouse with a hat"
- If successful, we should be able to download generated image with Pillow and visualise it in default graphic editor.
image = Image.open(requests.get(image_url, stream=True).raw)
image.show()