Overall, the project showcases how AWS AI services can be combined to build scalable applications for speech recognition, voice assistants, transcription services, accessibility tools, and automated content processing.
code snippet:
Steps to Build the Project:
• Step 1: Set Up an AWS Account
• Step 2: Create two S3 Buckets (Source S3 Bucket Name: amc-polly-source-bucket, Destination S3 Bucket Name: amc-polly-destination-bucket)
• Step 3: Create an IAM Policy (IAM Policy Name: amc-polly-lambda-policy)
Policy Defination:
- {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::amc-polly-source-bucket/*",
"arn:aws:s3:::amc-polly-destination-bucket/*"
]
},
{
"Effect": "Allow",
"Action": [
"polly:SynthesizeSpeech"
],
"Resource": "*"
}
]
}
• Step 4: Create an IAM Role (IAM Role Name: amc-polly-lambda-role) and attach amc-polly-lambda-policy and AWSLambdaBasicExecutionRole Policies
• Step 5: Create and Configure the Lambda Function (Lambda Function Name: TextToSpeechFunction)
◦ Set the runtime to Python 3.8.
◦ Set the execution role with necessary permissions for S3 and Polly. (Step 4)
◦ Add Environment Variables (SOURCE_BUCKET: Name of your source S3 bucket and DESTINATION_BUCKET: Name of your destination S3 bucket.
• Step 6: Configure S3 Event Notification
◦ Set up an event notification in the source S3 bucket to trigger the Lambda function on new object creation events with the .txt suffix.
• Step 7: Write Lambda Function Code
• Step 8: Test the System
Code:TextToSpeechFunction.py
import boto3
import json
import os
import logging
# Set up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
# Initialize S3 and Polly clients
s3 = boto3.client('s3')
polly = boto3.client('polly')
# Get the bucket names from environment variables
source_bucket = os.environ['SOURCE_BUCKET']
destination_bucket = os.environ['DESTINATION_BUCKET']
# Get the object key from the event
text_file_key = event['Records'][0]['s3']['object']['key']
audio_key = text_file_key.replace('.txt', '.mp3')
try:
# Retrieve text from the source S3 bucket
logger.info(f"Retrieving text file from bucket: {source_bucket}, key: {text_file_key}")
text_file = s3.get_object(Bucket=source_bucket, Key=text_file_key)
text = text_file['Body'].read().decode('utf-8')
# Send text to Polly
logger.info(f"Sending text to Polly for synthesis")
response = polly.synthesize_speech(
Text=text,
OutputFormat='mp3',
VoiceId='Joanna' # Choose the voice you prefer
)
# Save the audio file to the destination S3 bucket
if 'AudioStream' in response:
temp_audio_path = '/tmp/audio.mp3'
with open(temp_audio_path, 'wb') as file:
file.write(response['AudioStream'].read())
logger.info(f"Uploading audio file to bucket: {destination_bucket}, key: {audio_key}")
s3.upload_file(temp_audio_path, destination_bucket, audio_key)
logger.info(f"Text-to-Speech conversion completed successfully for file: {text_file_key}")
return {
'statusCode': 200,
'body': json.dumps('Text-to-Speech conversion completed successfully!')
}
except Exception as e:
logger.error(f"Error processing file {text_file_key} from bucket {source_bucket}: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps('An error occurred during the Text-to-Speech conversion.')
}