Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Binary file added speech/grpc/resources/audio.raw
Binary file not shown.
2 changes: 1 addition & 1 deletion speech/grpc/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _gcs_uri(text):
parser = argparse.ArgumentParser()
parser.add_argument('input_uri', type=_gcs_uri)
parser.add_argument(
'--encoding', default='FLAC', choices=[
'--encoding', default='LINEAR16', choices=[
'LINEAR16', 'FLAC', 'MULAW', 'AMR', 'AMR_WB'],
help='How the audio file is encoded. See {}#L67'.format(PROTO_URL))
parser.add_argument('--sample_rate', type=int, default=16000)
Expand Down
15 changes: 10 additions & 5 deletions speech/grpc/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import time

from google.cloud.credentials import get_credentials
from google.cloud.speech.v1beta1 import cloud_speech_pb2
from google.longrunning import operations_grpc_pb2
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
Expand Down Expand Up @@ -76,7 +76,7 @@ def main(input_uri, encoding, sample_rate, language_code='en-US'):
print(operation)

# Construct a long running operation endpoint.
service = operations_grpc_pb2.beta_create_Operations_stub(channel)
service = operations_pb2.beta_create_Operations_stub(channel)

name = operation.name

Expand All @@ -85,9 +85,14 @@ def main(input_uri, encoding, sample_rate, language_code='en-US'):
print('Waiting for server processing...')
time.sleep(1)
operation = service.GetOperation(
operations_grpc_pb2.GetOperationRequest(name=name),
operations_pb2.GetOperationRequest(name=name),
DEADLINE_SECS)

if operation.error.message:
print("")
print("Operation error:")
print(operation.error)
Copy link
Contributor

Choose a reason for hiding this comment

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

What? Jon didn't wasn't like, "Please use single quotes for strings"?? WHO ARE YOU AND WHAT HAVE YOU DONE TO @jonparrott ?!? :)

I'd just combine this into one string, for brevity:

print('\nOperation error:\n{}'.format(operation.error))

We should be paid proportional to like, the inverse of lines of code :) Except then we'd be incentivized to not write any code at all... hm...

Copy link
Contributor

Choose a reason for hiding this comment

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

I blame my lack of sleep.


if operation.done:
break

Expand All @@ -112,7 +117,7 @@ def _gcs_uri(text):
parser = argparse.ArgumentParser()
parser.add_argument('input_uri', type=_gcs_uri)
parser.add_argument(
'--encoding', default='FLAC', choices=[
'--encoding', default='LINEAR16', choices=[
'LINEAR16', 'FLAC', 'MULAW', 'AMR', 'AMR_WB'],
help='How the audio file is encoded. See {}#L67'.format(
'https://github.com/googleapis/googleapis/blob/master/'
Expand Down
38 changes: 38 additions & 0 deletions speech/grpc/transcribe_async_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

from google.cloud import storage

from transcribe_async import main


def test_main(resource, capsys):

# Upload an audio file to the default testing CLOUD_STORAGE_BUCKET
storage_client = storage.Client()
bucket = storage_client.get_bucket(os.environ.get('CLOUD_STORAGE_BUCKET'))
blob = bucket.blob('audio.raw')
blob.upload_from_filename('speech/grpc/resources/audio.raw')

# Run the transcribe_async sample on the audio file, verify correct results
speech_storage_uri = 'gs://' + os.environ.get('CLOUD_STORAGE_BUCKET')
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the cloud_config or remote_resource fixture.

speech_storage_uri += '/audio.raw'
main(speech_storage_uri, 'LINEAR16', 16000)
out, err = capsys.readouterr()
assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)

# Delete audio file from storage bucket
blob.delete()
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 just have the asset on the GCS bucket already :)

But for future reference, if you're going to do this, should probably put this in a context manager (so that eg if the assertion fails, the blob is still deleted), or in one of those fancy pytest fixtures (same idea)

38 changes: 38 additions & 0 deletions speech/grpc/transcribe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

from google.cloud import storage

from transcribe_async import main


def test_main(resource, capsys):

# Upload an audio file to the default testing CLOUD_STORAGE_BUCKET
storage_client = storage.Client()
bucket = storage_client.get_bucket(os.environ.get('CLOUD_STORAGE_BUCKET'))
blob = bucket.blob('audio.raw')
blob.upload_from_filename('speech/grpc/resources/audio.raw')

# Run the transcribe sample on the audio file, verify correct results
speech_storage_uri = 'gs://' + os.environ.get('CLOUD_STORAGE_BUCKET')
speech_storage_uri += '/audio.raw'
main(speech_storage_uri, 'LINEAR16', 16000)
out, err = capsys.readouterr()
assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)

# Delete audio file from storage bucket
blob.delete()