Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix(ci): integrate VAD spacing with current text regression sources

zhifu gao242a11caf37b
diff --git a/.github/workflows/test-funasr-onnx-package.yml b/.github/workflows/test-funasr-onnx-package.yml
index f94b86e09..226421da6 100644
--- a/.github/workflows/test-funasr-onnx-package.yml
+++ b/.github/workflows/test-funasr-onnx-package.yml
@@ -86,7 +86,7 @@ jobs:
import importlib.util
from importlib.metadata import requires, version
- assert version("funasr-onnx") == "0.4.9"
+ assert version("funasr-onnx") == "0.4.10"
assert importlib.util.find_spec("torch") is None
requirements = requires("funasr-onnx") or []
@@ -130,7 +130,7 @@ jobs:
if: matrix.python-version == '3.12'
uses: actions/upload-artifact@v7
with:
- name: funasr-onnx-0.4.9
+ name: funasr-onnx-0.4.10
path: dist/funasr-onnx/*
if-no-files-found: error
diff --git a/.github/workflows/test-text-postprocessing.yml b/.github/workflows/test-text-postprocessing.yml
index 55364def5..c851661af 100644
--- a/.github/workflows/test-text-postprocessing.yml
+++ b/.github/workflows/test-text-postprocessing.yml
@@ -3,6 +3,8 @@ name: Validate text postprocessing
on:
pull_request:
paths:
+ - "funasr/utils/type_utils.py"
+ - "tests/test_type_utils_compat.py"
- "funasr/utils/postprocess_hotwords.py"
- "funasr/utils/postprocess_utils.py"
- "runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py"
@@ -14,6 +16,8 @@ on:
push:
branches: [main]
paths:
+ - "funasr/utils/type_utils.py"
+ - "tests/test_type_utils_compat.py"
- "funasr/utils/postprocess_hotwords.py"
- "funasr/utils/postprocess_utils.py"
- "runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py"
@@ -52,6 +56,7 @@ jobs:
- name: Check hotword and rich transcription behavior without model weights
run: |
python -m pytest -q \
+ tests/test_type_utils_compat.py \
tests/test_postprocess_hotwords.py \
tests/test_rich_transcription_postprocess.py \
tests/test_sentence_postprocess_timestamps.py \
diff --git a/README.md b/README.md
index 8c4fc4501..108711e43 100644
--- a/README.md
+++ b/README.md
@@ -364,7 +364,8 @@ recordings with the same evaluation scope.
## What's new
- **MOSS-Transcribe-Diarize** brings long-form ASR, timestamps, and anonymous speaker labels to FunASR services, Docker, Kubernetes, vLLM/SGLang workflows, and FunClip. [Deploy MOSS ->](./docs/moss_transcribe_diarize.md)
-- **FunASR 1.4.24** ignores empty non-linguistic symbols to prevent tokenizer loops, fixes padded variance normalization, and prevents CIF crashes in mixed batches containing zero-fire samples. [Fix #3852](https://github.com/modelscope/FunASR/pull/3852), [fix #3853](https://github.com/modelscope/FunASR/pull/3853), [fix #3854](https://github.com/modelscope/FunASR/pull/3854). A checkpoint trained with both `norm_means=True` and `norm_vars=True` needs recognition-quality reevaluation because normalized features change; `norm_vars=False` remains the default. Install with `python -m pip install -U "funasr==1.4.24"`. [Release and verification scope ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
+- **FunASR 1.4.25** restores Python 3.12 boolean argument parsing without `distutils` and preserves normalized error messages; removes synthetic leading spaces from Latin sentence timestamps while preserving boundaries and raw text. [Fix #3863](https://github.com/modelscope/FunASR/pull/3863), [fix #3866](https://github.com/modelscope/FunASR/pull/3866). Install with `python -m pip install -U "funasr==1.4.25"`. [Release and verification scope ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.25)
+ **FunASR 1.4.24** ignores empty non-linguistic symbols to prevent tokenizer loops, fixes padded variance normalization, and prevents CIF crashes in mixed batches containing zero-fire samples. [Fix #3852](https://github.com/modelscope/FunASR/pull/3852), [fix #3853](https://github.com/modelscope/FunASR/pull/3853), [fix #3854](https://github.com/modelscope/FunASR/pull/3854). A checkpoint trained with both `norm_means=True` and `norm_vars=True` needs recognition-quality reevaluation because normalized features change; `norm_vars=False` remains the default. Install with `python -m pip install -U "funasr==1.4.24"`. [Release and verification scope ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
**FunASR 1.4.23** fixes the online frontend cursor for `lfr_m=lfr_n=1` and redundant empty final offline VAD chunks. With a supplied `token_list`, SentencePiece `ids2tokens` returns model pieces; use `tokens2text` for text. Without `token_list`, legacy text decoding is unchanged. [Fix #3849](https://github.com/modelscope/FunASR/pull/3849), [fix #3783](https://github.com/modelscope/FunASR/pull/3783). [Release and verification scope ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.23)
**FunASR 1.4.22** honors per-call `merge_length_s` and evaluates dynamic VAD silence thresholds per frame using the current segment duration, preserving the fixed-threshold opt-out. [Fix #3841](https://github.com/modelscope/FunASR/pull/3841), [fix #3833](https://github.com/modelscope/FunASR/pull/3833). [Release and verification scope ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.22)
**FunASR 1.4.21** preserves the streaming punctuation cache on empty or whitespace chunks; nonempty inference is unchanged. [Fix #3824](https://github.com/modelscope/FunASR/pull/3824). [Release and verification scope ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.21)
diff --git a/README_ja.md b/README_ja.md
index 4c1aaf906..64f634fe6 100644
--- a/README_ja.md
+++ b/README_ja.md
@@ -112,7 +112,8 @@ CER/WER をそろえて比較してください。オフラインのスループ
## 最新情報
- **MOSS-Transcribe-Diarize** を FunASR service、Docker、Kubernetes、vLLM/SGLang workflow、FunClip に統合し、長時間 ASR、timestamp、匿名 speaker label を一度に処理できます。[MOSS をデプロイ ->](./docs/moss_transcribe_diarize.md)
-- **FunASR 1.4.24** は空の非言語記号によるトークナイザーの無限ループ、パディングを含む分散正規化、発火回数ゼロのサンプルを含む混合バッチでの CIF クラッシュを修正します。[修正 #3852](https://github.com/modelscope/FunASR/pull/3852)、[修正 #3853](https://github.com/modelscope/FunASR/pull/3853)、[修正 #3854](https://github.com/modelscope/FunASR/pull/3854)。`norm_means=True` と `norm_vars=True` の両方で学習した checkpoint は正規化特徴が変わるため、認識品質の再評価が必要です。既定値は引き続き `norm_vars=False` です。`python -m pip install -U "funasr==1.4.24"` でインストールできます。[リリースと検証範囲 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
+- **FunASR 1.4.25** は Python 3.12 の真偽値引数を `distutils` なしで解析し、正規化されたエラーメッセージを維持します。ラテン文字で始まる文のタイムスタンプ出力から余分な先頭空白を除き、時刻境界と原文を保持します。[修正 #3863](https://github.com/modelscope/FunASR/pull/3863)、[修正 #3866](https://github.com/modelscope/FunASR/pull/3866)。`python -m pip install -U "funasr==1.4.25"` でインストールできます。[リリースと検証範囲 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.25)
+ **FunASR 1.4.24** は空の非言語記号によるトークナイザーの無限ループ、パディングを含む分散正規化、発火回数ゼロのサンプルを含む混合バッチでの CIF クラッシュを修正します。[修正 #3852](https://github.com/modelscope/FunASR/pull/3852)、[修正 #3853](https://github.com/modelscope/FunASR/pull/3853)、[修正 #3854](https://github.com/modelscope/FunASR/pull/3854)。`norm_means=True` と `norm_vars=True` の両方で学習した checkpoint は正規化特徴が変わるため、認識品質の再評価が必要です。既定値は引き続き `norm_vars=False` です。`python -m pip install -U "funasr==1.4.24"` でインストールできます。[リリースと検証範囲 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
**FunASR 1.4.23** は `lfr_m=lfr_n=1` のオンライン特徴抽出のフレーム位置と、オフライン VAD の余分な空の最終チャンクを修正します。`token_list` 指定時の SentencePiece `ids2tokens` はモデルのピースを返し、テキストへの変換には `tokens2text` を使います。未指定時の従来のテキスト復号は維持します。[修正 #3849](https://github.com/modelscope/FunASR/pull/3849)、[修正 #3783](https://github.com/modelscope/FunASR/pull/3783)。[リリースと検証範囲 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.23)
**FunASR 1.4.22** は呼び出しごとの `merge_length_s` を反映し、現在の発話区間の長さに基づいて動的 VAD 無音しきい値をフレーム単位で更新します。固定しきい値モードも維持します。[修正 #3841](https://github.com/modelscope/FunASR/pull/3841)、[修正 #3833](https://github.com/modelscope/FunASR/pull/3833)。[リリースと検証範囲 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.22)
**FunASR 1.4.21** はストリーミング句読点処理の空入力で cache を保持します。非空推論は変更されません。[修正 #3824](https://github.com/modelscope/FunASR/pull/3824)。[リリースと検証範囲 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.21)
diff --git a/README_ko.md b/README_ko.md
index 94819a9b4..caa493859 100644
--- a/README_ko.md
+++ b/README_ko.md
@@ -111,7 +111,8 @@ checkpoint/revision, 오디오 집합, 하드웨어, 배치, 워밍업, 측정
## 최신 소식
- **MOSS-Transcribe-Diarize**를 FunASR service, Docker, Kubernetes, vLLM/SGLang workflow, FunClip에 통합해 긴 오디오 ASR, timestamp, 익명 speaker label을 한 번에 처리합니다. [MOSS 배포 ->](./docs/moss_transcribe_diarize.md)
-- **FunASR 1.4.24**는 빈 비언어 기호로 인한 토크나이저 무한 루프, 패딩이 있는 입력의 분산 정규화 오류, 발화 횟수가 0인 샘플이 섞인 배치의 CIF 충돌을 수정합니다. [수정 #3852](https://github.com/modelscope/FunASR/pull/3852), [수정 #3853](https://github.com/modelscope/FunASR/pull/3853), [수정 #3854](https://github.com/modelscope/FunASR/pull/3854). `norm_means=True`와 `norm_vars=True`를 모두 사용해 학습한 checkpoint는 정규화 특징이 달라지므로 인식 품질을 다시 평가해야 합니다. 기본값은 계속 `norm_vars=False`입니다. `python -m pip install -U "funasr==1.4.24"`로 설치할 수 있습니다. [릴리스 및 검증 범위 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
+- **FunASR 1.4.25**는 Python 3.12에서 `distutils` 없이 불리언 인수를 처리하고 정규화된 오류 메시지를 유지합니다. 라틴 문자로 시작하는 문장의 타임스탬프 출력에서 불필요한 앞 공백을 제거하며 시간 경계와 원문은 보존합니다. [수정 #3863](https://github.com/modelscope/FunASR/pull/3863), [수정 #3866](https://github.com/modelscope/FunASR/pull/3866). `python -m pip install -U "funasr==1.4.25"`로 설치할 수 있습니다. [릴리스 및 검증 범위 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.25)
+ **FunASR 1.4.24**는 빈 비언어 기호로 인한 토크나이저 무한 루프, 패딩이 있는 입력의 분산 정규화 오류, 발화 횟수가 0인 샘플이 섞인 배치의 CIF 충돌을 수정합니다. [수정 #3852](https://github.com/modelscope/FunASR/pull/3852), [수정 #3853](https://github.com/modelscope/FunASR/pull/3853), [수정 #3854](https://github.com/modelscope/FunASR/pull/3854). `norm_means=True`와 `norm_vars=True`를 모두 사용해 학습한 checkpoint는 정규화 특징이 달라지므로 인식 품질을 다시 평가해야 합니다. 기본값은 계속 `norm_vars=False`입니다. `python -m pip install -U "funasr==1.4.24"`로 설치할 수 있습니다. [릴리스 및 검증 범위 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
**FunASR 1.4.23**은 `lfr_m=lfr_n=1` 온라인 프런트엔드의 프레임 위치와 오프라인 VAD의 불필요한 빈 마지막 청크를 수정합니다. `token_list`를 지정하면 SentencePiece `ids2tokens`는 모델 토큰을 반환하며, 텍스트 변환에는 `tokens2text`를 사용합니다. 미지정 시 기존 텍스트 디코딩은 유지됩니다. [수정 #3849](https://github.com/modelscope/FunASR/pull/3849), [수정 #3783](https://github.com/modelscope/FunASR/pull/3783). [릴리스 및 검증 범위 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.23)
**FunASR 1.4.22**는 호출별 `merge_length_s`를 반영하고 현재 발화 구간 길이에 따라 동적 VAD 무음 임계값을 프레임마다 갱신합니다. 고정 임계값 모드도 유지합니다. [수정 #3841](https://github.com/modelscope/FunASR/pull/3841), [수정 #3833](https://github.com/modelscope/FunASR/pull/3833). [릴리스 및 검증 범위 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.22)
**FunASR 1.4.21**은 스트리밍 문장부호 처리의 빈 입력에서 cache를 유지합니다. 비어 있지 않은 추론은 변경되지 않습니다. [수정 #3824](https://github.com/modelscope/FunASR/pull/3824). [릴리스 및 검증 범위 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.21)
diff --git a/README_zh.md b/README_zh.md
index 9562ad33d..ec13d2f34 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -157,7 +157,8 @@ checkpoint/revision、音频集、硬件、批量大小、预热、计时范围
## 最新动态
- **MOSS-Transcribe-Diarize** 已接入 FunASR 服务、Docker、Kubernetes、vLLM/SGLang 工作流和 FunClip,一次完成长音频转写、时间戳与匿名说话人标注。[部署 MOSS ->](./docs/moss_transcribe_diarize_zh.md)
-- **FunASR 1.4.24** 忽略空的非语言符号以避免分词死循环,修复带填充输入的方差归一化,并避免包含零触发样本的混合批次导致 CIF 崩溃。[修复 #3852](https://github.com/modelscope/FunASR/pull/3852)、[修复 #3853](https://github.com/modelscope/FunASR/pull/3853)、[修复 #3854](https://github.com/modelscope/FunASR/pull/3854)。同时启用 `norm_means=True` 和 `norm_vars=True` 训练的 checkpoint 会收到不同的归一化特征,需要重新评估识别质量;默认仍为 `norm_vars=False`。安装:`python -m pip install -U "funasr==1.4.24"`。[发布说明与验证范围 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
+- **FunASR 1.4.25** 修复 Python 3.12 下不依赖 `distutils` 的布尔参数解析并保留归一化错误信息;移除拉丁文字句子时间戳中的多余前导空格,保留时间边界和原始文本。[修复 #3863](https://github.com/modelscope/FunASR/pull/3863)、[修复 #3866](https://github.com/modelscope/FunASR/pull/3866)。安装:`python -m pip install -U "funasr==1.4.25"`。[发布说明与验证范围 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.25)
+ **FunASR 1.4.24** 忽略空的非语言符号以避免分词死循环,修复带填充输入的方差归一化,并避免包含零触发样本的混合批次导致 CIF 崩溃。[修复 #3852](https://github.com/modelscope/FunASR/pull/3852)、[修复 #3853](https://github.com/modelscope/FunASR/pull/3853)、[修复 #3854](https://github.com/modelscope/FunASR/pull/3854)。同时启用 `norm_means=True` 和 `norm_vars=True` 训练的 checkpoint 会收到不同的归一化特征,需要重新评估识别质量;默认仍为 `norm_vars=False`。安装:`python -m pip install -U "funasr==1.4.24"`。[发布说明与验证范围 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)
**FunASR 1.4.23** 修复 `lfr_m=lfr_n=1` 在线前端的帧游标和离线 VAD 多余的空尾块。提供 `token_list` 时,SentencePiece 的 `ids2tokens` 返回模型词元,请用 `tokens2text` 转为文本;未提供 `token_list` 时保持旧有文本解码行为。[修复 #3849](https://github.com/modelscope/FunASR/pull/3849)、[修复 #3783](https://github.com/modelscope/FunASR/pull/3783)。[发布说明与验证范围 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.23)
**FunASR 1.4.22** 支持逐调用覆盖 `merge_length_s`,并根据当前语音段时长逐帧更新动态 VAD 静音阈值,同时保留固定阈值模式。[修复 #3841](https://github.com/modelscope/FunASR/pull/3841)、[修复 #3833](https://github.com/modelscope/FunASR/pull/3833)。[发布说明与验证范围 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.22)
**FunASR 1.4.21** 在流式标点收到空白或空输入时保留 cache;非空推理保持不变。[修复 #3824](https://github.com/modelscope/FunASR/pull/3824)。[发布说明与验证范围 ->](https://github.com/modelscope/FunASR/releases/tag/v1.4.21)
diff --git a/docs/repository_roles.md b/docs/repository_roles.md
index 5fd68b165..bff0e53a9 100644
--- a/docs/repository_roles.md
+++ b/docs/repository_roles.md
@@ -5,7 +5,7 @@ This document explains the responsibility boundaries, user entry points, and iss
> **Directional roadmap, not a release promise.**
> This document records shipped capabilities and active work, but does not commit to
> future version numbers or dates. The current Python release is
-> [`funasr==1.4.24`](https://github.com/modelscope/FunASR/releases/tag/v1.4.24).
+> [`funasr==1.4.25`](https://github.com/modelscope/FunASR/releases/tag/v1.4.25).
> Any future breaking release still requires a maintainer-approved milestone and
> migration plan.
diff --git a/docs/repository_roles_zh.md b/docs/repository_roles_zh.md
index 597bc1c69..fd979aced 100644
--- a/docs/repository_roles_zh.md
+++ b/docs/repository_roles_zh.md
@@ -4,7 +4,7 @@
> **方向性路线图,不是版本承诺。**
> 本文档记录已交付能力与正在推进的工作,但不承诺未来版本号或日期。当前 Python
-> 版本是 [`funasr==1.4.24`](https://github.com/modelscope/FunASR/releases/tag/v1.4.24)。
+> 版本是 [`funasr==1.4.25`](https://github.com/modelscope/FunASR/releases/tag/v1.4.25)。
> 任何未来的 breaking release 仍需 maintainer 确认 milestone 与迁移方案。
---
diff --git a/funasr/bin/_server_app.py b/funasr/bin/_server_app.py
index 588f1fbb1..8822e94a4 100644
--- a/funasr/bin/_server_app.py
+++ b/funasr/bin/_server_app.py
@@ -20,7 +20,7 @@ import soundfile as sf
try:
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.middleware.cors import CORSMiddleware
- from fastapi.responses import JSONResponse
+ from fastapi.responses import JSONResponse, PlainTextResponse
except ImportError:
raise ImportError(
"funasr-server requires additional packages. Install with: pip install vllm fastapi uvicorn python-multipart"
@@ -529,7 +529,7 @@ def create_app(
if response_format == "verbose_json":
return JSONResponse(build_openai_verbose_json(result, requested_language=language))
elif response_format == "text":
- return JSONResponse(result["text"])
+ return PlainTextResponse(result["text"])
else:
return JSONResponse({"text": result["text"]})
diff --git a/funasr/utils/timestamp_tools.py b/funasr/utils/timestamp_tools.py
index b97e405f3..8009ec757 100644
--- a/funasr/utils/timestamp_tools.py
+++ b/funasr/utils/timestamp_tools.py
@@ -189,6 +189,11 @@ def timestamp_sentence(
punc_id = int(punc_id) if punc_id is not None else 1
sentence_end = timestamp[1] if timestamp is not None else sentence_end
+ # Latin tokens are inserted with a leading space. Drop one leading
+ # space, as timestamp_sentence_en does, so a sentence that starts
+ # with an English word is not stored as " iphone...".
+ if sentence_text.startswith(" "):
+ sentence_text = sentence_text[1:]
if punc_id > 1:
sentence_text += punc_list[punc_id - 2]
if sentence_text_seg.endswith(" "):
diff --git a/funasr/utils/type_utils.py b/funasr/utils/type_utils.py
index 0731e46f8..2b2323c35 100644
--- a/funasr/utils/type_utils.py
+++ b/funasr/utils/type_utils.py
@@ -1,4 +1,3 @@
-from distutils.util import strtobool
from typing import Optional
from typing import Tuple
from typing import Union
@@ -15,7 +14,12 @@ def str2bool(value: str) -> bool:
Args:
value: TODO.
"""
- return bool(strtobool(value))
+ normalized = value.lower()
+ if normalized in ("y", "yes", "t", "true", "on", "1"):
+ return True
+ if normalized in ("n", "no", "f", "false", "off", "0"):
+ return False
+ raise ValueError(f"invalid truth value {normalized!r}")
def remove_parenthesis(value: str):
diff --git a/funasr/version.txt b/funasr/version.txt
index de64192cf..df3487bee 100644
--- a/funasr/version.txt
+++ b/funasr/version.txt
@@ -1 +1 @@
-1.4.24
+1.4.25
diff --git a/runtime/python/libtorch/funasr_torch/paraformer_bin.py b/runtime/python/libtorch/funasr_torch/paraformer_bin.py
index dc9d2a596..93159e85c 100644
--- a/runtime/python/libtorch/funasr_torch/paraformer_bin.py
+++ b/runtime/python/libtorch/funasr_torch/paraformer_bin.py
@@ -120,10 +120,10 @@ class Paraformer:
pred = sentence_postprocess(pred)
asr_res.append({"preds": pred})
else:
- for pred, us_peaks_ in zip(preds, us_peaks):
+ for pred, us_peaks_, feat_len in zip(preds, us_peaks, feats_len):
raw_tokens = pred
timestamp, timestamp_raw = time_stamp_lfr6_onnx(
- us_peaks_, copy.copy(raw_tokens)
+ us_peaks_[: feat_len * 3], copy.copy(raw_tokens)
)
text_proc, timestamp_proc, _ = sentence_postprocess(
raw_tokens, timestamp_raw
diff --git a/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py b/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
index d9e1b0643..d5b7a6ffb 100644
--- a/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
+++ b/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
@@ -106,17 +106,18 @@ def abbr_dispose(words: List[Any], time_stamp: List[List] = None) -> List[Any]:
if num in abbr_begin:
if time_stamp is not None:
begin = time_stamp[ts_nums[num]][0]
- word_lists.append(words[num].upper())
+ abbr_word = words[num].upper()
num += 1
while num < words_size:
if num in abbr_end:
- word_lists.append(words[num].upper())
+ abbr_word += words[num].upper()
last_num = num
break
else:
if words[num].encode("utf-8").isalpha():
- word_lists.append(words[num].upper())
+ abbr_word += words[num].upper()
num += 1
+ word_lists.append(abbr_word)
if time_stamp is not None:
end = time_stamp[ts_nums[num]][1]
ts_lists.append([begin, end])
diff --git a/runtime/python/onnxruntime/README.md b/runtime/python/onnxruntime/README.md
index 8794ac4c7..e3d18c6d4 100644
--- a/runtime/python/onnxruntime/README.md
+++ b/runtime/python/onnxruntime/README.md
@@ -7,6 +7,12 @@ does not update the installed ONNX wrappers. Check their versions separately
with `python -m pip show funasr-onnx funasr`; use the source installation below
when a required wrapper fix is newer than the published PyPI package.
+Version 0.4.10 restores direct librosa decoding for non-WAV audio in offline
+and streaming Paraformer, SenseVoice and offline VAD (#3865), without creating a WAV sidecar
+or treating every non-WAV input as MP3. Codec support still depends on the
+installed audio backend. This is a runtime loading fix, not a model-weight
+or ASR accuracy update.
+
Version 0.4.9 keeps one result per input when offline VAD receives empty or sub-frame
audio, without invoking ONNX Runtime for zero-frame features (#3846). It also returns
an empty punctuation chunk while it preserves the streaming cache and avoids
@@ -39,9 +45,9 @@ and postprocessing fixes, not a model-weight update or an ASR accuracy claim.
install from pip
```shell
-pip install -U "funasr-onnx>=0.4.9"
+pip install -U "funasr-onnx>=0.4.10"
# For the users in China, you could install with the command:
-# pip install -U "funasr-onnx>=0.4.9" -i https://mirror.sjtu.edu.cn/pypi/web/simple
+# pip install -U "funasr-onnx>=0.4.10" -i https://mirror.sjtu.edu.cn/pypi/web/simple
# If you want to export .onnx file, you should install modelscope and funasr
pip install -U modelscope funasr
# For the users in China, you could install with the command:
diff --git a/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
index 517252cd8..5a2da2189 100644
--- a/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
@@ -131,10 +131,10 @@ class Paraformer:
pred = sentence_postprocess(pred)
asr_res.append({"preds": pred})
else:
- for pred, us_peaks_ in zip(preds, us_peaks):
+ for pred, us_peaks_, feat_len in zip(preds, us_peaks, feats_len):
raw_tokens = pred
timestamp, timestamp_raw = time_stamp_lfr6_onnx(
- us_peaks_, copy.copy(raw_tokens)
+ us_peaks_[: feat_len * 3], copy.copy(raw_tokens)
)
text_proc, timestamp_proc, _ = sentence_postprocess(
raw_tokens, timestamp_raw
@@ -384,10 +384,10 @@ class ContextualParaformer(Paraformer):
pred = sentence_postprocess(pred)
asr_res.append({"preds": pred})
else:
- for pred, us_peaks_ in zip(preds, us_peaks):
+ for pred, us_peaks_, feat_len in zip(preds, us_peaks, feats_len):
raw_tokens = pred
timestamp, timestamp_raw = time_stamp_lfr6_onnx(
- us_peaks_, copy.copy(raw_tokens)
+ us_peaks_[: feat_len * 3], copy.copy(raw_tokens)
)
text_proc, timestamp_proc, _ = sentence_postprocess(
raw_tokens, timestamp_raw
diff --git a/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py b/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py
index 84e220166..82c334f85 100644
--- a/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py
+++ b/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py
@@ -108,17 +108,18 @@ def abbr_dispose(words: List[Any], time_stamp: List[List] = None) -> List[Any]:
if num in abbr_begin:
if time_stamp is not None:
begin = time_stamp[ts_nums[num]][0]
- word_lists.append(words[num].upper())
+ abbr_word = words[num].upper()
num += 1
while num < words_size:
if num in abbr_end:
- word_lists.append(words[num].upper())
+ abbr_word += words[num].upper()
last_num = num
break
else:
if words[num].encode("utf-8").isalpha():
- word_lists.append(words[num].upper())
+ abbr_word += words[num].upper()
num += 1
+ word_lists.append(abbr_word)
if time_stamp is not None:
end = time_stamp[ts_nums[num]][1]
ts_lists.append([begin, end])
diff --git a/runtime/python/onnxruntime/setup.py b/runtime/python/onnxruntime/setup.py
index b5236ffc1..40290072f 100644
--- a/runtime/python/onnxruntime/setup.py
+++ b/runtime/python/onnxruntime/setup.py
@@ -13,7 +13,7 @@ def get_readme():
MODULE_NAME = "funasr_onnx"
-VERSION_NUM = "0.4.9"
+VERSION_NUM = "0.4.10"
setuptools.setup(
name=MODULE_NAME,
diff --git a/tests/test_funasr_onnx_installed.py b/tests/test_funasr_onnx_installed.py
index 4b3c254ee..b6a74c78e 100644
--- a/tests/test_funasr_onnx_installed.py
+++ b/tests/test_funasr_onnx_installed.py
@@ -19,6 +19,8 @@ if importlib.util.find_spec('funasr_onnx') is None:
raise unittest.SkipTest('Install the standalone funasr-onnx distribution first.')
import funasr_onnx
+import numpy as np
+import soundfile as sf
from funasr_onnx.paraformer_online_bin import Paraformer as ParaformerOnline
@@ -36,9 +38,60 @@ ENTRYPOINTS = (
class InstalledOnnxPackageTest(unittest.TestCase):
+ LOADERS = (funasr_onnx.Paraformer, ParaformerOnline,
+ funasr_onnx.SenseVoiceSmall, funasr_onnx.Fsmn_vad)
+
+ def test_lossless_non_wav_without_sidecar(self):
+ samples = (np.arange(4800, dtype=np.int16) - 2400).astype(np.float32) / 32768
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / 'sample.flac'
+ sf.write(path, samples, 16000, subtype='PCM_16')
+ before = path.read_bytes()
+ for cls in self.LOADERS:
+ with self.subTest(loader=cls.__module__):
+ result = cls.__new__(cls).load_data(str(path), fs=16000)
+ self.assertEqual(len(result), 1)
+ np.testing.assert_array_equal(result[0], samples)
+ self.assertEqual(path.read_bytes(), before)
+ self.assertEqual(sorted(p.name for p in path.parent.iterdir()), ['sample.flac'])
+
+ def test_wav_and_flac_list_preserves_input_order(self):
+ first = np.full(800, 0.125, dtype=np.float32)
+ second = np.full(1200, -0.25, dtype=np.float32)
+ with tempfile.TemporaryDirectory() as directory:
+ paths = [Path(directory) / 'first.wav', Path(directory) / 'second.flac']
+ for path, samples in zip(paths, [first, second]):
+ sf.write(path, samples, 16000, subtype='PCM_16')
+ for cls in self.LOADERS:
+ with self.subTest(loader=cls.__module__):
+ result = cls.__new__(cls).load_data([str(p) for p in paths], fs=16000)
+ self.assertEqual(len(result), 2)
+ np.testing.assert_array_equal(result[0], first)
+ np.testing.assert_array_equal(result[1], second)
+ self.assertEqual(sorted(p.name for p in Path(directory).iterdir()), ['first.wav', 'second.flac'])
+
+ def test_waveform_array_is_preserved(self):
+ samples = np.linspace(-0.25, 0.25, 320, dtype=np.float32)
+ for cls in self.LOADERS:
+ with self.subTest(loader=cls.__module__):
+ result = cls.__new__(cls).load_data(samples, fs=16000)
+ self.assertEqual(len(result), 1)
+ self.assertIs(result[0], samples)
+
+ def test_invalid_audio_remains_visible_without_sidecar(self):
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / 'invalid.flac'
+ path.write_bytes(b'not audio')
+ for cls in self.LOADERS:
+ with self.subTest(loader=cls.__module__):
+ with self.assertRaises(Exception):
+ cls.__new__(cls).load_data(str(path), fs=16000)
+ self.assertEqual(path.read_bytes(), b'not audio')
+ self.assertEqual(sorted(p.name for p in path.parent.iterdir()), ['invalid.flac'])
+
def test_installed_distribution_version_and_origin(self):
distribution = importlib.metadata.distribution('funasr-onnx')
- self.assertEqual(distribution.version, '0.4.9')
+ self.assertEqual(distribution.version, '0.4.10')
self.assertEqual(
Path(funasr_onnx.__file__).resolve().parent,
Path(distribution.locate_file('funasr_onnx')).resolve(),
diff --git a/tests/test_funasr_onnx_release.py b/tests/test_funasr_onnx_release.py
index 160c24aa4..cb57f6be5 100644
--- a/tests/test_funasr_onnx_release.py
+++ b/tests/test_funasr_onnx_release.py
@@ -7,7 +7,7 @@ import unittest
ROOT = Path(__file__).resolve().parents[1]
PACKAGE_ROOT = ROOT / "runtime" / "python" / "onnxruntime"
SETUP_PATH = PACKAGE_ROOT / "setup.py"
-EXPECTED_VERSION = "0.4.9"
+EXPECTED_VERSION = "0.4.10"
REQUIREMENT_NAME_PATTERN = re.compile(r"^\s*([A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?)")
@@ -44,7 +44,7 @@ def normalized_requirement_name(requirement):
class FunASROnnxReleaseContractTest(unittest.TestCase):
- def test_release_version_is_0_4_9(self):
+ def test_release_version_is_0_4_10(self):
self.assertEqual(assigned_literal(read_setup_tree(), "VERSION_NUM"), EXPECTED_VERSION)
def test_standalone_license_preserves_repository_notice(self):
@@ -68,6 +68,8 @@ class FunASROnnxReleaseContractTest(unittest.TestCase):
readme = (PACKAGE_ROOT / "README.md").read_text()
self.assertIn(f'pip install -U "funasr-onnx>={EXPECTED_VERSION}"', readme)
self.assertIn("does not update the installed ONNX wrappers", readme)
+ self.assertIn("#3865", readme)
+ self.assertIn("without creating a WAV sidecar", readme)
self.assertIn("short/final VAD", readme)
self.assertIn("zero-overlap", readme)
self.assertIn("one independent result per input", readme)
diff --git a/tests/test_punc_model_none.py b/tests/test_punc_model_none.py
index 42dfe34e6..d827bca01 100644
--- a/tests/test_punc_model_none.py
+++ b/tests/test_punc_model_none.py
@@ -796,7 +796,7 @@ class TestPuncModelNone(unittest.TestCase):
def test_mixed_vad_boundaries_preserve_timestamp_units(self):
self._check_vad_chunk_alignment(
["你 好 hello", "world 我 来"],
- ["你好 hello,", " world 我来。"],
+ ["你好 hello,", "world 我来。"],
"你 好 hello world 我 来",
raw_text=True,
)
diff --git a/tests/test_release_version_contract.py b/tests/test_release_version_contract.py
index a0c64371c..d2b076db2 100644
--- a/tests/test_release_version_contract.py
+++ b/tests/test_release_version_contract.py
@@ -4,13 +4,13 @@ import pytest
ROOT = Path(__file__).resolve().parents[1]
-RELEASE_VERSION = "1.4.24"
+RELEASE_VERSION = "1.4.25"
README_FILES = ("README.md", "README_zh.md", "README_ja.md", "README_ko.md")
REPOSITORY_ROLE_FILES = ("docs/repository_roles.md", "docs/repository_roles_zh.md")
@pytest.mark.parametrize("name", README_FILES)
def test_release_summary_covers_tokenizer_normalizer_and_cif(name):
summaries = [line for line in (ROOT / name).read_text(encoding="utf-8").splitlines()
- if f"**FunASR {RELEASE_VERSION}**" in line]
+ if "**FunASR 1.4.24**" in line]
assert len(summaries) == 1
assert "CIF" in summaries[0]
assert "norm_means=True" in summaries[0]
@@ -20,6 +20,17 @@ def test_release_summary_covers_tokenizer_normalizer_and_cif(name):
assert f"https://github.com/modelscope/FunASR/pull/{number}" in summaries[0]
assert "checkpoint" in summaries[0]
[email protected]("name", README_FILES)
+def test_release_summary_covers_parser_and_timestamp_fixes(name):
+ summaries = [line for line in (ROOT / name).read_text(encoding="utf-8").splitlines()
+ if f"**FunASR {RELEASE_VERSION}**" in line]
+ assert len(summaries) == 1
+ assert "Python 3.12" in summaries[0]
+ assert "distutils" in summaries[0]
+ for number in (3863, 3866):
+ assert f"https://github.com/modelscope/FunASR/pull/{number}" in summaries[0]
+
+
diff --git a/tests/test_runtime_eval_native.py b/tests/test_runtime_eval_native.py
index e0e615480..ee8d9933b 100644
--- a/tests/test_runtime_eval_native.py
+++ b/tests/test_runtime_eval_native.py
@@ -8,6 +8,7 @@ import importlib
import json
import os
from pathlib import Path
+import shutil
import subprocess
import sys
import wave
@@ -275,6 +276,99 @@ def test_hotword_wrappers_decode_every_batch(
assert model(paths, hotwords) == alone
[email protected](scope="module")
+def timestamp_fixture(hotword_fixture, tmp_path_factory):
+ np = pytest.importorskip("numpy")
+ source, _ = hotword_fixture
+ directory = tmp_path_factory.mktemp("runtime-timestamp")
+ # The graphs only have to load: the timestamp test replaces the acoustic model's session.
+ for name, target in [
+ ("tokens.json", "tokens.json"),
+ ("model_eb.onnx", "model_eb.onnx"),
+ ("model.onnx", "model.onnx"),
+ ("model_bb.torchscript", "model.torchscript"),
+ ]:
+ shutil.copy(source / name, directory / target)
+ (directory / "config.yaml").write_text(
+ json.dumps(
+ {
+ "frontend_conf": {"n_mels": 4, "lfr_m": 7, "lfr_n": 6, "dither": 0.0},
+ "model_conf": {"predictor_bias": 1},
+ }
+ )
+ )
+ (directory / "am.mvn").write_text(
+ "<AddShift> 28 28\n<LearnRateCoef> 1 [ " + "0 " * 28 + "]\n"
+ "<Rescale> 28 28\n<LearnRateCoef> 1 [ " + "1 " * 28 + "]\n"
+ )
+ paths = []
+ for seconds in (1, 3):
+ path = directory / f"tone {seconds}s.wav"
+ tone = np.sin(np.arange(16000 * seconds) * 2 * np.pi * 440 / 16000) * 3000
+ with wave.open(str(path), "wb") as output:
+ output.setnchannels(1)
+ output.setsampwidth(2)
+ output.setframerate(16000)
+ output.writeframes(tone.astype("<i2").tobytes())
+ paths.append(str(path))
+ return directory, paths
+
+
+def timestamp_outputs(feats, feats_len):
+ """BiCifParaformer-style outputs for 你 好, padded to the longest row like the exported graph.
+
+ Each row's three CIF peaks lie inside its own 3 * feats_len upsampled frames; the
+ frames after them are zero padding.
+ """
+ import numpy as np
+
+ batch = len(feats_len)
+ am_scores = np.zeros((batch, 3, 8), dtype=np.float32)
+ am_scores[:, [0, 1, 2], [3, 4, 2]] = 1.0 # 你 好 </s>
+ us_alphas = np.zeros((batch, 3 * feats.shape[1]), dtype=np.float32)
+ us_peaks = np.zeros_like(us_alphas)
+ for row, feat_len in enumerate(np.asarray(feats_len).tolist()):
+ frames = 3 * feat_len
+ us_alphas[row, :frames] = 3.0 / frames
+ us_peaks[row, np.linspace(5, frames - 6, 3).round().astype(int)] = 1.0
+ return [am_scores, np.full(batch, 3, dtype=np.int32), us_alphas, us_peaks]
+
+
+ "backend, wrapper",
+ [
+ ("onnx", "Paraformer"),
+ ("onnx", "ContextualParaformer"),
+ ("onnx", "SeacoParaformer"),
+ ("torch", "Paraformer"),
+ ],
+)
+def test_batched_timestamps_ignore_padding(timestamp_fixture, monkeypatch, backend, wrapper):
+ torch = pytest.importorskip("torch")
+ directory, paths = timestamp_fixture
+ location = "libtorch" if backend == "torch" else "onnxruntime"
+ monkeypatch.syspath_prepend(str(ROOT / "runtime/python" / location))
+ module = importlib.import_module("funasr_" + backend + ".paraformer_bin")
+ model = getattr(module, wrapper)(str(directory), batch_size=2, intra_op_num_threads=1)
+ if backend == "torch":
+ model.ort_infer = lambda feats, feats_len: tuple(
+ torch.from_numpy(output) for output in timestamp_outputs(feats, feats_len)
+ )
+ elif wrapper == "Paraformer":
+ model.ort_infer = lambda inputs: timestamp_outputs(*inputs)
+ else:
+ model.ort_infer_bb = lambda inputs: timestamp_outputs(*inputs[:2])
+ hotwords = () if wrapper == "Paraformer" else ("魔搭",)
+ alone = [model(path, *hotwords)[0] for path in paths]
+ # The 1 s tone ends at 1000 ms, the 3 s tone at 3000 ms.
+ assert [row["timestamp"] for row in alone] == [
+ [[70, 470], [470, 945]],
+ [[70, 670], [1450, 2925]],
+ ]
+ # Batched with the 3 s tone, the 1 s tone must not take on the padded frames.
+ assert model(paths, *hotwords) == alone
+
+
@pytest.mark.parametrize("extension", ["flac", "ogg", "mp3"])
def test_onnx_wrappers_read_non_wav_paths(native_fixture, tmp_path, monkeypatch, extension):
np = pytest.importorskip("numpy")
diff --git a/tests/test_sentence_postprocess_timestamps.py b/tests/test_sentence_postprocess_timestamps.py
index 0aa6965c7..921495097 100644
--- a/tests/test_sentence_postprocess_timestamps.py
+++ b/tests/test_sentence_postprocess_timestamps.py
@@ -96,6 +96,26 @@ class SentencePostprocessTimestampTest(unittest.TestCase):
("你好", ["你", "好"]),
)
+ def test_spelled_out_letters_are_one_word_with_one_timestamp(self):
+ self.assert_timestamps(
+ ["我", "用", "a", "i", "写", "代", "码"],
+ ["我", "用", "AI", "写", "代", "码"],
+ [[0, 100], [100, 200], [200, 400], [400, 500], [500, 600], [600, 700]],
+ )
+ self.assert_timestamps(
+ ["i", "b", "m", "is", "here"],
+ ["IBM", "is", "here"],
+ [[0, 300], [300, 400], [400, 500]],
+ )
+
+ def test_spelled_out_letters_are_one_word_without_timestamps(self):
+ for path in POSTPROCESS_UTILS:
+ with self.subTest(path=path):
+ self.assertEqual(
+ _load_sentence_postprocess(path)(["看", "c", "c", "t", "v", "新", "闻"]),
+ ("看CCTV新闻", ["看", "CCTV", "新", "闻"]),
+ )
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_server_app_openai_segments.py b/tests/test_server_app_openai_segments.py
index 06a4951f8..28df9bc6e 100644
--- a/tests/test_server_app_openai_segments.py
+++ b/tests/test_server_app_openai_segments.py
@@ -12,6 +12,11 @@ SERVICE_PATH = REPO_ROOT / "funasr" / "bin" / "_server_app.py"
SERVER_CLI_PATH = REPO_ROOT / "funasr" / "bin" / "server.py"
+class PlainTextResponseStub:
+ def __init__(self, content=None):
+ self.content = content
+
+
def load_server_app(monkeypatch):
class DummyFastAPI:
def __init__(self, *args, **kwargs):
@@ -57,6 +62,7 @@ def load_server_app(monkeypatch):
responses_stub = types.ModuleType("fastapi.responses")
responses_stub.JSONResponse = lambda content=None: content
+ responses_stub.PlainTextResponse = PlainTextResponseStub
monkeypatch.setitem(sys.modules, "fastapi", fastapi_stub)
monkeypatch.setitem(sys.modules, "fastapi.middleware", middleware_stub)
@@ -260,6 +266,28 @@ def test_n8n_whisper_model_alias_uses_preloaded_model(monkeypatch):
]
+def test_text_response_format_returns_plain_text(monkeypatch):
+ module = load_server_app(monkeypatch)
+ install_dummy_funasr(monkeypatch, generated_text='say "hi"')
+ monkeypatch.setattr(module.sf, "info", lambda path: types.SimpleNamespace(duration=1.25))
+ app = module.create_app(device="cpu", preload_model="sensevoice")
+ transcribe = app.routes[("POST", "/v1/audio/transcriptions")]
+
+ response = asyncio.run(
+ transcribe(
+ file=DummyUpload(),
+ model="sensevoice",
+ language=None,
+ response_format="text",
+ spk=False,
+ )
+ )
+
+ # OpenAI returns the bare transcript, not a JSON-encoded string.
+ assert isinstance(response, PlainTextResponseStub)
+ assert response.content == 'say "hi"'
+
+
def test_openai_verbose_json_preserves_speaker_labels(monkeypatch):
module = load_server_app(monkeypatch)
diff --git a/tests/test_timestamp_tools.py b/tests/test_timestamp_tools.py
index a9dd41d18..763c14e19 100644
--- a/tests/test_timestamp_tools.py
+++ b/tests/test_timestamp_tools.py
@@ -57,6 +57,31 @@ def test_timestamp_sentence_en_missing_trailing_timestamp_matches_zh_fallback():
assert en_result[1]["start"] == zh_result[1]["start"]
+def test_timestamp_sentence_does_not_prefix_latin_text_with_a_space():
+ mixed = timestamp_sentence(
+ punc_id_list=[1, 2, 1, 1, 3],
+ timestamp_postprocessed=[[0, 100], [100, 200], [200, 300], [300, 400], [400, 500]],
+ text_postprocessed="我 用 iphone pro max",
+ return_raw_text=True,
+ )
+ assert [sentence["text"] for sentence in mixed] == ["我用,", "iphone pro max。"]
+ assert [sentence["raw_text"] for sentence in mixed] == ["我 用", "iphone pro max"]
+
+ english = timestamp_sentence(
+ [1, 3, 1],
+ [[0, 100], [100, 200], [200, 300]],
+ "hello world again",
+ )
+ assert [sentence["text"] for sentence in english] == ["hello world。", "again"]
+
+ chinese = timestamp_sentence(
+ [1, 3],
+ [[0, 100], [100, 200]],
+ "你好 世界",
+ )
+ assert chinese[0]["text"] == "你好世界。"
+
+
def test_timestamp_sentence_raw_text_keeps_token_separators():
result = timestamp_sentence(
punc_id_list=[1, 2, 1, 1, 3],
diff --git a/tests/test_type_utils_compat.py b/tests/test_type_utils_compat.py
new file mode 100644
index 000000000..0be3cfbda
--- /dev/null
+++ b/tests/test_type_utils_compat.py
@@ -0,0 +1,65 @@
+"""Keep the command-line boolean parser compatible without distutils."""
+import importlib.util
+from pathlib import Path
+import subprocess
+import sys
+
+import pytest
+
+
+SOURCE = Path(__file__).resolve().parents[1] / "funasr/utils/type_utils.py"
+
+
[email protected](scope="module")
+def parser():
+ spec = importlib.util.spec_from_file_location("boolean_parser_under_test", SOURCE)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module.str2bool
+
+
+ ("value", "expected"),
+ [("y", True), ("yes", True), ("t", True), ("true", True), ("on", True),
+ ("1", True), ("n", False), ("no", False), ("f", False), ("false", False),
+ ("off", False), ("0", False), ("YeS", True), ("TRUE", True),
+ ("nO", False), ("OFF", False)],
+)
+def test_boolean_argument_preserves_values_and_boolean_type(parser, value, expected):
+ assert parser(value) is expected
+
+
+ ("value", "message"),
+ [("MAYBE", "invalid truth value 'maybe'"),
+ ("FaLsE!", "invalid truth value 'false!'"),
+ (" TRUE ", "invalid truth value ' true '"),
+ ("", "invalid truth value ''"),
+ ("2", "invalid truth value '2'"),
+ ("false!", "invalid truth value 'false!'")],
+)
+def test_invalid_boolean_argument_preserves_normalized_error(parser, value, message):
+ with pytest.raises(ValueError) as caught:
+ parser(value)
+ assert str(caught.value) == message
+
+
+def test_boolean_parser_imports_when_distutils_is_unavailable():
+ code = """
+import importlib.abc
+import runpy
+import sys
+
+class MissingDistutils(importlib.abc.MetaPathFinder):
+ def find_spec(self, fullname, path=None, target=None):
+ if fullname == 'distutils' or fullname.startswith('distutils.'):
+ raise ImportError('distutils is unavailable')
+
+sys.meta_path.insert(0, MissingDistutils())
+parse = runpy.run_path(sys.argv[1])['str2bool']
+assert parse('yes') is True
+assert parse('no') is False
+"""
+ result = subprocess.run([sys.executable, "-c", code, str(SOURCE)],
+ text=True, capture_output=True, timeout=20)
+ assert result.returncode == 0, result.stdout + result.stderr