diff --git a/python-api-examples/streaming-paraformer-asr-microphone.py b/python-api-examples/streaming-paraformer-asr-microphone.py index 4efc40ca4..96331f358 100755 --- a/python-api-examples/streaming-paraformer-asr-microphone.py +++ b/python-api-examples/streaming-paraformer-asr-microphone.py @@ -9,6 +9,8 @@ # to download pre-trained models import sys + +import numpy as np from pathlib import Path try: @@ -92,6 +94,20 @@ def main(): display.display() if is_endpoint: + # For streaming paraformer models, which have a large right + # chunk size, we need to pad ~1 second of silence on + # endpointing so that the last character can be recognized. + # (Mirrors the C++ example sherpa-onnx-microphone.cc; see + # https://github.com/k2-fsa/sherpa-onnx/issues/1373) + tail_paddings = np.zeros(int(1.0 * sample_rate), dtype=np.float32) + stream.accept_waveform(sample_rate, tail_paddings) + while recognizer.is_ready(stream): + recognizer.decode_stream(stream) + + result = recognizer.get_result(stream) + display.update_text(result) + display.display() + if result: display.finalize_current_sentence() display.display() diff --git a/python-api-examples/streaming_server.py b/python-api-examples/streaming_server.py index ef6bac8e3..55ac2d2cb 100755 --- a/python-api-examples/streaming_server.py +++ b/python-api-examples/streaming_server.py @@ -757,6 +757,26 @@ Go back to /streaming_record.html "segment": segment, } if self.recognizer.is_endpoint(stream): + if self.recognizer.config.model_config.paraformer.encoder: + # For streaming paraformer models, which have a large + # right chunk size, we need to pad ~1 second of silence + # on endpointing so that the last character can be + # recognized. (Mirrors sherpa-onnx-microphone.cc; see + # https://github.com/k2-fsa/sherpa-onnx/issues/1373) + tail_padding = np.zeros(self.sample_rate).astype(np.float32) + stream.accept_waveform( + sample_rate=self.sample_rate, waveform=tail_padding + ) + while self.recognizer.is_ready(stream): + await self.compute_and_decode(stream) + + result = self.recognizer.get_result(stream) + message = { + "text": result, + "segment": segment, + } + await socket.send(json.dumps(message)) + self.recognizer.reset(stream) segment += 1