Skip to content

Commit

Permalink
Add speech GCS samples [(#784)](GoogleCloudPlatform/python-docs-sampl…
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss authored and busunkim96 committed Sep 3, 2020
1 parent 3e7d3af commit e6974c0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 57 deletions.
20 changes: 12 additions & 8 deletions google-cloud-speech/samples/snippets/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 27 additions & 21 deletions google-cloud-speech/samples/snippets/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
63 changes: 41 additions & 22 deletions google-cloud-speech/samples/snippets/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,29 @@
"""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(
content,
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
Expand All @@ -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
Expand All @@ -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)
14 changes: 11 additions & 3 deletions google-cloud-speech/samples/snippets/transcribe_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 11 additions & 3 deletions google-cloud-speech/samples/snippets/transcribe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e6974c0

Please sign in to comment.