本文最后更新于:2023年10月26日 早上
完全可以阅读官方仓库的说明 代替本文,这里只是记录自己的部署过程。
1. 部署服务 1.1 使用一键部署工具 该脚本实际仍是使用docker启动的服务
2. 脚本的其他命令 启动服务
1 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh start
停止服务
1 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh stop
释放部署的服务
1 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh remove
重启服务
1 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh restart
更换模型
1 2 3 4 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update [--asr_model | --vad_model | --punc_model] <model_id or local model path> e.g sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update --asr_model damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch
修改参数
1 2 3 4 5 6 7 8 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update [--host_port | --docker_port] <port number> sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update [--decode_thread_num | --io_thread_num] <the number of threads> sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update [--workspace] <workspace in local > sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update [--ssl] <0: close SSL; 1: open SSL, default:1> e.g sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update --decode_thread_num 32 sudo bash funasr-runtime-deploy-offline-cpu-zh.sh update --workspace /root/funasr-runtime-resources
关闭ssl
1 sudo bash funasr-runtime-deploy-online-cpu-zh.sh update --ssl 0
3. 客户端 3.1 官方示例 执行完脚本后,会自动下载示例代码到./funasr-runtime-resources/samples/python/
中,也可以这里 下载。该脚本需要ffmpeg和websockets,如果缺的话再安装。
1 2 apt install ffmpeg pip install websockets ffmpeg-python
运行测试
1 python funasr_wss_client.py --host "127.0.0.1" --port 20002 --mode offline --audio_in "../audio/asr_example.wav" --ssl 0
输出:
1 2 3 4 5 Namespace(host='127.0.0.1' , port=20002, chunk_size=[5, 10, 5], chunk_interval=10, hotword='' , audio_in='../audio/asr_example.wav' , send_without_sleep=True, thread_num=1, words_max_print=10000, output_dir=None, ssl=0, use_itn=1, mode='offline' ) connect to ws://127.0.0.1:20002 pid0_0: demo: 欢迎大家来体验达摩院推出的语音识别模型。 Exception: sent 1000 (OK); then received 1000 (OK) end
该示例的其他参数
1 2 3 4 5 6 7 8 --server-ip 为FunASR runtime-SDK服务部署机器ip,默认为本机ip(127.0.0.1),如果client与服务不在同一台服务器, 需要改为部署机器ip --port xxxx 部署端口号 --wav-path 需要进行转写的音频文件,支持文件路径 --thread_num 设置并发发送线程数,默认为1 --ssl 设置是否开启ssl证书校验,默认1开启,设置为0关闭 --hotword 如果模型为热词模型,可以设置热词: *.txt(每行一个热词) 或者空格分隔的热词字符串 (阿里巴巴 达摩院) --use-itn 设置是否使用itn,默认1开启,设置为0关闭
3.2 裁切后 官方提供的示例功能多,所以内容也多,我实际上也就使用单音频的离线转换,所以下列示例就可以了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import websocketsimport asyncioimport waveimport jsonasync def ws_client (): url = "ws://127.0.0.1:20002" async with websockets.connect(url, subprotocols=["binary" ], ping_interval=None ) as websocket: wav_path = "../audio/asr_example.wav" with wave.open (wav_path, "rb" ) as wav_file: params = wav_file.getparams() sample_rate = wav_file.getframerate() frames = wav_file.readframes(wav_file.getnframes()) audio_bytes = bytes (frames) message = json.dumps ({"mode" : "offline" , "chunk_size" : [5 , 10 , 5 ], "chunk_interval" : 10 , "audio_fs" : sample_rate, "wav_name" : "demo" , "is_speaking" : True , "hotwords" : "" , "itn" : True }) await websocket.send(message) chunk_interval = 10 chunk_size = 10 stride = int (60 * chunk_size / chunk_interval / 1000 * 16000 * 2 ) chunk_num = (len (audio_bytes) - 1 ) // stride + 1 print ("chunk_num :{}" .format (chunk_num)) print ("stride :{}" .format (stride)) for i in range (chunk_num): beg = i * stride data = audio_bytes[beg:beg + stride] message = data await websocket.send(message) if i == chunk_num - 1 : is_speaking = False message = json.dumps({"is_speaking" : is_speaking}) await websocket.send(message) await asyncio.sleep(0.001 ) response = await websocket.recv() print (f"Received: {response} " ) asyncio.get_event_loop().run_until_complete(ws_client())
3.3 kuonasr仓库 将上述代码添加到kuonasr 仓库中,修改配置选择funasr,填写url后即可通过下列方式使用。
1 2 3 4 5 6 7 from kuonasr import ASR asr = ASR ()try : result = asr.convert ("./kuonasr/audio/asr_example.wav" ) print (result )except Exception as e : print (e )