【问题标题】:How to resume Google Cloud Speech API (longRunningRecognize) timeout on Cloud Functions如何在 Cloud Functions 上恢复 Google Cloud Speech API (longRunningRecognize) 超时
【发布时间】:2019-08-14 09:59:33
【问题描述】:

我正在尝试创建一个应用程序来使用云功能和云语音 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() 丢失。我该如何解决这个问题?

【问题讨论】:

    标签: javascript google-cloud-platform google-cloud-functions google-speech-api


    【解决方案1】:

    如果您的工作需要超过 540 秒,Cloud Functions 并不是解决此问题的最佳解决方案。相反,您可能需要考虑将 Cloud Functions 仅用作触发机制,然后使用 pubsub 将工作卸载到 App Engine 或 Compute Engine 以向其发送相关数据(例如文件在 Cloud Storage 中的位置,以及其他所需的元数据)发出识别语音的请求。

    【讨论】:

      【解决方案2】:

      这里是怎么做的(代码是PHP的,但是idea类是一样的)

      $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 相反)

      否则响应将被 protobuf 编码 (https://github.com/googleapis/google-cloud-php-speech/blob/master/src/V1/Gapic/SpeechGapicClient.php#L135)

      这个答案帮助找到了最终的解决方案https://stackoverflow.com/a/57209441/932473

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-15
        相关资源
        最近更新 更多