Skip to content

Commit

Permalink
Speech model selection [(#1361)](GoogleCloudPlatform/python-docs-samp…
Browse files Browse the repository at this point in the history
…les#1361)

* add transcribe_model_selection

* add transcribe_model_selection_test

* flake
  • Loading branch information
dizcology authored and busunkim96 committed Sep 3, 2020
1 parent 92d9d94 commit bf55a12
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
Binary file not shown.
102 changes: 102 additions & 0 deletions google-cloud-speech/samples/snippets/transcribe_model_selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python

# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.

"""Google Cloud Speech API sample that demonstrates how to select the model
used for speech recognition.
Example usage:
python transcribe_model_selection.py \
resources/Google_Gnome.wav --model video
python transcribe_model_selection.py \
gs://cloud-samples-tests/speech/Google_Gnome.wav --model video
"""

import argparse


# [START speech_transcribe_model_selection]
def transcribe_model_selection(speech_file, model):
"""Transcribe the given audio file synchronously with
the selected model."""
from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()

with open(speech_file, 'rb') as audio_file:
content = audio_file.read()

audio = speech.types.RecognitionAudio(content=content)

config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
model=model)

response = client.recognize(config, audio)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print('-' * 20)
print('First alternative of result {}'.format(i))
print('Transcript: {}'.format(alternative.transcript))
# [END speech_transcribe_model_selection]


# [START speech_transcribe_model_selection_gcs]
def transcribe_model_selection_gcs(gcs_uri, model):
"""Transcribe the given audio file asynchronously with
the selected model."""
from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()

audio = speech.types.RecognitionAudio(uri=gcs_uri)

config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
model=model)

operation = client.long_running_recognize(config, audio)

print('Waiting for operation to complete...')
response = operation.result(timeout=90)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print('-' * 20)
print('First alternative of result {}'.format(i))
print('Transcript: {}'.format(alternative.transcript))
# [END speech_transcribe_model_selection_gcs]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'path', help='File or GCS path for audio file to be recognized')
parser.add_argument(
'--model', help='The speech recognition model to use',
choices=['command_and_search', 'phone_call', 'video', 'default'],
default='default')

args = parser.parse_args()

if args.path.startswith('gs://'):
transcribe_model_selection_gcs(args.path, args.model)
else:
transcribe_model_selection(args.path, args.model)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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

import transcribe_model_selection

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')


def test_transcribe_model_selection_file(capsys):
transcribe_model_selection.transcribe_model_selection(
os.path.join(RESOURCES, 'Google_Gnome.wav'), 'video')
out, err = capsys.readouterr()

assert re.search(r'the weather outside is sunny', out, re.DOTALL | re.I)


def test_transcribe_model_selection_gcs(capsys):
transcribe_model_selection.transcribe_model_selection_gcs(
'gs://cloud-samples-tests/speech/Google_Gnome.wav', 'video')
out, err = capsys.readouterr()

assert re.search(r'the weather outside is sunny', out, re.DOTALL | re.I)

0 comments on commit bf55a12

Please sign in to comment.