Designed for developers familiar with Azure and generative AI, the guide walks you through the process of harnessing the power of the DALL-E model for image generation.
The Microsoft team has recently launched an Open AI challenge, in which a developer can learn how to build Azure AI solutions and apps.
Steps to Create Open AI Service on Azure with “Dall-E” model deployed. Day 1 — Azure Open AI Challenge
Considering Azure Open AI Service is running on the Azure portal and the DALL-E model is deployed successfully.
To test image generation, create a console application in Visual Studio or Visual Studio Code.
dotnet new console
Read the configuration from the appsettings.json file
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
string? aoaiEndpoint = configuration["AzureOAIEndpoint"] ?? "";
string? aoaiKey = configuration["AzureOAIKey"] ?? "";
Get the user prompt on the console ready for image generation
Console.Clear();
Console.WriteLine("Enter a prompt to request an image:");
string prompt = Console.ReadLine() ?? "";
Finally, Call the model using the HTTP client, and retrieve the blob URL from the API response as shown below.
using (var client = new HttpClient())
{
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
var api = "openai/deployments/test-dall-e-3/images/generations?api-version=2024-02-15-preview";
client.BaseAddress = new Uri(aoaiEndpoint);
client.DefaultRequestHeaders.Accept.Add(contentType);
client.DefaultRequestHeaders.Add("api-key", aoaiKey);
var data = new
{
prompt = prompt,
n = 1,
size = "1024x1024"
};
var jsonData = JsonSerializer.Serialize(data);
var contentData = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(api, contentData);
// Get the revised prompt and image URL from the response
var stringResponse = await response.Content.ReadAsStringAsync();
JsonNode contentNode = JsonNode.Parse(stringResponse)!;
JsonNode dataCollectionNode = contentNode!["data"];
JsonNode dataNode = dataCollectionNode[0]!;
JsonNode revisedPrompt = dataNode!["revised_prompt"];
JsonNode url = dataNode!["url"];
Console.WriteLine(revisedPrompt.ToJsonString());
Console.WriteLine(url.ToJsonString().Replace(@"\u0026", "&"));
}
Now, add the following prompt to the console window.
aliens in Punjabi attire
The console application will generate the following response and the user has to open the Blob URL in any browser.
"A fascinating scene showing a group of extraterrestrial creatures donning traditional Punjabi attire. The aliens each have their own unique physical characteristics, yet their outfits are distinctively Punjabi. Imagine elaborate turbans, vibrant salwar kameez, and sparkling bangles. To put an interesting spin on the scene, some of the aliens are participating in traditional Punjabi activities like performing bhangra and carrying a jug of lassi. The background is a typical Punjabi village with interesting architectural features that seem to blend into an alien landscape."
"<URL will be here>"
Now, add the following prompt to the console window.
a cartoon character playing circket in the football field
The console application will generate the following response and the user has to open the Blob URL in any browser.
"A cartoon character with spiky blue hair and emerald green eyes, wearing a traditional cricket uniform consisting of a white shirt, trousers, and a red cricket cap. This character is striking a cricket ball with a willow bat in the middle of a vast, well-maintained football field. The field is vibrant green and has white line markings, with two goal posts at each end. The sky above is a cheerful cerulean with a few fluffy cumulus clouds scattered about."
"<URL will be here>"
Please find below the complete code in the Program.cs file.
using System.Net.Http.Headers;
using System.Text.Json.Nodes;
using System.Text.Json;
using System.Text;
using Microsoft.Extensions.Configuration;
try
{
// Get config settings from AppSettings
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
string? aoaiEndpoint = configuration["AzureOAIEndpoint"] ?? "";
string? aoaiKey = configuration["AzureOAIKey"] ?? "";
// Get prompt for image to be generated
Console.Clear();
Console.WriteLine("Enter a prompt to request an image:");
string prompt = Console.ReadLine() ?? "";
// Call the DALL-E model
using (var client = new HttpClient())
{
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
var api = "openai/deployments/test-dall-e-3/images/generations?api-version=2024-02-15-preview";
client.BaseAddress = new Uri(aoaiEndpoint);
client.DefaultRequestHeaders.Accept.Add(contentType);
client.DefaultRequestHeaders.Add("api-key", aoaiKey);
var data = new
{
prompt = prompt,
n = 1,
size = "1024x1024"
};
var jsonData = JsonSerializer.Serialize(data);
var contentData = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(api, contentData);
// Get the revised prompt and image URL from the response
var stringResponse = await response.Content.ReadAsStringAsync();
JsonNode contentNode = JsonNode.Parse(stringResponse)!;
JsonNode dataCollectionNode = contentNode!["data"];
JsonNode dataNode = dataCollectionNode[0]!;
JsonNode revisedPrompt = dataNode!["revised_prompt"];
JsonNode url = dataNode!["url"];
Console.WriteLine(revisedPrompt.ToJsonString());
Console.WriteLine(url.ToJsonString().Replace(@"\u0026", "&"));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Make sure to give it a star on GitHub and provide feedback on how to improve the tool further..!! AzureOpenAI/samples/Azure.OpenAI.ImageGenerationExamples at main · ssukhpinder/AzureOpenAI
Thank you for being a part of the C# community! Before you leave:
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
Also published here