Exploring the benefits of Amazon Translate – AWS Application Services for AI/ML – MLS-C01 Study Guide

Exploring the benefits of Amazon Translate

Some of the benefits of using Amazon Translate include the following:

  • It uses neural machine translation, which mimics the way the human brain works.
  • You do not need to maintain resources or infrastructures for the Translation action.
  • Produces high-quality results and maintains their consistency.
  • You can customize brand names and model names. Other unique terms too can get translated using the custom terminology feature.
  • Can be easily integrated with applications through APIs.
  • Amazon Translate scales itself when you need it to do more.

Next, you will get hands-on with Amazon Translate.

Getting hands-on with Amazon Translate

In this section, you will build a product by integrating AWS Lambda with Amazon Rekognition, Amazon Comprehend, and Amazon Translate to read an image file stored in an S3 bucket. Then, you will detect the language of the text that has been extracted from the image so that you can translate it into English. You will also use CloudWatch to print the translated output. The following is a diagram of our use case:

Figure 8.17 – Architecture diagram of the required use case

Let’s start by creating an IAM role:

  1. Navigate to the IAM console page.
  2. Select Roles from the left-hand menu.
  3. Select Create role.
  4. Select Lambda as the trusted entity.
  5. Add the following managed policies:
    • AmazonS3ReadOnlyAccess
    • AmazonRekognitionFullAccess
    • ComprehendFullAccess
    • CloudWatchFullAccess
    • TranslateFullAccess
  6. Save the role as language-translation-from-image.
  7. The next immediate step is to create a Lambda function. Navigate to Lambda > Functions > Create Function.
  8. Name the function language-detection-from-image.
  9. Set the runtime to Python 3.6.
  10. Use an existing role; that is, language-detection-from-image-role.
  11. Paste the code available at https://github.com/PacktPublishing/AWS-Certified-Machine-Learning-Specialty-MLS-C01-Certification-Guide-Second-Edition/blob/main/Chapter08/Amazon%20Translate%20Demo/lambda_function/lambda_function.py and click Deploy. You will use the translate_text API to translate the input text.
  12. The next step is to create a bucket called language-translation-from-image.

Create a folder named image. Then, navigate to Properties > Events> Add notification.

  1. Fill in the required fields, as shown here, and click on Save (please make sure you select .jpg as the suffix; otherwise, it will trigger the Lambda function for any object creation process):
    • Name: translate-language-image
    • Events: All object create events
    • Prefix: image/
    • Suffix: .jpg
    • Send to: Lambda Function
    • Lambda: language-translation-from-image
  2. Navigate to Amazon S3 > language-detection-image > input-image. Upload the sign-image.jpg image into the folder. This file is available in this book’s GitHub repository: https://github.com/PacktPublishing/AWS-Certified-Machine-Learning-Specialty-MLS-C01-Certification-Guide-Second-Edition/tree/main/Chapter08/Amazon%20Translate%20Demo/input_image.
  3. Uploading this image will trigger the Lambda function. You can monitor the logs by going to CloudWatch > CloudWatch Logs > Log groups > /aws/lambda/language-translation-from-image.
  4. Click on the streams and select the latest one. It will look as follows:

Figure 8.18 – The logs in CloudWatch for verifying the output

The translation is as follows:

Translation of the text from the Image :

{‘PREVENCION DEL COVID-19’: ‘PREVENTION OF COVID-19’,

‘LAVATE LAS MANOS EVITA EL CONTACTO NO TE TOQUES oJOs,

EVITA’: ‘WASHE HANDS AVOID CONTACT DO NOT TOUCH EYES’,

’60 SEGUNDOS CON CONTAGIADOS NARIZ O BOCA

AGLOMERACIONES’: ’60 SECONDS WITH CONTAGIOUS NOSE OR

MOUTH AGGLOMERATIONS’, ‘NO COMPARTAS NO VIAJES A MENOS

SI TE PONES ENFERMO’: “DON’T SHARE NOT TRAVEL UNLESS

YOU GET SICK”, ‘CUBIERTOS NI COMIDA QUE SEA NECESARIO

BUSCA AYUDA MEDICA’: ‘CUTLERY OR FOOD NEEDED SEEK

MEDICAL HELP’}