How to get the result of a long-running Google Cloud Speech API operation later?

给你一囗甜甜゛ 提交于 2019-12-05 03:25:09

问题


Below is a snippet that calls the Google Cloud Speech API long running operation to convert an audio file to text

from google.cloud import speech
speech_client = speech.Client()

audio_sample = speech_client.sample(
    content=None,
    source_uri=gcs_uri,
    encoding='FLAC',
    sample_rate_hertz=44100)

operation = audio_sample.long_running_recognize('en-US')

retry_count = 100
while retry_count > 0 and not operation.complete:
    retry_count -= 1
    time.sleep(60)
    operation.poll()

However, as it is a long running operation, it could take a while and I ideally don't want to keep the session on while it waits. Is it possible to store some information and retrieve the result later ?


回答1:


As mentioned in another answer, you could use a separate thread to poll the operation while the main thread continues. Alternatively, you could pass operation.name of the returned operation to a separate service and have that other service handle polling. In practice the service calling the long running operation could publish operation.name to a Pub/Sub topic, for example.

Below is a possible way of retrieving a long running operation by looking it up by name:

from oauth2client.client import GoogleCredentials
from googleapiclient import discovery

credentials = GoogleCredentials.get_application_default()
speech_service = discovery.build('speech', 'v1', credentials=credentials)

operation_name = .... # operation.name

get_operation_request = speech_service.operations().get(name=operation_name)

# response is a dictionary
response = get_operation_response.execute()

# handle polling
retry_count = 100
while retry_count > 0 and not response.get('done', False):
    retry_count -= 1
    time.sleep(60)
    response = get_operation_response.execute()

When the operation is finished, the response dict might look something like the following:

{u'done': True,
 u'metadata': {u'@type': u'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata',
  u'lastUpdateTime': u'2017-06-21T19:38:14.522280Z',
  u'progressPercent': 100,
  u'startTime': u'2017-06-21T19:38:13.599009Z'},
 u'name': u'...................',
 u'response': {u'@type': u'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse',
  u'results': [{u'alternatives': [{u'confidence': 0.987629,
      u'transcript': u'how old is the Brooklyn Bridge'}]}]}}



回答2:


After reading the source, I found that GRPC has a 10 minute timeout. If you submit a large file, transcription can take over 10 minutes. The trick is to use the HTTP backend. The HTTP backend doesn't maintain a connection like GRPC, instead everytime you poll it sends a HTTP request. To use HTTP, do

speech_client = speech.Client(_use_grpc=False)




回答3:


No, there is not a way to do that. What you could do is use the threading module so it can run in the background as you have your next task run.



来源:https://stackoverflow.com/questions/43945114/how-to-get-the-result-of-a-long-running-google-cloud-speech-api-operation-later

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!