Day 2 — Azure Open AI Challenge: Image Generation
Tags: ai, csharp, chatgpt, beginners
Published: 2024-03-27 17:32:37

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](https://singhsukhpinder.medium.com/day-1-azure-open-ai-challenge-306e317662c2)

## 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.
```
    dotnet new console
```
### Step 2: Read the configuration

Read the configuration from appsettings.json file
```csharp
    IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
    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
```csharp
    Console.Clear();
    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.
```csharp
    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", "&"));
    
    }
```
### 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.
```
    "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>"
```
### Output

![](https://cdn-images-1.medium.com/max/2048/1*ThpCTe5zFe5aVsr4u4kREA.png)

### 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.
```
    "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>"
```
### Output

![](https://cdn-images-1.medium.com/max/2048/1*C5gTWanzgebZlYHM5j71fQ.png)

### Complete Code

Please find below the complete code in the Program.cs file
```csharp
    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);
    }
```
### 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](https://github.com/ssukhpinder/AzureOpenAI/tree/main/samples/Azure.OpenAI.ImageGenerationExamples)

### 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](https://twitter.com/sukhsukhpinder) | [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh/) | [Dev.to](https://dev.to/ssukhpinder) | [Hashnode](https://dotnet.hashnode.dev/) | [Newsletter](https://www.linkedin.com/newsletters/net-programming-7031098498754195456/) | [Tumblr](https://www.tumblr.com/settings/blog/codewithsukh)

Visit our other platforms: [GitHub](https://github.com/ssukhpinder) | [Instagram](https://www.instagram.com/codewithsukh/) | [Tiktok](https://www.tiktok.com/@codewithsukh) | [Quora](https://www.quora.com/profile/Sukhpinder-Singh-4) | [Daily.dev](https://app.daily.dev/devcard)

More content at [C# Programming](https://medium.com/c-sharp-progarmming)