PPASR: 基于PaddlePaddle的中文语音识别框架

PPASR (PaddlePaddle Automatic Speech Recognition) 是一个基于PaddlePaddle的中文语音识别框架,支持多种模型和部署场景。

项目结构

PPASR
├── docs                  # 文档
├── download_data         # 数据下载与处理
├── dataset               # 数据集相关
├── models                # 模型保存路径
├── lm                    # 语言模型
├── logs                  # 日志
├── utils                 # 工具函数
├── train.py              # 训练脚本
├── eval.py               # 评估脚本
├── export_model.py       # 模型导出
├── infer_path.py         # 路径预测
├── infer_server.py       # 服务预测
├── infer_gui.py          # GUI预测
└── README.md             # 项目说明

快速使用

安装依赖

pip install -r requirements.txt

模型快速预测

from ppasr.predict import PPASRPredictor

# 初始化预测器,使用预训练模型
predictor = PPASRPredictor(model_tag='conformer_streaming_fbank_wenetspeech')

# 短语音识别
wav_path = 'test.wav'
result = predictor.predict(audio_data=wav_path, use_pun=False)
score, text = result['score'], result['text']
print(f"识别结果: {text}, 得分: {int(score)}")

# 长语音识别
result = predictor.predict_long(audio_data=wav_path, use_pun=False)
score, text = result['score'], result['text']
print(f"长语音识别结果: {text}, 得分: {score}")

# 实时流式识别
import time
import wave

wf = wave.open(wav_path, 'rb')
CHUNK = int(16000 * 0.5)  # 每0.5秒一个chunk
data = wf.readframes(CHUNK)
while data != b'':
    start = time.time()
    result = predictor.predict_stream(audio_data=data, use_pun=False, is_end=data == b'')
    data = wf.readframes(CHUNK)
    if result:
        score, text = result['score'], result['text']
        print(f"实时结果: {text}, 得分: {int(score)}")
        print(f"耗时: {int((time.time() - start) * 1000)}ms")
predictor.reset_stream()

模型下载

支持模型列表

模型类型 模型名称 数据集 预处理 语言 下载地址
Conformer conformer_online WenetSpeech(10000小时) fbank 中文 下载
Conformer conformer_offline Aishell(179小时) fbank 中文 下载
DeepSpeech2 deepspeech2_online WenetSpeech(10000小时) fbank 中文 下载
SqueezeFormer squeezeformer_streaming WenetSpeech(10000小时) fbank 中文 下载

下载后处理

将下载的模型文件放到models/目录下,执行模型导出:

python export_model.py --resume_model=models/conformer_online_fbank/best_model/

数据准备

下载公开数据集

cd download_data
python download.py  # 下载Aishell、Free ST-Chinese-Mandarin-Corpus、THCHS-30

自定义数据格式

  1. 音频文件放在dataset/audio/目录
  2. 数据列表文件格式:每行包含音频路径和文本,用制表符分隔
dataset/audio/001.wav  这是一段测试语音
dataset/audio/002.wav  语音识别测试
  1. 生成数据列表和词汇表
python create_data.py

训练模型

单卡训练

python train.py

多卡训练

python -m paddle.distributed.launch --gpus '0,1' train.py

训练参数

  • --model_dir: 模型保存路径
  • --batch_size: 批次大小
  • --use_gpu: 是否使用GPU
  • --learning_rate: 学习率
  • --augment_conf_path: 数据增强配置文件路径

评估模型

python eval.py --resume_model=models/conformer_online_fbank/best_model/

模型导出

python export_model.py --resume_model=models/conformer_online_fbank/best_model/

部署方式

本地预测

python infer_path.py --wav_path=test.wav

服务部署

python infer_server.py

启动后可通过HTTP请求访问服务:

import requests
import wave

wav_data = wave.open('test.wav', 'rb').readframes(-1)
response = requests.post(
    "http://localhost:8080/transcribe",
    files={"audio": ("test.wav", wav_data)}
)
print(response.json())

GUI界面

python infer_gui.py

模型支持

  • 模型类型: conformer, deepspeech2, squeezeformer
  • 解码方式: ctc_greedy (贪心), ctc_beam_search (集束搜索)
  • 流式识别: online/offline 模式
  • 语言模型: 支持中文语言模型

相关项目

参考资料

  • PaddleSpeech: https://github.com/PaddlePaddle/PaddleSpeech
  • DeepSpeech-pytorch: https://github.com/jiwidi/DeepSpeech-pytorch
  • WenetSpeech: https://github.com/wenet-e2e/WenetSpeech

许可证

MIT License


有问题欢迎提 issue 交流

Xiaoye