Skip to content

docs(samples): Add & update samples for STT v2 #10388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions speech/snippets/adaptation_v2_custom_class_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
# limitations under the License.


# [START speech_adaptation_v2_custom_class_reference]
import argparse

# [START speech_adaptation_v2_custom_class_reference]
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech


def adaptation_v2_custom_class_reference(
project_id: str,
recognizer_id: str,
phrase_set_id: str,
custom_class_id: str,
audio_file: str,
Expand All @@ -30,7 +30,6 @@ def adaptation_v2_custom_class_reference(

Args:
project_id: The GCP project ID.
recognizer_id: The ID of the recognizer to use.
phrase_set_id: The ID of the phrase set to use.
custom_class_id: The ID of the custom class to use.
audio_file: The audio file to transcribe.
Expand All @@ -41,18 +40,6 @@ def adaptation_v2_custom_class_reference(
# Instantiates a client
client = SpeechClient()

request = cloud_speech.CreateRecognizerRequest(
parent=f"projects/{project_id}/locations/global",
recognizer_id=recognizer_id,
recognizer=cloud_speech.Recognizer(
language_codes=["en-US"], model="latest_short"
),
)

# Creates a Recognizer
operation = client.create_recognizer(request=request)
recognizer = operation.result()

# Reads a file as bytes
with open(audio_file, "rb") as f:
content = f.read()
Expand Down Expand Up @@ -88,11 +75,16 @@ def adaptation_v2_custom_class_reference(
]
)
config = cloud_speech.RecognitionConfig(
auto_decoding_config={}, adaptation=adaptation
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
adaptation=adaptation,
language_codes=["en-US"],
model="latest_short",
)

request = cloud_speech.RecognizeRequest(
recognizer=recognizer.name, config=config, content=content
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
config=config,
content=content,
)

# Transcribes the audio into text
Expand All @@ -108,4 +100,14 @@ def adaptation_v2_custom_class_reference(


if __name__ == "__main__":
adaptation_v2_custom_class_reference()
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("project_id", help="GCP Project ID")
parser.add_argument("phrase_set_id", help="ID for the phrase set to create")
parser.add_argument("custom_class_id", help="ID for the custom class to create")
parser.add_argument("audio_file", help="Audio file to stream")
args = parser.parse_args()
adaptation_v2_custom_class_reference(
args.project_id, args.phrase_set_id, args.custom_class_id, args.audio_file
)
16 changes: 2 additions & 14 deletions speech/snippets/adaptation_v2_custom_class_reference_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@

import adaptation_v2_custom_class_reference

RESOURCES = os.path.join(os.path.dirname(__file__), "resources")


def delete_recognizer(name: str) -> None:
client = SpeechClient()
request = cloud_speech.DeleteRecognizerRequest(name=name)
client.delete_recognizer(request=request)
_RESOURCES = os.path.join(os.path.dirname(__file__), "resources")


def delete_phrase_set(name: str) -> None:
Expand All @@ -47,16 +41,14 @@ def delete_custom_class(name: str) -> None:
def test_adaptation_v2_custom_class_reference() -> None:
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")

recognizer_id = "recognizer-" + str(uuid4())
phrase_set_id = "phrase-set-" + str(uuid4())
custom_class_id = "custom-class-" + str(uuid4())
response = (
adaptation_v2_custom_class_reference.adaptation_v2_custom_class_reference(
project_id,
recognizer_id,
phrase_set_id,
custom_class_id,
os.path.join(RESOURCES, "fair.wav"),
os.path.join(_RESOURCES, "fair.wav"),
)
)

Expand All @@ -66,10 +58,6 @@ def test_adaptation_v2_custom_class_reference() -> None:
re.DOTALL | re.I,
)

delete_recognizer(
f"projects/{project_id}/locations/global/recognizers/{recognizer_id}"
)

delete_phrase_set(
f"projects/{project_id}/locations/global/phraseSets/{phrase_set_id}"
)
Expand Down
34 changes: 16 additions & 18 deletions speech/snippets/adaptation_v2_inline_custom_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@
# limitations under the License.


# [START speech_adaptation_v2_inline_custom_class]
import argparse

# [START speech_adaptation_v2_inline_custom_class]
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech


def adaptation_v2_inline_custom_class(
project_id: str,
recognizer_id: str,
audio_file: str,
) -> cloud_speech.RecognizeResponse:
"""Transcribe audio file using inline custom class

Args:
project_id: The GCP project ID.
recognizer_id: The ID of the recognizer.
audio_file: The audio file to transcribe.

Returns:
Expand All @@ -37,18 +36,6 @@ def adaptation_v2_inline_custom_class(
# Instantiates a client
client = SpeechClient()

request = cloud_speech.CreateRecognizerRequest(
parent=f"projects/{project_id}/locations/global",
recognizer_id=recognizer_id,
recognizer=cloud_speech.Recognizer(
language_codes=["en-US"], model="latest_short"
),
)

# Creates a Recognizer
operation = client.create_recognizer(request=request)
recognizer = operation.result()

# Reads a file as bytes
with open(audio_file, "rb") as f:
content = f.read()
Expand All @@ -65,11 +52,16 @@ def adaptation_v2_inline_custom_class(
custom_classes=[custom_class],
)
config = cloud_speech.RecognitionConfig(
auto_decoding_config={}, adaptation=adaptation
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
adaptation=adaptation,
language_codes=["en-US"],
model="latest_short",
)

request = cloud_speech.RecognizeRequest(
recognizer=recognizer.name, config=config, content=content
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
config=config,
content=content,
)

# Transcribes the audio into text
Expand All @@ -85,4 +77,10 @@ def adaptation_v2_inline_custom_class(


if __name__ == "__main__":
adaptation_v2_inline_custom_class()
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("project_id", help="GCP Project ID")
parser.add_argument("audio_file", help="Audio file to stream")
args = parser.parse_args()
adaptation_v2_inline_custom_class(args.project_id, args.audio_file)
18 changes: 2 additions & 16 deletions speech/snippets/adaptation_v2_inline_custom_class_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,24 @@

import os
import re
from uuid import uuid4

from google.api_core.retry import Retry
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

import adaptation_v2_inline_custom_class

RESOURCES = os.path.join(os.path.dirname(__file__), "resources")


def delete_recognizer(name: str) -> None:
client = SpeechClient()
request = cloud_speech.DeleteRecognizerRequest(name=name)
client.delete_recognizer(request=request)
_RESOURCES = os.path.join(os.path.dirname(__file__), "resources")


@Retry()
def test_adaptation_v2_inline_custom_class() -> None:
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")

recognizer_id = "recognizer-" + str(uuid4())
response = adaptation_v2_inline_custom_class.adaptation_v2_inline_custom_class(
project_id, recognizer_id, os.path.join(RESOURCES, "fair.wav")
project_id, os.path.join(_RESOURCES, "fair.wav")
)

assert re.search(
r"the word",
response.results[0].alternatives[0].transcript,
re.DOTALL | re.I,
)

delete_recognizer(
f"projects/{project_id}/locations/global/recognizers/{recognizer_id}"
)
36 changes: 19 additions & 17 deletions speech/snippets/adaptation_v2_inline_phrase_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,20 @@
# limitations under the License.


# [START speech_adaptation_v2_inline_phrase_set]
import argparse

# [START speech_adaptation_v2_inline_phrase_set]
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech


def adaptation_v2_inline_phrase_set(
project_id: str,
recognizer_id: str,
audio_file: str,
) -> cloud_speech.RecognizeResponse:
# Instantiates a client
client = SpeechClient()

request = cloud_speech.CreateRecognizerRequest(
parent=f"projects/{project_id}/locations/global",
recognizer_id=recognizer_id,
recognizer=cloud_speech.Recognizer(
language_codes=["en-US"], model="latest_short"
),
)

# Creates a Recognizer
operation = client.create_recognizer(request=request)
recognizer = operation.result()

# Reads a file as bytes
with open(audio_file, "rb") as f:
content = f.read()
Expand All @@ -53,11 +41,16 @@ def adaptation_v2_inline_phrase_set(
]
)
config = cloud_speech.RecognitionConfig(
auto_decoding_config={}, adaptation=adaptation
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
adaptation=adaptation,
language_codes=["en-US"],
model="latest_short",
)

request = cloud_speech.RecognizeRequest(
recognizer=recognizer.name, config=config, content=content
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
config=config,
content=content,
)

# Transcribes the audio into text
Expand All @@ -73,4 +66,13 @@ def adaptation_v2_inline_phrase_set(


if __name__ == "__main__":
adaptation_v2_inline_phrase_set()
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("project_id", help="GCP Project ID")
parser.add_argument("audio_file", help="Audio file to stream")
args = parser.parse_args()
adaptation_v2_inline_phrase_set(
args.project_id,
args.audio_file,
)
18 changes: 2 additions & 16 deletions speech/snippets/adaptation_v2_inline_phrase_set_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,24 @@

import os
import re
from uuid import uuid4

from google.api_core.retry import Retry
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

import adaptation_v2_inline_phrase_set

RESOURCES = os.path.join(os.path.dirname(__file__), "resources")


def delete_recognizer(name: str) -> None:
client = SpeechClient()
request = cloud_speech.DeleteRecognizerRequest(name=name)
client.delete_recognizer(request=request)
_RESOURCES = os.path.join(os.path.dirname(__file__), "resources")


@Retry()
def test_adaptation_v2_inline_phrase_set() -> None:
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")

recognizer_id = "recognizer-" + str(uuid4())
response = adaptation_v2_inline_phrase_set.adaptation_v2_inline_phrase_set(
project_id, recognizer_id, os.path.join(RESOURCES, "fair.wav")
project_id, os.path.join(_RESOURCES, "fair.wav")
)

assert re.search(
r"the word is fare",
response.results[0].alternatives[0].transcript,
re.DOTALL | re.I,
)

delete_recognizer(
f"projects/{project_id}/locations/global/recognizers/{recognizer_id}"
)
Loading