Python-docs-samples: How to get cloud speech api response in python if I only have the operation name?

Created on 26 Aug 2019  路  8Comments  路  Source: GoogleCloudPlatform/python-docs-samples

scenario:
I am sending a _long_ speech recognition file for speech recognition.
operation = client.long_running_recognize(config, audio)
operation_name = operation._operation.name

Issue

There is another file where I have to use operation_name (returned by google speech API) to get back the response again.

I tried get_operation method of the "Long-Running Operations Client":

from google.api_core import operations_v1
api = operations_v1.OperationsClient()
name = ...
response = api.get_operation(name)

ref: https://stackoverflow.com/questions/53317626/python-way-of-polling-longrunning-operations-from-operation-name-in-google-cloud

but I get the following error:
----> 1 api = operations_v1.OperationsClient()
TypeError: __init__() missing 1 required positional argument: 'channel'

Most helpful comment

Got it, from:

response = api.get_operation(name)
print(response.response.type_url)  # This should be a LongRunningRecognizeResponse object
long_running_recognize_response = client.types.LongRunningRecognizeResponse()
long_running_recognize_response.ParseFromString(response.response.value)

for result in long_running_recognize_response.results:
        # The first alternative is the most likely one for this portion.
        print(u'Transcript: {}'.format(result.alternatives[0].transcript))
        print('Confidence: {}'.format(result.alternatives[0].confidence))

All 8 comments

From the SO. Looks like you also need to add the channel.

from google.cloud import speech
client = speech.SpeechClient()

from google.api_core import operations_v1
api = operations_v1.OperationsClient(client.transport.channel)
name = ...
response = api.get_operation(name)

Let me know if that doesn't fix the issue.

yes, thanks @nnegrey , this solved the problem indeed

@nnegrey the response in coming in bytes and is not even getting converted to string format ,
I not sure of encoding type too to convert it to string
I tried with utf-8 but it results in error

'utf-8' codec can't decode byte 0xdc in position 1: invalid continuation byte

Got it, from:

response = api.get_operation(name)
print(response.response.type_url)  # This should be a LongRunningRecognizeResponse object
long_running_recognize_response = client.types.LongRunningRecognizeResponse()
long_running_recognize_response.ParseFromString(response.response.value)

for result in long_running_recognize_response.results:
        # The first alternative is the most likely one for this portion.
        print(u'Transcript: {}'.format(result.alternatives[0].transcript))
        print('Confidence: {}'.format(result.alternatives[0].confidence))

Thanks again @nnegrey

I just login to thankyou @nnegrey !

I already take 3 days trying to parse this data.

Thank you very much!!!

From the SO. Looks like you also need to add the channel.

from google.cloud import speech
client = speech.SpeechClient()

from google.api_core import operations_v1
api = operations_v1.OperationsClient(client.transport.channel)
name = ...
response = api.get_operation(name)

@nnegrey I tried this but I'm getting this error. Any ideas how to solve it?

'SpeechClient' object has no attribute 'transport'
Was this page helpful?
0 / 5 - 0 ratings