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.
Introduction
The Microsoft team has recently launched an Open AI challenge, in which a developer can learn how to build Azure AI solutions and apps.
Prerequisite
Experience working with Azure and Azure portals.
An understanding of generative AI.
Experience in one high-level programming language like C# or Python
Steps to Create Open AI Service on Azure with “Dall-E” model deployed.
Day 1 — Azure Open AI Challenge
Getting Started
Considering Azure Open AI Service is running on the Azure portal and the DALL-E model is deployed successfully.
Step 1: Create a console application
To test image generation, create a console application in Visual Studio or Visual Studio Code.
Step 2: Read the configuration
Read the configuration from appsettings.json file
IConfigurationRoot configuration = builder.Build();
string? aoaiEndpoint = configuration[“AzureOAIEndpoint”] ?? “”;
string? aoaiKey = configuration[“AzureOAIKey”] ?? “”;
Step 3: Generate Console Prompt
Get the user prompt on the console ready for image generation
Console.WriteLine(“Enter a prompt to request an image:”);
string prompt = Console.ReadLine() ?? “”;
Step 4: Call the DALL-E model using HttpClient
Finally Call the model using httpclient and retrieve the blob URL from the API response as shown below.
{
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 = “1024×1024”
};
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″, “&”));
}
Test Run 1
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.
“<URL will be here>”
Output
Test Run 2
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.
“<URL will be here>”
Output
Complete Code
Please find below the complete code in the Program.cs file
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 = “1024×1024”
};
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);
}
Complete code on GitHub
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
C# Programming🚀
Thank you for being a part of the C# community! Before you leave:
If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
More content at C# Programming