Hands-On Lab834 words

Hands-On Lab: Exploring Specialized AWS AI Services (Comprehend & Polly)

Specialized AI Services

Hands-On Lab: Exploring Specialized AWS AI Services

Welcome to this 30-minute guided lab! In this session, you will explore AWS's specialized AI services, specifically focusing on Natural Language Processing (NLP) with Amazon Comprehend and Text-to-Speech (TTS) with Amazon Polly. These pre-trained services allow you to add advanced AI capabilities to your applications without needing deep machine learning expertise.

Prerequisites

Before starting this lab, ensure you have the following:

  • AWS Account: An active AWS account with permissions to access Amazon Comprehend and Amazon Polly.
  • CLI Tools: The AWS CLI (aws) installed and configured on your local machine.
  • IAM Permissions: The configured AWS profile must have ComprehendFullAccess and AmazonPollyFullAccess (or equivalent administrator rights).
  • Prior Knowledge: Basic familiarity with navigating the terminal and understanding JSON structures.

Learning Objectives

By completing this lab, you will be able to:

  1. Analyze unstructured text to detect sentiment using Amazon Comprehend.
  2. Extract key phrases and entities from text data.
  3. Convert textual data into lifelike speech using Amazon Polly.
  4. Seamlessly switch between the AWS CLI and the AWS Management Console to interact with specialized AI APIs.

Architecture Overview

AWS offers a broad taxonomy of AI services. This lab focuses on the Language and Speech domain.

Compiling TikZ diagram…
Running TeX engine…
This may take a few seconds

In this specific lab scenario, you will pass a single string of text into both services to observe their different capabilities:

Loading Diagram...

Step-by-Step Instructions

Step 1: Detect Sentiment with Amazon Comprehend

Amazon Comprehend uses NLP to extract insights from text. It returns a dominant sentiment along with confidence probability scores P(SentimentText)[0,1]P(\text{Sentiment} | \text{Text}) \in [0, 1] for Positive, Negative, Neutral, and Mixed.

Run the following command in your terminal to analyze a sample review:

bash
aws comprehend detect-sentiment \ --region us-east-1 \ --language-code en \ --text "I am absolutely thrilled with the new machine learning features in AWS. They are incredibly easy to use!"

[!TIP] Notice the output JSON. Look for the Sentiment key and the SentimentScore object to see how confident the AI is in its assessment.

💻 Console Alternative
  1. Log into the AWS Management Console.
  2. Search for Amazon Comprehend and select it.
  3. On the left sidebar, click Real-time analysis.
  4. Scroll down to the Input text box and paste: "I am absolutely thrilled with the new machine learning features in AWS. They are incredibly easy to use!"
  5. Click Analyze.
  6. View the results under the Insights -> Sentiment tab.

📸 Screenshot: Real-time analysis sentiment results showing "Positive".

Step 2: Extract Key Entities

Comprehend can also pull out named entities (like Organizations, Locations, and People).

bash
aws comprehend detect-entities \ --region us-east-1 \ --language-code en \ --text "Amazon Web Services is located in Seattle, Washington."
💻 Console Alternative
  1. In the same Real-time analysis page from Step 1, paste the text: "Amazon Web Services is located in Seattle, Washington."
  2. Click Analyze.
  3. Click the Entities tab below to see "Amazon Web Services" classified as an ORGANIZATION and "Seattle, Washington" as a LOCATION.

Step 3: Synthesize Speech with Amazon Polly

Now, let's take text and convert it to lifelike speech using Amazon Polly.

bash
aws polly synthesize-speech \ --region us-east-1 \ --output-format mp3 \ --voice-id Joanna \ --text "I am absolutely thrilled with the new machine learning features in AWS. They are incredibly easy to use!" \ output-speech.mp3

[!TIP] You can change the --voice-id to Matthew or Salli for different voices.

💻 Console Alternative
  1. In the AWS Management Console, search for Amazon Polly.
  2. In the left navigation pane, choose Text-to-Speech.
  3. Under the Text section, paste the same sentence.
  4. Select a voice (e.g., Joanna, Female).
  5. Click Listen to speech to play it directly in your browser, or click Download to save the MP3.

📸 Screenshot: Amazon Polly Text-to-Speech interface.

Checkpoints

Verify that your lab steps were successful by performing these checks:

  1. Verify Comprehend Output: Run aws comprehend detect-sentiment from Step 1 again. Does the output show "Sentiment": "POSITIVE" with a confidence score greater than 0.95?
  2. Verify Polly Output: In your terminal, verify that the MP3 file was created:
bash
ls -lh output-speech.mp3

(Expected outcome: You should see output-speech.mp3 listed with a file size greater than 0 bytes. Try playing it using your computer's media player!)

Clean-Up / Teardown

[!WARNING] Remember to run the teardown commands to avoid ongoing clutter, though note that Amazon Comprehend and Amazon Polly real-time API calls do not provision ongoing infrastructure. You are only billed per request (character count).

Since this lab only utilized stateless API calls, there are no EC2 instances or SageMaker endpoints to delete. However, you should clean up your local workspace:

bash
# Remove the generated audio file from your local machine rm output-speech.mp3

Troubleshooting

If you run into issues during the lab, reference this troubleshooting table:

Error Message / IssueLikely CauseSolution
AccessDeniedExceptionYour IAM user lacks permissions for Comprehend or Polly.Attach the ComprehendFullAccess and AmazonPollyFullAccess policies to your IAM user/role.
InvalidRequestException (Polly)The specified --voice-id does not exist or has a typo.Use a valid Voice ID (e.g., Joanna, Matthew, Ivy). Check Polly documentation for the full list.
Could not connect to the endpoint URLNetwork issue or invalid AWS region configuration.Ensure you specified a valid region (e.g., --region us-east-1) and that your internet connection is active.
aws: command not foundAWS CLI is not installed or not in your system PATH.Install the AWS CLI and run aws configure.

Ready to study AWS Certified AI Practitioner (AIF-C01)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free