[docs]classTranscriptFormat(Enum):"""Transcript format to use for the document loader."""TEXT="text""""One document with the transcription text"""SENTENCES="sentences""""Multiple documents, splits the transcription by each sentence"""PARAGRAPHS="paragraphs""""Multiple documents, splits the transcription by each paragraph"""SUBTITLES_SRT="subtitles_srt""""One document with the transcript exported in SRT subtitles format"""SUBTITLES_VTT="subtitles_vtt""""One document with the transcript exported in VTT subtitles format"""
[docs]classAssemblyAIAudioTranscriptLoader(BaseLoader):"""Load AssemblyAI audio transcripts. It uses the AssemblyAI 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 ``assemblyai`` python package installed, and the environment variable ``ASSEMBLYAI_API_KEY`` set with your API key. Alternatively, the API key can also be passed as an argument. Audio files can be specified via an URL or a local file path. """
[docs]def__init__(self,file_path:Union[str,Path],*,transcript_format:TranscriptFormat=TranscriptFormat.TEXT,config:Optional[assemblyai.TranscriptionConfig]=None,api_key:Optional[str]=None,):""" Initializes the AssemblyAI AudioTranscriptLoader. Args: file_path: An URL or a local file path. transcript_format: Transcript format to use. See class ``TranscriptFormat`` for more info. config: Transcription options and features. If ``None`` is given, the Transcriber's default configuration will be used. api_key: AssemblyAI API key. """try:importassemblyaiexceptImportError:raiseImportError("Could not import assemblyai python package. ""Please install it with `pip install assemblyai`.")ifapi_keyisnotNone:assemblyai.settings.api_key=api_keyself.file_path=str(file_path)self.transcript_format=transcript_formatself.transcriber=assemblyai.Transcriber(config=config)
[docs]deflazy_load(self)->Iterator[Document]:"""Transcribes the audio file and loads the transcript into documents. It uses the AssemblyAI API to transcribe the audio file and blocks until the transcription is finished. """transcript=self.transcriber.transcribe(self.file_path)# This will raise a ValueError if no API key is set.iftranscript.error:raiseValueError(f"Could not transcribe file: {transcript.error}")ifself.transcript_format==TranscriptFormat.TEXT:yieldDocument(page_content=transcript.text,metadata=transcript.json_response)elifself.transcript_format==TranscriptFormat.SENTENCES:sentences=transcript.get_sentences()forsinsentences:yieldDocument(page_content=s.text,metadata=s.dict(exclude={"text"}))elifself.transcript_format==TranscriptFormat.PARAGRAPHS:paragraphs=transcript.get_paragraphs()forpinparagraphs:yieldDocument(page_content=p.text,metadata=p.dict(exclude={"text"}))elifself.transcript_format==TranscriptFormat.SUBTITLES_SRT:yieldDocument(page_content=transcript.export_subtitles_srt())elifself.transcript_format==TranscriptFormat.SUBTITLES_VTT:yieldDocument(page_content=transcript.export_subtitles_vtt())else:raiseValueError("Unknown transcript format.")
[docs]classAssemblyAIAudioLoaderById(BaseLoader):""" Load AssemblyAI audio transcripts. It uses the AssemblyAI API to get an existing transcription and loads the transcribed text into one or more Documents, depending on the specified format. """
[docs]def__init__(self,transcript_id:str,api_key:str,transcript_format:TranscriptFormat):""" Initializes the AssemblyAI AssemblyAIAudioLoaderById. Args: transcript_id: Id of an existing transcription. transcript_format: Transcript format to use. See class ``TranscriptFormat`` for more info. api_key: AssemblyAI API key. """self.api_key=api_keyself.transcript_id=transcript_idself.transcript_format=transcript_format