Click here to Skip to main content
15,887,135 members
Articles / Artificial Intelligence
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
11 Oct 2023CPOL2 min read 8.5K   8   1
Explore the streamlined process of creating diverse image variations in .NET applications using OpenAI’s enhanced API functionality.
Dive into the newly introduced functionality of an OpenAI's API client package, allowing developers to efficiently generate image variations within .NET applications. This article provides a concise walkthrough on leveraging this feature for image processing tasks, from setting up your environment to making API calls and handling responses. Whether you’re working on data augmentation, testing, or content generation, discover how this updated feature can significantly streamline your development process, with practical code snippets and expected outputs included for a hands-on understanding.

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:

C#
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:

C#
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:

C#
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:

C#
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:

CMD
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
I am a self-employed software engineer working on .NET Core. I love TDD.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA11-Oct-23 19:45
professionalȘtefan-Mihai MOGA11-Oct-23 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.