AssemblyAI 音频转录
The AssemblyAIAudioTranscriptLoader 允许使用 AssemblyAI API 转录音频文件,并将转录的文本加载到文档中。
要使用它,您应该已经安装了assemblyai python 包,并设置了环境变量ASSEMBLYAI_API_KEY以包含您的 API 密钥。或者,API 密钥也可以作为参数传递。
有关AssemblyAI的更多信息:
安装
首先,你需要安装LangChain Python 包。
您可以在assemblyai-python-sdk GitHub 仓库中找到更多相关信息。
%pip install --upgrade --quiet assemblyai
示例
The AssemblyAIAudioTranscriptLoader 需至少包含一个 file_path 参数。音频文件可以指定为URL或本地文件路径。
from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
audio_file = "https://storage.googleapis.com/aai-docs-samples/nbc.mp3"
# or a local file path: audio_file = "./nbc.mp3"
loader = AssemblyAIAudioTranscriptLoader(file_path=audio_file)
docs = loader.load()
API 参考:AssemblyAI音频转录加载器
注释:调用loader.load()会阻塞直到转录完成。
The transcribed text is available in the page_content:
docs[0].page_content
"Load time, a new president and new congressional makeup. Same old ..."
The metadata 包含完整的 JSON 响应并带有更多元数据信息:
docs[0].metadata
{'language_code': <LanguageCode.en_us: 'en_us'>,
'audio_url': 'https://storage.googleapis.com/aai-docs-samples/nbc.mp3',
'punctuate': True,
'format_text': True,
...
}
Transcript Formats
您可以为不同的格式指定transcript_format参数。
取决于格式,可能会返回一个或多个文档。这些是不同的TranscriptFormat选项:
TEXT: 一份包含转录文本的文档SENTENCES: 多个文档,按每个句子分割转录PARAGRAPHS: 多个文档,通过每个段落分割转录SUBTITLES_SRT: 一份包含字幕格式导出的转录文件SUBTITLES_VTT: 一个包含字幕文件格式导出的转录文档
from langchain_community.document_loaders.assemblyai import TranscriptFormat
loader = AssemblyAIAudioTranscriptLoader(
file_path="./your_file.mp3",
transcript_format=TranscriptFormat.SENTENCES,
)
docs = loader.load()
API 参考:TranscriptFormat
转换配置
您还可以指定config参数以使用不同的音频智能模型。
Visit the AssemblyAI API 文档 to get an overview of all available models!
import assemblyai as aai
config = aai.TranscriptionConfig(
speaker_labels=True, auto_chapters=True, entity_detection=True
)
loader = AssemblyAIAudioTranscriptLoader(file_path="./your_file.mp3", config=config)
将 API 密钥作为参数传递
除了将API密钥设置为环境变量ASSEMBLYAI_API_KEY外,还可以将其作为参数传递。
loader = AssemblyAIAudioTranscriptLoader(
file_path="./your_file.mp3", api_key="YOUR_KEY"
)