部署本地语音识别Whisper-jax

1,003 阅读2分钟

安装jax

参见github.com/google/jax#…

  1. 查看本机CUDA版本

tail /usr/local/cuda/version.json

  1. CUDA 11 installation

pip install --upgrade "jax[cuda11_pip]" -f storage.googleapis.com/jax-release…

  1. 给CUDA添加软链接(如果没有/usr/local/cuda, 则使用类似/usr/local/cuda-X.X的路径)

sudo ln -s /usr/local/cuda/bin/nvcc /usr/bin/nvcc

安装whisper-jax(注意:必须先安装jax,再安装whisper)

pip install git+github.com/sanchit-gan…

测试

import time

from whisper_jax import FlaxWhisperPipline
import jax.numpy as jnp

# instantiate pipeline
pipeline = FlaxWhisperPipline("openai/whisper-tiny.en")

# JIT compile the forward call - slow, but we only do once
s_time = time.time()
text = pipeline("test.mp3")
e_time = time.time()
print(text)
cost = e_time - s_time
print(" cost: %s" % cost)

# used cached function thereafter - super fast!!
s_time = time.time()
text = pipeline("men.mp3", batch_size=5)
e_time = time.time()
print(text)
cost = e_time - s_time
print(" cost: %s" % cost)

s_time = time.time()
text = pipeline("men.mp3", batch_size=5)
e_time = time.time()
print(text)
cost = e_time - s_time
print(" cost: %s" % cost)

print("over")

错误解决

错误: ModuleNotFoundError: No module named 'cached_property'

解决: pip install cached-property

错误: FAILED_PRECONDITION: DNN library initialization failed

解决: 安装cuDNN, developer.nvidia.com/rdp/cudnn-a… (需要注册为会员) 下载(链接有跳转): wget -L developer.nvidia.com/downloads/c…

先tar -xf xxx.tar.xz 再复制include和lib到cuda目录 sudo cp include/* /usr/local/cuda-11.4/include sudo cp lib/libcudnn* /usr/local/cuda-11.4/lib64 sudo chmod a+r /usr/local/cuda-11.4/include/cudnn* sudo chmod a+r /usr/local/cuda-11.4/lib64/libcudnn*

错误: No such file or directory: 'ffmpeg'

解决: 安装ffmpeg 下载: wget -b -c johnvansickle.com/ffmpeg/rele…

解压 tar -xv ffmpeg-release-amd64-static.tar.xz mv ffmpeg-release-amd64-static /opt/ffmpeg

建立软链接

ln -s /opt/ffmpeg-6.0/ffmpeg /usr/bin/ffmpeg

ln -s /opt/ffmpeg-6.0/ffprobe /usr/bin/ffprobe

错误: We couldn't connect to 'huggingface.co' to load this file

当使用FlaxWhisperPipline("openai/whisper-base.en")加载模型时, 代码在未检测到本地缓存的情况下, 会去官网下载, 之前多试几次还能成功, 现在不行了.

登录huggingface.co/后, 搜索模型, 根据下图下载模型相关文件, 需要安装git-lfs

如果中国地区还是无法下载, 则可以使用外国的服务器下载后scp到国内机器(命令如下, 因为比较大所以使用nohup指令执行)

sshpass -f ./passwd.txt nohup scp -r whisper-medium root@IP:/opt/whisper 2>&1 > scp-whisper.log &

edd808e5-6a01-4384-a9b5-acaa2f881ded.png

将原模型名改为下载好的模型目录路径, 如: FlaxWhisperPipline("/opt/whisper/whisper-medium.en/")

错误: None of the algorithms provided by cuDNN heuristics worked; trying fallback algorithms.

原因: 可能是显存被占太多了, 部分模型没有加载到内存,所以找不到.

解决: 解除显存被占用太多的问题就好了