From a103f09fe6173f3acd8402f8c2a669ea3001ac6f Mon Sep 17 00:00:00 2001 From: Franklin Nunez <69214580+b-loved-dreamer@users.noreply.github.com> Date: Tue, 15 Dec 2020 12:55:12 -0800 Subject: [PATCH] feat: adds new multi region sample (#96) * I updated the comment on the transcribe_async file to reflect time limitations on local files for the long_running_recognize * I updated the comment on the transcribe_async file to reflect time limitations on local files for the long_running_recognize * docs: I updated the comment on the transcribe_async file to reflect time limitations on local files for the long_running_recognize * chore: I updated the comments on the transcribe_async file to reflect time limitations on local files for the long_running_recognize * fix: resolved conflicts pick f510e8f chore: I updated the comments on the transcribe_async file to reflect time limitations on local files for the long_running_recognize * chore: added a profanity filter sample * docs: fixed region tag * chore: updated the quickstart to gcs * feat: adds new multi-region sample * fix: migrated to speech 2.0.0 * fix: fixed lint issues * fix: deleted a duplicate line that calls the recognizer * docs: repaired region tag mismatch * chore: formatting * chore: added ] * docs: udated documentation to point to python-speech insteadof python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * docs: udated documentation to point to python-speech instead of python-docs-samples * fix: applied suggested changes * fix: applied suggested changes * fix: linting --- samples/snippets/multi_region.py | 56 +++++++++++++++++++++++++++ samples/snippets/multi_region_test.py | 21 ++++++++++ samples/snippets/profanity_filter.py | 16 +++----- samples/snippets/transcribe_async.py | 2 + 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 samples/snippets/multi_region.py create mode 100644 samples/snippets/multi_region_test.py diff --git a/samples/snippets/multi_region.py b/samples/snippets/multi_region.py new file mode 100644 index 00000000..0027912a --- /dev/null +++ b/samples/snippets/multi_region.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +# Copyright 2020 Google LLC +# +# 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. + + +def sync_recognize_with_multi_region_gcs(): + # [START speech_multi_region] + + # Imports the Google Cloud client library + from google.cloud import speech + from google.api_core import client_options + + # Instantiates a client + + # [START speech_multi_region_client] + + # Pass an additional argument, ClientOptions, to specify the new endpoint. + client_options = client_options.ClientOptions( + api_endpoint="eu-speech.googleapis.com" + ) + + client = speech.SpeechClient(client_options=client_options) + # [END speech_multi_region_client] + + # The name of the audio file to transcribe + gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw" + + audio = speech.RecognitionAudio(uri=gcs_uri) + + config = speech.RecognitionConfig( + encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, + sample_rate_hertz=16000, + language_code="en-US", + ) + + # Detects speech in the audio file + response = client.recognize(config=config, audio=audio) + + for result in response.results: + print("Transcript: {}".format(result.alternatives[0].transcript)) + # [END speech_multi_region] + + +sync_recognize_with_multi_region_gcs() diff --git a/samples/snippets/multi_region_test.py b/samples/snippets/multi_region_test.py new file mode 100644 index 00000000..b110b011 --- /dev/null +++ b/samples/snippets/multi_region_test.py @@ -0,0 +1,21 @@ +# Copyright 2020 Google LLC +# 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 multi_region + + +def test_multi_region(capsys): + multi_region.sync_recognize_with_multi_region_gcs() + out, _ = capsys.readouterr() + assert "Transcript: how old is the Brooklyn Bridge" in out diff --git a/samples/snippets/profanity_filter.py b/samples/snippets/profanity_filter.py index ca8a9a97..c5886917 100644 --- a/samples/snippets/profanity_filter.py +++ b/samples/snippets/profanity_filter.py @@ -18,17 +18,15 @@ python transcribe.py gs://cloud-samples-tests/speech/brooklyn.flac """ -import argparse - # [START speech_recognize_with_profanity_filter_gcs] -def sync_recognize_with_profanity_filter_gcs(storage_uri): +def sync_recognize_with_profanity_filter_gcs(gcs_uri): from google.cloud import speech client = speech.SpeechClient() - audio = {"uri": storage_uri} + audio = {"uri": gcs_uri} config = speech.RecognitionConfig( encoding=speech.RecognitionConfig.AudioEncoding.FLAC, @@ -46,10 +44,6 @@ def sync_recognize_with_profanity_filter_gcs(storage_uri): # [END speech_recognize_with_profanity_filter_gcs] -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - parser.add_argument("path", help="GCS path for audio file to be recognized") - args = parser.parse_args() - sync_recognize_with_profanity_filter_gcs(args.path) +sync_recognize_with_profanity_filter_gcs( + "gs://cloud-samples-tests/speech/brooklyn.flac" +) diff --git a/samples/snippets/transcribe_async.py b/samples/snippets/transcribe_async.py index b3a0fc34..b98d5516 100644 --- a/samples/snippets/transcribe_async.py +++ b/samples/snippets/transcribe_async.py @@ -48,7 +48,9 @@ def transcribe_file(speech_file): sample_rate_hertz=16000, language_code="en-US", ) + # [START speech_python_migration_async_response] + operation = client.long_running_recognize(config=config, audio=audio) # [END speech_python_migration_async_request]