FunASR开源部署中文实时语音听写服务(CPU)
FunASR实时语音听写服务封装,既可以实时地进行语音转文字,而且能够在说话句尾用可以的转写文字修改输出,输出文字带有标签点,支持高并发多路请求。这是开源库中原有的镜像与服务。并未做任何修改!
此次部署是在windows中的wsl环境中完成的。
FunASR实时语音听写,继承了语音端点检测、语音识别、标点预测模型等,采用多模型良好,既可以实时的进行语音转文字,输出带标点的文字,而且支持多路请求。
第一步:安装docker
如果在linux环境或者wsl虚拟环境中已经安装过docker,可忽略此步骤。
curl -O https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/shell/install_docker.sh sudo bash install_docker.sh
第二步:镜像启动
sudo docker pull \ registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.13 mkdir -p ./funasr-runtime-resources/models sudo docker run -p 10096:10095 -it --privileged=true \ -v $PWD/funasr-runtime-resources/models:/workspace/models \ registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.13
命令说明:
sudo docker pull \ registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.13
作用:从阿里云的镜像仓库中拉取FunASR的在线推理CPU版本的镜像。
镜像名称:funasr-runtime-sdk-online-cpu-0.1.13
拉下来后,本地就有了这个容器运行所需的环境和依赖
mkdir -p ./funasr-runtime-resources/models
作用:在当前目录下新建一个路径 ./funasr-runtime-resources/models(如果路径不存在就创建)。
这里用来存放模型文件,容器运行时会挂载这个目录。
启动容器:
sudo docker run -p 10096:10095 -it --privileged=true \
-v $PWD/funasr-runtime-resources/models:/workspace/models \
registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.13
逐项说明:
sudo docker run: 启动一个新的容器
-p 10096:10095:把宿主机的10096端口映射至容器的10095端口,方便外部调用容器里的ASR服务。
-it:交互模式(方便在容器中调试)
--privileged=true:给容器更高的权限(比如可以访问设备)
-v $PWD/funasr-runtime-resources/models:/workspace/models:
把宿主机当前目录下的funasr-runtime-resources/models挂载到容器内的/workspace/models.
这样宿主机下载/存放的模型,容器内部也能访问到。
registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.13
指定使用刚才拉取的镜像。
整套命令的流程就是:
从阿里云仓库拉取 FunASR 在线 CPU 运行时镜像。
在宿主机创建一个模型存放目录。
启动容器,映射端口
10096 -> 10095
,挂载模型目录,运行 FunASR 在线推理服务。
启动成功后,在powershell中会出现以下字样: Digest: sha256:2a54c20f6b9e2fd253b24abd5c083545751485bcc26e038ed12eab3380c86d51
Status: Image is up to date for registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.13
这表明镜像已经成功拉取,使用以下命令再次确认:
sudo docker images | grep funasr
看到类似这一行就说明在本地了:
registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr funasr-runtime-sdk-online-cpu-0.1.13 <IMAGE_ID> <CREATED> <SIZE>
第三步:启动服务
我启动的是funasr-wss-server-2pass服务。同时指定好模型和资源路径。
在容器内执行:
cd /workspace/FunASR/runtime/websocket/build/bin ./funasr-wss-server-2pass \
--download-model-dir "/workspace/models" \
--model-dir "damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-onnx" \
--online-model-dir "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx" \
--vad-dir "damo/speech_fsmn_vad_zh-cn-16k-common-onnx" \
--punc-dir "damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx" \
--itn-dir "thuduj12/fst_itn_zh" \
--lm-dir "damo/speech_ngram_lm_zh-cn-ai-wesp-fst" \
--decoder-thread-num $(nproc) \
--model-thread-num 1 \
--io-thread-num 2 \
--port 10095
说明:
--download-model-dir "/workspace/models"
挂载到宿主机的目录,模型会下载在这里,复用方便。
--model-dir
:离线大模型(高精度修正用)
--online-model-dir
:在线模型(低延迟初稿用)
--vad-dir
:VAD 语音端点检测模型
--punc-dir
:标点预测模型
--itn-dir
:数字/日期反向转换规则
--lm-dir
:语言模型
--port 10095
:容器监听的端口,你宿主机访问 http://localhost:10096
(因为 -p 10096:10095
)
日志最后出现这句说明服务已经启动成功:
asr model init finished. listen on port:10095
现在这个窗口就是服务主进程窗口,不要 Ctrl+C
或 exit
。想看日志滚动就保持开着;另开一个终端做测试。
另开一个终端做测试有两种方式,一是在宿主机用 websocat(最直观):
websocat ws://localhost:10096
能握手成功,说明通路 OK(只是没按协议发音频,不会返回识别结果)。
二是用容器自带客户端测试脚本做测试,我使用的是:funasr_wss_client.py,将容器内的脚本拷贝到本地:
sudo docker cp funasr_online_cpu:/workspace/FunASR/runtime/python/websocket/funasr_wss_client.py ./funasr_wss_client.py
然后就能在wsl中运行本地脚本了。
python funasr_wss_client.py --host localhost --port 10096 --ssl 0 --mode 2pass --audio_in "/mnt/d/ASR/test_audio/audio.wav"
/mnt/d:为wsl虚拟环境中本地环境D盘的写法,因为在wsl中识别不到D:/的写法。
运行成功后就可以看到终端会实时识别出音频内容转文字。