Рет қаралды 1,869
Video will help us to understand the concept of AWS Translate with one of the production use case. Below pointers covered:-
- Concept
- Demo:- Translating documents with Amazon Translate, AWS Lambda
Lambda Function:-
=================
import json
import boto3
s3_client = boto3.client(service_name='s3')
translate = boto3.client('translate')
def translate_text(text, lang_code):
result = translate.translate_text(
Text=text,
SourceLanguageCode='auto',
TargetLanguageCode=lang_code
)
return result['TranslatedText']
def lambda_handler(event, context):
file_name = event['Records'][0]['s3']['object']['key']
bucketName=event['Records'][0]['s3']['bucket']['name']
outfile="s3://outtransbucket/{}".format(file_name)
print("Event details : ",event)
print("Input File Name : ",file_name)
print("Input Bucket Name : ",bucketName)
print("Output File Name : ",outfile)
get S3 object
result = s3_client.get_object(Bucket=bucketName, Key=file_name)
#Read a text file line by line using splitlines object
final_document_array = ""
for line in result["Body"].read().splitlines():
each_line = line.decode('utf-8')
print("Input Line : ",each_line)
if(each_line!=''):
translated=translate_text(each_line, 'hi')
print("After translation : ",translated)
final_document_array+=translated
final_document_array+='
'
s3_client.put_object(Body=final_document_array, Bucket='outtransbucket', Key=file_name)
print("Done")
=========
Supported languages and language codes:-
docs.aws.amazo...
If you like the video please like, comment, share and subscribe to the channel to get more updates on technical videos.
Channel Link:- www.youtube.co...
Join Me on the LinkedIn Group for More updates:- / 13859292
Happy Learning !!!