From d80dc09181e982596683333cb137ab712eef2c38 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 15 Dec 2016 10:28:10 -0800 Subject: [PATCH 1/2] Update speech grpc samples to use new auth and grpc interfaces Change-Id: I1cb136817562318c2c70260430dc69cbef207876 --- speech/grpc/requirements.txt | 4 +- speech/grpc/transcribe.py | 41 ++++++-------- speech/grpc/transcribe_async.py | 33 +++++------- speech/grpc/transcribe_streaming.py | 84 ++++++++++++++--------------- 4 files changed, 71 insertions(+), 91 deletions(-) diff --git a/speech/grpc/requirements.txt b/speech/grpc/requirements.txt index fbbd12c97ef..b50b06e1c3d 100644 --- a/speech/grpc/requirements.txt +++ b/speech/grpc/requirements.txt @@ -1,5 +1,5 @@ -google-cloud-core==0.22.1 grpcio==1.0.4 PyAudio==0.2.9 -grpc-google-cloud-speech-v1beta1==0.11.1 +grpc-google-cloud-speech-v1beta1==0.14.0 six==1.10.0 +requests==2.12.4 diff --git a/speech/grpc/transcribe.py b/speech/grpc/transcribe.py index b8527bc1f51..5251630bfef 100644 --- a/speech/grpc/transcribe.py +++ b/speech/grpc/transcribe.py @@ -17,9 +17,10 @@ import argparse -from google.cloud.credentials import get_credentials -from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 as cloud_speech -from grpc.beta import implementations +import google.auth +import google.auth.transport.grpc +import google.auth.transport.requests +from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 # Keep the request alive for this many seconds DEADLINE_SECS = 60 @@ -27,35 +28,27 @@ def make_channel(host, port): - """Creates an SSL channel with auth credentials from the environment.""" - # In order to make an https call, use an ssl channel with defaults - ssl_channel = implementations.ssl_channel_credentials(None, None, None) - + """Creates a secure channel with auth credentials from the environment.""" # Grab application default credentials from the environment - creds = get_credentials().create_scoped([SPEECH_SCOPE]) - # Add a plugin to inject the creds into the header - auth_header = ( - 'Authorization', - 'Bearer ' + creds.get_access_token().access_token) - auth_plugin = implementations.metadata_call_credentials( - lambda _, cb: cb([auth_header], None), - name='google_creds') + credentials, _ = google.auth.default(scopes=[SPEECH_SCOPE]) - # compose the two together for both ssl and google auth - composite_channel = implementations.composite_channel_credentials( - ssl_channel, auth_plugin) + # Create a secure channel using the credentials. + http_request = google.auth.transport.requests.Request() + target = '{}:{}'.format(host, port) - return implementations.secure_channel(host, port, composite_channel) + return google.auth.transport.grpc.secure_authorized_channel( + credentials, http_request, target) def main(input_uri, encoding, sample_rate, language_code='en-US'): - service = cloud_speech.beta_create_Speech_stub( - make_channel('speech.googleapis.com', 443)) + service = cloud_speech_pb2.SpeechStub( + make_channel('speech.googleapis.com', 443)) + # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto - response = service.SyncRecognize(cloud_speech.SyncRecognizeRequest( - config=cloud_speech.RecognitionConfig( + response = service.SyncRecognize(cloud_speech_pb2.SyncRecognizeRequest( + config=cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB @@ -64,7 +57,7 @@ def main(input_uri, encoding, sample_rate, language_code='en-US'): # supported languages. language_code=language_code, # a BCP-47 language tag ), - audio=cloud_speech.RecognitionAudio( + audio=cloud_speech_pb2.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) diff --git a/speech/grpc/transcribe_async.py b/speech/grpc/transcribe_async.py index 5db43173b61..edecf6f7021 100644 --- a/speech/grpc/transcribe_async.py +++ b/speech/grpc/transcribe_async.py @@ -19,10 +19,11 @@ import argparse import time -from google.cloud.credentials import get_credentials +import google.auth +import google.auth.transport.grpc +import google.auth.transport.requests from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 from google.longrunning import operations_pb2 -from grpc.beta import implementations # Keep the request alive for this many seconds DEADLINE_SECS = 10 @@ -30,30 +31,22 @@ def make_channel(host, port): - """Creates an SSL channel with auth credentials from the environment.""" - # In order to make an https call, use an ssl channel with defaults - ssl_channel = implementations.ssl_channel_credentials(None, None, None) - + """Creates a secure channel with auth credentials from the environment.""" # Grab application default credentials from the environment - creds = get_credentials().create_scoped([SPEECH_SCOPE]) - # Add a plugin to inject the creds into the header - auth_header = ( - 'Authorization', - 'Bearer ' + creds.get_access_token().access_token) - auth_plugin = implementations.metadata_call_credentials( - lambda _, cb: cb([auth_header], None), - name='google_creds') + credentials, _ = google.auth.default(scopes=[SPEECH_SCOPE]) - # compose the two together for both ssl and google auth - composite_channel = implementations.composite_channel_credentials( - ssl_channel, auth_plugin) + # Create a secure channel using the credentials. + http_request = google.auth.transport.requests.Request() + target = '{}:{}'.format(host, port) - return implementations.secure_channel(host, port, composite_channel) + return google.auth.transport.grpc.secure_authorized_channel( + credentials, http_request, target) def main(input_uri, encoding, sample_rate, language_code='en-US'): channel = make_channel('speech.googleapis.com', 443) - service = cloud_speech_pb2.beta_create_Speech_stub(channel) + service = cloud_speech_pb2.SpeechStub(channel) + # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto @@ -76,7 +69,7 @@ def main(input_uri, encoding, sample_rate, language_code='en-US'): print(operation) # Construct a long running operation endpoint. - service = operations_pb2.beta_create_Operations_stub(channel) + service = operations_pb2.OperationsStub(channel) name = operation.name diff --git a/speech/grpc/transcribe_streaming.py b/speech/grpc/transcribe_streaming.py index fe7063381e3..bb8bd4dced7 100644 --- a/speech/grpc/transcribe_streaming.py +++ b/speech/grpc/transcribe_streaming.py @@ -22,10 +22,12 @@ import signal import sys -from google.cloud import credentials -from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 as cloud_speech + +import google.auth +import google.auth.transport.grpc +import google.auth.transport.requests +from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 from google.rpc import code_pb2 -from grpc.beta import implementations from grpc.framework.interfaces.face import face import pyaudio from six.moves import queue @@ -43,25 +45,16 @@ def make_channel(host, port): - """Creates an SSL channel with auth credentials from the environment.""" - # In order to make an https call, use an ssl channel with defaults - ssl_channel = implementations.ssl_channel_credentials(None, None, None) - + """Creates a secure channel with auth credentials from the environment.""" # Grab application default credentials from the environment - creds = credentials.get_credentials().create_scoped([SPEECH_SCOPE]) - # Add a plugin to inject the creds into the header - auth_header = ( - 'Authorization', - 'Bearer ' + creds.get_access_token().access_token) - auth_plugin = implementations.metadata_call_credentials( - lambda _, cb: cb([auth_header], None), - name='google_creds') + credentials, _ = google.auth.default(scopes=[SPEECH_SCOPE]) - # compose the two together for both ssl and google auth - composite_channel = implementations.composite_channel_credentials( - ssl_channel, auth_plugin) + # Create a secure channel using the credentials. + http_request = google.auth.transport.requests.Request() + target = '{}:{}'.format(host, port) - return implementations.secure_channel(host, port, composite_channel) + return google.auth.transport.grpc.secure_authorized_channel( + credentials, http_request, target) def _audio_data_generator(buff): @@ -142,7 +135,7 @@ def request_stream(data_stream, rate, interim_results=True): """ # The initial request must contain metadata about the stream, so the # server knows how to interpret it. - recognition_config = cloud_speech.RecognitionConfig( + recognition_config = cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding='LINEAR16', # raw 16-bit signed LE samples @@ -151,17 +144,17 @@ def request_stream(data_stream, rate, interim_results=True): # for a list of supported languages. language_code='en-US', # a BCP-47 language tag ) - streaming_config = cloud_speech.StreamingRecognitionConfig( + streaming_config = cloud_speech_pb2.StreamingRecognitionConfig( interim_results=interim_results, config=recognition_config, ) - yield cloud_speech.StreamingRecognizeRequest( + yield cloud_speech_pb2.StreamingRecognizeRequest( streaming_config=streaming_config) for data in data_stream: # Subsequent requests can all just have the content - yield cloud_speech.StreamingRecognizeRequest(audio_content=data) + yield cloud_speech_pb2.StreamingRecognizeRequest(audio_content=data) def listen_print_loop(recognize_stream): @@ -212,28 +205,29 @@ def listen_print_loop(recognize_stream): def main(): - with cloud_speech.beta_create_Speech_stub( - make_channel('speech.googleapis.com', 443)) as service: - # For streaming audio from the microphone, there are three threads. - # First, a thread that collects audio data as it comes in - with record_audio(RATE, CHUNK) as buffered_audio_data: - # Second, a thread that sends requests with that data - requests = request_stream(buffered_audio_data, RATE) - # Third, a thread that listens for transcription responses - recognize_stream = service.StreamingRecognize( - requests, DEADLINE_SECS) - - # Exit things cleanly on interrupt - signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel()) - - # Now, put the transcription responses to use. - try: - listen_print_loop(recognize_stream) - - recognize_stream.cancel() - except face.CancellationError: - # This happens because of the interrupt handler - pass + service = cloud_speech_pb2.SpeechStub( + make_channel('speech.googleapis.com', 443)) + + # For streaming audio from the microphone, there are three threads. + # First, a thread that collects audio data as it comes in + with record_audio(RATE, CHUNK) as buffered_audio_data: + # Second, a thread that sends requests with that data + requests = request_stream(buffered_audio_data, RATE) + # Third, a thread that listens for transcription responses + recognize_stream = service.StreamingRecognize( + requests, DEADLINE_SECS) + + # Exit things cleanly on interrupt + signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel()) + + # Now, put the transcription responses to use. + try: + listen_print_loop(recognize_stream) + + recognize_stream.cancel() + except face.CancellationError: + # This happens because of the interrupt handler + pass if __name__ == '__main__': From fb4fec7f8b798315d6e8eb9953aacbe26f828b2b Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 15 Dec 2016 11:29:07 -0800 Subject: [PATCH 2/2] Add google-auth to requirements.txt Change-Id: I517a4421fb3152fadfa75a1fc7d16279c74637a9 --- speech/grpc/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/speech/grpc/requirements.txt b/speech/grpc/requirements.txt index b50b06e1c3d..8c607a5fc22 100644 --- a/speech/grpc/requirements.txt +++ b/speech/grpc/requirements.txt @@ -3,3 +3,4 @@ PyAudio==0.2.9 grpc-google-cloud-speech-v1beta1==0.14.0 six==1.10.0 requests==2.12.4 +google-auth==0.5.0