Step 3 of 6 - About 45 min
Lab 03 - Speech: Transcribe and Polly
Speech-to-text with Amazon Transcribe and text-to-speech with Amazon Polly.
Time: about 45 minutes.
Overview
This lab covers two managed speech services:
- Amazon Transcribe converts audio to text in asynchronous jobs, with speaker labels and a custom vocabulary for domain terms.
- Amazon Polly converts text to natural-sounding speech with neural and standard voices, SSML markup, and a custom pronunciation lexicon.
What gets created
<account-id> is your 12-digit AWS account ID.
| Component | Resources |
|---|---|
s3-bucket |
Bucket ai-03-speech-test-speech-data-<account-id> with input/audio/, output/transcripts/, output/speech/ |
iam-role |
Role ai-03-speech-test-speech-services (trusted by Lambda) with Transcribe, Polly, S3, and CloudWatch Logs policies |
transcribe |
Custom vocabulary ai-03-speech-test-transcribe-vocabulary and role ai-03-speech-test-transcribe-transcribe-role |
polly |
Pronunciation lexicon ai03speechtest (created with aws polly put-lexicon) |
Polly lexicon names may only contain letters and digits (up to 20 characters), so the component strips the dashes from the naming template.
The custom vocabulary
uses the list format. In that format a phrase with several words joins them
with hyphens, so the stack lists Amazon-Web-Services and
machine-learning, not words separated by spaces.
Architecture
+---------------------------------------------------------------+
| S3 bucket |
| input/audio/ (MP3, MP4, WAV, FLAC, OGG, AMR, WebM) |
+--------+------------------------------------------------------+
|
v
+-------------------------------------------+
| Transcribe |
| - StartTranscriptionJob (async) |
| - Custom vocabulary (AWS terms) |
| - Speaker labels |
+---------------------+---------------------+
|
v
+-------------------------------------------+
| output/transcripts/<job-name>.json |
+-------------------------------------------+
+-------------------------------------------+
| Polly |
| - SynthesizeSpeech -> local MP3 file |
| - Custom lexicon (pronunciation) |
| - Neural and standard voices, SSML |
+-------------------------------------------+Before you start
- Complete the Prerequisites in the README.
- Have test audio in the lab folder:
sample-audio.mp3- a short English recording (you can record one on your phone, or create one with Polly in the Polly section below)conversation.mp3- optional, two or more people talking
- To play MP3 files:
afplayon macOS,mpg123orffplayon Linux, or open the file in any media player.
Deploy
The stack is stacks/labs/03-speech/test.yaml. Its vocabulary_phrases
list replaces the catalog list instead of adding to it, because atmos.yaml
sets list_merge_strategy: replace (see
Configure Variables). iam-role reads the
bucket ARN with
!terraform.output,
so apply s3-bucket first.
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-03-speech-test --process-functions=false
# Apply in order: iam-role reads the bucket ARN from s3-bucket
atmos terraform apply s3-bucket -s lab-03-speech-test
atmos terraform apply iam-role -s lab-03-speech-test
atmos terraform apply transcribe -s lab-03-speech-test
atmos terraform apply polly -s lab-03-speech-testThe custom vocabulary takes a minute or two to become READY after the
apply:
aws transcribe get-vocabulary \
--vocabulary-name ai-03-speech-test-transcribe-vocabulary \
--region us-east-1 --query VocabularyStateSet shell variables
STACK=lab-03-speech-test
BUCKET=$(atmos terraform output s3-bucket -s $STACK --skip-init -- -raw bucket_id)
VOCAB_NAME=$(atmos terraform output transcribe -s $STACK --skip-init -- -raw vocabulary_name)
LEXICON_NAME=$(atmos terraform output polly -s $STACK --skip-init -- -raw lexicon_name)
echo "$BUCKET $VOCAB_NAME $LEXICON_NAME"Test Polly
Start with Polly: it can create the test audio for Transcribe.
Synthesize speech
aws polly synthesize-speech \
--text "Hello! Welcome to the AWS AI services hands-on lab. Today we use Amazon Rekognition and Amazon Bedrock." \
--output-format mp3 \
--voice-id Joanna \
--engine neural \
--region us-east-1 \
sample-audio.mp3
afplay sample-audio.mp3 # macOS; use mpg123 or ffplay on LinuxDifferent voices
# US English, male
aws polly synthesize-speech \
--text "This is Matthew speaking. I am a neural voice." \
--output-format mp3 --voice-id Matthew --engine neural \
--region us-east-1 matthew-speech.mp3
# British English
aws polly synthesize-speech \
--text "Good day! This is Amy from the United Kingdom." \
--output-format mp3 --voice-id Amy --engine neural \
--region us-east-1 amy-speech.mp3List the voices for a language and engine:
aws polly describe-voices \
--language-code en-US \
--engine neural \
--region us-east-1 \
--query 'Voices[].[Id,Gender]' --output tableCustom lexicon
The lexicon tells Polly to say "AWS" as "Amazon Web Services" and to spell out S3, EC2, AI, ML, NLP, and API. Compare the two files:
TEXT="AWS provides AI and ML services like S3 and EC2 with powerful API capabilities."
aws polly synthesize-speech --text "$TEXT" \
--output-format mp3 --voice-id Joanna --engine neural \
--region us-east-1 no-lexicon.mp3
aws polly synthesize-speech --text "$TEXT" \
--output-format mp3 --voice-id Joanna --engine neural \
--lexicon-names "$LEXICON_NAME" \
--region us-east-1 with-lexicon.mp3See the lexicon content with
aws polly get-lexicon --name "$LEXICON_NAME" --region us-east-1.
SSML
SSML controls pauses, speed, and volume. Neural voices support <break>,
<prosody> with rate and volume, and most <say-as> types. They do not
support <emphasis> or prosody pitch; those work with the standard engine.
See Supported SSML tags.
# Neural engine: break, rate, volume, say-as
aws polly synthesize-speech \
--text-type ssml \
--text '<speak><prosody rate="slow">Welcome to the lab.</prosody><break time="1s"/><prosody volume="loud">This part is important.</prosody><break time="500ms"/>Lab <say-as interpret-as="digits">03</say-as> starts now.</speak>' \
--output-format mp3 --voice-id Joanna --engine neural \
--region us-east-1 ssml-neural.mp3
# Standard engine: emphasis and pitch
aws polly synthesize-speech \
--text-type ssml \
--text '<speak><emphasis level="strong">This is important!</emphasis><break time="500ms"/><prosody pitch="high">Questions?</prosody></speak>' \
--output-format mp3 --voice-id Joanna --engine standard \
--region us-east-1 ssml-standard.mp3Test Transcribe
Start a transcription job
aws s3 cp sample-audio.mp3 "s3://$BUCKET/input/audio/"
JOB=test-job-$(date +%s)
aws transcribe start-transcription-job \
--transcription-job-name "$JOB" \
--language-code en-US \
--media-format mp3 \
--media "MediaFileUri=s3://$BUCKET/input/audio/sample-audio.mp3" \
--output-bucket-name "$BUCKET" \
--output-key "output/transcripts/" \
--region us-east-1 \
--query 'TranscriptionJob.[TranscriptionJobName,TranscriptionJobStatus]'The job starts IN_PROGRESS. Check it until it is COMPLETED (usually under
a minute for a short file):
aws transcribe get-transcription-job \
--transcription-job-name "$JOB" \
--region us-east-1 \
--query TranscriptionJob.TranscriptionJobStatusBecause --output-key ends with /, Transcribe writes the result to
output/transcripts/<job-name>.json:
aws s3 cp "s3://$BUCKET/output/transcripts/$JOB.json" - | jq -r '.results.transcripts[0].transcript'With the custom vocabulary
JOB_VOCAB=test-vocab-$(date +%s)
aws transcribe start-transcription-job \
--transcription-job-name "$JOB_VOCAB" \
--language-code en-US \
--media-format mp3 \
--media "MediaFileUri=s3://$BUCKET/input/audio/sample-audio.mp3" \
--settings "VocabularyName=$VOCAB_NAME" \
--output-bucket-name "$BUCKET" \
--output-key "output/transcripts/" \
--region us-east-1Compare the two transcripts. The vocabulary helps most with product names such as "Rekognition" and "Bedrock" that a general model may misspell.
With speaker labels (optional)
aws s3 cp conversation.mp3 "s3://$BUCKET/input/audio/"
aws transcribe start-transcription-job \
--transcription-job-name "test-speakers-$(date +%s)" \
--language-code en-US \
--media-format mp3 \
--media "MediaFileUri=s3://$BUCKET/input/audio/conversation.mp3" \
--settings "ShowSpeakerLabels=true,MaxSpeakerLabels=4" \
--output-bucket-name "$BUCKET" \
--output-key "output/transcripts/" \
--region us-east-1The result JSON has a speaker_labels section that assigns each segment to
spk_0, spk_1, and so on.
Console testing (optional)
- Amazon Transcribe console: Real-time transcription lets you speak into your microphone and watch the text appear (streaming, which the CLI does not cover).
- Amazon Polly console: type text, pick a voice and engine, and listen.
Cleanup
Destroy in the reverse order of the apply:
atmos terraform destroy polly -s lab-03-speech-test
atmos terraform destroy transcribe -s lab-03-speech-test
atmos terraform destroy iam-role -s lab-03-speech-test
atmos terraform destroy s3-bucket -s lab-03-speech-testdestroy on polly deletes the lexicon with aws polly delete-lexicon.
Transcription jobs are not Terraform resources. Delete the job records:
for j in $(aws transcribe list-transcription-jobs --region us-east-1 \
--query "TranscriptionJobSummaries[?starts_with(TranscriptionJobName, 'test-')].TranscriptionJobName" \
--output text); do
aws transcribe delete-transcription-job --transcription-job-name "$j" --region us-east-1
doneDelete the local MP3 files when you are done.
Troubleshooting
- Vocabulary is FAILED - see the reason with
aws transcribe get-vocabulary --vocabulary-name "$VOCAB_NAME" --region us-east-1 --query FailureReason. The usual cause is a phrase with spaces or unsupported characters. - BadRequestException: vocabulary not ready - wait until the vocabulary
is
READY, then start the job again. - Polly lexicon error during apply - lexicon names must match
[0-9A-Za-z]{1,20}, and the AWS CLI must be installed and signed in. - Polly "Unsupported Neural feature" - remove
<emphasis>or prosodypitchfrom SSML, or use--engine standard.
Cost
Transcribe bills per second of audio (with a minimum per request). Polly bills per character synthesized, and neural voices cost more than standard voices. See Transcribe pricing and Polly pricing. Free Tier terms depend on your account; see AWS Free Tier.
Next steps
- Previous: Lab 02 - Language: Comprehend and Translate
- Next: Lab 04 - Conversational AI: Lex V2 - build a chatbot.
- Back to the lab overview.
- Try Polly speech marks (
--output-format json --speech-mark-types word) to get word timings for captions.
Atmos docs
- Configure Variables - how the lab stack's
vocabulary_phrasesandpollysettings override the catalog. - Stack Catalogs - the
transcribeandpollydefaults instacks/catalog/speech/. - Using !terraform.output - the bucket ARN that
iam-rolereads. - atmos describe component - see the final merged vars, for example
atmos describe component transcribe -s lab-03-speech-test. - atmos terraform apply - apply runs the
local-execprovisioner that creates the lexicon.
Exam connection
This lab practices:
- AIF-C01, Fundamentals of AI and ML - speech recognition and speech synthesis use cases (call transcripts, captions, accessibility, voice interfaces) and the AWS services that serve them.
- AIF-C01, Applications of Foundation Models - turning audio into text is often the first step before you send content to a model for summarization.
- MLA-C01, ML Model Development - customizing a pre-trained service with a custom vocabulary instead of training a speech model.
- Terraform Associate (004), Understand Terraform Basics - using several
providers (
aws,local,null), generating files withlocal_file,null_resourcetriggers that recreate a resource when its content changes, and destroy-time provisioners.
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.