diff --git a/packages/google-cloud-python-speech/samples/snippets/multi_region.py b/packages/google-cloud-python-speech/samples/snippets/multi_region.py new file mode 100644 index 000000000000..0027912adc9f --- /dev/null +++ b/packages/google-cloud-python-speech/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/packages/google-cloud-python-speech/samples/snippets/multi_region_test.py b/packages/google-cloud-python-speech/samples/snippets/multi_region_test.py new file mode 100644 index 000000000000..b110b0110857 --- /dev/null +++ b/packages/google-cloud-python-speech/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/packages/google-cloud-python-speech/samples/snippets/profanity_filter.py b/packages/google-cloud-python-speech/samples/snippets/profanity_filter.py index ca8a9a97af00..c588691776b2 100644 --- a/packages/google-cloud-python-speech/samples/snippets/profanity_filter.py +++ b/packages/google-cloud-python-speech/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/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py b/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py index b3a0fc342faf..b98d5516880d 100644 --- a/packages/google-cloud-python-speech/samples/snippets/transcribe_async.py +++ b/packages/google-cloud-python-speech/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]