AssemblyAI 音訊轉錄
AssemblyAIAudioTranscriptLoader
允許使用 AssemblyAI API 轉錄音訊檔案,並將轉錄的文字載入到文件中。
要使用它,您應該已安裝 assemblyai
Python 套件,並且環境變數 ASSEMBLYAI_API_KEY
設定為您的 API 金鑰。 此外,API 金鑰也可以作為參數傳遞。
關於 AssemblyAI 的更多資訊
安裝
首先,您需要安裝 assemblyai
Python 套件。
您可以在 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
之外,也可以將其作為參數傳遞。
loader = AssemblyAIAudioTranscriptLoader(
file_path="./your_file.mp3", api_key="YOUR_KEY"
)