[docs]@deprecated(since="0.0.32",removal="1.0",alternative_import="langchain_google_community.SpeechToTextLoader",)classGoogleSpeechToTextLoader(BaseLoader):""" Loader for Google Cloud Speech-to-Text audio transcripts. It uses the Google Cloud Speech-to-Text API to transcribe audio files and loads the transcribed text into one or more Documents, depending on the specified format. To use, you should have the ``google-cloud-speech`` python package installed. Audio files can be specified via a Google Cloud Storage uri or a local file path. For a detailed explanation of Google Cloud Speech-to-Text, refer to the product documentation. https://cloud.google.com/speech-to-text """
[docs]def__init__(self,project_id:str,file_path:str,location:str="us-central1",recognizer_id:str="_",config:Optional[RecognitionConfig]=None,config_mask:Optional[FieldMask]=None,):""" Initializes the GoogleSpeechToTextLoader. Args: project_id: Google Cloud Project ID. file_path: A Google Cloud Storage URI or a local file path. location: Speech-to-Text recognizer location. recognizer_id: Speech-to-Text recognizer id. config: Recognition options and features. For more information: https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v2.types.RecognitionConfig config_mask: The list of fields in config that override the values in the ``default_recognition_config`` of the recognizer during this recognition request. For more information: https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v2.types.RecognizeRequest """try:fromgoogle.api_core.client_optionsimportClientOptionsfromgoogle.cloud.speech_v2import(AutoDetectDecodingConfig,RecognitionConfig,RecognitionFeatures,SpeechClient,)exceptImportErrorasexc:raiseImportError("Could not import google-cloud-speech python package. ""Please install it with `pip install google-cloud-speech`.")fromexcself.project_id=project_idself.file_path=file_pathself.location=locationself.recognizer_id=recognizer_id# Config must be set in speech recognition request.self.config=configorRecognitionConfig(auto_decoding_config=AutoDetectDecodingConfig(),language_codes=["en-US"],model="chirp",features=RecognitionFeatures(# Automatic punctuation could be useful for language applicationsenable_automatic_punctuation=True,),)self.config_mask=config_maskself._client=SpeechClient(client_info=get_client_info(module="speech-to-text"),client_options=(ClientOptions(api_endpoint=f"{location}-speech.googleapis.com")iflocation!="global"elseNone),)self._recognizer_path=self._client.recognizer_path(project_id,location,recognizer_id)
[docs]defload(self)->List[Document]:"""Transcribes the audio file and loads the transcript into documents. It uses the Google Cloud Speech-to-Text API to transcribe the audio file and blocks until the transcription is finished. """try:fromgoogle.cloud.speech_v2importRecognizeRequestexceptImportErrorasexc:raiseImportError("Could not import google-cloud-speech python package. ""Please install it with `pip install google-cloud-speech`.")fromexcrequest=RecognizeRequest(recognizer=self._recognizer_path,config=self.config,config_mask=self.config_mask,)if"gs://"inself.file_path:request.uri=self.file_pathelse:withopen(self.file_path,"rb")asf:request.content=f.read()response=self._client.recognize(request=request)return[Document(page_content=result.alternatives[0].transcript,metadata={"language_code":result.language_code,"result_end_offset":result.result_end_offset,},)forresultinresponse.results]