微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何序列化和反序列化 `longRunningRecognize` 操作以在稍后获得其结果?

如何解决如何序列化和反序列化 `longRunningRecognize` 操作以在稍后获得其结果?

我正在使用 firebase 云函数通过 the example code for longRunningRecognize 转录用户上传的音频文件

// 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();

代码适用于可以比 9-minute firebase cloud function maximum execution limit 更快地转录的短音频文件,但是 1) 我的许多 ~ 小时长的用户上传文件不能那么快地转录,并且 2) 它拥有一个云函数 getting billed for each tenth of a second it's running 只是坐在那里等待 API 响应似乎很浪费。

我认为这里明显的解决方法是让 Google's Speech-to-Text API 支持网络钩子。

在此之前,我如何序列化和反序列化 SpeechClient operation 以便稍后从 scheduled function 获得此转录作业的结果?

具体来说,我正在寻找类似于本示例中的组合 SERIALIZEDESERIALIZE 函数的东西:

// start speech recognition job:
const [operation] = await client.longRunningRecognize(request);
const serializedOperation = operation.SERIALIZE();
db.doc("jobs/job1").set(serializedOperation);

// get the result later in a scheduled function:
const snap = await db.doc("jobs/job1").get();
const serializedOperation = snap.data();
const operation = DESERIALIZE(serializedOperation);
const [response] = await operation.promise();

解决方法

LongRunningRecognize 返回一个操作。 Operation 名称是唯一的。

您可以将操作名称保存在某处,然后稍后调用 GetOperation

,

感谢 Brendan 提供指向 GetOperation 的指针——这是我需要弄清楚的关键。

序列化 operation 非常简单:只需调用 operation.name 即可获得操作的唯一 ID。

使用 operationName 节点库反序列化 @google-cloud/speech 非常令人沮丧,无法弄清楚该怎么做,但我终于想通了。

要检查 Operation 的状态并从 operation.name 获取其结果,请像这样使用 client.checkLongRunningRecognizeProgress(operation.name)

const operation = await client.checkLongRunningRecognizeProgress(operationName);
if(operation.done) {
  console.log(JSON.stringify(operation.result));
} else {
  const {progressPercent,startTime,lastUpdateTime} = op.metadata;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。