当前位置: 欣欣网 > 码农

开源初探 - stt

2024-03-03码农

介绍说明

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

前置准备

  • 安装 CUDA

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

    首次登录,发现正在安装 C UDA

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

  • 安装 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.listsed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.listsudo apt-get updatesudo apt-get install -y nvidia-container-toolkit

    配置 docker:

    sudo nvidia-ctk runtime configure --runtime=dockersudo systemctl restart docker

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

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

    安装运行

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

  • 准备 Dockerfile

  • FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04# 安装 pythonRUN apt-get updateRUN apt-getinstall python3 python3-pip -y# 安装 gitRUN apt-getinstall git -y# 下载代码RUN git clone https://github.com/jianchang512/stt.gitWORKDIR /sttRUN pip install -r requirements.txtRUN pip uninstall -y torchRUN 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/libCMD ["python3""start.py"]

  • 构建 docker 镜像

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

    docker build -t stt:latest -f ./Dockerfile .dockerimages | grep stt

    使用简介

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

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

  • ; after update set , please restart the app; ip:port 监听地址和端口web_address=0.0.0.0:9977;en or zhlang=; cpu or cuda,使用 cpu 还是 gpu,这里使用 gpudevtype=cuda; int8 or float32 only gpucuda_com_type=int8;Reducing these two numbers will use less graphics memorybeam_size=1best_of=1;vad set to false,use litter GPU memory,true is morevad=true;0 is use litter GPU,other is moretemperature=0;false is litter GPU,ture is morecondition_on_previous_text=falseinitial_prompt_zh=以下是普通话内容,请转录为中文简体。

  • 下载模型文件

  • 可以在以下地址下载:

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

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

    wget https://huggingface.co/spaces/mortimerme/s4/resolve/main/largeV3Model-extract-models-folder-解压到models目录下.7zapt-get install p7zip-full7za x largeV3Model-extract-models-folder-解压到models目录下.7z

  • 运行 stt

  • dockerrm -f sttdockerrun --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:latestdockerps | grep stt

  • 准备测试文件 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=600data=data,files=files)print(response.json())

  • 准备测试音频文件

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

  • 执行转换

  • pip install requestspython3 test.py

    GPU 使用情况:

    转换结果 - json:

    转换结果 - text

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

    项目地址

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

    https://github.com/jianchang512/stt

    快捷镜像

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

    更多快捷镜像

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

    写在最后

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