audio.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
  2. from langchain_community.document_loaders.assemblyai import TranscriptFormat
  3. import assemblyai as aai
  4. from dotenv import load_dotenv, find_dotenv
  5. import os
  6. class Audio:
  7. audio_file_path = ""
  8. api_key = ""
  9. def __init__(self):
  10. load_dotenv(find_dotenv())
  11. self.api_key = os.getenv("AI_AUDIO_TRANSCRIPT_KEY")
  12. def transcribe(self, audio_file_path):
  13. # Validar que el archivo existe
  14. self.audio_file_path = audio_file_path
  15. config = aai.TranscriptionConfig(
  16. speaker_labels=True,
  17. auto_chapters=False,
  18. entity_detection=True,
  19. language_code=aai.LanguageCode.es,
  20. )
  21. loader = AssemblyAIAudioTranscriptLoader(
  22. file_path=self.audio_file_path,
  23. api_key=self.api_key,
  24. config=config,
  25. transcript_format=TranscriptFormat.TEXT,
  26. )
  27. docs = loader.load()
  28. return docs[0].page_content