importosfromgoogle.cloud.speech_v2importSpeechClientfromgoogle.cloud.speech_v2.typesimportcloud_speechPROJECT_ID=os.getenv("GOOGLE_CLOUD_PROJECT")defquickstart_v2(audio_file:str)-> cloud_speech.RecognizeResponse:"""Transcribe an audio file. Args: audio_file (str): Path to the local audio file to be transcribed. Returns: cloud_speech.RecognizeResponse: The response from the recognize request, containing the transcription results """# Reads a file as byteswithopen(audio_file,"rb")asf:audio_content=f.read()# Instantiates a clientclient=SpeechClient()config=cloud_speech.RecognitionConfig(auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),language_codes=["en-US"],model="long",)request=cloud_speech.RecognizeRequest(recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",config=config,content=audio_content,)# Transcribes the audio into textresponse=client.recognize(request=request)forresultinresponse.results:print(f"Transcript: {result.alternatives[0].transcript}")returnresponse
importosfromgoogle.api_core.client_optionsimportClientOptionsfromgoogle.cloud.speech_v2importSpeechClientfromgoogle.cloud.speech_v2.typesimportcloud_speechPROJECT_ID=os.getenv("GOOGLE_CLOUD_PROJECT")defchange_speech_v2_location(audio_file:str,location:str)-> cloud_speech.RecognizeResponse:"""Transcribe an audio file in a specific region. It allows for specifying the location to potentially reduce latency and meet data residency requirements. Args: audio_file (str): Path to the local audio file to be transcribed. location (str): The region where the Speech API will be accessed. E.g., "europe-west3" Returns: cloud_speech.RecognizeResponse: The full response object which includes the transcription results. """# Reads a file as byteswithopen(audio_file,"rb")asf:audio_content=f.read()# Instantiates a client to a regionalized Speech endpoint.client=SpeechClient(client_options=ClientOptions(api_endpoint=f"{location}-speech.googleapis.com",))config=cloud_speech.RecognitionConfig(auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),language_codes=["en-US"],model="long",)request=cloud_speech.RecognizeRequest(recognizer=f"projects/{PROJECT_ID}/locations/{location}/recognizers/_",config=config,content=audio_content,)# Transcribes the audio into textresponse=client.recognize(request=request)forresultinresponse.results:print(f"Transcript: {result.alternatives[0].transcript}")returnresponse
importosfromgoogle.cloud.speech_v2importSpeechClientfromgoogle.cloud.speech_v2.typesimportcloud_speechPROJECT_ID=os.getenv("GOOGLE_CLOUD_PROJECT")defcreate_recognizer(recognizer_id:str)-> cloud_speech.Recognizer:"""Сreates a recognizer with an unique ID and default recognition configuration. Args: recognizer_id (str): The unique identifier for the recognizer to be created. Returns: cloud_speech.Recognizer: The created recognizer object with configuration. """# Instantiates a clientclient=SpeechClient()request=cloud_speech.CreateRecognizerRequest(parent=f"projects/{PROJECT_ID}/locations/global",recognizer_id=recognizer_id,recognizer=cloud_speech.Recognizer(default_recognition_config=cloud_speech.RecognitionConfig(language_codes=["en-US"],model="long"),),)# Sends the request to create a recognizer and waits for the operation to completeoperation=client.create_recognizer(request=request)recognizer=operation.result()print("Created Recognizer:",recognizer.name)returnrecognizer