Skip to content

Commit dedcac2

Browse files
b-loved-dreamertelpirion
authored andcommitted
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
1 parent b0a2dbd commit dedcac2

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

speech/snippets/multi_region.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def sync_recognize_with_multi_region_gcs():
19+
# [START speech_multi_region]
20+
21+
# Imports the Google Cloud client library
22+
from google.cloud import speech
23+
from google.api_core import client_options
24+
25+
# Instantiates a client
26+
27+
# [START speech_multi_region_client]
28+
29+
# Pass an additional argument, ClientOptions, to specify the new endpoint.
30+
client_options = client_options.ClientOptions(
31+
api_endpoint="eu-speech.googleapis.com"
32+
)
33+
34+
client = speech.SpeechClient(client_options=client_options)
35+
# [END speech_multi_region_client]
36+
37+
# The name of the audio file to transcribe
38+
gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
39+
40+
audio = speech.RecognitionAudio(uri=gcs_uri)
41+
42+
config = speech.RecognitionConfig(
43+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
44+
sample_rate_hertz=16000,
45+
language_code="en-US",
46+
)
47+
48+
# Detects speech in the audio file
49+
response = client.recognize(config=config, audio=audio)
50+
51+
for result in response.results:
52+
print("Transcript: {}".format(result.alternatives[0].transcript))
53+
# [END speech_multi_region]
54+
55+
56+
sync_recognize_with_multi_region_gcs()

speech/snippets/multi_region_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2020 Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
15+
import multi_region
16+
17+
18+
def test_multi_region(capsys):
19+
multi_region.sync_recognize_with_multi_region_gcs()
20+
out, _ = capsys.readouterr()
21+
assert "Transcript: how old is the Brooklyn Bridge" in out

speech/snippets/profanity_filter.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
python transcribe.py gs://cloud-samples-tests/speech/brooklyn.flac
1919
"""
2020

21-
import argparse
22-
2321

2422
# [START speech_recognize_with_profanity_filter_gcs]
25-
def sync_recognize_with_profanity_filter_gcs(storage_uri):
23+
def sync_recognize_with_profanity_filter_gcs(gcs_uri):
2624

2725
from google.cloud import speech
2826

2927
client = speech.SpeechClient()
3028

31-
audio = {"uri": storage_uri}
29+
audio = {"uri": gcs_uri}
3230

3331
config = speech.RecognitionConfig(
3432
encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
@@ -46,10 +44,6 @@ def sync_recognize_with_profanity_filter_gcs(storage_uri):
4644

4745
# [END speech_recognize_with_profanity_filter_gcs]
4846

49-
if __name__ == "__main__":
50-
parser = argparse.ArgumentParser(
51-
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
52-
)
53-
parser.add_argument("path", help="GCS path for audio file to be recognized")
54-
args = parser.parse_args()
55-
sync_recognize_with_profanity_filter_gcs(args.path)
47+
sync_recognize_with_profanity_filter_gcs(
48+
"gs://cloud-samples-tests/speech/brooklyn.flac"
49+
)

speech/snippets/transcribe_async.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def transcribe_file(speech_file):
4848
sample_rate_hertz=16000,
4949
language_code="en-US",
5050
)
51+
5152
# [START speech_python_migration_async_response]
53+
5254
operation = client.long_running_recognize(config=config, audio=audio)
5355
# [END speech_python_migration_async_request]
5456

0 commit comments

Comments
 (0)