Pad 1s of silence on endpoint for streaming paraformer in Python examples (mirror C++ behavior, #1373) (#4052)

This commit is contained in:
Jasper authored and GitHub committed 2026-10-15 13:52:34 +08:00
1 parent 1654a3054e
commit fa29eb91c8
2 files changed
+36

No files matched your search

@@ -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()
+20
View File
@@ -757,6 +757,26 @@ Go back to <a href="/streaming_record.html">/streaming_record.html</a>
"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