I'm trying to use the Google Speech to Text API, so I'm trying to implement it in a Cloud function (Cloud Function API). The thing is that I want to call that function from a javascript file that is running the code for a part of a website that creates voice-generated orders. So then I have 3 issues:
· I don't know how to make the call to the URL and send the parameters the function needs at the same time. I'm guessing a POST. The data is something like:
const data = {
audio: base64AudioFormat,
lan: language
}
· Then once in the Cloud function, I don't know how to modify the Google given code so it works in this situation. The code given is this:
// Imports the Google Cloud client library
const fs = require('fs');
const speech = require('@google-cloud/speech');
// Creates a client
const client = new speech.SpeechClient();
const config = {
encoding: LINEAR16,
sampleRateHertz: 16000,
languageCode: (my 'lan' parameter from data),
};
const audio = {
content: (my 'base64AudioFormat' parameter from data),
};
const request = {
config: config,
audio: audio,
};
// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log('Transcription: ', transcription);
· And finally I want to receive the string that the API returns in my javascript file. Which I'm guessing a GET.
I'd appreciate any help!
I used your code to be used in cloud functions. This cloud function will accept a request with data { "audio": "base64 format data", "lan": "en-US"}
. The request parameters will be put int req.body.audio
and req.body.lan
and you can now use them in your code.
For testing, I used the sample data in quickstart of speech to text. Cloud function uses Node.js14.
index.js:
exports.speechToText = async (req, res) => {
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
// Creates a client
const client = new speech.SpeechClient();
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: req.body.lan,
};
const audio = {
content: req.body.audio,
};
const request = {
config: config,
audio: audio,
};
// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log('Transcription: ', transcription);
res.status(200).send(transcription);
};
package.json:
{
"dependencies": {
"@google-cloud/speech": "^4.5.1"
},
"name": "sample-http",
"version": "0.0.1"
}
Here is the request and response when I tested the code in cloud function:
See Cloud function Triggers if you want to see the different ways to trigger a function.