2

I am trying to develop an application that will take an audio file and send it to Amazon Lex using the aws-sdk, specifically the lexruntime to postContent.

Currently I have audio files out on the web that I am downloading locally and then trying to put the name of that audio file in my postContent params. However, lex is returning an empty transcription and an elicit intent meaning it was not able to process my audio file correctly. Here is how I am downloading/posting to lex:

var file = fs.createWriteStream("file.wav");
var request = https.get(url, function(response) {
  response.pipe(file);
});

var params = {
    botAlias: 'prod', /* required */
    botName: 'OrderFlowers', /* required */
    //inputText: `${command}`, /* required */
    contentType: 'audio/x-l16; sample-rate=16000; channel-count=1', /*required */
    inputStream: './file.wav',
    accept: 'text/plain; charset=utf-8',
    userId: 'Test'/* required */
    //requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
    //sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
  };


lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);  
}); 

The above gets the following response from Lex:

{ contentType: 'text/plain;charset=utf-8',
  message: 'I didn\'t understand you, what would you like to do?',
  messageFormat: 'PlainText',
  dialogState: 'ElicitIntent',
  inputTranscript: '',
  audioStream: <Buffer > }

I know that the audio file gets downloaded correctly and would map to the correct Lex intent, but I do not think I am sending the audio file correctly. If it would be best to never download the audio file locally from the URL, I am fine with that approach.

Here is the amazon documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/LexRuntime.html

Any help would be appreciated.

rocketlobster
  • 690
  • 7
  • 18

1 Answers1

1

I needed to save the file in a different format, change content type and create a read stream to send to lex. Solution here:

var file = fs.createWriteStream("file.pcm");
var request = https.get(url, function(response) {
  response.pipe(file);
});

var params = {
    botAlias: 'prod', /* required */
    botName: 'OrderFlowers', /* required */
    //inputText: `${command}`, /* required */
    contentType: 'audio/lpcm; sample-rate=8000; sample-size-bits=16; channel-count=1; is-big-endian=false',
    accept: 'text/plain; charset=utf-8',
    userId: 'Test'/* required */
    //requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
    //sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
  };

var lexFileStream = fs.createReadStream("file.pcm");
params.inputStream = lexFileStream;


lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);  
}); 
rocketlobster
  • 690
  • 7
  • 18