-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathadaptation_v2_inline_custom_class.py
87 lines (71 loc) · 2.88 KB
/
adaptation_v2_inline_custom_class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2022 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
#
# https://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.
# [START speech_adaptation_v2_inline_custom_class]
import os
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
def adaptation_v2_inline_custom_class(
audio_file: str,
) -> cloud_speech.RecognizeResponse:
"""Transcribe audio file using inline custom class.
The inline custom class helps the recognizer produce more accurate transcriptions for specific terms.
Args:
audio_file (str): Path to the local audio file to be transcribed.
Returns:
cloud_speech.RecognizeResponse: The response object which includes the transcription results.
"""
# Instantiates a client
client = SpeechClient()
# Reads a file as bytes
with open(audio_file, "rb") as f:
audio_content = f.read()
# Define an inline custom class to enhance recognition accuracy with specific items like "fare" etc.
custom_class_name = "your-class-name"
custom_class = cloud_speech.CustomClass(
name=custom_class_name,
items=[{"value": "fare"}],
)
# Build inline phrase set to produce a more accurate transcript
phrase_set = cloud_speech.PhraseSet(
phrases=[{"value": custom_class_name, "boost": 20}]
)
adaptation = cloud_speech.SpeechAdaptation(
phrase_sets=[
cloud_speech.SpeechAdaptation.AdaptationPhraseSet(
inline_phrase_set=phrase_set
)
],
custom_classes=[custom_class],
)
config = cloud_speech.RecognitionConfig(
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
adaptation=adaptation,
language_codes=["en-US"],
model="short",
)
# Prepare the request which includes specifying the recognizer, configuration, and the audio content
request = cloud_speech.RecognizeRequest(
recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
config=config,
content=audio_content,
)
# Transcribes the audio into text
response = client.recognize(request=request)
for result in response.results:
print(f"Transcript: {result.alternatives[0].transcript}")
return response
# [END speech_adaptation_v2_inline_custom_class]
if __name__ == "__main__":
adaptation_v2_inline_custom_class("resources/fair.wav")