From aa0bd399248bcd6680ab818ac51ab520addcd055 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Tue, 7 Feb 2017 09:53:31 -0800 Subject: [PATCH] Add speech GCS samples [(#784)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/784) --- .../samples/snippets/README.rst | 20 +++--- .../samples/snippets/transcribe.py | 48 +++++++------- .../samples/snippets/transcribe_async.py | 63 ++++++++++++------- .../samples/snippets/transcribe_async_test.py | 14 ++++- .../samples/snippets/transcribe_test.py | 14 ++++- 5 files changed, 102 insertions(+), 57 deletions(-) diff --git a/packages/google-cloud-python-speech/samples/snippets/README.rst b/packages/google-cloud-python-speech/samples/snippets/README.rst index cd798d24946b..a8a842059b66 100644 --- a/packages/google-cloud-python-speech/samples/snippets/README.rst +++ b/packages/google-cloud-python-speech/samples/snippets/README.rst @@ -93,18 +93,20 @@ To run this sample: $ python transcribe.py - usage: transcribe.py [-h] speech_file + usage: transcribe.py [-h] path Google Cloud Speech API sample application using the REST API for batch processing. - Example usage: python transcribe.py resources/audio.raw + Example usage: + python transcribe.py resources/audio.raw + python transcribe.py gs://cloud-samples-tests/speech/brooklyn.flac positional arguments: - speech_file Full path of audio file to be recognized + path File or GCS path for audio file to be recognized optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit Transcribe async @@ -118,18 +120,20 @@ To run this sample: $ python transcribe_async.py - usage: transcribe_async.py [-h] speech_file + usage: transcribe_async.py [-h] path Google Cloud Speech API sample application using the REST API for async batch processing. - Example usage: python transcribe_async.py resources/audio.raw + Example usage: + python transcribe_async.py resources/audio.raw + python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac positional arguments: - speech_file Full path of audio file to be recognized + path File or GCS path for audio file to be recognized optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit diff --git a/packages/google-cloud-python-speech/samples/snippets/transcribe.py b/packages/google-cloud-python-speech/samples/snippets/transcribe.py index 829cec5dd5da..fbf57b2019fd 100644 --- a/packages/google-cloud-python-speech/samples/snippets/transcribe.py +++ b/packages/google-cloud-python-speech/samples/snippets/transcribe.py @@ -17,7 +17,9 @@ """Google Cloud Speech API sample application using the REST API for batch processing. -Example usage: python transcribe.py resources/audio.raw +Example usage: + python transcribe.py resources/audio.raw + python transcribe.py gs://cloud-samples-tests/speech/brooklyn.flac """ # [START import_libraries] @@ -26,44 +28,48 @@ # [END import_libraries] -def main(speech_file): - """Transcribe the given audio file. - - Args: - speech_file: the name of the audio file. - """ - # [START authenticating] - # Application default credentials provided by env variable - # GOOGLE_APPLICATION_CREDENTIALS +def transcribe_file(speech_file): + """Transcribe the given audio file.""" from google.cloud import speech speech_client = speech.Client() - # [END authenticating] - # [START construct_request] - # Loads the audio into memory with io.open(speech_file, 'rb') as audio_file: content = audio_file.read() audio_sample = speech_client.sample( - content, + content=content, source_uri=None, encoding='LINEAR16', sample_rate=16000) - # [END construct_request] - # [START send_request] alternatives = speech_client.speech_api.sync_recognize(audio_sample) for alternative in alternatives: print('Transcript: {}'.format(alternative.transcript)) - # [END send_request] -# [START run_application] +def transcribe_gcs(gcs_uri): + """Transcribes the audio file specified by the gcs_uri.""" + from google.cloud import speech + speech_client = speech.Client() + + audio_sample = speech_client.sample( + content=None, + source_uri=gcs_uri, + encoding='FLAC', + sample_rate=16000) + + alternatives = speech_client.speech_api.sync_recognize(audio_sample) + for alternative in alternatives: + print('Transcript: {}'.format(alternative.transcript)) + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( - 'speech_file', help='Full path of audio file to be recognized') + 'path', help='File or GCS path for audio file to be recognized') args = parser.parse_args() - main(args.speech_file) - # [END run_application] + if args.path.startswith('gs://'): + transcribe_gcs(args.path) + else: + transcribe_file(args.path) diff --git a/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py b/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py index 73abae717ac9..d7a8fce05edb 100644 --- a/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py +++ b/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py @@ -17,31 +17,21 @@ """Google Cloud Speech API sample application using the REST API for async batch processing. -Example usage: python transcribe_async.py resources/audio.raw +Example usage: + python transcribe_async.py resources/audio.raw + python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac """ -# [START import_libraries] import argparse import io import time -# [END import_libraries] -def main(speech_file): - """Transcribe the given audio file asynchronously. - - Args: - speech_file: the name of the audio file. - """ - # [START authenticating] - # Application default credentials provided by env variable - # GOOGLE_APPLICATION_CREDENTIALS +def transcribe_file(speech_file): + """Transcribe the given audio file asynchronously.""" from google.cloud import speech speech_client = speech.Client() - # [END authenticating] - # [START construct_request] - # Loads the audio into memory with io.open(speech_file, 'rb') as audio_file: content = audio_file.read() audio_sample = speech_client.sample( @@ -49,9 +39,7 @@ def main(speech_file): source_uri=None, encoding='LINEAR16', sample_rate=16000) - # [END construct_request] - # [START send_request] operation = speech_client.speech_api.async_recognize(audio_sample) retry_count = 100 @@ -61,7 +49,7 @@ def main(speech_file): operation.poll() if not operation.complete: - print("Operation not complete and retry limit reached.") + print('Operation not complete and retry limit reached.') return alternatives = operation.results @@ -71,13 +59,44 @@ def main(speech_file): # [END send_request] -# [START run_application] +def transcribe_gcs(gcs_uri): + """Asynchronously transcribes the audio file specified by the gcs_uri.""" + from google.cloud import speech + speech_client = speech.Client() + + audio_sample = speech_client.sample( + content=None, + source_uri=gcs_uri, + encoding='FLAC', + sample_rate=16000) + + operation = speech_client.speech_api.async_recognize(audio_sample) + + retry_count = 100 + while retry_count > 0 and not operation.complete: + retry_count -= 1 + time.sleep(2) + operation.poll() + + if not operation.complete: + print('Operation not complete and retry limit reached.') + return + + alternatives = operation.results + for alternative in alternatives: + print('Transcript: {}'.format(alternative.transcript)) + print('Confidence: {}'.format(alternative.confidence)) + # [END send_request_gcs] + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( - 'speech_file', help='Full path of audio file to be recognized') + 'path', help='File or GCS path for audio file to be recognized') args = parser.parse_args() - main(args.speech_file) - # [END run_application] + if args.path.startswith('gs://'): + transcribe_gcs(args.path) + else: + transcribe_file(args.path) diff --git a/packages/google-cloud-python-speech/samples/snippets/transcribe_async_test.py b/packages/google-cloud-python-speech/samples/snippets/transcribe_async_test.py index d90f45608c8b..6142d43db96b 100644 --- a/packages/google-cloud-python-speech/samples/snippets/transcribe_async_test.py +++ b/packages/google-cloud-python-speech/samples/snippets/transcribe_async_test.py @@ -13,11 +13,19 @@ import re -from transcribe_async import main +import transcribe_async -def test_main(resource, capsys): - main(resource('audio.raw')) +def test_transcribe(resource, capsys): + transcribe_async.transcribe_file(resource('audio.raw')) + out, err = capsys.readouterr() + + assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I) + + +def test_transcribe_gcs(resource, capsys): + transcribe_async.transcribe_gcs( + 'gs://python-docs-samples-tests/speech/audio.flac') out, err = capsys.readouterr() assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I) diff --git a/packages/google-cloud-python-speech/samples/snippets/transcribe_test.py b/packages/google-cloud-python-speech/samples/snippets/transcribe_test.py index c8cb0a703331..5940fc7f9862 100644 --- a/packages/google-cloud-python-speech/samples/snippets/transcribe_test.py +++ b/packages/google-cloud-python-speech/samples/snippets/transcribe_test.py @@ -13,11 +13,19 @@ import re -from transcribe import main +import transcribe -def test_main(resource, capsys): - main(resource('audio.raw')) +def test_transcribe_file(resource, capsys): + transcribe.transcribe_file(resource('audio.raw')) + out, err = capsys.readouterr() + + assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I) + + +def test_transcribe_gcs(resource, capsys): + transcribe.transcribe_gcs( + 'gs://python-docs-samples-tests/speech/audio.flac') out, err = capsys.readouterr() assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)