Skip to content

Commit b4ce5d5

Browse files
authored
Add code snippet for auto-detect language feature. (GoogleCloudPlatform#10923)
docs(samples): Add code snippet for chirp auto-detect language feature
1 parent c0d091b commit b4ce5d5

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import argparse
17+
18+
# [START speech_transcribe_chirp_auto_detect_language]
19+
20+
from google.api_core.client_options import ClientOptions
21+
from google.cloud.speech_v2 import SpeechClient
22+
from google.cloud.speech_v2.types import cloud_speech
23+
24+
25+
def transcribe_chirp_auto_detect_language(
26+
project_id: str,
27+
audio_file: str,
28+
region: str = "us-central1",
29+
) -> cloud_speech.RecognizeResponse:
30+
"""Transcribe an audio file and auto-detect spoken language using Chirp.
31+
32+
Please see https://cloud.google.com/speech-to-text/v2/docs/encoding for more
33+
information on which audio encodings are supported.
34+
"""
35+
# Instantiates a client
36+
client = SpeechClient(
37+
client_options=ClientOptions(
38+
api_endpoint=f"{region}-speech.googleapis.com",
39+
)
40+
)
41+
42+
# Reads a file as bytes
43+
with open(audio_file, "rb") as f:
44+
content = f.read()
45+
46+
config = cloud_speech.RecognitionConfig(
47+
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
48+
language_codes=["auto"], # Set language code to auto to detect language.
49+
model="chirp",
50+
)
51+
52+
request = cloud_speech.RecognizeRequest(
53+
recognizer=f"projects/{project_id}/locations/{region}/recognizers/_",
54+
config=config,
55+
content=content,
56+
)
57+
58+
# Transcribes the audio into text
59+
response = client.recognize(request=request)
60+
61+
for result in response.results:
62+
print(f"Transcript: {result.alternatives[0].transcript}")
63+
print(f"Detected Language: {result.language_code}")
64+
65+
return response
66+
67+
# [END speech_transcribe_chirp_auto_detect_language]
68+
69+
70+
if __name__ == "__main__":
71+
parser = argparse.ArgumentParser(
72+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
73+
)
74+
parser.add_argument("project_id", help="GCP Project ID")
75+
parser.add_argument("audio_file", help="Audio file to stream")
76+
args = parser.parse_args()
77+
transcribe_chirp_auto_detect_language(args.project_id, args.audio_file)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import re
17+
18+
from google.api_core.retry import Retry
19+
20+
import transcribe_chirp_auto_detect_language
21+
22+
_RESOURCES = os.path.join(os.path.dirname(__file__), "resources")
23+
24+
25+
@Retry()
26+
def test_transcribe_chirp() -> None:
27+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
28+
29+
response = transcribe_chirp_auto_detect_language.transcribe_chirp_auto_detect_language(
30+
project_id, os.path.join(_RESOURCES, "audio.wav")
31+
)
32+
33+
assert re.search(
34+
r"how old is the Brooklyn Bridge",
35+
response.results[0].alternatives[0].transcript,
36+
re.DOTALL | re.I,
37+
)
38+
39+
assert response.results[0].language_code == 'en'

0 commit comments

Comments
 (0)