SenseVoice- 中国版 Whisper 来了,5分钟带你部署体验

铁佛  金牌会员 | 2025-2-13 18:40:47 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 877|帖子 877|积分 2631

中国版 Whisper 来了,5分钟带你部署体验




原创 猴哥 猴哥的AI知识库 2024年07月23日 11:29 江苏


猴哥的第 46 期分享,欢迎追看
前段时间,带着各人捏了一个对话呆板人:手把手带你搭建一个语音对话呆板人,5分钟定制个人AI小助手(新手入门篇)
此中语音识别(ASR)方案,接纳的是阿里开源的 FunASR,这刚不久,阿里又开源了一个更强的音频基础模型,该模型具有如下本领:


  • 语音识别(ASR)
  • 语种识别(LID)
  • 语音情感识别(SER)
  • 声学事件分类(AEC)
  • 声学事件检测(AED)
   传送门:https://github.com/FunAudioLLM/SenseVoice
  本日就带着各人体验一番~
0. 项目简介

模型结构如下图所示:

模型亮点:


  • 多语言语音识别:颠末超过40万小时的数据训练,支持50多种语言,其识别性能逾越了Whisper模型。
  • 丰富的转录本领:具备出色的情感识别本领,在测试数据上逾越了当前最佳模型。提供声音事件检测本领,支持检测各种常见的人机交互事件,如背景音乐、掌声、笑声、哭泣、咳嗽和打喷嚏。
  • 高效推理:SenseVoice-Small模型接纳非自回归的端到端框架,具有极低的推理延迟。处理10秒音频仅需70毫秒,比Whisper-Large快15倍。
  • 便捷的微调:提供便捷的微调脚本和策略,使用户能够根据业务场景轻松解决长尾样本题目。
1. 在线体验

   在线体验地点:https://www.modelscope.cn/studios/iic/SenseVoice
  语音识别:支持中、粤、英、日、韩语等 50 多种语言。


情感识别:比如积极 or 灰心,以 Emoji 表情输出。


音频事件检测:同样以 Emoji 表情输出。


2. 本地部署

2.1 安装 & 测试

首先 git 下载到本地,然后安装必要的包:
  1. git clone https://github.com/FunAudioLLM/SenseVoice.git
  2. pip install -r requirements.txt
复制代码
注意:


  • 本项目依赖的 funasr 版本要 >=1.1.2,这个和 funasr 语音识别模型的版本是不匹配的,假如要同时使用这两个模型,会出现版本冲突,所以最好接纳 conda 管理 python 环境。
  • 本项目依赖的 torchaudio 必要更新到最新版本,否则会出现报错。
接下来,我们接纳官方脚本进行测试:
  1. from funasr import AutoModel
  2. from funasr.utils.postprocess_utils import rich_transcription_postprocess
  3. model_dir = "iic/SenseVoiceSmall"
  4. model = AutoModel(
  5.     model=model_dir,
  6.     trust_remote_code=True,
  7.     remote_code="./model.py",
  8.     vad_model="fsmn-vad",
  9.     vad_kwargs={"max_single_segment_time": 30000},
  10.     device="cuda:0",
  11. )
  12. res = model.generate(
  13.     input=f"{model.model_path}/example/en.mp3",
  14.     cache={},
  15.     language="auto",  # "zn", "en", "yue", "ja", "ko", "nospeech"
  16.     use_itn=True,
  17.     batch_size_s=60,
  18.     merge_vad=True,  #
  19.     merge_length_s=15,
  20. )
  21. text = rich_transcription_postprocess(res[0]["text"])
  22. print(text)
复制代码
首次使用,会下载模型,默认保存在你的根目次下:~/.cache/modelscope/。
2.2 FastAPI 部署

测试成功后,我们接纳 FastAPI 把模型部署成一个服务,方便提供给其他应用调用。
2.2.1 服务端

首先预备好服务端代码 speech_server.py:
  1. import torch
  2. import base64
  3. import uvicorn
  4. from fastapi import FastAPI
  5. from funasr import AutoModel
  6. from funasr.utils.postprocess_utils import rich_transcription_postprocess
  7. from pydantic import BaseModel
  8. # asr model
  9. model = AutoModel(
  10.     model="iic/SenseVoiceSmall",
  11.     trust_remote_code=True,
  12.     remote_code="./model.py",
  13.     vad_model="fsmn-vad",
  14.     vad_kwargs={"max_single_segment_time": 30000},
  15.     device="cuda:0",
  16. )
  17. # 定义asr数据模型,用于接收POST请求中的数据
  18. class ASRItem(BaseModel):
  19.     wav : str # 输入音频
  20. app = FastAPI()
  21. @app.post("/asr")
  22. async def asr(item: ASRItem):
  23.     try:
  24.         data = base64.b64decode(item.wav)
  25.         with open("test.wav", "wb") as f:
  26.             f.write(data)
  27.         res = model.generate("test.wav", 
  28.                             language="auto",  # "zn", "en", "yue", "ja", "ko", "nospeech"
  29.                             use_itn=True,
  30.                             batch_size_s=60,
  31.                             merge_vad=True,  #
  32.                             merge_length_s=15,)
  33.         text = rich_transcription_postprocess(res[0]["text"])
  34.         result_dict = {"code": 0, "msg": "ok", "res": text}
  35.     except Exception as e:
  36.         result_dict = {"code": 1, "msg": str(e)}
  37.     return result_dict
  38. if __name__ == '__main__':
  39.     uvicorn.run(app, host='0.0.0.0', port=2002)
复制代码
2.2.2 服务启动

  1. CUDA_VISIBLE_DEVICES=0 python speech_server.py > log.txt 2>&1 &
复制代码
服务成功启动,可以发现显存只占用 1202 M,比上一篇的 FunASR 更轻量~
  1. +-----------------------------------------------------------------------------------------+
  2. | Processes:                                                                              |
  3. |  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
  4. |        ID   ID                                                               Usage      |
  5. |=========================================================================================|
  6. |    2   N/A  N/A   3178377      C   python                                       1202MiB |
  7. +-----------------------------------------------------------------------------------------+
复制代码
2.2.3 客户端

末了,我们来编写客户端代码:
  1. import base64
  2. import requests
  3. url = "http://10.18.32.170:2002/"
  4. def asr_damo_api(wav_path):
  5.     headers = {'Content-Type': 'application/json'}
  6.     with open(wav_path, "rb") as f:
  7.         wav = base64.b64encode(f.read()).decode()
  8.     data = {"wav": wav}
  9.     response = requests.post(url+"asr", headers=headers, json=data)
  10.     response = response.json()
  11.     if response['code'] == 0:
  12.         res = response['res']
  13.         return res
  14.     else:
  15.         return response['msg']
  16. if __name__ == '__main__':
  17.     res = asr_damo_api("xxx/.cache/modelscope/hub/iic/SenseVoiceSmall/example/en.mp3")
  18.     print(res)
复制代码
写在末了

本文通过对 SenseVoice 模型的实操,带领各人快速上手语音识别模型。
结合语音识别、大模型,可以轻松实现打造一个 AI 语音对话助手。感爱好的小伙伴快去试试吧~

假如本文对你有帮助,欢迎点赞在看转发备用。你的支持是我创作的最大动力。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

铁佛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表