© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
A. Satapathi, A. MishraDeveloping Cloud-Native Solutions with Microsoft Azure and .NET https://doi.org/10.1007/978-1-4842-9004-0_10

10. Build a Multilanguage Text Translator Using Azure Cognitive Services

Ashirwad Satapathi1   and Abhishek Mishra2
(1)
Gajapati, Odisha, India
(2)
Navi MUmbai, India
 

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

In this chapter, we will explore the following topics related to Azure Cognitive Services:
  • Introduction to Azure Cognitive Services

  • Provision a Translator Service in the Azure portal

  • Build a multilanguage text translator app

Objectives

After studying this chapter, you should be able to
  • Understand the fundamentals of Azure Cognitive Services

  • Integrate the prowess of translator service in your application

Azure Cognitive Services

Microsoft provides a suite of cloud-based AI services to add intelligence to applications by leveraging state-of-the-art AI models built and designed over time through extensive research. Microsoft calls this suite of cloud-based AI service Azure Cognitive Services. These services help developers add cognitive capabilities to their applications with prior knowledge of machine learning or AI. These services provide a set of REST APIs and client libraries that we can leverage to integrate the prowess of these cognitive services into our applications to make them smart. We can securely connect with these services using different methods of authentication. The following are the ways we can authenticate our requests to 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.

Cognitive Services in Azure are broadly classified into four categories:
  • 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

After going through the initial set of requirements, you have distilled the problem statement into two phases:
  • 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.

Before we start building the web API, we need a couple of things in place. The following are the prerequisites to start development activities:
  1. 1.

    Create an Azure Translator Resource

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

To create an Azure Translator instance, go to the Azure portal and type Translators in the search box. Click the Translators in the search results as shown in Figure 10-1.
Figure 10-1

Search for Translator

Click Create to provision a new Azure Translator instance, as shown in Figure 10-2.
Figure 10-2

Click Create

Next, select the subscription and resource group. If you don’t have a subscription, you can create a new one in this screen. After that, select the region and pricing tier and enter the name of tour resource, as shown in Figure 10-3. The name of the resource needs to be globally unique. For the purpose of this chapter, we will be going ahead with the free tier, Free F0. This tier provides us with the capability to translate 2 million characters every month. If you wanted to use this resource for production workloads, you would need to have a higher tier that satisfies your requirement. Once you have entered all the details, click Review + create.
Figure 10-3

Click Review + create

Next you will see a summary of the configuration for the translator resource from the previous screen, as shown in Figure 10-4. A validation check will be performed on the configuration. Once the validation of the configuration is done, click Create to provision our resource. (If you wanted to make some changes, you could click Previous and make the necessary configuration changes for the resource.)
Figure 10-4

Click Create

Once the resource has been successfully provisioned, you will see the message Your deployment is complete, as shown in Figure 10-5. Once you see that message, click Go to resource to go to the newly provisioned translator resource.
Figure 10-5

Click Go to resource

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.

Click the Keys and Endpoint section, as shown in Figure 10-6, to fetch the key, endpoint, and region details for our translator resource.
Figure 10-6

Click Keys and Endpoint

We will have to get the either one of the primary or secondary key, the resource endpoint, and region from the current screen (see Figure 10-7) to use later in our application for authentication purposes.
Figure 10-7

Get the key, endpoint, and region

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.

As we have already covered the business requirement and provisioned the required resources, let’s start building our web API using the ASP.NET Core Web API template. Open Visual Studio 2022 and click Create a new project as shown in Figure 10-8.
Figure 10-8

Click Create a new project

Select the ASP.NET Core Web API project template as shown in Figure 10-9 and click Next.
Figure 10-9

Click Next

Enter the project name, location, and solution name in the corresponding fields, as shown in Figure 10-10, and click Next.
Figure 10-10

Enter the project name, location, and solution name

Now check the Use controllers check box and keep the other options as shown in Figure 10-11. Click Create to create our ASP.NET Core Web API project.
Figure 10-11

Click Create

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.

Open the Package Manager Console and run the following command to install the NuGet package Newtonsoft.Json, which will be used for serialization and deserialization of objects:
Install-Package Newtonsoft.Json
Once we have installed the NuGet packages, we need to store the translator service’s key, region, and endpoint in our appsettings.json file. Go to the appsettings.json file of our project and add the following key/value pairs:
"key": "<enter your key>",
"endpoint": "<enter your endpoint>",
"location": "<enter the region>"

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.

After creating both folders, create a class called TranslatePayload.cs in the Models folder and paste the following code. This class represents the data sent by the client app in the request payload, which would contain the text to be translated along with the source and destination language. We have used data annotations to mark the necessary fields as required.
public class TranslatePayload
{
    [Required]
    public string SourceLanguageCode { get; set; }
    [Required]
    public string TargetLanguageCode { get; set; }
    [Required]
    public string TextToBeTranslated { get; set; }
}
Now that our model is ready, let’s create an interface called ITranslateBusiness.cs in the Business folder and add the following method definition. As shown, our interface contains one method definition, TranslateText, which will be responsible for taking the TranslatePayload data coming from the request payload and returning the translated text in the target language.
public interface ITranslateBusiness
{
    Task<string> TranslateText(TranslatePayload translatePayload);
}
Now that we know what each of the methods is designed to perform, let’s create a class called TranslateBusiness.cs to implement the ITranslateBusiness interface. Before implementing the interface, add the following properties:
private string key;
private string endpoint;
private string location;
Now instantiate the properties by using constructor injection. Replace your constructor with the following code:
public TranslateBusiness(IConfiguration configuration)
{
    key = configuration.GetValue<String>("key");
    endpoint = configuration.GetValue<String>("endpoint");
    location = configuration.GetValue<String>("location");
}

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.

Now we need to implement the TranslateText method of the ITranslateBusiness interface in the class. We will be making calls to the REST endpoints of Translator APIs because there is no available client library for the translator service at the time of writing. The TranslateText method will create an HttpClient instance to interact with the APIs of the Translator Service. We are passing the source and target language in the query string and passing the text to be translated in the request body of our object of type HttpRequestMessage. We specify the key and region in headers of our HttpRequestMessage object and call the SendAsync method of our HttpClient object by passing the HttpRequestMessage object as parameters to make a call to the API of the translator service. Once the request is processed, we will receive the translated text in the response. Add the following code snippet to implement the methods.
public async Task<string> TranslateText(TranslatePayload translatePayload)
{
    string route = $"/translate?api-version=3.0&from={translatePayload.SourceLanguageCode}&to={translatePayload.TargetLanguageCode}";
    object[] body = new object[] { new { Text = translatePayload.TextToBeTranslated } };
    var requestBody = JsonConvert.SerializeObject(body);
    string result = String.Empty;
    using (var client = new HttpClient())
    using (var request = new HttpRequestMessage())
    {
        request.Method = HttpMethod.Post;
        request.RequestUri = new Uri(endpoint + route);
        request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
        request.Headers.Add("Ocp-Apim-Subscription-Key", key);
        request.Headers.Add("Ocp-Apim-Subscription-Region", location);
        HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
        result = await response.Content.ReadAsStringAsync();
    }
    return result;
}
With the implementation of the TranslateText method in place, let’s go to our program.cs file and add a singleton service in our DI container as follows:
builder.Services.AddSingleton<ITranslateBusiness, TranslateBusiness>();
Next, add an empty API controller called TranslateController.cs in the Controllers folder and then add the following constructor code in the controller to inject the ITranslateBusiness object into our _translateBusiness property using constructor dependency injection:
private readonly ITranslateBusiness _translateBusiness;
public TranslateController(ITranslateBusiness translateBusiness)
{
    _translateBusiness = translateBusiness;
}
Let’s add our action in our TranslateController. We are going to have only one action, GetTranslatedText. The purpose of this action is to call the TranslateText method of the TranslateBusiness class by passing the payload coming from the request body.
  • URL route: http://localhost:5185/api/Translate

  • HTTP verb: GET

Add the following code snippet to add the GetTranslatedText action in our controller:
[HttpGet]
public async Task<IActionResult> GetTranslatedText(TranslatePayload translatePayload)
{
    var translatedText = await _translateBusiness.TranslateText(translatePayload);
    if (String.IsNullOrEmpty(translatedText))
    {
        return BadRequest();
    }
    return Ok(new { result = translatedText});
}

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

Now that we have developed our API and have run it locally, let’s perform a sanity test on the functionalities of our API. Open Postman and create a collection for requests. After creating the collection, add a request to test the TranslateText API. Define the request method as GET and define the route as http://localhost:5185/api/Translate and then click the Body tab to add the required information, SourceLanguageCode, TargetLanguageCode, and TextToBeTranslated, in the payload as key/value pairs. The SourceLanguageCode and TargetLanguageCode needs to be code for the language. Refer to https://learn.microsoft.com/en-us/azure/cognitive-services/translator/language-support to find the languages for different codes. After you add the preceding information, click Send to send a request to our API. The API will now receive a request, process this information, and return the translated text in the target language, as shown in Figure 10-12.
Figure 10-12

Test the API

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset