开源初探 - stt

介绍说明

这是一个离线运行的本地语音识别转文字工具,基于 fast-whipser 开源模型,可将视频/音频中的人类声音识别并转为文字,可输出 json 格式、srt 字幕带时间戳格式、纯文字格式。可用于替代 openai 的语音识别接口或百度语音识别等,准确率基本等同 openai 官方 api 接口。

前置准备

  • 安装 CUDA

由于要使用 GPU,所以需要安装 CUDA,这里使用公有云,可以自动完成安装

734c96423b0e4e9c07d8d355b88cca18.png

首次登录,发现正在安装 CUDA:

a6eaf96bba62fd7c14ace6745bf70678.png

完成后,使用 nvidia-smi 检查:

0b3f936a1ba1ad16678eae98df28449b.png

  • 安装 NVIDIA Container Toolkit

用于在容器中使用 CUDA

参考:

https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list


sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list


sudo apt-get update


sudo apt-get install -y nvidia-container-toolkit

1081d70ea9537f400171bdcad7893e2f.png

配置 docker:

sudo nvidia-ctk runtime configure --runtime=docker


sudo systemctl restart docker

21bcac60e369e9b230a1ab1af8bba036.png

完成后,使用以下指令检查:

docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi

e0206a80ef454ab04843cb6516ad47ef.png

安装运行

为了方便演示,将使用 docker 的方式来安装运行 stt。

  • 准备 Dockerfile

FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04


# 安装 python
RUN apt-get update
RUN apt-get install python3 python3-pip -y


# 安装 git
RUN apt-get install git -y


# 下载代码
RUN git clone https://github.com/jianchang512/stt.git
WORKDIR /stt
RUN pip install -r requirements.txt
RUN pip uninstall -y torch
RUN pip install torch --index-url https://download.pytorch.org/whl/cu121


# 添加环境变量
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/python3.10/dist-packages/nvidia/cublas/lib:/usr/local/lib/python3.10/dist-packages/nvidia/cudnn/lib


CMD ["python3", "start.py"]
  • 构建 docker 镜像

在项目根目录或 Dockerfile 所在目录执行以下指令:

docker build -t stt:latest -f ./Dockerfile .


docker images | grep stt

a597fd8aeaaf9bc6fb96a7825dbb85b6.png

使用简介

镜像打好后就可以使用 stt 了。

  • 准备启动配置文件 set.ini

; after update set , please restart the app
; ip:port 监听地址和端口
web_address=0.0.0.0:9977
;en or zh
lang=
; cpu or cuda,使用 cpu 还是 gpu,这里使用 gpu
devtype=cuda
; int8 or float32 only gpu
cuda_com_type=int8
;Reducing these two numbers will use less graphics memory
beam_size=1
best_of=1
;vad set to false,use litter GPU memory,true is more
vad=true
;0 is use litter GPU,other is more
temperature=0
;false is litter GPU,ture is more
condition_on_previous_text=false
initial_prompt_zh=以下是普通话内容,请转录为中文简体。
  • 下载模型文件

可以在以下地址下载:

https://github.com/jianchang512/stt/releases/tag/0.0

1f0c1b990639d46adc118a921bceaf8f.png

这里使用 large-v3,使用以下指令下载并解压:

wget https://huggingface.co/spaces/mortimerme/s4/resolve/main/largeV3Model-extract-models-folder-%E8%A7%A3%E5%8E%8B%E5%88%B0models%E7%9B%AE%E5%BD%95%E4%B8%8B.7z


apt-get install p7zip-full


7za x largeV3Model-extract-models-folder-解压到models目录下.7z

d99baa92737d5767f17de3702343e48b.png

  • 运行 stt

docker rm -f stt


docker run --name stt -d \
--runtime=nvidia --gpus all \
-p 9977:9977 \
-v ${PWD}/set.ini:/stt/set.ini \
-v ${PWD}/models--Systran--faster-whisper-large-v3:/stt/models/models--Systran--faster-whisper-large-v3 \
stt:latest


docker ps | grep stt

6110b604db34c695fc89325a3242b532.png

  • 准备测试文件 test.py

import requests
# 请求地址
url = "http://127.0.0.1:9977/api"
# 请求参数  file:音视频文件,language:语言代码,model:模型,response_format:text|json|srt
# 返回 code==0 成功,其他失败,msg==成功为ok,其他失败原因,data=识别后返回文字
files = {"file": open("/root/test.wav", "rb")}
data={"language":"zh","model":"large-v3","response_format":"json"}
response = requests.request("POST", url, timeout=600, data=data,files=files)
print(response.json())
  • 准备测试音频文件

这里使用的是 wav 文件,mp3 文件也是支持的。

e4b04381a1591ab0c2d32bb3ca8d3bc3.png

  • 执行转换

pip install requests
python3 test.py

GPU 使用情况:

791b889f29cd465d7d94ecdff0308c11.png

转换结果 - json:

763f3ab287db227bbdea96dc6f34afac.png

转换结果 - text:

1af85f8b65fe2e423f5b6040727f7e27.png

对比了下音频文件,会有点瑕疵,准确率大概 90% 左右。

项目地址

更多项目详细信息请到项目主页获取

https://github.com/jianchang512/stt

快捷镜像

ccr.ccs.tencentyun.com/erik_xu/stt:latest

更多快捷镜像

https://zhuanlan.zhihu.com/p/678429467

写在最后

以上就是本文希望分享的内容,大家如果有问题咨询、技术支持、开源推荐等,欢迎在公众号【跬步之巅】留言交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值