From 4428f43608d036cb4c18ffded5661a9c3f9541f2 Mon Sep 17 00:00:00 2001 From: Mariana Quinde Garcia Date: Fri, 7 May 2021 15:38:23 -0400 Subject: [PATCH 1/6] samples: Spoken punctuation and emojis samples --- samples/snippets/beta_snippets.py | 36 ++++++++++++++++++++++++++ samples/snippets/beta_snippets_test.py | 7 +++++ 2 files changed, 43 insertions(+) diff --git a/samples/snippets/beta_snippets.py b/samples/snippets/beta_snippets.py index 45f661cd..de49308f 100644 --- a/samples/snippets/beta_snippets.py +++ b/samples/snippets/beta_snippets.py @@ -25,6 +25,7 @@ python beta_snippets.py multi-channel python beta_snippets.py multi-language python beta_snippets.py word-level-conf + python beta_snippets.py spoken-punctuation-emojis """ import argparse @@ -290,6 +291,39 @@ def transcribe_file_with_word_level_confidence(): ) # [END speech_transcribe_word_level_confidence_beta] +def transcribe_file_with_spoken_punctuation_end_emojis(): + """Transcribe the given audio file with spoken punctuation and emojis enabled.""" + # [START speech_transcribe_spoken_punctuation_emoji_beta] + from google.cloud import speech_v1p1beta1 as speech + from google.protobuf import wrappers_pb2 + + client = speech.SpeechClient() + + speech_file = "resources/commercial_mono.wav" + + with io.open(speech_file, "rb") as audio_file: + content = audio_file.read() + + audio = speech.RecognitionAudio(content=content) + config = speech.RecognitionConfig( + encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, + sample_rate_hertz=8000, + language_code="en-US", + # Enable spoken punctuation + enable_spoken_punctuation=wrappers_pb2.BoolValue(value=True), + # Enable spoken emoji + enable_spoken_emoji=wrappers_pb2.BoolValue(value=True), + ) + + response = client.recognize(config=config, audio=audio) + + for i, result in enumerate(response.results): + alternative = result.alternatives[0] + print("-" * 20) + print(u"First alternative of result {}".format(i)) + print(u"Transcript: {}".format(alternative.transcript)) + # [END speech_transcribe_spoken_punctuation_emoji_beta] + if __name__ == "__main__": parser = argparse.ArgumentParser( @@ -313,3 +347,5 @@ def transcribe_file_with_word_level_confidence(): transcribe_file_with_multilanguage() elif args.command == "word-level-conf": transcribe_file_with_word_level_confidence() + elif args.command == "spoken-punctuation-emojis": + transcribe_file_with_spoken_punctuation_end_emojis() diff --git a/samples/snippets/beta_snippets_test.py b/samples/snippets/beta_snippets_test.py index d1242df5..d300ba68 100644 --- a/samples/snippets/beta_snippets_test.py +++ b/samples/snippets/beta_snippets_test.py @@ -21,6 +21,7 @@ transcribe_file_with_multichannel, transcribe_file_with_multilanguage, transcribe_file_with_word_level_confidence, + transcribe_file_with_spoken_punctuation_end_emojis, ) RESOURCES = os.path.join(os.path.dirname(__file__), "resources") @@ -74,3 +75,9 @@ def test_transcribe_word_level_confidence(capsys): out, err = capsys.readouterr() assert "OK Google stream stranger things from Netflix to my TV" in out + +def transcribe_file_with_spoken_punctuation_end_emojis(capsys): + transcribe_file_with_spoken_punctuation_end_emojis() + out, err = capsys.readouterr() + + assert "OK Google stream stranger things from Netflix to my TV" in out From a866006880bd48a9d82815b4b3df2b21a9d3e1c8 Mon Sep 17 00:00:00 2001 From: Mariana Quinde Garcia Date: Fri, 7 May 2021 15:42:44 -0400 Subject: [PATCH 2/6] samples: Spoken punctuation and emojis sample test --- samples/snippets/beta_snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/beta_snippets_test.py b/samples/snippets/beta_snippets_test.py index d300ba68..e707d365 100644 --- a/samples/snippets/beta_snippets_test.py +++ b/samples/snippets/beta_snippets_test.py @@ -80,4 +80,4 @@ def transcribe_file_with_spoken_punctuation_end_emojis(capsys): transcribe_file_with_spoken_punctuation_end_emojis() out, err = capsys.readouterr() - assert "OK Google stream stranger things from Netflix to my TV" in out + assert "First alternative of result " in out From f278364de470a03705bdd1ea91ed1eb622a24202 Mon Sep 17 00:00:00 2001 From: Mariana Quinde Garcia Date: Fri, 7 May 2021 15:49:53 -0400 Subject: [PATCH 3/6] samples: Spoken punctuation and emojis sample test fix --- samples/snippets/beta_snippets.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/snippets/beta_snippets.py b/samples/snippets/beta_snippets.py index de49308f..e225461b 100644 --- a/samples/snippets/beta_snippets.py +++ b/samples/snippets/beta_snippets.py @@ -293,7 +293,7 @@ def transcribe_file_with_word_level_confidence(): def transcribe_file_with_spoken_punctuation_end_emojis(): """Transcribe the given audio file with spoken punctuation and emojis enabled.""" - # [START speech_transcribe_spoken_punctuation_emoji_beta] + # [START speech_transcribe_spoken_punctuation_emojis_beta] from google.cloud import speech_v1p1beta1 as speech from google.protobuf import wrappers_pb2 @@ -311,8 +311,8 @@ def transcribe_file_with_spoken_punctuation_end_emojis(): language_code="en-US", # Enable spoken punctuation enable_spoken_punctuation=wrappers_pb2.BoolValue(value=True), - # Enable spoken emoji - enable_spoken_emoji=wrappers_pb2.BoolValue(value=True), + # Enable spoken emojis + enable_spoken_emojis=wrappers_pb2.BoolValue(value=True), ) response = client.recognize(config=config, audio=audio) @@ -322,7 +322,7 @@ def transcribe_file_with_spoken_punctuation_end_emojis(): print("-" * 20) print(u"First alternative of result {}".format(i)) print(u"Transcript: {}".format(alternative.transcript)) - # [END speech_transcribe_spoken_punctuation_emoji_beta] + # [END speech_transcribe_spoken_punctuation_emojis_beta] if __name__ == "__main__": From 22e4edafa5ceefa53067783076e3757313eee36a Mon Sep 17 00:00:00 2001 From: Mariana Quinde Garcia Date: Tue, 27 Jul 2021 13:21:40 -0400 Subject: [PATCH 4/6] samples: reformatting changed files with black --- samples/snippets/beta_snippets.py | 1 + samples/snippets/beta_snippets_test.py | 1 + 2 files changed, 2 insertions(+) diff --git a/samples/snippets/beta_snippets.py b/samples/snippets/beta_snippets.py index e225461b..9e81f514 100644 --- a/samples/snippets/beta_snippets.py +++ b/samples/snippets/beta_snippets.py @@ -291,6 +291,7 @@ def transcribe_file_with_word_level_confidence(): ) # [END speech_transcribe_word_level_confidence_beta] + def transcribe_file_with_spoken_punctuation_end_emojis(): """Transcribe the given audio file with spoken punctuation and emojis enabled.""" # [START speech_transcribe_spoken_punctuation_emojis_beta] diff --git a/samples/snippets/beta_snippets_test.py b/samples/snippets/beta_snippets_test.py index e707d365..04833936 100644 --- a/samples/snippets/beta_snippets_test.py +++ b/samples/snippets/beta_snippets_test.py @@ -76,6 +76,7 @@ def test_transcribe_word_level_confidence(capsys): assert "OK Google stream stranger things from Netflix to my TV" in out + def transcribe_file_with_spoken_punctuation_end_emojis(capsys): transcribe_file_with_spoken_punctuation_end_emojis() out, err = capsys.readouterr() From d1798b8434614e0898c92691ef933dc25bd7bc39 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 28 Jul 2021 12:20:23 -0400 Subject: [PATCH 5/6] add missing prefix `test` --- samples/snippets/beta_snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/beta_snippets_test.py b/samples/snippets/beta_snippets_test.py index 04833936..e596ab27 100644 --- a/samples/snippets/beta_snippets_test.py +++ b/samples/snippets/beta_snippets_test.py @@ -77,7 +77,7 @@ def test_transcribe_word_level_confidence(capsys): assert "OK Google stream stranger things from Netflix to my TV" in out -def transcribe_file_with_spoken_punctuation_end_emojis(capsys): +def test_transcribe_file_with_spoken_punctuation_end_emojis(capsys): transcribe_file_with_spoken_punctuation_end_emojis() out, err = capsys.readouterr() From 57e0d3aa9a103012b11b88f6ea525835f7a7eb31 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 28 Jul 2021 12:26:54 -0400 Subject: [PATCH 6/6] lint --- samples/snippets/beta_snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/beta_snippets_test.py b/samples/snippets/beta_snippets_test.py index e596ab27..c5cadede 100644 --- a/samples/snippets/beta_snippets_test.py +++ b/samples/snippets/beta_snippets_test.py @@ -20,8 +20,8 @@ transcribe_file_with_metadata, transcribe_file_with_multichannel, transcribe_file_with_multilanguage, - transcribe_file_with_word_level_confidence, transcribe_file_with_spoken_punctuation_end_emojis, + transcribe_file_with_word_level_confidence, ) RESOURCES = os.path.join(os.path.dirname(__file__), "resources")