FunASR/runtime/docs/SDK_advanced_guide_offline_...

283 lines
15 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# FunASR离线文件转写服务开发指南
(简体中文|[English](SDK_advanced_guide_offline.md))
FunASR离线文件转写软件包提供了一款功能强大的语音离线文件转写服务。拥有完整的语音识别链路结合了语音端点检测、语音识别、标点等模型可以将几十个小时的长音频与视频识别成带标点的文字而且支持上百路请求同时进行转写。输出为带标点的文字含有字级别时间戳支持ITN与用户自定义热词等。服务端集成有ffmpeg支持各种音视频格式输入。软件包提供有html、python、c++、java与c#等多种编程语言客户端,用户可以直接使用与进一步开发。
本文档为FunASR离线文件转写服务开发指南。如果您想快速体验离线文件转写服务可参考[快速上手](#快速上手)。
<img src="images/offline_structure.jpg" width="900"/>
| 时间 | 详情 | 镜像版本 | 镜像ID |
|------------|---------------------------------------------------|------------------------------|--------------|
| 2024.05.15 | 适配FunASR 1.0模型结构 | funasr-runtime-sdk-cpu-0.4.5 | 058b9882ae67 |
| 2024.03.05 | docker镜像支持arm64平台升级modelscope版本 | funasr-runtime-sdk-cpu-0.4.4 | 2dc87b86dc49 |
| 2024.01.25 | 优化vad数据处理方式大幅降低峰值内存占用内存泄漏优化| funasr-runtime-sdk-cpu-0.4.2 | befdc7b179ed |
| 2024.01.08 | 优化句子级时间戳json格式 | funasr-runtime-sdk-cpu-0.4.1 | 0250f8ef981b |
| 2024.01.03 | 新增支持8k模型、优化时间戳不匹配问题及增加句子级别时间戳、优化英文单词fst热词效果、支持自动化配置线程参数同时修复已知的crash问题及内存泄漏问题 | funasr-runtime-sdk-cpu-0.4.0 | c4483ee08f04 |
| 2023.11.08 | 支持标点大模型、支持Ngram模型、支持fst热词、支持服务端加载热词、runtime结构变化适配 | funasr-runtime-sdk-cpu-0.3.0 | caa64bddbb43 |
| 2023.09.19 | 支持ITN模型 | funasr-runtime-sdk-cpu-0.2.2 | 2c5286be13e9 |
| 2023.08.22 | 集成ffmpeg支持多种音视频输入、支持热词模型、支持时间戳模型 | funasr-runtime-sdk-cpu-0.2.0 | 1ad3d19e0707 |
| 2023.07.03 | 1.0 发布 | funasr-runtime-sdk-cpu-0.1.0 | 1ad3d19e0707 |
## 服务器配置
用户可以根据自己的业务需求,选择合适的服务器配置,推荐配置为:
- 配置1: X86计算型4核vCPU内存8G单机可以支持大约32路的请求
- 配置2: X86计算型16核vCPU内存32G单机可以支持大约64路的请求
- 配置3: X86计算型64核vCPU内存128G单机可以支持大约200路的请求
详细性能测试报告([点击此处](./benchmark_onnx_cpp.md)
云服务厂商针对新用户有3个月免费试用活动申请教程[点击此处](https://github.com/alibaba-damo-academy/FunASR/blob/main/runtime/docs/aliyun_server_tutorial.md)
## 快速上手
### docker安装
如果您已安装docker忽略本步骤!
通过下述命令在服务器上安装docker
```shell
curl -O https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/shell/install_docker.sh
sudo bash install_docker.sh
```
docker安装失败请参考 [Docker Installation](https://alibaba-damo-academy.github.io/FunASR/en/installation/docker.html)
### 镜像启动
通过下述命令拉取并启动FunASR软件包的docker镜像
```shell
sudo docker pull \
registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-cpu-0.4.5
mkdir -p ./funasr-runtime-resources/models
sudo docker run -p 10095:10095 -it --privileged=true \
-v $PWD/funasr-runtime-resources/models:/workspace/models \
registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-cpu-0.4.5
```
### 服务端启动
docker启动之后启动 funasr-wss-server服务程序
```shell
cd FunASR/runtime
nohup bash run_server.sh \
--download-model-dir /workspace/models \
--vad-dir damo/speech_fsmn_vad_zh-cn-16k-common-onnx \
--model-dir damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-onnx \
--punc-dir damo/punc_ct-transformer_cn-en-common-vocab471067-large-onnx \
--lm-dir damo/speech_ngram_lm_zh-cn-ai-wesp-fst \
--itn-dir thuduj12/fst_itn_zh \
--hotword /workspace/models/hotwords.txt > log.txt 2>&1 &
# 如果您想关闭ssl增加参数--certfile 0
# 如果您想使用时间戳或者nn热词模型进行部署请设置--model-dir为对应模型
# damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-onnx时间戳
# damo/speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404-onnxnn热词
# 如果您想在服务端加载热词,请在宿主机文件./funasr-runtime-resources/models/hotwords.txt配置热词docker映射地址为/workspace/models/hotwords.txt:
# 每行一个热词,格式(热词 权重):阿里巴巴 20热词理论上无限制但为了兼顾性能和效果建议热词长度不超过10个数不超过1k权重1~100
```
如果您想定制ngram参考文档([如何训练LM](./lm_train_tutorial.md))
如果您想部署8k的模型请使用如下命令启动服务
```shell
cd FunASR/runtime
nohup bash run_server.sh \
--download-model-dir /workspace/models \
--vad-dir damo/speech_fsmn_vad_zh-cn-8k-common-onnx \
--model-dir damo/speech_paraformer_asr_nat-zh-cn-8k-common-vocab8358-tensorflow1-onnx \
--punc-dir damo/punc_ct-transformer_cn-en-common-vocab471067-large-onnx \
--lm-dir damo/speech_ngram_lm_zh-cn-ai-wesp-fst-token8358 \
--itn-dir thuduj12/fst_itn_zh \
--hotword /workspace/models/hotwords.txt > log.txt 2>&1 &
```
服务端详细参数介绍可参考[服务端用法详解](#服务端用法详解)
### 客户端测试与使用
下载客户端测试工具目录samples
```shell
wget https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/sample/funasr_samples.tar.gz
```
我们以Python语言客户端为例进行说明支持多种音频格式输入.wav, .pcm, .mp3等也支持视频输入(.mp4等)以及多文件列表wav.scp输入其他版本客户端请参考文档[点击此处](#客户端用法详解)),定制服务部署请参考[如何定制服务部署](#如何定制服务部署)
```shell
python3 funasr_wss_client.py --host "127.0.0.1" --port 10095 --mode offline --audio_in "../audio/asr_example.wav"
```
## 客户端用法详解
在服务器上完成FunASR服务部署以后可以通过如下的步骤来测试和使用离线文件转写服务。
目前分别支持以下几种编程语言客户端
- [Python](#python-client)
- [CPP](#cpp-client)
- [html网页版本](#Html网页版)
- [Java](#Java-client)
### python-client
若想直接运行client进行测试可参考如下简易说明以python版本为例
```shell
python3 funasr_wss_client.py --host "127.0.0.1" --port 10095 --mode offline \
--audio_in "../audio/asr_example.wav" --output_dir "./results"
```
命令参数说明:
```text
--host 为FunASR runtime-SDK服务部署机器ip默认为本机ip127.0.0.1如果client与服务不在同一台服务器
需要改为部署机器ip
--port 10095 部署端口号
--mode offline表示离线文件转写
--audio_in 需要进行转写的音频文件支持文件路径文件列表wav.scp
--thread_num 设置并发发送线程数默认为1
--ssl 设置是否开启ssl证书校验默认1开启设置为0关闭
--hotword 热词文件,每行一个热词,格式(热词 权重):阿里巴巴 20
--use_itn 设置是否使用itn默认1开启设置为0关闭
```
### cpp-client
进入samples/cpp目录后可以用cpp进行测试指令如下
```shell
./funasr-wss-client --server-ip 127.0.0.1 --port 10095 --wav-path ../audio/asr_example.wav
```
命令参数说明:
```text
--server-ip 为FunASR runtime-SDK服务部署机器ip默认为本机ip127.0.0.1如果client与服务不在同一台服务器
需要改为部署机器ip
--port 10095 部署端口号
--wav-path 需要进行转写的音频文件,支持文件路径
--hotword 热词文件,每行一个热词,格式(热词 权重):阿里巴巴 20
--use-itn 设置是否使用itn默认1开启设置为0关闭
```
### Html网页版
在浏览器中打开 html/static/index.html即可出现如下页面支持麦克风输入与文件上传直接进行体验
<img src="images/html.png" width="900"/>
### Java-client
```shell
FunasrWsClient --host localhost --port 10095 --audio_in ./asr_example.wav --mode offline
```
详细可以参考文档([点击此处](../java/readme.md)
## 服务端用法详解:
### 启动FunASR服务
```shell
cd /workspace/FunASR/runtime
nohup bash run_server.sh \
--download-model-dir /workspace/models \
--model-dir damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx \
--vad-dir damo/speech_fsmn_vad_zh-cn-16k-common-onnx \
--punc-dir damo/punc_ct-transformer_cn-en-common-vocab471067-large-onnx \
--lm-dir damo/speech_ngram_lm_zh-cn-ai-wesp-fst \
--itn-dir thuduj12/fst_itn_zh \
--certfile ../../../ssl_key/server.crt \
--keyfile ../../../ssl_key/server.key \
--hotword ../../hotwords.txt > log.txt 2>&1 &
```
**run_server.sh命令参数介绍**
```text
--download-model-dir 模型下载地址通过设置model ID从Modelscope下载模型
--model-dir modelscope model ID 或者 本地模型路径
--vad-dir modelscope model ID 或者 本地模型路径
--punc-dir modelscope model ID 或者 本地模型路径
--lm-dir modelscope model ID 或者 本地模型路径
--itn-dir modelscope model ID 或者 本地模型路径
--port 服务端监听的端口号,默认为 10095
--decoder-thread-num 服务端线程池个数(支持的最大并发路数)
脚本会根据服务器线程数自动配置decoder-thread-num、io-thread-num
--io-thread-num 服务端启动的IO线程数
--model-thread-num 每路识别的内部线程数(控制ONNX模型的并行),默认为 1
其中建议 decoder-thread-num*model-thread-num 等于总线程数
--certfile ssl的证书文件默认为../../../ssl_key/server.crt如果需要关闭ssl参数设置为0
--keyfile ssl的密钥文件默认为../../../ssl_key/server.key
--hotword 热词文件路径,每行一个热词,格式:热词 权重(例如:阿里巴巴 20)
如果客户端提供热词,则与客户端提供的热词合并一起使用,服务端热词全局生效,客户端热词只针对对应客户端生效。
```
### 关闭FunASR服务
```text
# 查看 funasr-wss-server 对应的PID
ps -x | grep funasr-wss-server
kill -9 PID
```
### 修改模型及其他参数
替换正在使用的模型或者其他参数需先关闭FunASR服务修改需要替换的参数并重新启动FunASR服务。其中模型需为ModelScope中的ASR/VAD/PUNC模型或者从ModelScope中模型finetune后的模型。
```text
# 例如替换ASR模型为 damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx则如下设置参数 --model-dir
--model-dir damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx
# 设置端口号 --port
--port <port number>
# 设置服务端启动的推理线程数 --decoder-thread-num
--decoder-thread-num <decoder thread num>
# 设置服务端启动的IO线程数 --io-thread-num
--io-thread-num <io thread num>
# 关闭SSL证书
--certfile 0
```
执行上述指令后启动离线文件转写服务。如果模型指定为ModelScope中model id会自动从MoldeScope中下载如下模型
[FSMN-VAD模型](https://www.modelscope.cn/models/damo/speech_fsmn_vad_zh-cn-16k-common-onnx/summary),
[Paraformer-lagre模型](https://www.modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx/summary),
[CT-Transformer标点预测模型](https://www.modelscope.cn/models/damo/punc_ct-transformer_cn-en-common-vocab471067-large-onnx/summary),
[基于FST的中文ITN](https://www.modelscope.cn/models/thuduj12/fst_itn_zh/summary),
[Ngram中文语言模型](https://www.modelscope.cn/models/damo/speech_ngram_lm_zh-cn-ai-wesp-fst/summary)
如果您希望部署您finetune后的模型例如10epoch.pb需要手动将模型重命名为model.pb并将原modelscope中模型model.pb替换掉将路径指定为`model_dir`即可。
------------------
## 如何定制服务部署
FunASR-runtime的代码已开源如果服务端和客户端不能很好的满足您的需求您可以根据自己的需求进行进一步的开发
### c++ 客户端:
https://github.com/alibaba-damo-academy/FunASR/tree/main/runtime/websocket
### python 客户端:
https://github.com/alibaba-damo-academy/FunASR/tree/main/runtime/python/websocket
### 自定义客户端:
如果您想定义自己的client参考[websocket通信协议](./websocket_protocol_zh.md)
### c++ 服务端:
#### VAD
```c++
// VAD模型的使用分为FsmnVadInit和FsmnVadInfer两个步骤
FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
// 其中model_path 包含"model-dir"、"quantize"thread_num为onnx线程数
FUNASR_RESULT result=FsmnVadInfer(vad_hanlde, wav_file.c_str(), NULL, 16000);
// 其中vad_hanlde为FunOfflineInit返回值wav_file为音频路径sampling_rate为采样率(默认16k)
```
使用示例详见https://github.com/alibaba-damo-academy/FunASR/blob/main/runtime/onnxruntime/bin/funasr-onnx-offline-vad.cpp
#### ASR
```text
// ASR模型的使用分为FunOfflineInit和FunOfflineInfer两个步骤
FUNASR_HANDLE asr_hanlde=FunOfflineInit(model_path, thread_num);
// 其中model_path 包含"model-dir"、"quantize"thread_num为onnx线程数
FUNASR_RESULT result=FunOfflineInfer(asr_hanlde, wav_file.c_str(), RASR_NONE, NULL, 16000);
// 其中asr_hanlde为FunOfflineInit返回值wav_file为音频路径sampling_rate为采样率(默认16k)
```
使用示例详见https://github.com/alibaba-damo-academy/FunASR/blob/main/runtime/onnxruntime/bin/funasr-onnx-offline.cpp
#### PUNC
```text
// PUNC模型的使用分为CTTransformerInit和CTTransformerInfer两个步骤
FUNASR_HANDLE punc_hanlde=CTTransformerInit(model_path, thread_num);
// 其中model_path 包含"model-dir"、"quantize"thread_num为onnx线程数
FUNASR_RESULT result=CTTransformerInfer(punc_hanlde, txt_str.c_str(), RASR_NONE, NULL);
// 其中punc_hanlde为CTTransformerInit返回值txt_str为文本
```
使用示例详见https://github.com/alibaba-damo-academy/FunASR/blob/main/runtime/onnxruntime/bin/funasr-onnx-offline-punc.cpp