本文介绍了使用 Hugging Face 提供的模型将音频文件转录为文本。
安装依赖
安装txtai和所有依赖项。由于本文使用的是可选管道,因此我们需要安装管道附加包。我们还将演示如何通过 API 运行此管道。
pip install txtai[api,pipeline]
Get test data
wget -N https://github.com/neuml/txtai/releases/download/v3.5.0/tests.tar.gz
tar -xvzf tests.tar.gz
创建转录实例
Transcription 实例是将音频转录为文本的主要入口点。管道将转录音频抽象为一行调用!
管道执行逻辑以将音频文件读入内存,通过机器学习模型运行数据并将结果输出为文本。
from txtai.pipeline import Transcription
Create transcription model
transcribe = Transcription()
将音频转录为文本
下面的示例显示了如何将音频文件列表转录为文本。让我们将音频转录为文本并查看每个结果。
from IPython.display import Audio, display
files = [“Beijing_mobilises.wav”, “Canadas_last_fully.wav”, “Maine_man_wins_1_mil.wav”, “Make_huge_profits.wav”, “The_National_Park.wav”, “US_tops_5_million.wav”]
files = [“txtai/%s” % x for x in files]
for x, text in enumerate(transcribe(files)):
display(Audio(files[x]))
print(text)
print()
Baging mobilizes invasion kraft along coast as tie one tensions escalates
Canodas last fully intact ice shelf has suddenly collapsed forming a manhattan sized iceberge
Main man wins from lottery ticket
Make huge profits without working make up to one hundred thousand dollars a day
National park service warns against sacrificing slower friends in a bare attack
Ues virus cases top a million
总的来说,结果是可靠的。每个结果在语音上听起来都像音频。
OpenAI 耳语
2022 年 9 月,OpenAI Whisper发布。该模型显着提高了转录质量。v4.23.0 中的 Hugging Face Transformers 添加了耳语支持。试一试吧。
transcribe = Transcription(“openai/whisper-base”)
for text in transcribe(files):
print(text)
Beijing mobilizes invasion craft along coast as Taiwan tensions escalate.
Canada’s last fully intact ice shelf has suddenly collapsed, forming a Manhattan sized iceberg.
Maine Man wins from lottery ticket.
make huge profits without working. Make up to $100,000 a day.
National Park Service warns against sacrificing slower friends in a bear attack.
U.S. virus cases top of million.
结果以近乎完美的准确度转录,太棒了!
这也可以作为 txtai 应用程序或 API 实例运行。让我们使用 txtai 应用程序尝试完整的索引工作流程。
writable: true
embeddings:
path: sentence-transformers/nli-mpnet-base-v2
content: true
transcription:
path: openai/whisper-base
workflow:
index:
tasks:
- transcription
- index
from txtai.app import Application
app = Application(“workflow.yml”)
list(app.workflow(“index”, files))
app.search(“feel good story”, 1)
[{‘id’: ‘2’,
‘text’: ‘Maine Man wins from lottery ticket.’,
‘score’: 0.1285860687494278}]
该工作流程转录输入文件,将转录内容加载到嵌入索引中,最后运行搜索。我们要做的最后一件事是将工作流作为 API 实例运行。
CONFIG=workflow.yml uvicorn “txtai.api:app” &> api.log &
sleep 30
Run indexing workflow
curl -s -o /dev/null
-X POST “http://localhost:8000/workflow”
-H “Content-Type: application/json”
-d ‘{“name”:“index”, “elements”:[“txtai/Beijing_mobilises.wav”, “txtai/Canadas_last_fully.wav”, “txtai/Maine_man_wins_1_mil.wav”, “txtai/Make_huge_profits.wav”, “txtai/The_National_Park.wav”, “txtai/US_tops_5_million.wav”]}’
Test API search
curl “http://localhost:8000/search?query=feel+good+story&limit=1”
[{“id”:“2”,“text”:“Maine Man wins from lottery ticket.”,“score”:0.1285860687494278}]
同样,结果与 Python 和应用程序相同。
包起来
音频转录领域有很多发展。只需几行代码,即可轻松获得高质量的转录模型!