CRM Crate

Creating a custom Translation API using Azure Cognitive Services in Dynamics 365

We will learn to create our own translation Web API using the Azure Cognitive Services and use it in Dynamics 365. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Dynamics 365.

Creating a custom  Translation API using Azure Cognitive Services in Dynamics 365

What is an Azure Cognitive Service?

Azure Cognitive Services are cloud-based services with REST APIs, client library SDKs, and user interfaces available to help you build cognitive intelligence into your applications. You can add cognitive features to your applications without having artificial intelligence (AI) or data science skills. Cognitive Services comprises various AI services that enable you to build cognitive solutions that can see, hear, speak, understand, and even make decisions.

Translator is a cloud-based machine translation service you can use to translate text in with a simple REST API call. The service uses modern neural machine translation technology and offers statistical machine translation technology. Custom Translator is an extension of Translator, which allows you to build neural translation systems. The customized translation system can be used to translate text with Translator or Microsoft Speech Services.

Steps for creating a custom Web API for language translation.

We will use the Azure Cognitive Service’s translator feature & Azure Functions for achieving the translations.

Step 1 – Create a translator in the Azure Cognitive Service

  • Open an Azure Portal (https://portal.azure.com/) and sign-in with the valid Azure credentials.
  • In the services, open the “Cognitive Services” and navigate to “Translator” as shown below.
  • Create a new translator as per your pricing preference.
  • Copy the essential authentication information such as “Key”, “Endpoint” and “Region” which will be required later while consuming the translator from an Azure Function as shown below.

Step 2 – Create an Azure Function for communicating with the translator

We have used an Azure Function as a middle layer for communicating with our Azure Translator.

  • Create & configure a HTTP based Azure Function for communicating with the translator as per the code given below.
  • Deploy your Go-Live function once the configuration is completed.
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{

    //Retrieving HTTP Content From The Request.
    string inputText = req.Query["inputText"];
    string inputLan = req.Query["inputLan"];
    string outputLan = req.Query["outputLan"];

    //Deseriallizing The Data From The Request.
    string requestBodyPre = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBodyPre);
    inputText = inputText ?? data?.inputText;
    inputLan = inputLan ?? data?.inputLan;
    outputLan = outputLan ?? data?.outputLan;

    //Variable Declaration.    
    string responseOutput = "";
    string inputLanguage = inputLan;
    string outputlanguage = outputLan;
    string route = $"/translate?api-version=3.0&from={inputLanguage}&to={outputlanguage}";
    string textToTranslate = inputText;
    string subscriptionKey = "YOUR_KEY";
    string subscriptionRegion = "YOUR_LOCATION";
    string endpoint = "YOUR_ENDPOINT_API";

    //Defining Request Body.
    object[] body = new object[] { new { Text = textToTranslate } };
    var requestBody = JsonConvert.SerializeObject(body);

    //Requesting & Consuming The CRM Crate Azure Cognitive Services
    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", subscriptionKey);
        request.Headers.Add("Ocp-Apim-Subscription-Region", subscriptionRegion);
        HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
        string responsetext = await response.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<List<Dictionary<string, List<Dictionary<string, string>>>>>(responsetext);
        var translation = result[0]["translations"][0]["text"];
        responseOutput = translation;
    }

    //Returning Service API Response.
    return new OkObjectResult(responseOutput);
}

Step 3 – Utilize the newly created Azure Function in Dynamics 365

We will feed the inputs from the Dynamics 365 entity record to our Azure Function which will eventually communicate with the Cognitive Service.

  • We have created the fields in Dynamics 365 for driving the translations. This fields include the input language type, output language type, input language text & output language translated text as shown below.
  • One filling the translation information, we have called our Azure Function on the ribbon button click.

Validating our implementation in Dynamics 365

Once the above steps are completed, navigate to the Dynamics 365 CRM and validate the translation implementation as shown below.

Thus, we learned to create our own translation Web API using an Azure Cognitive Services and used it in Dynamics 365.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.