Microsoft Azure Cognitive Services Fluent API v1.0

Introduction

Azure Cognitive services are a powerful, consumer friendly set of API's that allow developers to easily make use of powerful artificial intelligence and machine learning services within their applications. Services such as text recognition, sentiment analysis, facial recognition and emotion detection to name a few. You can go check them out here.

However, even though they are relatively easy to consume, they all utilise a common usage pattern which can get a little tedious or verbose. This common mechanic is:

  • Initialise your HTTP communication stack
  • Construct your Uri, headers, arguments and payload, then send to the relevant API.
  • Parse the response, extracting out the relevant values to make sense of it all.
  • For multiple calls, this process is repeated multiple times.

While this is not necessarily difficult, it can be tedious.

What if....

What if you wanted to do some sentiment plus keyphrase analysis and could simply do this:

var result = await TextAnalyticConfigurationSettings
.CreateUsingConfigurationKeys(TestConfig.TextAnalyticsApiKey, LocationKeyIdentifier.WestUs)
.SetDiagnosticLoggingLevel(LoggingLevel.Everything)
.AddDebugDiagnosticLogging()
.UsingHttpCommunication()
.WithTextAnalyticAnalysisActions()
.AddSentimentAnalysis("I am having a terrible time.")
.AddKeyPhraseAnalysis("This is a basic sentence. I have absolutely nothing to assert here.")
.AnalyseAllAsync();

 and get back a nice strongly typed response?

Well, you can.

Introducing v1.0 of the Fluent API for Azure Cognitive Services.

With this fluent API, you can use simple syntax for multiple operations against the cognitive services with very concise code. The fluent library currently supports TextAnalytics, Face and ComputerVision sets of API's. Full source and documentation can be found here: https://github.com/glav/CognitiveServicesFluentApi

The nuget packages are available here:

This library takes away the tedious aspects of communicating with cognitive services and makes them even easier, but it also builds on this by adding a number of extensions or value add features. These are (but not limited to):

  • Convenience methods that perform context specific functions on the result set, like determining the number of positive responses in a sentiment analysis result set.
  • Provides a default but customisable scoring system with descriptive support to better parse the confidence levels or scores that are returned in a more semantic, and fluid way.
  • Allow an easy way to provide your own customisations or extensions.
  • Supports testability by allowing easy substitution of components in the pipeline, such as the communications stack.
  • Support for retry and back off protocols automatically without you having to do anything. This makes using the free tier easy, within its limited usage restrictions. If Azure indicates that a request should wait for 2 seconds before trying again, thats exactly what happens.
  • Provides an easy mechanism to work with the "polling" API's where a jo is submitted and you must query and endpoint for completion. This can be done in one line of code.

 

Examples

There is more detailed examples and documentation here (https://github.com/glav/CognitiveServicesFluentApi) but as a  simple example, to perform sentiment analysis on a block of text, you can simply do the following:

var result = await TextAnalyticConfigurationSettings
.CreateUsingConfigurationKeys("{your-api-key}", LocationKeyIdentifier.WestUs)

    .SetDiagnosticLoggingLevel(LoggingLevel.Everything)
    .UsingHttpCommunication()
    .WithTextAnalyticAnalysisActions()
    .AddSentimentAnalysis("I am having a fantastic time.")
    .AnalyseAllAsync();

 

Similarly for Keyphrase analysis:

var result = await TextAnalyticConfigurationSettings
.CreateUsingConfigurationKeys(TestConfig.TextAnalyticsApiKey, LocationKeyIdentifier.WestUs)
    .SetDiagnosticLoggingLevel(LoggingLevel.Everything)
    .UsingHttpCommunication()
    .WithTextAnalyticAnalysisActions()
    .AddKeyPhraseAnalysis("This is a basic sentence. I have absolutely nothing to assert here.")
    .AnalyseAllAsync();

 And what about other cognitive services? Here is an example of ComputerVision support that will look for adult content, include tags, and look for celebrities:

var result = await ComputerVisionConfigurationSettings
   .CreateUsingConfigurationKeys(TestConfig.ComputerVisionApiKey, LocationKeyIdentifier.SouthEastAsia)
   .SetDiagnosticLoggingLevel(LoggingLevel.Everything)
   .AddDebugDiagnosticLogging()
   .UsingHttpCommunication()
   .WithComputerVisionAnalysisActions()
   .AddUrlForImageAnalysis("http://www.scface.org/examples/001_frontal.jpg",
               ImageAnalysisVisualFeatures.Adult | ImageAnalysisVisualFeatures.Tags, ImageAnalysisDetails.Celebrities, SupportedLanguageType.English)
   .AnalyseAllAsync();

However, Instead of doing each of those operations separately, we can group common API operations together which will perform both keyphrase and sentiment analysis in this case:

var result = await TextAnalyticConfigurationSettings
.CreateUsingConfigurationKeys(TestConfig.TextAnalyticsApiKey, LocationKeyIdentifier.WestUs)
    .SetDiagnosticLoggingLevel(LoggingLevel.Everything)
    .UsingHttpCommunication()
    .WithTextAnalyticAnalysisActions()
    .AddKeyPhraseAnalysis("This is a basic sentence. I have absolutely nothing to assert here.")
    .AddSentimentAnalysis("I am having a fantastic time.")
    .AnalyseAllAsync();

Doing this you can perform as many operations as you like. Generally this means multiple API calls to the cognitive service, however some support batch operations (such as TextAnalytics) and this is automatically handled by the fluent API.

Results of the operations are stored in an object which contains a reference to the configuration used, the inputs passed in and finally the respective context objects to represent each set of operations. So in the keyphrase example above, the results can be obtained using something like:

var keyphrase = result.KeyPhraseAnalysis.AnalysisResult.ResponseData.documents[0].keyPhrases[0]; // "basic sentence"

And for the sentiment analysis

var score = result.SentimentAnalysis.AnalysisResult.ResponseData.documents[0].score; // a double like 0.9

Convenience Extensions

There is a separate package for each cognitive service fluent API package that just contains a series of extension methods to act as convenience methods or helpers. This allows further extensions to be provided easily. An example using the Face detection API is:

var firstResult = result.FaceDetectionAnalysis.GetResults().First();

var isNoiseLevelLow = firstResult.IsNoiseLevel(NoiseLevel.Low);
var isGoodExposure = firstResult.IsExposureLevel(ExposureLevel.GoodExposure);
var isBlurLevelLow = firstResult.IsBlurLevel(BlurLevel.Low);

There are many more but this intended as a start.

Scoring system

Common to almost all cognitive service operations is a confidence score for the returned result. This value is always in between 0 and 1 inclusive. 0 represents a negative, or low confidence score whereas 1 represents a positive, or high confidence score. The fluent API contains support for parsing these results much easier through a scoring system that is fully customisable. A default set of "scores" is provided.

The default score levels are the following:

  • 0 - 0.35 : "Negative"
  • 0.35 - 0.45 : "Slightly Negative"
  • 0.45 - 0.55 : "Neutral"
  • 0.55 - 0.75 : "Slightly Positive"
  • 0.75 - 1 : "Positive"

This allows you to do things like:

var items = result.SentimentAnalysis.GetResults();
var firstItem = items.First();
var score = result.SentimentAnalysis.Score(firstItem);
Console.WriteLine($"Score level is: {score.Name}");

You can also utilise convenience methods to operations such as the following to get all negative results from a SentimentAnalysis operation:

var negativeResults = result.SentimentAnalysis.GetResults(DefaultScoreLevels.Negative);

Wrapping up

This has really only touched the surface of what you can do. The Face API provides extensive support for not only face recognition, but also working with people groups, face detection and group processing. This library has been in the works for a little while as simply a plaything that has grown over time. Rather than just leave it lie, I thought I'd polish it up, release it, and see if it helps anyone or garners any interest. If not, nothing lost as its been a great learning experience. I certainly welcome any feedback. For full documentation, see the Github link below.

Github: https://github.com/glav/CognitiveServicesFluentApi

Nuget packages:

Sonarcloud analysis: https://sonarcloud.io/dashboard?id=CognitiveFluentApi 

 

14 Comments

  • Galvanized tank construction may be done in the workshop or at the place of use of the tank. Small galvanized tanks are normally made in the workshop and transported to the place of operation, but larger tanks are implemented at the installation site. However, sometimes due to the impassability or location of the water tank on the floors of the building where access to large objects is not possible, galvanized sheets are transported to the site and made in the same place.

  • hello

  • There are few parts of your plumbing system more vital than the composite septic tank. Domestic sewage is treated and stored in a septic tank.

  • You need to be a piece of a test for manifestly the satisfactory web site page at the web. I will advise this website!

  • Hello. Do you need to write a psychopharmacology essay, but you don't know what to write about or how to write it? Then you will find it useful and interesting to visit https://www.nursingpaper.com/examples/psychopharmacology/ , which has examples of various essays about this field. By analysing each essay, you will be able to understand all the key aspects of how to write a perfect essay and get an excellent grade.

  • Absolutely fantastic! Your insight on this topic is truly enlightening. It's evident that you've put a lot of thought into your perspective, and your positive approach is both refreshing and inspiring. Keep sharing your valuable thoughts – they make a meaningful contribution to the conversation.

  • amazing

  • The way you delve into the intricacies of [specific aspect] is truly enlightening. I appreciate your meticulous research and the clarity with which you present complex ideas. Your writing not only informs but also captivates the reader's attention. The real-world examples you provide make the content relatable and applicable. I find myself nodding along as I read, appreciating the depth of your insights.

  • Thanks for sharing such informative post! Exciting to see the release of Microsoft Azure Cognitive Services Fluent API v1.0! This is a game-changer for developers, making it so much easier to harness the power of Azure's cognitive services in our applications. The simplified, human-readable code makes integration a breeze, allowing us to focus on building innovative features rather than getting caught up in the details. Kudos to the team for streamlining the development process and making AI more accessible for all.





  • Negotiate Prices, When you spot an escort with an attractive profile, don't be shy about discussing price. Say you're interested and ask if they have deals or discounts. Agencies often cut prices for returning customers or longer bookings.

  • Most men seek the company of a young independent girl in laxmi nagar at the peak of their youth. They can make themselves feel just as young with their charming. You can feel bubbly personality and wild and naughty moves in bed.

  • I appreciate you sharing this insightful post! The Microsoft Azure Cognitive Services Fluent API v1.0 release is exciting to witness! For developers, this is revolutionary since it makes utilizing Azure's cognitive capabilities in our apps much simpler.

  • The blog's Content is very informative. The blog posts were interesting. This is an excellent site. This site also contains links to other websites for additional information.

  • Start with a good idea. Your design is unique. I'll label it a unique concept. There are many styles to choose from! This is a great piece. You have a great writing style and tone.

Add a Comment

As it will appear on the website

Not displayed

Your website