Skip to content

Commit 0a5a003

Browse files
authored
fix(cli): route Paraformer hotwords correctly (#3237)
1 parent 68a21aa commit 0a5a003

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

funasr/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ def main():
139139
if args.language:
140140
gen_kw["language"] = args.language
141141
if args.hotwords:
142-
gen_kw["hotwords"] = args.hotwords.split(",")
142+
hotwords = [
143+
word.strip() for word in args.hotwords.split(",") if word.strip()
144+
]
145+
if args.model == "paraformer":
146+
gen_kw["hotword"] = " ".join(hotwords)
147+
else:
148+
gen_kw["hotwords"] = hotwords
143149

144150
result = model.generate(**gen_kw)
145151
elapsed = time.time() - t0

tests/test_cli.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, **kwargs):
1515
self.instances.append(self)
1616

1717
def generate(self, **kwargs):
18+
self.generate_kwargs = kwargs
1819
return [{"text": "hello"}]
1920

2021

@@ -39,3 +40,32 @@ def test_cli_passes_hub_to_auto_model(tmp_path):
3940
assert DummyAutoModel.instances[0].kwargs["hub"] == "hf"
4041
assert stdout.getvalue().strip() == "hello"
4142

43+
44+
def test_cli_routes_multiple_hotwords_to_paraformer_hotword(tmp_path):
45+
audio_path = tmp_path / "sample.wav"
46+
audio_path.write_bytes(b"not a real wav")
47+
fake_torch = types.SimpleNamespace(
48+
cuda=types.SimpleNamespace(is_available=lambda: False),
49+
)
50+
51+
DummyAutoModel.instances = []
52+
argv = [
53+
"funasr",
54+
"--model",
55+
"paraformer",
56+
"--hotwords",
57+
"FunASR, ModelScope",
58+
str(audio_path),
59+
]
60+
61+
with (
62+
patch.object(sys, "argv", argv),
63+
patch.dict(sys.modules, {"torch": fake_torch}),
64+
patch("funasr.AutoModel", DummyAutoModel),
65+
redirect_stdout(io.StringIO()),
66+
):
67+
cli.main()
68+
69+
generate_kwargs = DummyAutoModel.instances[0].generate_kwargs
70+
assert generate_kwargs["hotword"] == "FunASR ModelScope"
71+
assert "hotwords" not in generate_kwargs

0 commit comments

Comments
 (0)