阿里开源 SenseVoice:打造 STT 语音转文本实战应用

1、引言

1.1、SenseVoice 简介

阿里通义实验室推出音频基座大模型 FunAudioLLM,包含 SenseVoice 和 CosyVoice 两大模型。
在这里插入图片描述

SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测

多语言识别

  • 采用超过40万小时数据训练,支持超过50种语言,识别效果上优于Whisper模型。

富文本识别

  • 具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。
  • 支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。

高效推理

  • SenseVoice-Small模型采用非自回归端到端框架,推理延迟极低,10s音频推理仅耗时70ms,15倍优于Whisper-Large。

微调定制

  • 具备便捷的微调脚本与策略,方便用户根据业务场景修复长尾样本问题。

服务部署

  • 具有完整的服务部署链路,支持多并发请求,支持客户端语言有,python、c++、html、java与c#等。

在这里插入图片描述

1.2、SenseVoice 资源

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2、安装

2.1、安装 Anaconda

Linux 安装 Anaconda 参考文章

MAC 安装 Anaconda 参考文章

Windows 安装 Anaconda 参考文章

2.2、创建独立环境

# 创建一个名为 wn_sensevoice 的环境,并指定在该环境中安装 Python 3.10 版本
conda create -n wn_sensevoice -y python=3.10

# 激活并选择环境
conda activate wn_sensevoice

2.3、下载 SenseVoiceSmall 模型

魔搭平台下载

# 安装魔搭社区
pip install modelscope

# 下载 CosyVoice2-0.5B 模型到本地指定目录(替换自己本地路径)
modelscope download --model iic/SenseVoiceSmall --local_dir C:\Users\woniu\model\FunAudioLLM\SenseVoiceSmall

2.4、安装 Python 项目依赖项

# 安装 CPU 版本的 PyTorch、torchvision 和 torchaudio
pip install torch torchvision torchaudio

# 安装 ffmpeg
conda install ffmpeg

# 安装 funasr
pip install funasr

2.5、GPU 加速(根据自己电脑配置)

# 验证 GPU 环境是否可用
python -c "import torch; print(torch.__version__); print(torch.version.cuda); print(torch.cuda.is_available())"

# 卸载 CPU 版本 torch
pip uninstall torch torchvision torchaudio

# 安装 GPU 版本 torch
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126

3、项目

3.1、创建新项目

使用 PyCharm 工具创建一个新的 Python 项目,环境选择刚才创建的新环境
在这里插入图片描述

3.2、示例代码

import torch
from typing import Optional
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess

class SenseVoiceSmallDemo:
    def __init__(self):
        print("SenseVoiceSmallDemo 初始化")

        # 指定模型路径
        model_dir = r"C:\Users\woniu\model\FunAudioLLM\SenseVoiceSmall"

        # 检查当前环境中是否支持 NVIDIA 的 CUDA 平台
        device = "cuda" if torch.cuda.is_available() else "cpu"

        # 初始化模型
        # trust_remote_code 当设置为 True 时,表示允许加载远程代码
        # remote_code 指定一个本地文件路径(如 ./model.py),该文件包含与远程模型相关的自定义代码。
        # vad_model 语音活动检测(Voice Activity Detection,VAD)模型的名称或标识符(如 fsmn-vad)
        self.model = AutoModel(
            model=model_dir,
            disable_update=True,  # 禁用版本检查
            trust_remote_code=True,
            vad_model="fsmn-vad",
            vad_kwargs={"max_single_segment_time": 30000},
            device="cuda:0",  # 指定使用 GPU 设备
        )

    # 语音文件转文本
    def audio_to_text(self, file_path: Optional[str]):
        # 进行语音识别
        res = self.model.generate(
            input=file_path,
            language="auto",  # "zn", "en", "yue", "ja", "ko", "nospeech"
            use_itn=True,
            batch_size_s=60,
            merge_vad=True,  #
            merge_length_s=15,
        )

        # 处理结果
        if res and res[0]["text"]:
            text = rich_transcription_postprocess(res[0]["text"])
            if text:
                return text
        return ""

if __name__ == "__main__":
    wnDemo = SenseVoiceSmallDemo()
    file_path = r"..\\CosyVoice2\\zero_shot_0.wav"
    text_content = wnDemo.audio_to_text(file_path)
    print(text_content)

本文教程到此结束,祝愿小伙伴们在编程之旅中能够愉快地探索、学习、成长!
### 添加时间戳到 SenseVoiceSmall 为了在 SenseVoiceSmall 中添加时间戳功能,可以考虑通过修改现有的语音识别流程来实现实时或近实时的时间标记。由于 SenseVoice 不是为流式录设计的[^1],因此需要额外开发以支持这一特性。 一种常见的做法是在每次接收到新的音频片段时记录当前时间,并将其与对应的文本一起保存。下面是一个简单的 Python 实现方案: ```python import time from datetime import timedelta, datetime class TimestampedTranscriber: def __init__(self): self.transcriptions = [] def transcribe_with_timestamp(self, audio_chunk, recognizer_function): start_time = datetime.now() transcription_result = recognizer_function(audio_chunk) end_time = datetime.now() timestamp_entry = { 'start': str(start_time), 'end': str(end_time), 'transcription': transcription_result, 'duration_seconds': (end_time - start_time).total_seconds() } self.transcriptions.append(timestamp_entry) return timestamp_entry def mock_recognizer_function(audio_data): # 这里只是一个模拟函数 """Simulate ASR processing delay.""" time.sleep(0.5) # Simulated processing latency return "This is a test sentence." # 使用示例 ts_transcriber = TimestampedTranscriber() audio_sample = b'some binary data representing an audio chunk' # 替换为实际获取到的音频数据 result = ts_transcriber.transcribe_with_timestamp(audio_sample, mock_recognizer_function) print(result) ``` 此代码展示了如何创建一个带有时间戳的录音本条目列表。每当调用 `transcribe_with_timestamp` 方法并传入一段音频以及用于执行识别的具体函数时,就会生成一个新的包含起始时间和结束时间的对象,并将其追加到内部存储中。 需要注意的是,在真实的应用场景下,应当替换掉这里的 `mock_recognizer_function` 函数,代之以真正连接至 SenseVoice API 或 SDK 的接口来进行有效的语音识别操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值