Skip to content

Commit 2f6c306

Browse files
chore: updated quickstart (#94)
* 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
1 parent ace605b commit 2f6c306

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

speech/snippets/profanity_filter.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
""" Google Cloud Speech API sample application using the REST API for batch
15+
processing.
16+
17+
Example usage:
18+
python transcribe.py gs://cloud-samples-tests/speech/brooklyn.flac
19+
"""
20+
21+
import argparse
22+
23+
24+
# [START speech_recognize_with_profanity_filter_gcs]
25+
def sync_recognize_with_profanity_filter_gcs(storage_uri):
26+
27+
from google.cloud import speech
28+
29+
client = speech.SpeechClient()
30+
31+
audio = {"uri": storage_uri}
32+
33+
config = speech.RecognitionConfig(
34+
encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
35+
sample_rate_hertz=16000,
36+
language_code="en-US",
37+
profanity_filter=True,
38+
)
39+
40+
response = client.recognize(config=config, audio=audio)
41+
42+
for i, result in enumerate(response.results):
43+
alternative = result.alternatives[0]
44+
print(u"Transcript: {}".format(alternative.transcript))
45+
46+
47+
# [END speech_recognize_with_profanity_filter_gcs]
48+
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)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import re
15+
16+
import profanity_filter
17+
18+
19+
def test_profanity_filter(capsys):
20+
profanity_filter.sync_recognize_with_profanity_filter_gcs(
21+
"gs://cloud-samples-tests/speech/brooklyn.flac"
22+
)
23+
out, err = capsys.readouterr()
24+
assert re.search(r"how old is the Brooklyn Bridge", out, re.DOTALL | re.I)

speech/snippets/quickstart.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
def run_quickstart():
1919
# [START speech_quickstart]
20-
import io
21-
import os
2220

2321
# Imports the Google Cloud client library
2422
# [START speech_python_migration_imports]
@@ -32,12 +30,9 @@ def run_quickstart():
3230
# [END speech_python_migration_client]
3331

3432
# The name of the audio file to transcribe
35-
file_name = os.path.join(os.path.dirname(__file__), "resources", "audio.raw")
33+
gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
3634

37-
# Loads the audio into memory
38-
with io.open(file_name, "rb") as audio_file:
39-
content = audio_file.read()
40-
audio = speech.RecognitionAudio(content=content)
35+
audio = speech.RecognitionAudio(uri=gcs_uri)
4136

4237
config = speech.RecognitionConfig(
4338
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,

speech/snippets/transcribe_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def transcribe_file(speech_file):
3838
content = audio_file.read()
3939

4040
"""
41-
Note that transcription is limited to 60 seconds audio.
41+
Note that transcription is limited to a 60 seconds audio file.
4242
Use a GCS file for audio longer than 1 minute.
4343
"""
4444
audio = speech.RecognitionAudio(content=content)

0 commit comments

Comments
 (0)