The world around us has been embracing AI to build intelligent solutions like never before. Many applications coming to the market these days include intelligent features out of the box. Some common examples of such features include auto-completion of texts in chat-based apps and the use of facial recognition to unlock our smartphones. Often, these features aren’t easy to build and involve complex algorithms. Most of the time, these complex algorithms require vast amounts of data, extensive research, and a huge amount computational resources to train, process, and build AI models. Many companies may not even have the required resources to build their own AI models for a particular use case. To overcome this, developers tend to use services or pretrained AI models offered by other companies.
In this chapter, we explore ways to integrate AI into our applications by leveraging the power of Azure Cognitive Services, a set services that allows us to use state-of-the-art algorithms and AI models developed by Microsoft to perform operations ranging from speech recognition to image classification. By the end of this chapter, we will build a web API that is capable of translating text from one language to another.
Structure
Introduction to Azure Cognitive Services
Provision a Translator Service in the Azure portal
Build a multilanguage text translator app
Objectives
Understand the fundamentals of Azure Cognitive Services
Integrate the prowess of translator service in your application
Azure Cognitive Services
Subscription key
Token
Azure Active Directory (Azure AD)
A word of caution here that not all services that are part of the suite of Azure Cognitive Services support all the of the preceding methods of authentications. For example, the Speech services and text translation service currently support token-based authentication, but that is not the case with other services that are part of the suite. The number of services that support token-based authentication is expanding, though, and may change by the time this book gets published.
Apart from providing different mechanisms to authenticate your support, Azure Cognitive Services also has support for virtual networks. This enables you to control access to your resources from allowed IP addresses.
Vision: Vision services comprise the Computer Vision, Custom Vision, and Face services. As the name suggests, the set of services falling under this category assists our application to work with image-based data and make intelligent decisions. Use cases where the Vision services can come in handy are facial recognition and character recognition.
Speech: Speech services comprise the text-to-speech, speech-to-text, speaker recognition, and speech translation services. This set of services assists us in integrating speech capabilities in our applications. We can use these services to build solutions that can help us texts in our meetings or calls. Voice assistants are a popular use case for which these services are of great help.
Language: Language services is a collection of various NLP features along with services like Translator, LUIS, and QnA Maker. These services assist us by integrating the ability to process and analyze unstructured texts in our applications. We can leverage these services to perform complex operations like sentiment analysis, opinion mining, and language detection, to name a few. Sentiment classification is a popular use of the Language services.
Decision: Decision services is a collection of services that include Anomaly Detector, Content Moderator, and Personalizer. These services assist us by integrating the ability to provide recommendation to take informed decision in our application. We can leverage these services to detect potential abnormalities in a time series data or to provide monitoring for censored or abusive content.
With that review of Azure Cognitive Services and its use cases in mind, we will now explore the translator service to solve the problem of our fictional company.
Problem Statement
You are working for a fictional company named Aztec Corp. that wants to add a text translation feature to one of its flagship products. You are part of the team that will be working on this feature. Your product owner and management have decided to go ahead with Azure Translation service to perform text translation. You are assigned with the task to perform a proof of concept (PoC) to translate any given text from language A to language B. Once done, your team members can incrementally work on your PoC to build the feature by using your PoC as a base for further development.
Proposed Solution
Develop an interface that takes text as input, language to be translated in and returns the translated text to the user.
Integrate the Azure text analytics service to our interface to perform the text translation.
You have decided the interface is going to be an ASP.NET Core Web API that will take the source language, text to be translated, and target language in the request body and output the translated text.
- 1.
Create an Azure Translator Resource
- 2.
Get the key and endpoint of the Translator Resource
Once we have these two things in place, we will start building our web API using Visual Studio 2022. Before we start provisioning our Translator resource, let’s look at what Azure Translator is and what capabilities it has.
What Is Azure Translator?
Azure Translator is a cloud-based AI service that enables developers to integrate text translation capabilities into their applications. It is part of the Azure Cognitive Services suite of Microsoft. Azure Translator supports text translation from over 100+ languages. It leverages the neural machine translator technology to process the texts and generate the desired outcome Apart from text translation, Azure Translator enables us to perform operations such as language detection and transliteration of text, to name a few. It also comes with an offering called Custom Translator that enables you to build your own customized neural machine translation system. We will be leveraging Azure Translator to complete our PoC in the upcoming sections.
Create an Azure Translator Instance in Azure Portal
Having provisioned the translator resource in Azure, now we need to access it to integrate it with our application. To access or interact with Azure Translator service from our application, we would need to authenticate our application’s request to the translator resource. This can be done in different ways, such as using Azure Active Directory–based authentication or key-based authentication. For the purpose of this demonstration, we will be using key-based authentication. For enterprise applications, it is recommended to use Azure AD–based authentication or managed identity if the applications are deployed inside Azure in an Azure Function or App Service by providing necessary RBAC permissions to the Azure Translator instance.
Now that we have provisioned the resource and have the required information to authenticate our request to the translator service, we can start building the application to translate languages from one language to the other in the next section.
Create a Multilanguage Text Translator Using ASP.NET Core
In this section, we’re going to complete the proof of concept for our fictional company to build a feature for its flagship product, as briefly discussed in the “Proposed Solution” section.
Visual Studio will create a sample ASP.NET Core Web API project that contains a simple WeatherForecast API, which returns some randomly generated weather forecast information. Remove the WeatherForecastController.cs and WeatherForecast.cs files from our project, as we don’t need them.
Let’s create two folders in our project: Business and Models. The Business folder will contain our interface and the classes implementing them. The Models folder will contain the classes of our data models.
In the preceding code snippet, we are leveraging an IConfiguration instance to access the values that we had stored with the key name as key, endpoint, and location in our appsetting.json file.
URL route: http://localhost:5185/api/Translate
HTTP verb: GET
And with this our API project to schedule notifications for our fictional dental clinic is complete. Press Ctrl+F5 to build and run our project. You can find the complete source code of this API project at the following GitHub repository: https://github.com/AshirwadSatapathi/MyTextTranslator.
Test Our API Using Postman
In the last few sections, we have developed a web API to translate text from one language to another, but if we wanted to translate text from one language to multiple languages, we could absolutely do that by adding the language code for the same in the query string of our HttpRequestMessage uri property. The source and target language of the text can be any language that is supported by Azure Translator service. If we wanted to automatically detect the source language without having to specify it in our request payload, then we could leverage the language detection feature and detect the source language of the given text and then translate it to the target language.
Summary
Azure Cognitive Services provides a suite of AI services as SaaS offering that enable developers to add cognitive capabilities to their applications. We can perform operations ranging from image classification to text-to-speech detection. These services help developers add cognitive capabilities in their applications without requiring knowledge of the underlying algorithms or any investment of time and resources to train and test such algorithms. As part of this chapter, we explored a particular service of Azure Cognitive Services, Azure Translator, to solve a common problem that people and developers often face while interacting with people from different regions who speak different languages. We built an API that translates text from one source language to a target language. Apart from this, the translator service allows us to perform document translation as well as to create our own custom translator by building a neural machine translation system by leveraging the Azure Translator service.