gpt4 book ai didi

javascript - 从 Google Cloud Function 检查 Google Speech API 操作

转载 作者:行者123 更新时间:2023-11-30 11:19:41 24 4
gpt4 key购买 nike

我正在使用 Google Speech API 转录长文件。该 API 是从 Google Cloud Functions 调用的。 我想查看 longRunningRecognize 的结果后来与operations.get 。我知道 name/id操作,但我找不到通过操作名称从 Google Cloud Function 检查操作状态的好方法。

当然,我可以向这个 url 发出一个 GET HTTP 请求:

https://speech.googleapis.com/v1/operations/{name}?key=API_KEY 

这是一个有效的示例代码:

const functions = require('firebase-functions');
const speech = require('@google-cloud/speech');
const request = require('request');

exports.transcribe = functions.storage.object().onFinalize((object) => {
// some code to get data required for speech API
const payload = {
audio: {
uri: 'some_uri/to/google/storage/file'
},
config: {
encoding: 'FLAC',
languageCode: 'en-US'
}
};

const client = new speech.SpeechClient({
projectId: 'my-project-id'
});

client.longRunningRecognize(payload)
.then(responses => {
const operation = responses[0];
// current example of getting operation status by operation name with HTTP call
request(`https://speech.googleapis.com/v1/operations/${operation.latestResponse.name}?key=MY-API-KEY`, (error, response, body) => {
console.log('Operation status response: ', body);
});
});
});

但似乎应该有更明确的方法来做到这一点。至少我能找到这个 ruby way of getting operation status和这个 OperationsClient 的描述,所以我想要这样的东西来检查状态:

// this line is the most confusing part of the puzzle
const client = longrunning.operationsClient();
const name = '';
client.getOperation({name: name}).then(function(responses) {
var response = responses[0];
// doThingsWith(response)
});

感谢您的帮助!

最佳答案

我怀疑你已经继续前进,但我会为了其他人而回答。

当您运行longRunningRecognize 方法时,SDK 开始为您轮询 longrunning operations.get。您只需要使用 .on 设置 Node 事件监听器。

Operation 对象(从 longRunningRecognize 返回的 promise 的第一个数组元素)在 progresscompleteerror 上发出 Node 事件

OP 代码的更新:

client.longRunningRecognize(payload)
.then(responses => {
const operation = responses[0];
operation.on('progress', (metadata, apiResponse) => {
console.log(JSON.stringify(metadata))
});
});

示例输出:(与 https://speech.googleapis.com/v1/operations/... 相同)

{"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629182","nanos":661910000}}
{"progressPercent":26,"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629245","nanos":48465000}}
{"progressPercent":52,"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629307","nanos":516891000}}
{"progressPercent":78,"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629369","nanos":680341000}}

请注意,没有明显的方法可以通过异步操作获取部分转录的文本,只有状态百分比。

关于javascript - 从 Google Cloud Function 检查 Google Speech API 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50278696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com