对云函数的Javascript请求
我试图使用Google语音到文本API,所以我试图在一个云函数(云函数API)中实现它。 问题是,我想从一个javascript文件调用该函数,该文件正在为创建语音生成订单的网站的一部分运行代码。那么我有三个问题: ·我不知道如何调用URL,同时发送函数需要的参数。我在猜一个帖子。 数据类似于:对云函数的Javascript请求,javascript,google-cloud-platform,google-speech-to-text-api,Javascript,Google Cloud Platform,Google Speech To Text Api,我试图使用Google语音到文本API,所以我试图在一个云函数(云函数API)中实现它。 问题是,我想从一个javascript文件调用该函数,该文件正在为创建语音生成订单的网站的一部分运行代码。那么我有三个问题: ·我不知道如何调用URL,同时发送函数需要的参数。我在猜一个帖子。 数据类似于: const data = { audio: base64AudioFormat, lan: language } ·一旦进入云计算功能,我不知道如何修改Google给定的代码,使其在这
const data = {
audio: base64AudioFormat,
lan: language
}
·一旦进入云计算功能,我不知道如何修改Google给定的代码,使其在这种情况下工作。
给出的代码如下:
// 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);
·最后,我希望接收API在javascript文件中返回的字符串。我猜这是个好机会
我将感谢任何帮助 我将您的代码用于云函数。此云函数将接受数据为
{“audio”:“base64格式数据”,“lan”:“en-US”}
的请求。请求参数将放在intreq.body.audio
和req.body.lan
中,您现在可以在代码中使用它们
为了进行测试,我在中使用了示例数据。云函数使用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);
};
{
"dependencies": {
"@google-cloud/speech": "^4.5.1"
},
"name": "sample-http",
"version": "0.0.1"
}
package.json:
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);
};
{
"dependencies": {
"@google-cloud/speech": "^4.5.1"
},
"name": "sample-http",
"version": "0.0.1"
}
以下是我在云函数中测试代码时的请求和响应:
查看是否要查看触发函数的不同方式。谢谢!那帮了大忙!您建议我如何从javascript请求云函数?@javierdeiosg。您可以使用javascript使用HTTP发送请求,请参见