如何在以后获得长期运行的Google Cloud Speech API操作的结果?

时间:2017-05-12 19:26:02

标签: python google-cloud-speech

以下是调用Google Cloud Speech API长时间运行操作将音频文件转换为文本的代码段

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

但是,因为它是一个长时间运行的操作,它可能需要一段时间,我理想情况下不希望在等待时保持会话。是否可以存储一些信息并在以后检索结果?

3 个答案:

答案 0 :(得分:2)

阅读完源代码后,我发现GRPC有10分钟超时。如果您提交大文件,转录可能需要10分钟以上。诀窍是使用HTTP后端。 HTTP后端不像GRPC那样维护连接,而是每次轮询它都会发送HTTP请求。要使用HTTP,请执行

speech_client = speech.Client(_use_grpc=False)

答案 1 :(得分:2)

如另一个答案所述,您可以使用单独的线程在主线程继续时轮询操作。或者,您可以将返回操作的operation.name传递给单独的服务,并让其他服务处理轮询。实际上,调用长时间运行操作的服务可以将operation.name发布到Pub / Sub主题,例如。

以下是通过名称查找来检索长时间运行操作的可能方法:

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

操作完成后,response dict可能如下所示:

{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 :(得分:-1)

不,没有办法做到这一点。你可以做的是使用线程模块,这样它就可以在你下一个任务运行时在后台运行。