DALL-E Art-Generator with C# .NET
In this blog post, we will explore the steps to develop a C# application using DALL-E model to generate images from text.
We will explore how to use the DALL-E Image Generator in conjunction with C# .NET.
With just a few steps, we'll develop a small POC (Proof of Concept) that uses the DALL-E model to generate images from text phrases using C#.
What is DALL-E Art-Generator
DALL-E can generate images of almost anything, from a yellow submarine to a pig with wings. It has been trained on a massive dataset of images and textual descriptions, allowing it to learn how to generate images from natural language input.
DALLΒ·E 2 is an AI system that can create realistic images and art from a description in natural language.
https://openai.com/research/dall-e
Getting Started with C# and DALL-E
In this project, we will use Visual Studio and C# and .NET 6 to create the Console Application.
Step 1: Create the Console Application and Install the Dependencies
Let's create our Console Application with C# and .NET 6 and install the required dependencies.
Project Name: ConsoleAppOpenAI.DALL_E
Step 2: Create the IOpenAIProxy Interface
Within this interface, we'll expose only the methods to Generate and Download the images from Open AI.
Step 3: Generate Image Models
Let's define our models using records. Records simplify the reading since they are only POCO classes.
Step 4: Create an Open AI Account
We need to create an account on the Open AI platform to use the Open AI API.
The registration process is straightforward and can be completed in a few minutes.
- We need to visit the Open AI website at https://platform.openai.com/overview.
- Then click the "Sign Up" button in the top right corner.
- Click on the button to start the registration process.
Step 5: Set up the Configuration File
To access the DALL-E model, we'll need to set up our application's Subscription Id and API key.
Collect them from these menus:
Then update the appsettings.json with the values:
Step 6: Open AI HTTP Service Implementation
Create a class named OpenAIHttpService with a single constructor receiving Β the IConfiguration and read the configuration we just set in place.
Next should be the implementation of the GenerateImages()
method:
Then the last step is the DownloadImage()
method implementation:
Step 7: Consuming the APIs
Back to the file Program.cs, let's wire everything together and start calling the APIs to generate images.
using ConsoleAppOpenAI.DALL_E.HttpServices;
using ConsoleAppOpenAI.DALL_E.Services;
using Microsoft.Extensions.Configuration;
using System.Reflection;
Console.WriteLine("Starting commandline for DALL-E [Open AI]");
var config = BuildConfig();
IOpenAIProxy aiClient = new OpenAIHttpService(config);
Console.WriteLine("Type your first Prompt");
var msg = Console.ReadLine();
var nImages = int.Parse(config["OpenAi:DALL-E:N"]);
var imageSize = config["OpenAi:DALL-E:Size"];
var prompt = new GenerateImageRequest(msg, nImages, imageSize);
var result = await aiClient.GenerateImages(prompt);
foreach (var item in result.Data)
{
Console.WriteLine(item.Url);
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), $"{Guid.NewGuid()}.png");
var img = await aiClient.DownloadImage(item.Url);
await File.WriteAllBytesAsync(fullPath, img);
Console.WriteLine("New image saved at {0}", fullPath);
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
static IConfiguration BuildConfig()
{
var dir = Directory.GetCurrentDirectory();
var configBuilder = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(dir, "appsettings.json"), optional: false)
.AddUserSecrets(Assembly.GetExecutingAssembly());
return configBuilder.Build();
}
With all this, we have a complete Application with the DALL-E Art-Generator.
Generate our very first Image
Run the Application and it try yourself.
Here is my first run:
Prompt: Wide and green garden with a lot of flowers, with sunflowers, and a small dog running around
Take a look at this beautiful image:
Conclusion
Integrating C# with DALL-E is a straightforward process allowing us to programmatically generate images.
Using Open AI's API, we can easily send textual descriptions and receive high-quality images in response.
This integration opens up many possibilities, such as generating images for data visualization, creating custom artwork, or automating image creation tasks. As DALL-E continues to improve, we can expect even more exciting applications.
https://github.com/ricardodemauro/OpenAILabs.Console