我正在尝试创建一个应用程序来使用云函数和云语音API来转录一些wav文件。正式文档展示了如何做到这一点( https://cloud.google.com/speech-to-text/docs/async-recognize)。然而,云函数有处理时间限制(最高540秒),有些长wav文件可能会超过等待转录API的时间。我在寻找一种恢复的方式。
正式文档显示了以下代码。(我使用节点实现云功能)
// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
const [operation] = await client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();client.longRunningRecognize()发送请求并在几秒钟内返回请求信息,而operation.promise()等待转录API完成。但是,大型文件可能需要540秒以上的时间,并且进程可能会在这一行被终止。因此,不知何故,我希望在另一个进程中使用“操作”对象继续处理。我尝试将“操作”对象序列化到一个文件中,然后加载它,但是它不能包含函数,operation.promise()丢失了。我该如何解决这个问题?
发布于 2019-03-23 17:02:51
如果您的工作要花费540秒以上,云函数实际上不是解决这个问题的最佳方法。相反,您可能需要考虑使用云函数作为一种触发机制,然后将工作卸载到App或Compute Engine,使用pubsub向其发送相关数据(例如,Cloud中文件的位置,以及发出语音识别请求所需的其他元数据)。
发布于 2021-02-22 23:46:38
下面是如何做到这一点(代码在PHP中,但思想类是相同的)
$client = new SpeechClient([
'credentials' => json_decode(file_get_contents('keys.json'), true)
]);
$operation = $client->longRunningRecognize($config, $audio);
$operationName = $operation->getName()现在作业已经开始,您可以将"$operationName“保存在某个地方(比如DB中),以便在另一个进程中使用。
在另一个过程中
$client = new SpeechClient([
'credentials' => json_decode(file_get_contents('keys.json'), true)
]);
CloudSpeech::initOnce();
$newOperationResponse = $speechClient->resumeOperation($name, 'LongRunningRecognize');
if ($newOperationResponse->operationSucceeded()) {
$result = $newOperationResponse->getResult();
}
...通知:确保将"LongRunningRecognize“作为简历操作名称,而不是"longRunningRecognize”(第一个字母应为大写字母--与文档https://github.com/googleapis/google-cloud-php-speech/blob/master/src/V1/Gapic/SpeechGapicClient.php#L312相反)
否则,响应将被编码(https://github.com/googleapis/google-cloud-php-speech/blob/master/src/V1/Gapic/SpeechGapicClient.php#L135)。
这个答案有助于找到最终的解决方案-- https://stackoverflow.com/a/57209441/932473
https://stackoverflow.com/questions/55316150
复制相似问题