Skip to content

Commit

Permalink
Update python README
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed May 15, 2024
1 parent e6a1f06 commit c4f76bb
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 168 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/gen_whl_to_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ name: Push rapid_paraformer to pypi

on:
push:
branches: [ main ]
paths:
- 'python/rapid_paraformer/**'
- 'python/docs/doc_whl.md'
- 'python/setup.py'
- '.github/workflows/gen_whl_to_pypi.yml'
# branches: [ main ]
# paths:
# - 'python/rapid_paraformer/**'
# - 'python/docs/doc_whl.md'
# - 'python/setup.py'
# - '.github/workflows/gen_whl_to_pypi.yml'
tags:
- v*

# env:
# RESOURCES_URL: https://github.com/RapidAI/RapidLatexOCR/releases/download/v0.0.0/models.zip
Expand Down Expand Up @@ -59,7 +61,7 @@ jobs:
pip install -r requirements.txt
python -m pip install --upgrade pip
pip install wheel get_pypi_latest_version
python setup.py bdist_wheel ${{ github.event.head_commit.message }}
python setup.py bdist_wheel ${{ github.ref_name }}
# - name: Publish distribution 📦 to Test PyPI
# uses: pypa/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<a href="https://huggingface.co/spaces/SWHL/RapidASRDemo" target="_blank"><img src="https://img.shields.io/badge/%F0%9F%A4%97-Hugging Face Demo-blue"></a>
<a href="https://www.modelscope.cn/studios/liekkas/RapidASRDemo/summary" target="_blank"><img src="https://img.shields.io/badge/ModelScope-Demo-blue"></a>
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.12-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/C++-aff.svg"></a>
</p>

Expand Down
35 changes: 35 additions & 0 deletions python/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text
125 changes: 68 additions & 57 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,71 @@
## rapid_paraformer

<p align="left">
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.12-aff.svg"></a>
<a href="https://pepy.tech/project/rapid_paraformer"><img src="https://static.pepy.tech/personalized-badge/rapid_paraformer?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
<a href="https://pypi.org/project/rapid_paraformer/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid_paraformer"></a>
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
</p>

- 模型出自阿里达摩院[Paraformer语音识别-中文-通用-16k-离线-large-pytorch](https://www.modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/summary)
- 🎉该项目核心代码已经并入[FunASR](https://github.com/alibaba-damo-academy/FunASR)
- 本仓库仅对模型做了转换,只采用ONNXRuntime推理引擎

#### TODO
<div align="center">
<div align="center">
<h1><b>Rapid Paraformer</b></h1>
</div>

<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
<a href="https://pepy.tech/project/rapid_paraformer"><img src="https://static.pepy.tech/personalized-badge/rapid_paraformer?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
<a href="https://pypi.org/project/rapid_paraformer/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid_paraformer"></a>
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
</div>

## 简介
rapid_paraformer是一个基于阿里达摩院[Paraformer语音识别-中文-通用-16k-离线-large-pytorch](https://www.modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/summary)的语音识别工具。

🎉该项目核心代码已经并入[FunASR](https://github.com/alibaba-damo-academy/FunASR)

本仓库仅对模型做了转换,只采用ONNXRuntime推理引擎

## TODO
- [ ] 整合vad + asr + pun三个模型,打造可部署使用的方案

#### 使用步骤
1. Install
1. 安装`rapid_paraformer`
```bash
pip install rapid_paraformer
```
2. 下载**resources.zip** ([Google Drive](https://drive.google.com/drive/folders/1RVQtMe0eB_k6G5TJlmXwPELx4VtF2oCw?usp=sharing) | [百度网盘](https://pan.baidu.com/s/1zf8Ta6QxFHY3Z75fHNYKrQ?pwd=6ekq))
```bash
resources
├── [ 700] config.yaml
└── [4.0K] models
├── [ 11K] am.mvn
├── [824M] asr_paraformerv2.onnx
└── [ 50K] token_list.pkl
```
3. **asr_paraformerv2.onnx**文件可基于modescope下的notebook环境自助转换:
1. 打开[快速体验](https://www.modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/summary)
2. 打开notebook → Cell中输入以下命令, 执行即可。
```python
!python -m funasr.export.export_model --model-name 'damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch' --export-dir "./export"
```

2. 使用
```python
from rapid_paraformer import RapidParaformer
config_path = "resources/config.yaml"
paraformer = RapidParaformer(config_path)
wav_path = [
"test_wavs/0478_00017.wav",
"test_wavs/asr_example_zh.wav",
]
result = paraformer(wav_path)
print(result)
```
3. 查看结果
```text
['y', '欢迎大家来体验达摩院推出的语音识别模型']
## 安装
```bash
pip install rapid_paraformer
```

## 模型下载
方法一:从Hugging Face上下载([link](https://huggingface.co/SWHL/RapidParaformer)
```python
from rapid_paraformer import download_hf_model

download_hf_model(repo_id="SWHL/RapidParaformer", save_dir=".")
```

方法二:([Google Drive](https://drive.google.com/drive/folders/1RVQtMe0eB_k6G5TJlmXwPELx4VtF2oCw?usp=sharing) | [百度网盘](https://pan.baidu.com/s/1zf8Ta6QxFHY3Z75fHNYKrQ?pwd=6ekq))
```bash
resources
├── [ 700] config.yaml
└── [4.0K] models
├── [ 11K] am.mvn
├── [824M] asr_paraformerv2.onnx
└── [ 50K] token_list.pkl
```

## 模型转换
基于modescope下的notebook环境自助转换:
1. 打开[快速体验](https://www.modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/summary)
2. 打开notebook → Cell中输入以下命令, 执行即可。
```bash
!python -m funasr.export.export_model --model-name 'damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch' --export-dir "./export"
```

## 使用
```python
from rapid_paraformer import RapidParaformer
config_path = "resources/config.yaml"
paraformer = RapidParaformer(config_path)
wav_path = [
"test_wavs/0478_00017.wav",
"test_wavs/asr_example_zh.wav",
]
result = paraformer(wav_path)
print(result)
# ['y', '欢迎大家来体验达摩院推出的语音识别模型']
```
4 changes: 3 additions & 1 deletion python/demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
from rapid_paraformer import RapidParaformer
from rapid_paraformer import RapidParaformer, download_hf_model

download_hf_model(repo_id="SWHL/RapidParaformer", save_dir=".")

config_path = "resources/config.yaml"

Expand Down
46 changes: 1 addition & 45 deletions python/docs/doc_whl.md
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
## rapid_paraformer

<p align="left">
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.12-aff.svg"></a>
<a href="https://pepy.tech/project/rapid_paraformer"><img src="https://static.pepy.tech/personalized-badge/rapid_paraformer?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
<a href="https://pypi.org/project/rapid_paraformer/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid_paraformer"></a>
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
</p>


### Use
1. Install
1. Install the `rapid_paraformer`
```bash
pip install rapid_paraformer
```
2. Download the **resources.zip** ([Google Drive](https://drive.google.com/drive/folders/1RVQtMe0eB_k6G5TJlmXwPELx4VtF2oCw?usp=sharing) | [Baidu NetDisk](https://pan.baidu.com/s/1zf8Ta6QxFHY3Z75fHNYKrQ?pwd=6ekq))
```bash
resources
├── [ 700] config.yaml
└── [4.0K] models
├── [ 11K] am.mvn
├── [824M] asr_paraformerv2.onnx
└── [ 50K] token_list.pkl
```
2. Use
```python
from rapid_paraformer import RapidParaformer
config_path = "resources/config.yaml"
paraformer = RapidParaformer(config_path)
wav_path = [
"test_wavs/0478_00017.wav",
"test_wavs/asr_example_zh.wav",
]
result = paraformer(wav_path)
print(result)
```

### See details for [RapidASR](https://github.com/RapidAI/RapidASR).
See [link](https://github.com/RapidAI/RapidASR/tree/main/python) for details.
3 changes: 2 additions & 1 deletion python/rapid_paraformer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
from .rapid_paraformer import RapidParaformer
from .main import RapidParaformer
from .utils import download_hf_model
File renamed without changes.
33 changes: 25 additions & 8 deletions python/rapid_paraformer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import numpy as np
import yaml
from huggingface_hub import snapshot_download
from onnxruntime import (
GraphOptimizationLevel,
InferenceSession,
Expand Down Expand Up @@ -221,8 +222,8 @@ def apply_cmvn(self, inputs: np.ndarray) -> np.ndarray:
"""
frame, dim = inputs.shape
means = np.tile(self.cmvn[0:1, :dim], (frame, 1))
vars = np.tile(self.cmvn[1:2, :dim], (frame, 1))
inputs = (inputs + means) * vars
vars_np = np.tile(self.cmvn[1:2, :dim], (frame, 1))
inputs = (inputs + means) * vars_np
return inputs

def load_cmvn(
Expand All @@ -249,8 +250,8 @@ def load_cmvn(
continue

means = np.array(means_list).astype(np.float64)
vars = np.array(vars_list).astype(np.float64)
cmvn = np.array([means, vars])
vars_np = np.array(vars_list).astype(np.float64)
cmvn = np.array([means, vars_np])
return cmvn


Expand All @@ -275,10 +276,6 @@ class TokenIDConverterError(Exception):
pass


class ONNXRuntimeError(Exception):
pass


class OrtInferSession:
def __init__(self, config):
sess_opt = SessionOptions()
Expand Down Expand Up @@ -351,6 +348,10 @@ def _verify_model(model_path):
raise FileExistsError(f"{model_path} is not a file.")


class ONNXRuntimeError(Exception):
pass


def read_yaml(yaml_path: Union[str, Path]) -> Dict:
if not Path(yaml_path).exists():
raise FileExistsError(f"The {yaml_path} does not exist.")
Expand Down Expand Up @@ -390,3 +391,19 @@ def get_logger(name="rapdi_paraformer"):
logger_initialized[name] = True
logger.propagate = False
return logger


def download_hf_model(
repo_id: str, save_dir: Union[str, Path], resume_download: bool = True
) -> str:
model_dir = snapshot_download(
repo_id=repo_id,
local_dir=str(save_dir),
local_dir_use_symlinks=False,
local_files_only=False,
resume_download=resume_download,
ignore_patterns=[
"*.md",
],
)
return model_dir
2 changes: 1 addition & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
librosa
numpy
onnxruntime
scipy
typeguard==2.13.3
huggingface_hub
31 changes: 0 additions & 31 deletions python/resources/config.yaml

This file was deleted.

Loading

0 comments on commit c4f76bb

Please sign in to comment.