Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speech GCS #784

Merged
merged 3 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 12 additions & 8 deletions speech/cloud-client/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 speech/cloud-client/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 speech/cloud-client/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()
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought they were going to change this interface soon.... hmm. @dhermes?

Copy link
Contributor

Choose a reason for hiding this comment

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

_OperationFuture has landed in GAX/GAPIC interfaces and we are slowly working it in

/cc @lukesneeringer

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, cool, we'll catch it when @dpebot bumps our dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll skip this for now.


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)
13 changes: 10 additions & 3 deletions speech/cloud-client/transcribe_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@

import re

from transcribe_async import main
from transcribe_async import transcribe_file, transcribe_gcs
Copy link
Contributor

Choose a reason for hiding this comment

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

Final nit: import modules, not names. Just do import transcribe_async and use transcribe_async.transcribe_file, etc.



def test_main(resource, capsys):
main(resource('audio.raw'))
def test_transcribe(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_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)
13 changes: 10 additions & 3 deletions speech/cloud-client/transcribe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +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_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)