AssemblyAI 音频转录
这AssemblyAIAudioTranscriptLoader允许使用 AssemblyAI API 转录音频文件并将转录的文本加载到文档中。
要使用它,您应该拥有assemblyaipython 软件包,并且
环境变量ASSEMBLYAI_API_KEYset 替换为您的 API 密钥。或者,API 密钥也可以作为参数传递。
有关 AssemblyAI 的更多信息:
安装
首先,您需要安装assemblyaipython 软件包。
您可以在 assemblyai-python-sdk GitHub 存储库中找到有关它的更多信息。
%pip install --upgrade --quiet assemblyai
例
这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()
注意:调用loader.load()阻塞,直到转录完成。
转录的文本位于page_content:
docs[0].page_content
"Load time, a new president and new congressional makeup. Same old ..."
这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_format不同格式的参数。
根据格式,将返回一个或多个文档。这些是不同的TranscriptFormat选项:
TEXT:一个包含转录文本的文档SENTENCES:多个文档,按每个句子拆分转录PARAGRAPHS:多个文档,按每个段落拆分转录SUBTITLES_SRT:一个文档,其中的成绩单以 SRT 字幕格式导出SUBTITLES_VTT:一个文档,其中的转录文本以 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参数来使用不同的音频智能模型。
访问 AssemblyAI API 文档以获取所有可用模型的概述!
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,也可以将其作为 argument 传递。
loader = AssemblyAIAudioTranscriptLoader(
file_path="./your_file.mp3", api_key="YOUR_KEY"
)