Paso 2 de 6 - Unos 40 min
Lab 02 - Language: Comprehend and Translate
Natural language processing with Amazon Comprehend and translation with Amazon Translate.
Time: about 40 minutes.
Overview
This lab covers two managed language services:
- Amazon Comprehend finds sentiment, entities, key phrases, the dominant language, and personally identifiable information (PII) in text.
- Amazon Translate translates text between languages in real time or in batch jobs, with optional custom terminology for brand and product names.
What gets created
<account-id> is your 12-digit AWS account ID.
| Component | Resources |
|---|---|
s3-bucket |
Bucket ai-02-language-test-language-data-<account-id> with input/text/, input/training/, output/comprehend/, output/translate/ |
iam-role |
Role ai-02-language-test-language-services (trusted by Lambda) with Comprehend, Translate, S3, and CloudWatch Logs policies |
comprehend |
Role ai-02-language-test-comprehend-comprehend-role that Comprehend can assume for async jobs and custom models |
translate |
Role ai-02-language-test-translate-translate-role that Translate can assume for batch jobs |
Custom classifiers, entity recognizers, and custom terminology are off by
default (they need training data or a terminology file). The real-time
commands below need no resources at all; the service roles and the bucket
are a starting point for batch jobs and custom models. A service role can
read the bucket only after you set the component's s3_bucket_arn
variable.
Architecture
+---------------------------------------------------------------+
| S3 bucket |
| input/text/ input/training/ |
+--------+------------------------------------------------------+
| (batch jobs and custom models use the service roles)
v
+-------------------------------------------+
| Comprehend |
| - DetectSentiment - DetectEntities |
| - DetectKeyPhrases - DetectDominantLanguage |
| - DetectPiiEntities - Batch* APIs |
+---------------------+---------------------+
|
v
+-------------------------------------------+
| Translate |
| - TranslateText - Batch jobs |
| - Custom terminology (optional) |
+---------------------+---------------------+
|
v
+-------------------------------------------+
| output/comprehend/ output/translate/ |
+-------------------------------------------+Before you start
Complete the Prerequisites in the README.
Deploy
The stack is stacks/labs/02-language/test.yaml. As in Lab 01, iam-role
reads the bucket ARN from s3-bucket with
!terraform.output,
so apply s3-bucket first and destroy it last.
cd atmos-aws-ai-infra # the lab folder
# Check the stack (YAML functions off: nothing is applied yet)
atmos validate stacks
atmos describe stacks -s lab-02-language-test --process-functions=false
# Apply in order: iam-role reads the bucket ARN from s3-bucket
atmos terraform apply s3-bucket -s lab-02-language-test
atmos terraform apply iam-role -s lab-02-language-test
atmos terraform apply comprehend -s lab-02-language-test
atmos terraform apply translate -s lab-02-language-testSet shell variables
STACK=lab-02-language-test
BUCKET=$(atmos terraform output s3-bucket -s $STACK --skip-init -- -raw bucket_id)
echo "$BUCKET"Test Comprehend
Example outputs are abridged; your scores differ slightly.
Detect sentiment
aws comprehend detect-sentiment \
--text "I love using AWS AI services! They are amazing and easy to use." \
--language-code en \
--region us-east-1{
"Sentiment": "POSITIVE",
"SentimentScore": {
"Positive": 0.98,
"Negative": 0.001,
"Neutral": 0.015,
"Mixed": 0.004
}
}Detect entities
aws comprehend detect-entities \
--text "Amazon Web Services was founded in 2006 and is headquartered in Seattle, Washington." \
--language-code en \
--region us-east-1 \
--query 'Entities[].[Text,Type]' --output tableExpect Amazon Web Services as ORGANIZATION, 2006 as DATE, and
Seattle and Washington as LOCATION.
Detect key phrases
aws comprehend detect-key-phrases \
--text "Machine learning is transforming how businesses operate. AWS provides powerful AI services for developers." \
--language-code en \
--region us-east-1 \
--query 'KeyPhrases[].Text'Detect the dominant language
aws comprehend detect-dominant-language \
--text "Bonjour, comment allez-vous aujourd'hui?" \
--region us-east-1{
"Languages": [
{"LanguageCode": "fr", "Score": 0.99}
]
}Detect PII
aws comprehend detect-pii-entities \
--text "John Smith's email is john.smith@example.com and his phone is 555-123-4567." \
--language-code en \
--region us-east-1 \
--query 'Entities[].[Type,BeginOffset,EndOffset]' --output tableExpect NAME, EMAIL, and PHONE with character offsets. Comprehend
returns where the PII is, not the values, so you can redact it yourself.
Batch sentiment (up to 25 documents per call)
aws comprehend batch-detect-sentiment \
--text-list "I love this product!" "The service was terrible." "It was okay, nothing special." \
--language-code en \
--region us-east-1 \
--query 'ResultList[].[Index,Sentiment]' --output tableTest Translate
Translate text
aws translate translate-text \
--text "Hello, I am learning about AWS AI services." \
--source-language-code en \
--target-language-code es \
--region us-east-1{
"TranslatedText": "Hola, estoy aprendiendo sobre los servicios de IA de AWS.",
"SourceLanguageCode": "en",
"TargetLanguageCode": "es"
}Let Translate detect the source language
--source-language-code auto asks Translate to detect the language (it uses
Comprehend for that):
aws translate translate-text \
--text "Bienvenue au laboratoire" \
--source-language-code auto \
--target-language-code en \
--region us-east-1Several target languages
for lang in fr de ja; do
aws translate translate-text \
--text "Welcome to the AI hands-on lab!" \
--source-language-code en \
--target-language-code "$lang" \
--region us-east-1 \
--query TranslatedText --output text
doneOptional: custom terminology
Custom terminology keeps names such as "Amazon Bedrock" untranslated. The
translate component imports it with the AWS CLI. To try it, edit the
translate component in stacks/labs/02-language/test.yaml: change
create_custom_terminology to true (do not add a second
create_custom_terminology key) and add terminology_file_content. The
lab stack overrides the catalog
defaults:
create_custom_terminology: true
terminology_file_content: |
en,es
Amazon Bedrock,Amazon Bedrock
AI services,servicios de IAApply translate again, then pass the terminology name to a translation:
atmos terraform apply translate -s lab-02-language-test
TERMS=$(atmos terraform output translate -s $STACK --skip-init -- -raw terminology_name)
aws translate translate-text \
--text "Amazon Bedrock is one of the AI services on AWS." \
--source-language-code en \
--target-language-code es \
--terminology-names "$TERMS" \
--region us-east-1Console testing (optional)
- Amazon Comprehend console: open Real-time analysis, paste text, and choose Analyze.
- Amazon Translate console: open Real-time translation and pick the source and target languages.
Cleanup
Destroy in the reverse order of the apply:
atmos terraform destroy translate -s lab-02-language-test
atmos terraform destroy comprehend -s lab-02-language-test
atmos terraform destroy iam-role -s lab-02-language-test
atmos terraform destroy s3-bucket -s lab-02-language-testIf you enabled custom terminology, destroy on translate deletes it with
aws translate delete-terminology.
Troubleshooting
- TextSizeLimitExceededException - real-time Comprehend calls have a per-document size limit (5,000 bytes of UTF-8 text for sentiment). Split long text or use an async job.
- UnsupportedLanguageException - not every Comprehend feature supports every language. Check the Comprehend documentation for the language list.
- AccessDenied - the commands run as your own identity; add the
comprehend:*ortranslate:*action you need to it.
Cost
Comprehend bills per unit of 100 characters (with a minimum per request). Translate bills per character translated. Custom models and batch jobs have their own prices. See Comprehend pricing and Translate pricing. Free Tier terms depend on your account; see AWS Free Tier.
Next steps
- Previous: Lab 01 - Vision: Rekognition and Textract
- Next: Lab 03 - Speech: Transcribe and Polly - speech-to-text and text-to-speech.
- Back to the lab overview.
- Try a Comprehend custom classifier: upload a training CSV to
input/training/, then setcreate_document_classifier: true,classifier_training_data_s3_uri, ands3_bucket_arn(so the Comprehend role can read the bucket) on thecomprehendcomponent. Training takes a while and is billed.
Atmos docs
- Configure Variables - how the lab stack overrides catalog values such as
create_custom_terminology. - Stack Catalogs - where the
comprehendandtranslatedefaults live. - Using !terraform.output - how
iam-rolegets the bucket ARN. - atmos terraform output - reading
bucket_idandterminology_name. - atmos terraform destroy - the cleanup commands.
Exam connection
This lab practices:
- AIF-C01, Fundamentals of AI and ML - natural language processing use cases (sentiment, entities, key phrases, language detection, translation) and the managed services that solve them without training a model.
- AIF-C01, Security, Compliance, and Governance for AI Solutions - detecting PII before you store, log, or send text to another system.
- MLA-C01, Data Preparation for ML - using Comprehend to find and redact PII in training data.
- MLA-C01, ML Model Development - pre-trained services versus custom classifiers and entity recognizers.
- Terraform Associate (004), Understand Terraform Basics -
local-execprovisioners with create and destroy steps, and why they are a last resort when a provider has no resource.
Related certification pages: AIF-C01, MLA-C01, Terraform Associate (004).
Practice exam-style questions on these topics on CertLabPro: on the web at https://certlabpro.com/cert/aws-aif-c01, or in the app: https://certlabpro.com/mobile.