Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions speech/grpc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing google-auth

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

requests==2.12.4
google-auth==0.5.0
41 changes: 17 additions & 24 deletions speech/grpc/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,38 @@

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
SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'


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
Expand All @@ -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)
Expand Down
33 changes: 13 additions & 20 deletions speech/grpc/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,34 @@
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
SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'


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
Expand All @@ -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

Expand Down
84 changes: 39 additions & 45 deletions speech/grpc/transcribe_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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__':
Expand Down