Day 2 — Azure Open AI Challenge: Image Generation

Sukhpinder Singh - Mar 27 - - Dev Community

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.

    dotnet new console
Enter fullscreen mode Exit fullscreen mode

Step 2: Read the configuration

Read the configuration from appsettings.json file

    IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
    IConfigurationRoot configuration = builder.Build();
    string? aoaiEndpoint = configuration["AzureOAIEndpoint"] ?? "";
    string? aoaiKey = configuration["AzureOAIKey"] ?? "";
Enter fullscreen mode Exit fullscreen mode

Step 3: Generate Console Prompt

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() ?? "";
Enter fullscreen mode Exit fullscreen mode

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.

    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", "&"));

    }
Enter fullscreen mode Exit fullscreen mode

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>"
Enter fullscreen mode Exit fullscreen mode

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.

    "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>"
Enter fullscreen mode Exit fullscreen mode

Output

Complete Code

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);
    }
Enter fullscreen mode Exit fullscreen mode

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .