A Simple Machine Learning step by step tutorial: Using Amazon Translate + Lambda + API Gateway & Postman

In this blog, I am going to explain the step-by-step tutorial for a small project, which includes Machine Learning & Artificial intelligence. Majorly we are using Amazon Translate to Utilize AI & ML.

Amazon Translate provides neural network translation for 75 languages and 5,550 language pairs. So, a user can write anything in any language and it will be translated as per the desired pair of languages.

AWS Architecture:

Integration of frontend as API gateway and backend as a Lambda function with Amazon Translate. All logs will be captured by AWS CloudWatch Logs.

Objective:

We want to build a Language Translator System, which will translate the text from one language to another language. For example, A person will type anything in Hindi and Translator will convert it into English.

As shown below:

खराब मौसम के चलते पीएम मोदी की उत्तराखंड में होने वाली वर्चुअल चुनाव रैली रद्द कर दी गई है।Due to bad weather, PM Modi's virtual election rally in Uttarakhand has been canceled.

Before jumping to hands-on, let’s familiarize with the AWS resources below:

What Is Amazon Translate?

Amazon Translate is a neural machine translation service that delivers fast, high-quality, affordable, and customizable language translation.

Neural machine translation is a form of language translation automation that uses deep learning models to deliver more accurate and more natural-sounding translation than traditional statistical and rule-based translation algorithms.

As the given example below you can paste anything in the left text box as a source language & it will automatically convert to the right text box as the target language.

As shown below:

You may also choose or change the source and target languages as per your need.

What is Amazon API Gateway?

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the “front door” for applications to access data, business logic, or functionality from your backend services.

Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.

What is Amazon Lambda Function?

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. These events may include changes in state or an update, such as a user placing an item in a shopping cart on an eCommerce website.

You can use AWS Lambda to extend other AWS services with custom logic or create your own backend services that operate at AWS scale, performance, and security.

So let's Start ….!

Follow the simple steps given below:

Step 1: Log in to the AWS console.

Step 2: Create an AWS IAM ROLE.

In this Step, we are authorizing AWS Lambda to access CloudWatch and AWS Translate, using IAM Role.

JSON Policy for CloudWatch logs full access.

{    "Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}

JSON Policy for Amazon Translate power user.

{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"translate:*",
"comprehend:DetectDominantLanguage",
"cloudwatch:GetMetricStatistics",
"cloudwatch:ListMetrics",
"s3:ListAllMyBuckets",
"s3:ListBucket",
"s3:GetBucketLocation",
"iam:ListRoles",
"iam:GetRole"
],
"Effect": "Allow",
"Resource": "*"
}
]
}

Step 3: Create a Lambda Function.

  1. Create a new Lambda function and select the IAM role from the previous step.

2. Once the Lambda is ready, copy and paste the below-mentioned code to your lambda_function file.

import boto3translate_client = boto3.client('translate')def lambda_handler(event, context): 
review_text = event['text']
translate_response = translate_client.translate_text(
Text=review_text,
SourceLanguageCode='auto',
TargetLanguageCode='en'
)
print(translate_response)
return translate_response['TranslatedText']

Your Lambda function will look like below:

3. Now, you can deploy & test your lambda function, after clicking on Deploy.

4. Then click on the Test tab and write the below lines in your test event in text format:

{"text"  : "खराब मौसम के चलते पीएम मोदी की उत्तराखंड में होने वाली वर्चुअल चुनाव रैली रद्द कर दी गई है।"}

5. Then click on the test and you will get your expected result like below:

"Due to bad weather, PM Modi's virtual election rally in Uttarakhand has been canceled."

It means your Lambda function and Amazon Translate interact with each other and work successfully.

Step 4: AWS CloudWatch Logs.

You are able to check the AWS Lambda function logs in AWS CloudWatch Logs as shown below:

Now, half of the project is done. Let’s go for the frontend side now.

Step 5: Create a Rest API in AWS API GATEWAY.

  1. Create a new Rest API and name it Translate in AWS API Gateway.
  2. Then create a post method from the drop-down list of Actions tab.

3. Write your lambda function name in the text box. In my case “translate-hindi-to-english” is the name.

4. Now, click OK. you will see something like the below:

5. Then, Click on Stage & Deploy your API with a description.

Once it is deployed successfully, it will look like below:

Invoke URL: 
https://0ue7hq0vy1.execute-api.us-east-1.amazonaws.com/dev

Step 6: Test the API Endpoint in Postman.

In the end, you will open postman software to test the API Endpoints.

  1. Select Post method.
  2. Paste the URL in the text box.
  3. Click on the Body radio button and Paste the raw data.
  4. Click on Send. You will get the results as mentioned below.

Now, your second part of the project is also successfully done.

Use Case:

Language Localization

  • It’s very difficult for human translation teams to keep up with dynamic or real-time content. With Amazon Translate, you can easily translate massive volumes of user-generated content in real-time.
  • Websites and applications can automatically make content such as feed stories, profile descriptions, and comments, available in the user’s preferred language with a click of a “translate” button.

Text Analytics

  • With Amazon Translate, you are not restricted by language barriers. Understand the social sentiment of your brand, product, or service while monitoring online conversations in different languages.
  • Simply translate the text to English before using a natural language processing (NLP) application like Amazon Comprehend to analyze textual content in a multitude of languages.

Communication

  • Amazon Translate can provide automatic translation to enable cross-lingual communications between users for your applications.
  • By adding real-time translation to chat, email, helpdesk, and ticketing applications, an English-speaking agent or employee can communicate with customers across multiple languages.

Conclusion:

We have successfully integrated AWS Lambda, API Gateway & Amazon Translate and we are able to translate the Languages as per our need while sending the API from Postman.

For more info please connect & Follow me:

LinkedIn: https://www.linkedin.com/in/vkmanik/

Email: varunmanik1@gmail.com

Facebook: https://www.facebook.com/cloudvirtualization/

YouTube: https://bit.ly/32fknRN

Twitter: https://twitter.com/varunkmanik

References:

  1. https://aws.amazon.com/translate/
  2. https://aws.amazon.com/translate/faqs/
  3. https://aws.amazon.com/api-gateway/

--

--

AWS APN Ambassador | SME of DevOps DevSecOps | Cloud Architect & Trainer | Blogger | Youtuber |Chef

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store