Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ wav_file = f"{model.model_path}/example/asr_example.wav"
res = model.generate(input=wav_file, batch_size_s=300, batch_size_threshold_s=60, hotword='魔搭')
print(res)
```

#### Postprocess hotword correction

Model-level `hotword` / `hotwords` boosts a small set of terms during decoding. For large vocabularies (for example thousands of stock names), apply text-level correction after recognition:

```python
from funasr import AutoModel

model = AutoModel(model="paraformer-zh", vad_model="fsmn-vad", punc_model="ct-punc")

res = model.generate(
input="asr_example.wav",
postprocess_hotwords={
"科大迅飞": "科大讯飞",
"东方财富": "东方财富",
},
postprocess_hotword_threshold=0.85,
return_postprocess_hotword_matches=True,
)
print(res[0]["text"])
print(res[0].get("postprocess_hotword_matches"))
```

You can also pass `postprocess_hotword_file` with one target word per line, or an explicit mapping such as `wrong=>right`. Fuzzy matching requires optional `pypinyin` and `rapidfuzz`; explicit mappings work without them.

Notes:
- Typically, the input duration for models is limited to under 30 seconds. However, when combined with `vad_model`, support for audio input of any length is enabled, not limited to the paraformer model—any audio input model can be used.
- Parameters related to model can be directly specified in the definition of AutoModel; parameters related to `vad_model` can be set through `vad_kwargs`, which is a dict; similar parameters include `punc_kwargs` and `spk_kwargs`.
Expand Down
25 changes: 25 additions & 0 deletions docs/tutorial/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ wav_file = f"{model.model_path}/example/asr_example.wav"
res = model.generate(input=wav_file, batch_size_s=300, batch_size_threshold_s=60, hotword='魔搭')
print(res)
```

#### ASR 后处理热词替换

模型层 `hotword` / `hotwords` 用于在解码阶段提升少量高优先级词的识别率;如果词表很大(例如几千个股票名),更适合在识别完成后做文本级纠错:

```python
from funasr import AutoModel

model = AutoModel(model="paraformer-zh", vad_model="fsmn-vad", punc_model="ct-punc")

res = model.generate(
input="asr_example.wav",
postprocess_hotwords={
"科大迅飞": "科大讯飞",
"东方财富": "东方财富",
},
postprocess_hotword_threshold=0.85,
return_postprocess_hotword_matches=True,
)
print(res[0]["text"])
print(res[0].get("postprocess_hotword_matches"))
```

也支持热词文件 `postprocess_hotword_file`,每行一个目标词,或一行一个显式映射(`错误词=>目标词`)。模糊匹配需要额外安装 `pypinyin` 和 `rapidfuzz`;未安装时仍可使用显式映射。

注意:
- 通常模型输入限制时长30s以下,组合`vad_model`后,支持任意时长音频输入,不局限于paraformer模型,所有音频输入模型均可以。
- `model`相关的参数可以直接在`AutoModel`定义中直接指定;与`vad_model`相关参数可以通过`vad_kwargs`来指定,类型为dict;类似的有`punc_kwargs`,`spk_kwargs`;
Expand Down
12 changes: 10 additions & 2 deletions funasr/auto/auto_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from funasr.train_utils.set_all_random_seed import set_all_random_seed
from funasr.train_utils.load_pretrained_model import load_pretrained_model
from funasr.utils import export_utils
from funasr.utils.postprocess_hotwords import apply_postprocess_hotwords_to_results
from funasr.utils import misc


Expand Down Expand Up @@ -458,6 +459,12 @@ def generate(self, input, input_len=None, progress_callback=None, **cfg):
**cfg: Runtime parameters:
- cache (dict): State cache for streaming mode. Pass {} for first call.
- hotword (str/list): Keywords to boost recognition accuracy.
- postprocess_hotwords (str/list/dict): Text-level hotword correction after
decoding. Unlike model-level ``hotword``, this runs on the final text.
- postprocess_hotword_file (str): Hotword file path. Each line is a target
word or an explicit mapping like ``错误词=>目标词``.
- postprocess_hotword_threshold (float): Fuzzy match threshold in [0, 1].
- return_postprocess_hotword_matches (bool): Include replacement details.
- language (str): Language hint ("auto", "zh", "en", "Chinese", etc.)
- batch_size_s (int): Dynamic batch total duration in seconds.
- is_final (bool): Last chunk flag for streaming mode.
Expand Down Expand Up @@ -486,12 +493,13 @@ def generate(self, input, input_len=None, progress_callback=None, **cfg):
if cfg.get("return_raw_text", self.kwargs.get("return_raw_text", False)):
result["raw_text"] = copy.copy(result["text"])
result["text"] = punc_res[0]["text"]
return results
return apply_postprocess_hotwords_to_results(results, cfg)

else:
return self.inference_with_vad(
results = self.inference_with_vad(
input, input_len=input_len, progress_callback=progress_callback, **cfg
)
return apply_postprocess_hotwords_to_results(results, cfg)

def inference(
self,
Expand Down
Loading