visit
Steps to Create Open AI Service on Azure with “Dall-E” model deployed. Day 1 — Azure Open AI Challenge
dotnet new console
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
string? aoaiEndpoint = configuration["AzureOAIEndpoint"] ?? "";
string? aoaiKey = configuration["AzureOAIKey"] ?? "";
Console.Clear();
Console.WriteLine("Enter a prompt to request an image:");
string prompt = Console.ReadLine() ?? "";
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", "&"));
}
aliens in Punjabi attire
"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>"
a cartoon character playing circket in the football field
"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>"
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);
}
Also published