forked from brianklaas/awsPlaybox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Includes new transcribe, translate, and speak Step Functions workflow.
- Loading branch information
1 parent
5e2d794
commit b19ef9b
Showing
23 changed files
with
681 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.project | ||
.DS_Store |
This file contains 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
This file contains 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
Binary file not shown.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 30 additions & 0 deletions
30
nodejs/lambda/transcribeTranslateExample/checkTranscribeJobStatus.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const util = require('util'); | ||
const AWS = require('aws-sdk'); | ||
const transcribe = new AWS.TranscribeService; | ||
|
||
exports.handler = (event, context, callback) => { | ||
console.log("Reading input from event:\n", util.inspect(event, {depth: 5})); | ||
|
||
var jobName = event.jobName; | ||
|
||
var params = { | ||
TranscriptionJobName: jobName | ||
} | ||
|
||
var request = transcribe.getTranscriptionJob(params, function(err, data) { | ||
if (err) { // an error occurred | ||
console.log(err, err.stack); | ||
callback(err, null); | ||
} else { | ||
console.log(data); // successful response, return job status | ||
var returnData = { | ||
jobName: jobName, | ||
jobStatus: data.TranscriptionJob.TranscriptionJobStatus, | ||
transcriptFileUri: data.TranscriptionJob.Transcript.TranscriptFileUri, | ||
transcriptFileName: jobName | ||
}; | ||
callback(null, returnData); | ||
} | ||
}); | ||
|
||
}; |
88 changes: 88 additions & 0 deletions
88
nodejs/lambda/transcribeTranslateExample/convertTextToSpeech.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// As speaking jobs can take a few seconds, the default Lambda function timeout will not be enough. Set the timeout to 10 seconds to give Polly time to do the work. | ||
|
||
const util = require('util'); | ||
const AWS = require('aws-sdk'); | ||
const Polly = new AWS.Polly(); | ||
const S3 = new AWS.S3(); | ||
|
||
exports.handler = (event, context, callback) => { | ||
console.log("Reading input from event:\n", util.inspect(event, {depth: 5})); | ||
|
||
var textToSpeak = event.textToSpeak; | ||
var languageOfText = event.languageOfText; | ||
var fileNameForOutput = event.transcriptFileName; | ||
|
||
// We have to use promises for both steps in the process because S3 operations are async | ||
makeMP3FromText(textToSpeak, languageOfText).then(function(makeMP3Result) { | ||
console.log("Result from speaking transcript:", makeMP3Result); | ||
return writeFileToS3(makeMP3Result.AudioStream, languageOfText, fileNameForOutput); | ||
}).then(function(mp3FileNameOnS3) { | ||
console.log("mp3FileNameOnS3:" + mp3FileNameOnS3); | ||
var returnData = {}; | ||
returnData["mp3FileNameOnS3-"+languageOfText] = mp3FileNameOnS3 | ||
callback(null, returnData); | ||
}).catch(function(err) { | ||
console.error("Failed to generate MP3!", err); | ||
callback(err, null); | ||
}) | ||
}; | ||
|
||
function makeMP3FromText(textToSpeak, languageOfText) { | ||
return new Promise(function(resolve, reject) { | ||
console.log("Making an MP3 in the language: " + languageOfText); | ||
var voiceToUse = 'Ivy'; | ||
// Polly has a current maximum character length of 3000 characters | ||
var maxLength = 2900; | ||
var trimmedText = textToSpeak.substr(0, maxLength); | ||
switch(languageOfText) { | ||
case 'es': | ||
voiceToUse = (Math.random() >= 0.5) ? "Penelope" : "Miguel"; | ||
break; | ||
case 'fr': | ||
voiceToUse = (Math.random() >= 0.5) ? "Celine" : "Mathieu"; | ||
break; | ||
case 'de': | ||
voiceToUse = (Math.random() >= 0.5) ? "Vicki" : "Hans"; | ||
break; | ||
} | ||
var params = { | ||
OutputFormat: "mp3", | ||
SampleRate: "8000", | ||
Text: trimmedText, | ||
VoiceId: voiceToUse | ||
} | ||
var speakPromise = Polly.synthesizeSpeech(params).promise(); | ||
speakPromise.then(function(data) { | ||
console.log('Successfully generated MP3 file'); | ||
resolve(data); | ||
}).catch(function(err) { | ||
console.log("Error generating MP3 file:\n", err); | ||
reject(Error(err)); | ||
}); | ||
}); | ||
} | ||
|
||
function writeFileToS3(mp3AudioStream, languageOfText, fileNameForOutput) { | ||
return new Promise(function(resolve, reject) { | ||
let audioFileName = fileNameForOutput; | ||
// Remove .txt from the file name, if it exists | ||
if (audioFileName.split('.').pop() == 'txt') { | ||
audioFileName = audioFileName.slice(0, -4); | ||
} | ||
let filePathOnS3 = 'audio/' + audioFileName + '-' + languageOfText + '.mp3'; | ||
var params = { | ||
Bucket: 'NAME OF YOUR BUCKET WHERE YOU WANT OUTPUT TO GO', | ||
Key: filePathOnS3, | ||
Body: mp3AudioStream, | ||
ContentType: 'audio/mpeg3' | ||
}; | ||
var putObjectPromise = S3.putObject(params).promise(); | ||
putObjectPromise.then(function(data) { | ||
console.log('Successfully put audio file on S3'); | ||
resolve(filePathOnS3); | ||
}).catch(function(err) { | ||
console.log("Error putting file on S3:\n", err); | ||
reject(Error(err)); | ||
}); | ||
}); | ||
} |
70 changes: 70 additions & 0 deletions
70
nodejs/lambda/transcribeTranslateExample/getTranscriptionFile.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const util = require('util'); | ||
const AWS = require('aws-sdk'); | ||
const https = require('https'); | ||
const S3 = new AWS.S3(); | ||
|
||
exports.handler = (event, context, callback) => { | ||
console.log("Reading input from event:\n", util.inspect(event, {depth: 5})); | ||
|
||
// Note: The authenticated URL that Transcribe provides to you only stays valid for a couple minutes | ||
var transcriptFileUri = event.transcriptFileUri; | ||
var transcriptFileName = event.transcriptFileName; | ||
|
||
// We have to use promises for both steps in the process because S3 operations are async | ||
getTranscript(transcriptFileUri).then(function(getTranscriptResponse) { | ||
console.log("Retrieved transcript:", getTranscriptResponse); | ||
return writeTranscriptToS3(getTranscriptResponse,transcriptFileName); | ||
}).then(function(filePathOnS3) { | ||
console.log("filePathOnS3 is " + filePathOnS3); | ||
var returnData = { | ||
transcriptFilePathOnS3: filePathOnS3, | ||
transcriptFileName: transcriptFileName | ||
}; | ||
callback(null, returnData); | ||
}).catch(function(err) { | ||
console.error("Failed to write transcript file!", err); | ||
callback(err, null); | ||
}) | ||
}; | ||
|
||
function getTranscript(transcriptFileUri) { | ||
return new Promise(function(resolve, reject) { | ||
https.get(transcriptFileUri, res => { | ||
res.setEncoding("utf8"); | ||
let body = ""; | ||
res.on("data", data => { | ||
body += data; | ||
}); | ||
res.on("end", () => { | ||
body = JSON.parse(body); | ||
let transcript = body.results.transcripts[0].transcript; | ||
console.log("Here's the transcript:\n", transcript); | ||
resolve(transcript); | ||
}); | ||
res.on("error", (err) => { | ||
console.log("Error getting transcript:\n", err); | ||
reject(Error(err)); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function writeTranscriptToS3(transcript,transcriptFileName) { | ||
return new Promise(function(resolve, reject) { | ||
console.log("Writing transcript to S3 with the name" + transcriptFileName); | ||
let filePathOnS3 = 'transcripts/' + transcriptFileName + '.txt'; | ||
var params = { | ||
Bucket: 'NAME OF YOUR BUCKET WHERE YOU WANT OUTPUT TO GO', | ||
Key: filePathOnS3, | ||
Body: transcript | ||
}; | ||
var putObjectPromise = S3.putObject(params).promise(); | ||
putObjectPromise.then(function(data) { | ||
console.log('Successfully put transcript file on S3'); | ||
resolve(filePathOnS3); | ||
}).catch(function(err) { | ||
console.log("Error putting file on S3:\n", err); | ||
reject(Error(err)); | ||
}); | ||
}); | ||
} |
16 changes: 16 additions & 0 deletions
16
nodejs/lambda/transcribeTranslateExample/prepTranslatedTextForSpeech.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// This function simply translates variable names between the cfdemoTranslateText and cfDemoConvertTextToSpeech functions because Step Functions states language can't do that. | ||
|
||
exports.handler = (event, context, callback) => { | ||
|
||
var textToSpeak = event.translatedText; | ||
var languageOfText = event.languageOfText; | ||
var transcriptFileName = event.sourceTranscriptFileName; | ||
|
||
var returnData = { | ||
textToSpeak: textToSpeak, | ||
languageOfText: languageOfText, | ||
transcriptFileName: transcriptFileName | ||
} | ||
|
||
callback(null, returnData); | ||
}; |
Oops, something went wrong.