How to use Azure Cloud Cognitive Services?

What is Azure Cognitive Services?

According to Microsoft, the Cognitive Services in the Microsoft Azure Cloud bring AI within reach of every developer, without requiring machine learning expertise.

Using the Azure Cognitive Services API you can use API’s to embed the ability to hear, speak, translate and understand directly into your applications.

How are Azure Services configured?

Inside the Azure Cloud Services, different functionality is grouped together and made available to developers as online “Resources” that link to a subscription. Cognitive Services is a grouping of a number of AI-based services, with each service offered as a Resource (that you create to make active against your subscription).

The Azure Resource and Subscription model allow each resource to be billed individually and provided from any one of the specific data centers around the world. This makes it possible to develop functionality that fulfills local legal restrictions around the movement of data. It also makes it possible to provide customer-specific configurations when deploying inside an app.

For many of the Azure services, a free tier is available which is ideal for small deployments and testing.

Code Example for using Azure Cognitive Services

I originally looked at Using Azure Translator Services with Delphi in 2015. Since then, the original XML-based API has been deprecated in favour of a JSON-centric API, as part of the re-branding and re-organisation of the Azure cloud with the launch of Cognitive Services. The following code has been re-built from the ground up to take advantage of the latest language features in Delphi.

UPDATED CODE: Download from Github https://github.com/DelphiABall/Azure-Cognitive-Services

The classes in the GitHub repo make it simple to use the following Microsoft Azure Cloud services:

Authentication

Each service uses the same authentication class (TAzureToken) to convert the access key for the resource into a session token. Note: Translator and Speech Services are different resources and as such use different instances of the authentication class.

unit Azure.API3.Constants provide a list of Regions based on the current Azure Regions, however, if these change, each class has overridden constructors that allow you to pass in the region as a string.

When you create a TAzureToken, use the constructor with the region for the resource, and then set the SubscriptionKey. This will automatically set the correct endpoints for the security to work. At the point the token is requested from the object, the API will be called, and the result cached. When the token times out, a new token will automatically be collected for you.

The following service and features are available to use: Each feature is in the corresponding unit following the naming conversion of Azure.API3.<Service>.<Feature>

Translator

  • Text Translation (converting text from one to multiple languages)
  • Dictionary Look up (looking up a words between languages and reporting on its usage)

Speech

  • Text To Speech (converting text using one, or multiple voices into audio file)
  • Speech to Text (converting .wav or .ogg files to text)

To see how to use these services, watch the videos below.

Azure Cloud – Translator Services

Azure Cloud – Text to Speech

Azure Cloud – Speech to Text

Getting Started with Delphi

The code sample is written to work in Delphi 10.4 and above. This provides the advantage of being able to work on Windows, macOS, iOS, Android and Linux.

You can download the latest Trial of Delphi from Embarcadero www.embarcadero.com/products/delphi/start-for-free.

For students or open-source projects, check out the latest Delphi community edition