Skip to content

Commit

Permalink
chore: updated quickstart (#94)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
b-loved-dreamer authored Dec 10, 2020
1 parent 1bb769c commit d534088
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.

""" Google Cloud Speech API sample application using the REST API for batch
processing.
Example usage:
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):

from google.cloud import speech

client = speech.SpeechClient()

audio = {"uri": storage_uri}

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=16000,
language_code="en-US",
profanity_filter=True,
)

response = client.recognize(config=config, audio=audio)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print(u"Transcript: {}".format(alternative.transcript))


# [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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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 re

import profanity_filter


def test_profanity_filter(capsys):
profanity_filter.sync_recognize_with_profanity_filter_gcs(
"gs://cloud-samples-tests/speech/brooklyn.flac"
)
out, err = capsys.readouterr()
assert re.search(r"how old is the Brooklyn Bridge", out, re.DOTALL | re.I)
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

def run_quickstart():
# [START speech_quickstart]
import io
import os

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

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

# Loads the audio into memory
with io.open(file_name, "rb") as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)
audio = speech.RecognitionAudio(uri=gcs_uri)

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def transcribe_file(speech_file):
content = audio_file.read()

"""
Note that transcription is limited to 60 seconds audio.
Note that transcription is limited to a 60 seconds audio file.
Use a GCS file for audio longer than 1 minute.
"""
audio = speech.RecognitionAudio(content=content)
Expand Down

0 comments on commit d534088

Please sign in to comment.