From 72ef8059ab34133a30829da82acbdcbcf1dfb286 Mon Sep 17 00:00:00 2001 From: Tanishq Date: Thu, 12 Nov 2026 23:21:25 +0530 Subject: [PATCH] fix(websocket): send file-mode hotwords as the protocol's JSON object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The websocket protocol sends hotwords as a JSON object string that maps each hotword to a weight, and funasr_wss_client.py builds that string from the --hotword file in microphone mode. Since #2791 the file mode (--audio_in) sent the first word of each line joined with commas, such as "阿里巴巴,通义实验室", and dropped the weights. The C++ servers load a client's hotwords and weights into the WFST decoder only from a JSON object, and pass other strings to the NN hotword model as they are. There the comma string is one hotword that is not all Chinese characters and not in seg_dict, so it is skipped: the client's hotwords had no effect. Build the hotwords field in file mode as the microphone mode does. An empty --hotword and one that is not a file are still sent unchanged. --- .github/workflows/test-python-websocket.yml | 5 +- runtime/python/websocket/funasr_wss_client.py | 14 ++- tests/test_websocket_client_hotwords.py | 100 ++++++++++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 tests/test_websocket_client_hotwords.py diff --git a/.github/workflows/test-python-websocket.yml b/.github/workflows/test-python-websocket.yml index d083808a3c..a3e6114422 100644 --- a/.github/workflows/test-python-websocket.yml +++ b/.github/workflows/test-python-websocket.yml @@ -8,6 +8,7 @@ on: - "tests/test_websocket_server_asr_model.py" - "tests/test_websocket_server_hotwords.py" - "tests/test_websocket_file_finalization.py" + - "tests/test_websocket_client_hotwords.py" - ".github/workflows/test-python-websocket.yml" push: branches: @@ -18,6 +19,7 @@ on: - "tests/test_websocket_server_asr_model.py" - "tests/test_websocket_server_hotwords.py" - "tests/test_websocket_file_finalization.py" + - "tests/test_websocket_client_hotwords.py" - ".github/workflows/test-python-websocket.yml" permissions: @@ -40,12 +42,13 @@ jobs: run: | python -m pip install pytest numpy scipy websockets python -m pip install torch --index-url https://download.pytorch.org/whl/cpu - - name: Run constructor, hotword and file-finalization regressions + - name: Run constructor, hotword, client hotword and file-finalization regressions run: >- python -m pytest -q tests/test_websocket_server_asr_model.py tests/test_websocket_server_hotwords.py tests/test_websocket_file_finalization.py + tests/test_websocket_client_hotwords.py --junitxml=websocket-test-results.xml - uses: actions/upload-artifact@v4 if: always() diff --git a/runtime/python/websocket/funasr_wss_client.py b/runtime/python/websocket/funasr_wss_client.py index b95c3867e2..16ed4dc32f 100644 --- a/runtime/python/websocket/funasr_wss_client.py +++ b/runtime/python/websocket/funasr_wss_client.py @@ -207,15 +207,21 @@ async def record_from_scp(chunk_begin, chunk_size, completion_queue): with open(args.hotword, encoding="utf-8") as f_scp: hot_lines = f_scp.readlines() - hot_list = [] + # the protocol's {"hotword": weight} JSON object, as in record_microphone + fst_dict = {} for line in hot_lines: words = line.strip().split() if not words: continue - # Python AutoModel: 用逗号分隔多个热词 - hot_list.append(words[0]) + if len(words) < 2: + print("Please checkout format of hotwords") + continue + try: + fst_dict[" ".join(words[:-1])] = int(words[-1]) + except ValueError: + print("Please checkout format of hotwords") - hotword_msg = ",".join(hot_list) + hotword_msg = json.dumps(fst_dict, ensure_ascii=False) else: hotword_msg = args.hotword diff --git a/tests/test_websocket_client_hotwords.py b/tests/test_websocket_client_hotwords.py new file mode 100644 index 0000000000..86eb8493ff --- /dev/null +++ b/tests/test_websocket_client_hotwords.py @@ -0,0 +1,100 @@ +"""Hotwords that runtime/python/websocket/funasr_wss_client.py sends in file mode. + +The websocket protocol (runtime/docs/websocket_protocol.md) sends ``hotwords`` as a +JSON object string that maps each hotword to a weight. The microphone mode builds it +from the --hotword file ("阿里巴巴 20" per line); the file mode sent the first word of +each line joined with commas instead. +""" + +import ast +import asyncio +import json +import os +import time +from pathlib import Path +from types import SimpleNamespace + +import pytest + +CLIENT_PATH = Path(__file__).resolve().parents[1] / "runtime/python/websocket/funasr_wss_client.py" + + +def _load_client_function(name, namespace): + # Importing the client would parse sys.argv, so only the function is compiled. + tree = ast.parse(CLIENT_PATH.read_text(encoding="utf-8"), filename=str(CLIENT_PATH)) + function = next( + node for node in tree.body if isinstance(node, ast.AsyncFunctionDef) and node.name == name + ) + exec(compile(ast.Module(body=[function], type_ignores=[]), str(CLIENT_PATH), "exec"), namespace) + return namespace[name] + + +class RecordingWebSocket: + def __init__(self): + self.sent = [] + + async def send(self, message): + self.sent.append(message) + + async def close(self): + pass + + +def _file_mode_hotwords(tmp_path, hotword): + """Send 0.1 s of audio with record_from_scp and return the config message's hotwords.""" + audio_path = tmp_path / "audio.pcm" + audio_path.write_bytes(b"\x00\x00" * 1600) + args = SimpleNamespace( + audio_in=str(audio_path), + hotword=hotword, + audio_fs=16000, + use_itn=1, + chunk_size=[5, 10, 5], + chunk_interval=10, + encoder_chunk_look_back=4, + decoder_chunk_look_back=0, + mode="offline", + send_without_sleep=True, + result_timeout=1.0, + ) + websocket = RecordingWebSocket() + namespace = { + "asyncio": asyncio, + "json": json, + "os": os, + "time": time, + "args": args, + "websocket": websocket, + "latency_first_audio_time": {}, + "latency_last_audio_time": {}, + "latency_first_text_printed": {}, + } + record_from_scp = _load_client_function("record_from_scp", namespace) + + async def send_file(): + acknowledgement = asyncio.Queue() + acknowledgement.put_nowait({"is_end": True, "is_final": True}) + await record_from_scp(0, 1, acknowledgement) + + asyncio.run(send_file()) + return json.loads(websocket.sent[0])["hotwords"] + + +@pytest.mark.parametrize( + ("lines", "expected"), + [ + ("阿里巴巴 20\n通义实验室 30\n", '{"阿里巴巴": 20, "通义实验室": 30}'), + ("hello world 10\n阿里巴巴\n通义实验室 high\n", '{"hello world": 10}'), + ], + ids=["two_hotwords", "phrase_and_lines_without_integer_weight"], +) +def test_file_mode_sends_hotword_file_as_json_object(tmp_path, lines, expected): + hotword_file = tmp_path / "hotwords.txt" + hotword_file.write_text(lines, encoding="utf-8") + + assert _file_mode_hotwords(tmp_path, str(hotword_file)) == expected + + +@pytest.mark.parametrize("hotword", ["", "阿里巴巴 通义实验室"], ids=["empty", "not_a_file"]) +def test_file_mode_sends_other_hotword_arguments_unchanged(tmp_path, hotword): + assert _file_mode_hotwords(tmp_path, hotword) == hotword