[1302]FunAudioLLM – 阿里通义团队推出的开源语音大模型

通义听悟:https://tingwu.aliyun.com/home
计费说明:https://help.aliyun.com/zh/tingwu/pricing-and-billing-rules

  • FunAudioLLM:https://github.com/FunAudioLLM

SenseVoice

音频将被转录成相应的文本,并附带相关情感(😊 高兴,😡 生气/激动,😔 悲伤) 和 声音事件类型(😀 笑声,🎼 音乐,👏 掌声,🤧 咳嗽&打喷嚏,😭 哭泣)

CosyVoice


SenseVoice 实测

前段时间,带着大家捏了一个对话机器人:手把手带你搭建一个语音对话机器人,5分钟定制个人AI小助手

其中语音识别(ASR)方案,采用的是阿里开源的 FunASR,这刚不久,阿里又开源了一个更强的音频基础模型,该模型具有如下能力:

  • 语音识别(ASR)
  • 语种识别(LID)
  • 语音情感识别(SER)
  • 声学事件分类(AEC)
  • 声学事件检测(AED)

传送门:https://github.com/FunAudioLLM/SenseVoice

SenseVoice项目简介

模型结构如下图所示:

模型亮点:

  • 多语言语音识别:
    经过超过40万小时的数据训练,支持50多种语言,其识别性能超越了Whisper模型。

  • 丰富的转录能力:
    具备出色的情感识别能力,在测试数据上超越了当前最佳模型。
    提供声音事件检测能力,支持检测各种常见的人机交互事件,如背景音乐、掌声、笑声、哭泣、咳嗽和打喷嚏。

  • 高效推理:
    SenseVoice-Small模型采用非自回归的端到端框架,具有极低的推理延迟。处理10秒音频仅需70毫秒,比Whisper-Large快15倍。

  • 便捷的微调:
    提供便捷的微调脚本和策略,使用户能够根据业务场景轻松解决长尾样本问题。

在线体验

在线体验地址:https://www.modelscope.cn/studios/iic/SenseVoice

语音识别:支持中、粤、英、日、韩语等 50 多种语言。

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

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

本地部署

安装 & 测试

首先 git 下载到本地,然后安装必要的包:

git clone https://github.com/FunAudioLLM/SenseVoice.git
pip install -r requirements.txt

注意:

  • 本项目依赖的 funasr 版本要 >=1.1.2,这个和 funasr 语音识别模型的版本是不匹配的,如果要同时使用这两个模型,会出现版本冲突,所以最好采用 conda 管理 python 环境。
  • 本项目依赖的 torchaudio 需要更新到最新版本,否则会出现报错。

接下来,我们采用官方脚本进行测试:

from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess

model_dir = "iic/SenseVoiceSmall"
model = AutoModel(
    model=model_dir,
    trust_remote_code=True,
    remote_code="./model.py",
    vad_model="fsmn-vad",
    vad_kwargs={"max_single_segment_time": 30000},
    device="cuda:0",
)

res = model.generate(
    input=f"{model.model_path}/example/en.mp3",
    cache={},
    language="auto",  # "zn", "en", "yue", "ja", "ko", "nospeech"
    use_itn=True,
    batch_size_s=60,
    merge_vad=True,  #
    merge_length_s=15,
)
text = rich_transcription_postprocess(res[0]["text"])
print(text)

首次使用,会下载模型,默认保存在你的根目录下:~/.cache/modelscope/

参数说明:

  • model_dir:模型名称,或本地磁盘中的模型路径。
  • trust_remote_code
    • True 表示 model 代码实现从 remote_code 处加载,remote_code 指定 model 具体代码的位置(例如,当前目录下的 model.py),支持绝对路径与相对路径,以及网络 url。
    • False 表示,model 代码实现为 FunASR 内部集成版本,此时修改当前目录下的 model.py 不会生效,因为加载的是 funasr 内部版本,模型代码 点击查看
  • vad_model:表示开启 VAD,VAD 的作用是将长音频切割成短音频,此时推理耗时包括了 VAD 与 SenseVoice 总耗时,为链路耗时,如果需要单独测试 SenseVoice 模型耗时,可以关闭 VAD 模型。
  • vad_kwargs:表示 VAD 模型配置,max_single_segment_time: 表示 vad_model 最大切割音频时长,单位是毫秒 ms。
  • use_itn:输出结果中是否包含标点与逆文本正则化。
  • batch_size_s 表示采用动态 batch,batch 中总音频时长,单位为秒 s。
  • merge_vad:是否将 vad 模型切割的短音频碎片合成,合并后长度为 merge_length_s,单位为秒 s。
  • ban_emo_unk:禁用 emo_unk 标签,禁用后所有的句子都会被赋与情感标签。默认 False

如果输入均为短音频(小于 30s),并且需要批量化推理,为了加快推理效率,可以移除 vad 模型,并设置 batch_size

model = AutoModel(model=model_dir, trust_remote_code=True, device="cuda:0")

res = model.generate(
    input=f"{model.model_path}/example/en.mp3",
    cache={},
    language="auto", # "zh", "en", "yue", "ja", "ko", "nospeech"
    use_itn=True,
    batch_size=64, 
)

更多详细用法,请参考 文档

FastAPI 部署

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

服务端

首先准备好服务端代码 speech_server.py

import torch
import base64
import uvicorn
from fastapi import FastAPI
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess
from pydantic import BaseModel

# asr model
model = AutoModel(
    model="iic/SenseVoiceSmall",
    trust_remote_code=True,
    remote_code="./model.py",
    vad_model="fsmn-vad",
    vad_kwargs={"max_single_segment_time": 30000},
    device="cuda:0",
)

# 定义asr数据模型,用于接收POST请求中的数据
class ASRItem(BaseModel):
    wav : str # 输入音频

app = FastAPI()
@app.post("/asr")
async def asr(item: ASRItem):
    try:
        data = base64.b64decode(item.wav)
        with open("test.wav", "wb") as f:
            f.write(data)
        res = model.generate("test.wav", 
                            language="auto",  # "zn", "en", "yue", "ja", "ko", "nospeech"
                            use_itn=True,
                            batch_size_s=60,
                            merge_vad=True,  #
                            merge_length_s=15,)
        text = rich_transcription_postprocess(res[0]["text"])
        result_dict = {"code": 0, "msg": "ok", "res": text}
    except Exception as e:
        result_dict = {"code": 1, "msg": str(e)}
    return result_dict

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=2002)
服务启动
CUDA_VISIBLE_DEVICES=0 python speech_server.py > log.txt 2>&1 &

服务成功启动,可以发现显存只占用 1202 M,比上一篇的 FunASR 更轻量~

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    2   N/A  N/A   3178377      C   python                                       1202MiB |
+-----------------------------------------------------------------------------------------+
客户端

最后,我们来编写客户端代码:

import base64
import requests

url = "http://10.18.32.170:2002/"

def asr_damo_api(wav_path):
    headers = {'Content-Type': 'application/json'}
    with open(wav_path, "rb") as f:
        wav = base64.b64encode(f.read()).decode()
    data = {"wav": wav}
    response = requests.post(url+"asr", headers=headers, json=data)
    response = response.json()
    if response['code'] == 0:
        res = response['res']
        return res
    else:
        return response['msg']

if __name__ == '__main__':
    res = asr_damo_api("xxx/.cache/modelscope/hub/iic/SenseVoiceSmall/example/en.mp3")
    print(res)

录音文件识别:https://ai.aliyun.com/nls/filetrans
https://help.aliyun.com/zh/model-studio/developer-reference/sensevoice-api
dashscope灵积服务:https://dashscope.console.aliyun.com/overview
阿里云百练:https://bailian.console.aliyun.com/#/home
常见问题:https://help.aliyun.com/zh/model-studio/support/faq-about-alibaba-cloud-model-studio

参考:https://blog.youkuaiyun.com/u010522887/article/details/140624599
https://blog.youkuaiyun.com/qq_44373268/article/details/140891810

其它参考:https://developer.aliyun.com/article/1562292
https://github.com/zxx1218/voice_translation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值