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

How do serialize and deserialize a `longRunningRecognize` operation to get its result later?

我正在使用 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, but 1) many of my ~hour-long user-uploaded files don't get transcribed that quickly, and 2) it seems wasteful to have a cloud function getting billed for each tenth of a second it's running 只是坐等 API 响应更快的短音频文件。

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

在此之前,我如何序列化和反序列化 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 returns an Operation. Operation 名称是唯一的。

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

谢谢 Brendan 指点 GetOperation——这是我解决这个问题的关键。

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

使用 @google-cloud/speech 节点库反序列化一个 operationName 非常难以弄清楚该怎么做,但我终于弄明白了。

要检查 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;
}