65.9K
CodeProject is changing. Read more.
Home

Effortlessly Generate Image Variations in .NET Applications with OpenAI’s Enhanced API

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Oct 11, 2023

CPOL

2 min read

viewsIcon

9160

Explore the streamlined process of creating diverse image variations in .NET applications using OpenAI’s enhanced API functionality.

Introduction

The ConnectingApps.Refit.OpenAI NuGet package now includes a valuable new feature that allows developers to generate variations of images using OpenAI’s API efficiently. This article will briefly introduce and demonstrate this new functionality. If you are new to this NuGet package, consider reading the introductory article on CodeProject for more background.

Background

Generating variations of an image is crucial for different applications, including data augmentation for machine learning, testing, or content generation. With the introduction of image variation functionality in the ConnectingApps.Refit.OpenAI package, developers can effortlessly implement these tasks in their .NET applications.

Using the Code

The following steps guide you on how to use the newly introduced image variation feature:

Firstly, you need to set up your OpenAI API key as an environment variable and create an authorization header:

var apiKey = Environment.GetEnvironmentVariable("OPENAI_KEY");
var authorizationHeader = $"Bearer {apiKey}";

Next, open and read the image you want to create variations for using a FileStream object:

await using (var image = new FileStream("otter.png", FileMode.Open, FileAccess.Read))

Then, create an instance of the IVariation interface and initiate the GetImageVariations method to get the image variations:

var openAiApi = RestService.For<ivariation>
                ("https://api.openai.com", OpenAiRefitSettings.RefitSettings);
var streamPart = new StreamPart(image, "otter.png");
var response = await openAiApi.GetImageVariations
               (authorizationHeader, streamPart, 2, "1024x1024");

The method GetImageVariations will return a response containing the status code and the image variations. You can then access and use the variations as needed:

Console.WriteLine($"Returned response status code {response.StatusCode}");
Console.WriteLine($"Number of new items created {response.Content!.Data.Count}");
Console.WriteLine($"First item url {response.Content!.Data.First().Url}");
Console.WriteLine($"Second item url {response.Content!.Data.Last().Url}");

The output of this code will be something like:

Returned response status code OK
Number of new items created 2
First item url [IMAGE URL 1]
Second item url [IMAGE URL 2]

For a deeper dive into the code, feel free to check out the package’s GitHub repository.

Points of Interest

With the introduction of image variation functionality, the ConnectingApps.Refit.OpenAI package becomes an even more powerful tool for developers working with OpenAI’s API. It simplifies the interaction while ensuring that developers have access to the wide range of features offered by OpenAI’s API.

History

  • 11th October, 2023: Initial post

This update covers the introduction of the image variations feature in the ConnectingApps.Refit.OpenAI NuGet package. Future updates may bring more features and improvements, enhancing the package’s utility and efficiency for .NET developers further.