|
| 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) |
0 commit comments