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
5 changes: 4 additions & 1 deletion .github/workflows/test-python-websocket.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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()
Expand Down
14 changes: 10 additions & 4 deletions runtime/python/websocket/funasr_wss_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
100 changes: 100 additions & 0 deletions tests/test_websocket_client_hotwords.py
Original file line number Diff line number Diff line change
@@ -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
Loading