Google-cloud-python: Passing json_credentials_path to SpeechClient

Created on 25 Jan 2018  Â·  9Comments  Â·  Source: googleapis/google-cloud-python

Hi,
I could not find how to pass on the json_credentials_path to create a SpeechClient in the documentation. I am aware that env variable GOOGLE_APPLICATION_CREDENTIALS can be set. But is there a way to set in code to do streaming_recognise.
This is what I have been trying to do:

from google.cloud.gapic.speech.v1 import speech_client
from google.oauth2 import service_account

creds = service_account.Credentials.from_service_account_file(credentials_json)
self.client = speech_client.SpeechClient(credentials=creds)

config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=self.sample_rate,
        language_code='en-US')
streaming_config = types.StreamingRecognitionConfig(
        config=config,
         interim_results=True)

requests = (types.StreamingRecognizeRequest(audio_content=content) for content in data_gerator(self.buffer))
responses = self.client.streaming_recognize(requests, streaming_config)
 ```

Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(self.__args, *self.__kwargs)
File "/home/cbarobotics/dev/catkin_ws/src/stt/scripts/GoogleSTTClient_new.py", line 72, in stream
responses = self.client.streaming_recognize(requests, streaming_config)
File "/home/cbarobotics/.local/lib/python2.7/site-packages/google/cloud/gapic/speech/v1/speech_client.py", line 283, in streaming_recognize
return self._streaming_recognize(requests, options)
File "/home/cbarobotics/.local/lib/python2.7/site-packages/google/gax/api_callable.py", line 442, in inner
this_options = _merge_options_metadata(options, settings)
File "/home/cbarobotics/.local/lib/python2.7/site-packages/google/gax/api_callable.py", line 389, in _merge_options_metadata
kwargs = options.kwargs
AttributeError: 'StreamingRecognitionConfig' object has no attribute 'kwargs'

The docs mention to use 

from google.cloud import speech
client = speech.SpeechClient()
```
But does this 'SpeechClient' does not take credentials.

question speech p2

All 9 comments

At first glance, the problem is with streaming_config and not with the credentials that you set. I will have to look at the code further to see why you are getting the error you are getting.

That's right, i am not sure of how to create a SpeechClient for this purpose

From a quick glance at the code, it doesn't seem like the feature has been
implemented. I could be wrong though. Are you able to use another
recognition function?

On Wed, Jan 24, 2018, 5:57 PM job notifications@github.com wrote:

That's right, i am not sure of which SpeechClient to use

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/GoogleCloudPlatform/google-cloud-python/issues/4787#issuecomment-360336169,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADzDDDoLAwUpf3yMs0789JC8IGgcvdNJks5tN9-YgaJpZM4RsM4d
.

This is duplicate of https://github.com/googleapis/toolkit/issues/1718

@lukesneeringer ping on this, it's relatively important.

@g0josh in the meantime you can do:

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(filename)

client = speech_v1.SpeechClient(credentials=credentials)

My apologies, I misunderstood this.

@g0josh you should be able to construct the higher-level client google.cloud.speech.SpeechClient with the credentials object instead of using the lower-level client. Is there anything preventing you from doing that? Do you get an error?

Thank you @jonparrott, figured that out from your previous comment. Now my code is

from google.oauth2 import service_account
from google.cloud import speech_v1
from google.cloud.speech import enums
from google.cloud.speech import types

creds = service_account.Credentials.from_service_account_file(credentials_json)
self.client = speech_v1.SpeechClient(credentials=creds)
config = types.RecognitionConfig(
            encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz=self.sample_rate,
            language_code='en-US')
streaming_config = types.StreamingRecognitionConfig(
            config=config,
            interim_results=True)

requests = (types.StreamingRecognizeRequest(audio_content=content) for content in self.data_generator(self.buffer))

responses = self.client.streaming_recognize(requests, streaming_config)
self.listen_print_loop(responses)

The credentials issue seems sorted out. But...

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/cbarobotics/dev/catkin_ws/src/stt/scripts/GoogleSTTClient_new.py", line 76, in stream
    self.listen_print_loop(responses)
  File "/home/cbarobotics/dev/catkin_ws/src/stt/scripts/GoogleSTTClient_new.py", line 130, in listen_print_loop
    for response in responses:
  File "/home/cbarobotics/.local/lib/python2.7/site-packages/grpc/_channel.py", line 347, in next
    return self._next()
  File "/home/cbarobotics/.local/lib/python2.7/site-packages/grpc/_channel.py", line 338, in _next
    raise self
_Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNKNOWN, Exception iterating requests!)>

This indicates there's a problem in the thing that generates your requests. I would recommend checking out https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming.py for more details on how to construct the streaming requests.

(I'm going to go ahead and close this as the original issue is resolved, but feel free to comment if you're still having issues)

Hi @jonparrott
Fixed that by

responses = self.client.streaming_recognize(streaming_config, requests)

But now i get invalid scope error.

from google.cloud import speech_v1
from google.oauth2 import service_account

creds = service_account.Credentials.from_service_account_file(credentials_json)
self.client = speech_v1.SpeechClient(credentials=creds)
# tried this too, but same result
# self.client = speech_v1.SpeechClient(credentials=creds, scopes='https://www.googleapis.com/auth/cloud-platform')

config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=self.sample_rate,
    language_code='en-US')
streaming_config = types.StreamingRecognitionConfig(
    config=config,
    interim_results=True)

requests = (types.StreamingRecognizeRequest(audio_content=content) for content in self.data_generator(self.buffer))

responses = self.client.streaming_recognize(streaming_config, requests)

self.listen_print_loop(responses)
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/cbarobotics/dev/catkin_ws/src/stt/scripts/GoogleSTTClient_new.py", line 75, in stream
    self.listen_print_loop(responses)
  File "/home/cbarobotics/dev/catkin_ws/src/stt/scripts/GoogleSTTClient_new.py", line 121, in listen_print_loop
    for response in responses:
  File "/home/cbarobotics/.local/lib/python2.7/site-packages/grpc/_channel.py", line 347, in next
    return self._next()
  File "/home/cbarobotics/.local/lib/python2.7/site-packages/grpc/_channel.py", line 338, in _next
    raise self
_Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAUTHENTICATED, Getting metadata from plugin failed with error: ('invalid_scope: Empty or missing scope not allowed.', u'{\n  "error" : "invalid_scope",\n  "error_description" : "Empty or missing scope not allowed."\n}'))>

Passing in scopes and credentials has no effect (we should probably warn/ValueError), you'll need to do:

creds = service_account.Credentials.from_service_account_file(
    credentials_json,
    scopes=['https://www.googleapis.com/auth/cloud-platform'])

or

creds = creds.with_scopes(['https://www.googleapis.com/auth/cloud-platform'])
Was this page helpful?
0 / 5 - 0 ratings