From 6ceb52500f7764d48e6ef38f2bc5556707d9738e Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 6 Feb 2017 14:29:56 -0800 Subject: [PATCH 1/3] Adds GCS examples and tests --- speech/cloud-client/README.rst | 20 +++--- speech/cloud-client/transcribe.py | 51 ++++++++++++--- speech/cloud-client/transcribe_async.py | 65 +++++++++++++++++--- speech/cloud-client/transcribe_async_test.py | 9 ++- speech/cloud-client/transcribe_test.py | 7 +++ 5 files changed, 125 insertions(+), 27 deletions(-) diff --git a/speech/cloud-client/README.rst b/speech/cloud-client/README.rst index cd798d24946b..a8a842059b66 100644 --- a/speech/cloud-client/README.rst +++ b/speech/cloud-client/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/speech/cloud-client/transcribe.py b/speech/cloud-client/transcribe.py index 829cec5dd5da..386aad6cdc9f 100644 --- a/speech/cloud-client/transcribe.py +++ b/speech/cloud-client/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,12 +28,8 @@ # [END import_libraries] -def main(speech_file): - """Transcribe the given audio file. - - Args: - speech_file: the name of the audio file. - """ +def transcribe_file(speech_file): + """Transcribe the given audio file.""" # [START authenticating] # Application default credentials provided by env variable # GOOGLE_APPLICATION_CREDENTIALS @@ -57,13 +55,48 @@ def main(speech_file): # [END send_request] +def transcribe_gcs(gcs_uri): + """Transcribes the audio file specified by the gcs_uri.""" + # [START authenticating_gcs] + # Application default credentials provided by env variable + # GOOGLE_APPLICATION_CREDENTIALS + from google.cloud import speech + speech_client = speech.Client() + # [END authenticating] + + # [START construct_request_gcs] + audio_sample = speech_client.sample( + None, + source_uri=gcs_uri, + encoding='FLAC', + sample_rate=16000) + # [END construct_request_gcs] + + # [START send_request_gcs] + alternatives = speech_client.speech_api.sync_recognize(audio_sample) + for alternative in alternatives: + print('Transcript: {}'.format(alternative.transcript)) + # [END send_request_gcs] + + +def main(path): + """Transcribe the given audio file. + Args: + path: the name of the audio file. + """ + if path.startswith('gs://'): + transcribe_gcs(path) + else: + transcribe_file(path) + + # [START run_application] 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) + main(args.path) # [END run_application] diff --git a/speech/cloud-client/transcribe_async.py b/speech/cloud-client/transcribe_async.py index 73abae717ac9..2470bb4b4f2e 100644 --- a/speech/cloud-client/transcribe_async.py +++ b/speech/cloud-client/transcribe_async.py @@ -17,7 +17,9 @@ """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] @@ -27,12 +29,8 @@ # [END import_libraries] -def main(speech_file): - """Transcribe the given audio file asynchronously. - - Args: - speech_file: the name of the audio file. - """ +def transcribe_file(speech_file): + """Transcribe the given audio file asynchronously.""" # [START authenticating] # Application default credentials provided by env variable # GOOGLE_APPLICATION_CREDENTIALS @@ -71,13 +69,62 @@ def main(speech_file): # [END send_request] +def transcribe_gcs(gcs_uri): + """Asynchronously transcribes the audio file specified by the gcs_uri.""" + # [START authenticating_gcs] + # Application default credentials provided by env variable + # GOOGLE_APPLICATION_CREDENTIALS + from google.cloud import speech + speech_client = speech.Client() + # [END authenticating_gcs] + + # [START construct_request_gcs] + # Loads the audio into memory + audio_sample = speech_client.sample( + content=None, + source_uri=gcs_uri, + encoding='FLAC', + sample_rate=16000) + # [END construct_request_gcs] + + # [START send_request_gcs] + 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] + + +def main(path): + """Transcribe the given audio file. + Args: + path: the name of the audio file. + """ + if path.startswith('gs://'): + transcribe_gcs(path) + else: + transcribe_file(path) + + # [START run_application] 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) + main(args.path) # [END run_application] diff --git a/speech/cloud-client/transcribe_async_test.py b/speech/cloud-client/transcribe_async_test.py index d90f45608c8b..05082bcea876 100644 --- a/speech/cloud-client/transcribe_async_test.py +++ b/speech/cloud-client/transcribe_async_test.py @@ -16,8 +16,15 @@ from transcribe_async import main -def test_main(resource, capsys): +def test_transcribe(resource, capsys): main(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): + main('gs://cloud-samples-tests/speech/brooklyn.flac') + out, err = capsys.readouterr() + + assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I) diff --git a/speech/cloud-client/transcribe_test.py b/speech/cloud-client/transcribe_test.py index c8cb0a703331..50817746236f 100644 --- a/speech/cloud-client/transcribe_test.py +++ b/speech/cloud-client/transcribe_test.py @@ -21,3 +21,10 @@ def test_main(resource, capsys): out, err = capsys.readouterr() assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I) + + +def test_main_gcs(resource, capsys): + main('gs://cloud-samples-tests/speech/brooklyn.flac') + out, err = capsys.readouterr() + + assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I) From 364f7cb2830269dd2a7f69608d7d1470845e41f6 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 6 Feb 2017 16:40:51 -0800 Subject: [PATCH 2/3] Addresses review comments. --- speech/cloud-client/transcribe.py | 39 +++---------------- speech/cloud-client/transcribe_async.py | 40 +++----------------- speech/cloud-client/transcribe_async_test.py | 6 +-- speech/cloud-client/transcribe_test.py | 10 ++--- 4 files changed, 20 insertions(+), 75 deletions(-) diff --git a/speech/cloud-client/transcribe.py b/speech/cloud-client/transcribe.py index 386aad6cdc9f..fbf57b2019fd 100644 --- a/speech/cloud-client/transcribe.py +++ b/speech/cloud-client/transcribe.py @@ -30,67 +30,38 @@ def transcribe_file(speech_file): """Transcribe the given audio file.""" - # [START authenticating] - # Application default credentials provided by env variable - # GOOGLE_APPLICATION_CREDENTIALS 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] def transcribe_gcs(gcs_uri): """Transcribes the audio file specified by the gcs_uri.""" - # [START authenticating_gcs] - # Application default credentials provided by env variable - # GOOGLE_APPLICATION_CREDENTIALS from google.cloud import speech speech_client = speech.Client() - # [END authenticating] - # [START construct_request_gcs] audio_sample = speech_client.sample( - None, + content=None, source_uri=gcs_uri, encoding='FLAC', sample_rate=16000) - # [END construct_request_gcs] - # [START send_request_gcs] alternatives = speech_client.speech_api.sync_recognize(audio_sample) for alternative in alternatives: print('Transcript: {}'.format(alternative.transcript)) - # [END send_request_gcs] -def main(path): - """Transcribe the given audio file. - Args: - path: the name of the audio file. - """ - if path.startswith('gs://'): - transcribe_gcs(path) - else: - transcribe_file(path) - - -# [START run_application] if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, @@ -98,5 +69,7 @@ def main(path): parser.add_argument( 'path', help='File or GCS path for audio file to be recognized') args = parser.parse_args() - main(args.path) - # [END run_application] + if args.path.startswith('gs://'): + transcribe_gcs(args.path) + else: + transcribe_file(args.path) diff --git a/speech/cloud-client/transcribe_async.py b/speech/cloud-client/transcribe_async.py index 2470bb4b4f2e..d7a8fce05edb 100644 --- a/speech/cloud-client/transcribe_async.py +++ b/speech/cloud-client/transcribe_async.py @@ -22,24 +22,16 @@ python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac """ -# [START import_libraries] import argparse import io import time -# [END import_libraries] def transcribe_file(speech_file): """Transcribe the given audio file asynchronously.""" - # [START authenticating] - # Application default credentials provided by env variable - # GOOGLE_APPLICATION_CREDENTIALS 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( @@ -47,9 +39,7 @@ def transcribe_file(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 @@ -59,7 +49,7 @@ def transcribe_file(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,23 +61,15 @@ def transcribe_file(speech_file): def transcribe_gcs(gcs_uri): """Asynchronously transcribes the audio file specified by the gcs_uri.""" - # [START authenticating_gcs] - # Application default credentials provided by env variable - # GOOGLE_APPLICATION_CREDENTIALS from google.cloud import speech speech_client = speech.Client() - # [END authenticating_gcs] - # [START construct_request_gcs] - # Loads the audio into memory audio_sample = speech_client.sample( content=None, source_uri=gcs_uri, encoding='FLAC', sample_rate=16000) - # [END construct_request_gcs] - # [START send_request_gcs] operation = speech_client.speech_api.async_recognize(audio_sample) retry_count = 100 @@ -97,7 +79,7 @@ def transcribe_gcs(gcs_uri): 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 @@ -107,18 +89,6 @@ def transcribe_gcs(gcs_uri): # [END send_request_gcs] -def main(path): - """Transcribe the given audio file. - Args: - path: the name of the audio file. - """ - if path.startswith('gs://'): - transcribe_gcs(path) - else: - transcribe_file(path) - - -# [START run_application] if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, @@ -126,5 +96,7 @@ def main(path): parser.add_argument( 'path', help='File or GCS path for audio file to be recognized') args = parser.parse_args() - main(args.path) - # [END run_application] + if args.path.startswith('gs://'): + transcribe_gcs(args.path) + else: + transcribe_file(args.path) diff --git a/speech/cloud-client/transcribe_async_test.py b/speech/cloud-client/transcribe_async_test.py index 05082bcea876..c884a2db1307 100644 --- a/speech/cloud-client/transcribe_async_test.py +++ b/speech/cloud-client/transcribe_async_test.py @@ -13,18 +13,18 @@ import re -from transcribe_async import main +from transcribe_async import transcribe_file, transcribe_gcs def test_transcribe(resource, capsys): - main(resource('audio.raw')) + 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): - main('gs://cloud-samples-tests/speech/brooklyn.flac') + 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/speech/cloud-client/transcribe_test.py b/speech/cloud-client/transcribe_test.py index 50817746236f..cadbd40ce41e 100644 --- a/speech/cloud-client/transcribe_test.py +++ b/speech/cloud-client/transcribe_test.py @@ -13,18 +13,18 @@ import re -from transcribe import main +from transcribe import transcribe_file, transcribe_gcs -def test_main(resource, capsys): - main(resource('audio.raw')) +def test_transcribe_file(resource, capsys): + 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_main_gcs(resource, capsys): - main('gs://cloud-samples-tests/speech/brooklyn.flac') +def test_transcribe_gcs(resource, capsys): + 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) From cede16c3d8d86e8bdbe53d44a8a25f547a633c08 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Tue, 7 Feb 2017 09:41:45 -0800 Subject: [PATCH 3/3] import modules not functions --- speech/cloud-client/transcribe_async_test.py | 7 ++++--- speech/cloud-client/transcribe_test.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/speech/cloud-client/transcribe_async_test.py b/speech/cloud-client/transcribe_async_test.py index c884a2db1307..6142d43db96b 100644 --- a/speech/cloud-client/transcribe_async_test.py +++ b/speech/cloud-client/transcribe_async_test.py @@ -13,18 +13,19 @@ import re -from transcribe_async import transcribe_file, transcribe_gcs +import transcribe_async def test_transcribe(resource, capsys): - transcribe_file(resource('audio.raw')) + 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_gcs('gs://python-docs-samples-tests/speech/audio.flac') + 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/speech/cloud-client/transcribe_test.py b/speech/cloud-client/transcribe_test.py index cadbd40ce41e..5940fc7f9862 100644 --- a/speech/cloud-client/transcribe_test.py +++ b/speech/cloud-client/transcribe_test.py @@ -13,18 +13,19 @@ import re -from transcribe import transcribe_file, transcribe_gcs +import transcribe def test_transcribe_file(resource, capsys): - transcribe_file(resource('audio.raw')) + 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_gcs('gs://python-docs-samples-tests/speech/audio.flac') + 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)