-
-
Save jforge/b12570d744d7a1b8b45a7410ed8c39b1 to your computer and use it in GitHub Desktop.
Lambda function to interact with Amazon Polly & Amazon Translate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var AWS = require('aws-sdk') | |
| AWS.config.update({region: 'us-east-1'}) | |
| const uuidV4 = require('uuid/v4') | |
| var translate = new AWS.Translate(); | |
| var polly = new AWS.Polly(); | |
| var s3 = new AWS.S3({ | |
| params: { | |
| Bucket: 'YOURBUCKETNAME', | |
| } | |
| }) | |
| exports.handler = (event, context, callback) => { | |
| // Step 1: translate the text | |
| let message = '' | |
| var translateParams = { | |
| SourceLanguageCode: 'en', | |
| TargetLanguageCode: event.code, | |
| Text: event.sentence | |
| } | |
| translate.translateText(translateParams, function (err, data) { | |
| if (err) callback(err) | |
| message = data.TranslatedText | |
| const voices = { | |
| 'es': 'Penelope', | |
| 'pt': 'Vitoria', | |
| 'de': 'Vicki', | |
| 'en': 'Joanna', | |
| 'fr': 'Celine' | |
| } | |
| const voice = voices[event.code] | |
| var pollyParams = { | |
| OutputFormat: "mp3", | |
| SampleRate: "8000", | |
| Text: message, | |
| TextType: "text", | |
| VoiceId: voice | |
| }; | |
| // step 2: synthesize the translation into speech | |
| polly.synthesizeSpeech(pollyParams, function(err, data) { | |
| if (err) console.log(err, err.stack); // an error occurred | |
| else { | |
| let key = uuidV4() | |
| var params2 = { | |
| Key: key, | |
| ContentType: 'audio/mpeg', | |
| Body: data.AudioStream, | |
| ACL: 'public-read' | |
| }; | |
| s3.putObject(params2, function(err, data) { | |
| if (err) { | |
| callback('error putting item: ', err) | |
| } else { | |
| callback(null, { sentence: key }) | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment