Skip to content

Translate text to speech with language of choice using AWS AppSync Amazon Polly, Amazon Translate, & AWS Lambda

Notifications You must be signed in to change notification settings

dabit3/appsync-web-translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AppSync React Translator

Text to audio translation using AWS AppSync, React, Amazon Polly, Amazon Translate, & AWS Lambda

Getting started

  1. Clone the project & change into the new directory
git clone https://github.com/dabit3/appsync-web-translator.git

cd appsync-web-translator
  1. Install dependencies
npm install
  1. Initialize a new AWS Amplify project
amplify init
  1. Add auth, storage, & AppSync services
amplify add auth

amplify add api

amplify add storage

amplify add function

amplify push
  1. Update the AppSync Schema in your dashboard to the following:
type Query {
	getTranslatedSentence(sentence: String!, code: String!): TranslatedSentence
}

type TranslatedSentence {
	sentence: String!
}
  1. Update the getTranslatedSentence resolver to the following:
Request mapping template
{
    "version" : "2017-02-28",
    "operation": "Invoke",
    "payload": $util.toJson($context.args)
}
Response mapping template
$util.toJson($context.result)
  1. Add the new Lambda function as a data source. Update the getTranslatedSentence resolver data source to use the Lambda function as the data source.

  2. Update the Lambda function code to the following (make sure to replace the bucket name with your bucket name):

const AWS = require('aws-sdk')
AWS.config.update({region: 'us-east-2'})
const uuidV4 = require('uuid/v4')
const translate = new AWS.Translate();
const polly = new AWS.Polly();
const s3 = new AWS.S3({
  params: {
    Bucket: '<YOURBUCKETNAME>',
  }
})

exports.handler = (event, context, callback) => {
  // Step 1: translate the text
  let message = ''
  const 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',
      'ar': 'Zeina',
      'ru': 'Tatyana',
      'hi': 'Aditi',
      'it': 'Carla'
    }

    const voice = voices[event.code]

    const 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()
        const params2 = {
          Key: 'public/' + 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 })
          }
        });
      }
    });
  });  
};
  1. Add permissions to Lambda role for Polly, Translate as well as S3

About

Translate text to speech with language of choice using AWS AppSync Amazon Polly, Amazon Translate, & AWS Lambda

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published