From ed4f5cb6d9d1995a6a8e1eee1a4bd69b4778830c Mon Sep 17 00:00:00 2001 From: Bifang <915779419@qq.com> Date: Sat, 10 Oct 2026 11:14:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=84=E7=90=86=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=E9=97=B4=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 9 +- app/core/config.py | 66 ++----- app/services/asr/engines/global_models.py | 9 +- app/services/asr/qwen3_engine.py | 6 +- app/services/asr/qwen3_vllm.py | 6 +- app/services/asr/qwenasr_rust.py | 4 +- app/services/qwen3_websocket_asr.py | 217 +++++----------------- test_web/index.html | 52 +++--- 8 files changed, 103 insertions(+), 266 deletions(-) diff --git a/.env.example b/.env.example index 6cd22c1..df32e21 100644 --- a/.env.example +++ b/.env.example @@ -77,10 +77,15 @@ # 实时 / 推理调优(按需覆盖) # ----------------------------------------------------------------------------- # REALTIME_STREAM_CHUNK_SEC=0.6 -# REALTIME_STREAM_MAX_PENDING_CHUNKS=3 +# REALTIME_STREAM_MAX_PENDING_CHUNKS=2 +# REALTIME_MIN_PARTIAL_SEC=0.6 +# REALTIME_PARTIAL_EMIT_INTERVAL_SEC=0.6 +# REALTIME_PARTIAL_HOLDBACK_CHARS=0 # REALTIME_ENABLE_VAD=true -# REALTIME_VAD_CHUNK_MS=200 +# FunASR WebSocket defaults: 60 ms VAD frames, 600 ms online decode cadence, four concurrent VAD calls. +# REALTIME_VAD_CHUNK_MS=60 # REALTIME_VAD_PRE_ROLL_MS=600 +# REALTIME_VAD_CONCURRENCY=4 # MAX_SEGMENT_SEC=60 # QWEN_RUST_CPU_WORKERS=4 # QWEN_RUST_ASR_CONCURRENCY=0 diff --git a/app/core/config.py b/app/core/config.py index de29043..7fc033e 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -103,30 +103,28 @@ class Settings: REALTIME_SV_MODEL: str = "iic/speech_eres2netv2_sv_zh-cn_16k-common" REALTIME_SV_MODEL_REVISION: str = "" SV_THRESHOLD: float = 0.6 - REALTIME_FORCE_STABLE_SEGMENT_SEC: float = 8.0 - REALTIME_FORCE_STABLE_MIN_CHARS: int = 24 - REALTIME_MIN_PARTIAL_SEC: float = 0.45 - REALTIME_PARTIAL_EMIT_INTERVAL_SEC: float = 0.25 + # Match FunASR's 600 ms online decode cadence for 2-pass WebSocket streaming. + REALTIME_MIN_PARTIAL_SEC: float = 0.6 + REALTIME_PARTIAL_EMIT_INTERVAL_SEC: float = 0.6 REALTIME_PARTIAL_WINDOW_SEC: float = 8.0 # Match FunASR's roughly 600 ms online decode cadence. Qwen partial only # re-decodes the recent rolling window; final transcription still uses full audio. REALTIME_STREAM_CHUNK_SEC: float = 0.6 - REALTIME_STREAM_MAX_PENDING_CHUNKS: int = 3 + # Qwen-specific prefix warm-up; FunASR encoder/decoder look-back has no direct equivalent. + REALTIME_STREAM_MAX_PENDING_CHUNKS: int = 2 # 实时 partial 重解码窗口长度;完整定稿音频保存在 WebSocket turn buffer 中。 REALTIME_STREAM_WINDOW_SEC: float = 8.0 - REALTIME_STREAM_STABLE_TAIL_CHARS: int = 8 - REALTIME_STREAM_STABLE_MIN_GROW_CHARS: int = 2 - REALTIME_STREAM_DIVERGENCE_TOLERANCE_CHARS: int = 2 - REALTIME_PARTIAL_HOLDBACK_CHARS: int = 6 + # FunASR forwards each non-empty online partial without withholding tail chars. + REALTIME_PARTIAL_HOLDBACK_CHARS: int = 0 REALTIME_LONGFORM_MIN_SEC: float = 8.0 REALTIME_LONGFORM_CHUNK_SEC: float = 6.0 REALTIME_LONGFORM_OVERLAP_SEC: float = 1.2 - REALTIME_VAD_CHECK_INTERVAL_SEC: float = 0.8 REALTIME_VAD_FINALIZE_SILENCE_SEC: float = 0.6 REALTIME_ENABLE_VAD: bool = True - REALTIME_VAD_CHUNK_MS: int = 200 + # FunASR's WebSocket VAD cache advances on 60 ms frames. + REALTIME_VAD_CHUNK_MS: int = 60 REALTIME_VAD_PRE_ROLL_MS: int = 600 - REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC: float = 8.0 + REALTIME_VAD_CONCURRENCY: int = 4 REALTIME_ENABLE_DIARIZATION: bool = True REALTIME_ENABLE_SEGMENT_REFINE: bool = False REALTIME_DIARIZATION_MIN_SEC: float = 3.0 @@ -291,18 +289,6 @@ class Settings: self.REALTIME_SV_MODEL_REVISION, ) self.SV_THRESHOLD = float(os.getenv("SV_THRESHOLD", str(self.SV_THRESHOLD))) - self.REALTIME_FORCE_STABLE_SEGMENT_SEC = float( - os.getenv( - "REALTIME_FORCE_STABLE_SEGMENT_SEC", - str(self.REALTIME_FORCE_STABLE_SEGMENT_SEC), - ) - ) - self.REALTIME_FORCE_STABLE_MIN_CHARS = int( - os.getenv( - "REALTIME_FORCE_STABLE_MIN_CHARS", - str(self.REALTIME_FORCE_STABLE_MIN_CHARS), - ) - ) self.REALTIME_MIN_PARTIAL_SEC = float( os.getenv( "REALTIME_MIN_PARTIAL_SEC", @@ -339,24 +325,6 @@ class Settings: str(self.REALTIME_STREAM_WINDOW_SEC), ) ) - self.REALTIME_STREAM_STABLE_TAIL_CHARS = int( - os.getenv( - "REALTIME_STREAM_STABLE_TAIL_CHARS", - str(self.REALTIME_STREAM_STABLE_TAIL_CHARS), - ) - ) - self.REALTIME_STREAM_STABLE_MIN_GROW_CHARS = int( - os.getenv( - "REALTIME_STREAM_STABLE_MIN_GROW_CHARS", - str(self.REALTIME_STREAM_STABLE_MIN_GROW_CHARS), - ) - ) - self.REALTIME_STREAM_DIVERGENCE_TOLERANCE_CHARS = int( - os.getenv( - "REALTIME_STREAM_DIVERGENCE_TOLERANCE_CHARS", - str(self.REALTIME_STREAM_DIVERGENCE_TOLERANCE_CHARS), - ) - ) self.REALTIME_PARTIAL_HOLDBACK_CHARS = int( os.getenv( "REALTIME_PARTIAL_HOLDBACK_CHARS", @@ -381,12 +349,6 @@ class Settings: str(self.REALTIME_LONGFORM_OVERLAP_SEC), ) ) - self.REALTIME_VAD_CHECK_INTERVAL_SEC = float( - os.getenv( - "REALTIME_VAD_CHECK_INTERVAL_SEC", - str(self.REALTIME_VAD_CHECK_INTERVAL_SEC), - ) - ) self.REALTIME_VAD_FINALIZE_SILENCE_SEC = float( os.getenv( "REALTIME_VAD_FINALIZE_SILENCE_SEC", @@ -403,11 +365,9 @@ class Settings: self.REALTIME_VAD_PRE_ROLL_MS = int( os.getenv("REALTIME_VAD_PRE_ROLL_MS", str(self.REALTIME_VAD_PRE_ROLL_MS)) ) - self.REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC = float( - os.getenv( - "REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC", - str(self.REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC), - ) + self.REALTIME_VAD_CONCURRENCY = max( + int(os.getenv("REALTIME_VAD_CONCURRENCY", str(self.REALTIME_VAD_CONCURRENCY))), + 1, ) self.REALTIME_ENABLE_DIARIZATION = ( os.getenv( diff --git a/app/services/asr/engines/global_models.py b/app/services/asr/engines/global_models.py index 909cd77..382e5c4 100644 --- a/app/services/asr/engines/global_models.py +++ b/app/services/asr/engines/global_models.py @@ -19,7 +19,12 @@ logger = logging.getLogger(__name__) # 全局语音活动检测(VAD)模型缓存(避免重复加载) _global_vad_model = None _vad_model_lock = threading.Lock() -_vad_inference_lock = threading.Lock() # 推理互斥锁,防止并发状态混乱 +# FunASR's WebSocket server permits four concurrent VAD calls by default. +# Each call owns a per-session cache, so a bounded semaphore preserves that +# concurrency while limiting pressure on the shared model instance. +_vad_inference_lock = threading.BoundedSemaphore( + max(1, int(settings.REALTIME_VAD_CONCURRENCY)) +) # 全局标点符号模型缓存(避免重复加载) _global_punc_model = None @@ -82,7 +87,7 @@ def get_global_vad_model(device: str): def get_vad_inference_lock(): - """获取VAD模型推理锁(线程安全)""" + """Return the bounded VAD inference gate shared by online and offline calls.""" return _vad_inference_lock diff --git a/app/services/asr/qwen3_engine.py b/app/services/asr/qwen3_engine.py index 2bca0bd..367de7c 100644 --- a/app/services/asr/qwen3_engine.py +++ b/app/services/asr/qwen3_engine.py @@ -132,7 +132,7 @@ def _handle_asr_error(operation: str): class Qwen3StreamingState: internal_state: Any chunk_size_sec: float = 0.6 - unfixed_chunk_num: int = 3 + unfixed_chunk_num: int = 2 unfixed_token_num: int = 5 max_new_tokens: int = 32 language: Optional[str] = None @@ -579,7 +579,7 @@ class Qwen3ASREngine(BaseASREngine): if context: logger.debug("QwenASR Rust backend ignores streaming context hints") chunk_size_sec = float(kwargs.get("chunk_size_sec", 0.6)) - unfixed_chunk_num = int(kwargs.get("unfixed_chunk_num", 3)) + unfixed_chunk_num = int(kwargs.get("unfixed_chunk_num", 2)) unfixed_token_num = int(kwargs.get("unfixed_token_num", 5)) max_new_tokens = int(kwargs.get("max_new_tokens", 32)) stream_handle = self.model.create_stream( @@ -610,7 +610,7 @@ class Qwen3ASREngine(BaseASREngine): return Qwen3StreamingState( internal_state=streaming_state, chunk_size_sec=float(kwargs.get("chunk_size_sec", 0.6)), - unfixed_chunk_num=int(kwargs.get("unfixed_chunk_num", 3)), + unfixed_chunk_num=int(kwargs.get("unfixed_chunk_num", 2)), unfixed_token_num=int(kwargs.get("unfixed_token_num", 5)), max_new_tokens=int(kwargs.get("max_new_tokens", 32)), language=language, diff --git a/app/services/asr/qwen3_vllm.py b/app/services/asr/qwen3_vllm.py index f0d8483..00a0d97 100644 --- a/app/services/asr/qwen3_vllm.py +++ b/app/services/asr/qwen3_vllm.py @@ -703,7 +703,7 @@ class Qwen3VLLMBackend: *, context: str = "", language: Optional[str] = None, - chunk_size_sec: float = 1.2, + chunk_size_sec: float = 0.6, unfixed_chunk_num: int = 2, unfixed_token_num: int = 5, max_new_tokens: int = 32, @@ -725,6 +725,10 @@ class Qwen3VLLMBackend: ) def _decode_stream(self, state: VLLMRealtimeState) -> VLLMRealtimeState: + # The Qwen vLLM interface has no FunASR-style encoder/decoder cache. + # Each online update is therefore a bounded rolling-window snapshot; + # chunk_size_sec controls when to refresh it, while final ASR uses the + # complete utterance retained by the WebSocket service. prefix = "" # After the audio window rolls, decode a fresh window snapshot and stitch # its text overlap; old token prefixes would pin the model to stale audio. diff --git a/app/services/asr/qwenasr_rust.py b/app/services/asr/qwenasr_rust.py index 0079ac9..f3ef853 100644 --- a/app/services/asr/qwenasr_rust.py +++ b/app/services/asr/qwenasr_rust.py @@ -400,7 +400,7 @@ class QwenASRRustBackend: def create_stream( self, *, - chunk_size_sec: float = 1.2, + chunk_size_sec: float = 0.6, unfixed_chunk_num: int = 2, rollback_tokens: int = 5, max_new_tokens: int = 32, @@ -506,7 +506,7 @@ class QwenASRRustRuntime: def create_stream( self, *, - chunk_size_sec: float = 1.2, + chunk_size_sec: float = 0.6, unfixed_chunk_num: int = 2, rollback_tokens: int = 5, max_new_tokens: int = 32, diff --git a/app/services/qwen3_websocket_asr.py b/app/services/qwen3_websocket_asr.py index 44b2ea7..703d7aa 100644 --- a/app/services/qwen3_websocket_asr.py +++ b/app/services/qwen3_websocket_asr.py @@ -262,6 +262,8 @@ class ConnectionContext: realtime_stream_state: Optional[Qwen3StreamingState] = None # Keep stream failure diagnostics to one event per turn to avoid log flooding. realtime_stream_failure_logged: bool = False + last_stream_push_ms: float = 0.0 + vad_push_ms_since_partial: float = 0.0 sentence_active: bool = False silence_samples: int = 0 total_samples: int = 0 @@ -288,7 +290,6 @@ class ConnectionContext: best_partial_text: str = "" best_partial_language: str = "" last_partial_decode_samples: int = 0 - partial_stable_rounds: int = 0 send_lock: asyncio.Lock = field(default_factory=asyncio.Lock) background_tasks: set[asyncio.Task[Any]] = field(default_factory=set) speaker_job_queue: asyncio.Queue[Any] = field(default_factory=asyncio.Queue) @@ -1101,27 +1102,22 @@ class Qwen3ASRService: def _partial_emit_interval_samples(self, ctx: ConnectionContext) -> int: """ - Control how often we surface partial text to the client. + Match FunASR's 600 ms online update cadence where the backend allows it. - When native Qwen streaming is healthy we can emit much more frequently, - because decoding is already happening incrementally in memory. - If native streaming falls back to window retranscription, keep the - cadence a little slower to avoid excessive temp-file retranscribes. + Qwen vLLM refreshes a bounded audio-window snapshot per complete model + chunk, so polling for a partial more frequently cannot produce new text. """ interval_sec = max( float( getattr( settings, "REALTIME_PARTIAL_EMIT_INTERVAL_SEC", - 0.25, - ) or 0.25 + 0.6, + ) or 0.6 ), - 0.12, + 0.6, ) - if ctx.params.get("enable_native_partial_stream") is False: - interval_sec = max(interval_sec, max(self._stream_chunk_size_sec(ctx), 0.9)) - elif ctx.realtime_stream_state is None: - interval_sec = max(interval_sec, max(min(self._stream_chunk_size_sec(ctx), 0.8), 0.55)) + interval_sec = max(interval_sec, self._stream_chunk_size_sec(ctx)) return int(interval_sec * 16000) def _stream_window_samples(self) -> int: @@ -1156,18 +1152,6 @@ class Qwen3ASRService: 200, ) - def _soft_segment_limit_samples(self, ctx: ConnectionContext) -> int: - limit_sec = float( - ctx.params.get( - "soft_limit_sec", - getattr(settings, "REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC", 8.0), - ) - or 0.0 - ) - if limit_sec <= 0: - return 0 - return int(max(limit_sec, 1.0) * 16000) - def _clip_partial_text( self, text: str, @@ -1216,7 +1200,7 @@ class Qwen3ASRService: *, is_final: bool = False, ) -> List[List[List[int]]]: - """Run cached FSMN-VAD inference over fixed 200 ms mono audio frames.""" + """Run cached FSMN-VAD inference over fixed per-session audio frames.""" vad_model = get_global_vad_model(settings.DEVICE) results: List[List[List[int]]] = [] with get_vad_inference_lock(): @@ -1242,10 +1226,10 @@ class Qwen3ASRService: return [] chunk_size_ms = max( - int(getattr(settings, "REALTIME_VAD_CHUNK_MS", 200) or 200), - 100, + int(getattr(settings, "REALTIME_VAD_CHUNK_MS", 60) or 60), + 20, ) - chunk_samples = max(int(chunk_size_ms * 16), 1600) + chunk_samples = max(int(chunk_size_ms * 16), 320) ctx.vad_pending_audio = np.concatenate([ctx.vad_pending_audio, audio]) complete_count = int(ctx.vad_pending_audio.size // chunk_samples) if complete_count <= 0: @@ -1258,6 +1242,7 @@ class Qwen3ASRService: ] ctx.vad_pending_audio = np.asarray(ctx.vad_pending_audio[split_at:], dtype=np.float32) + vad_started = time.perf_counter() try: outputs = await run_sync( self._generate_realtime_vad_chunks, @@ -1270,6 +1255,9 @@ class Qwen3ASRService: ctx.vad_speech_active = False logger.warning("Realtime FSMN-VAD inference failed; using energy fallback: %s", exc) return [] + finally: + # Aggregate 60 ms VAD frame time until the next partial diagnostic. + ctx.vad_push_ms_since_partial += (time.perf_counter() - vad_started) * 1000.0 completed: List[tuple[int, int]] = [] for values in outputs: @@ -1298,8 +1286,8 @@ class Qwen3ASRService: return [] chunk_size_ms = max( - int(getattr(settings, "REALTIME_VAD_CHUNK_MS", 200) or 200), - 100, + int(getattr(settings, "REALTIME_VAD_CHUNK_MS", 60) or 60), + 20, ) final_chunk = np.asarray(ctx.vad_pending_audio, dtype=np.float32) ctx.vad_pending_audio = np.array([], dtype=np.float32) @@ -1343,17 +1331,6 @@ class Qwen3ASRService: def _enable_realtime_refine(self, ctx: ConnectionContext) -> bool: return False - @staticmethod - def _sentence_count(text: str) -> int: - if not text: - return 0 - return len(re.findall(r"[。!??!]", text)) - - @staticmethod - def _ends_with_sentence_punctuation(text: str) -> bool: - stripped = str(text or "").rstrip() - return bool(stripped) and stripped[-1] in "。!??!" - def _stream_chunk_size_sec(self, ctx: ConnectionContext) -> float: return max( float( @@ -1363,7 +1340,7 @@ class Qwen3ASRService: ) or settings.REALTIME_STREAM_CHUNK_SEC ), - 0.4, + 0.6, ) def _stream_unfixed_chunk_num(self, ctx: ConnectionContext) -> int: @@ -1446,6 +1423,7 @@ class Qwen3ASRService: engine: Qwen3ASREngine, audio: np.ndarray, ) -> None: + ctx.last_stream_push_ms = 0.0 if audio.size == 0: return if not self._should_use_native_partial_stream(ctx, engine): @@ -1456,6 +1434,7 @@ class Qwen3ASRService: if ctx.realtime_stream_state is None: return + push_started = time.perf_counter() try: ctx.realtime_stream_state = await run_sync( engine.streaming_transcribe, @@ -1474,6 +1453,10 @@ class Qwen3ASRService: ) ctx.realtime_stream_failure_logged = True ctx.realtime_stream_state = None + finally: + # Include actual stream inference time in the same diagnostic event + # as the snapshot read and WebSocket partial emission. + ctx.last_stream_push_ms = (time.perf_counter() - push_started) * 1000.0 async def _finish_realtime_stream_text( self, @@ -1633,23 +1616,9 @@ class Qwen3ASRService: ctx.best_partial_text = "" ctx.best_partial_language = "" ctx.last_partial_decode_samples = 0 - ctx.partial_stable_rounds = 0 ctx.realtime_stream_state = None ctx.realtime_stream_failure_logged = False - def _update_partial_stability( - self, - ctx: ConnectionContext, - text: str, - ) -> int: - current = text.strip() - previous = ctx.last_partial_text.strip() - if current and previous and current == previous: - ctx.partial_stable_rounds += 1 - else: - ctx.partial_stable_rounds = 0 - return ctx.partial_stable_rounds - def _prefer_segment_text( self, primary: str, @@ -1937,7 +1906,7 @@ class Qwen3ASRService: candidate = self._sanitize_candidate_text(text) if not candidate: return - if self._is_unstable_expansion(ctx.best_partial_text, candidate): + if not replace_snapshot and self._is_unstable_expansion(ctx.best_partial_text, candidate): return if replace_snapshot: # Qwen partial 是整句快照;修订句尾时应淘汰旧快照,避免旧文本被 final 选回。 @@ -1956,43 +1925,6 @@ class Qwen3ASRService: idx += 1 return idx - def _should_emit_partial( - self, - ctx: ConnectionContext, - text: str, - language: str, - ) -> bool: - stripped = self._sanitize_candidate_text(text) - if not stripped: - return False - if len(stripped) <= 1: - return False - if self._is_unstable_expansion(ctx.last_partial_display_text, stripped): - return False - - normalized_language = self._normalize_output_language(language, ctx) - if not normalized_language: - normalized_language = self._infer_text_language(stripped) - - if ( - stripped == ctx.last_partial_display_text - and normalized_language == ctx.last_partial_language - ): - return False - - dominant_language = self._get_session_dominant_language(ctx) - duration_sec = float(ctx.segment_audio_buffer.size) / 16000.0 - if len(stripped) <= 6 and self._is_suspicious_segment_text( - stripped, - language=normalized_language, - dominant_language=dominant_language, - duration_sec=max(duration_sec, 0.1), - explicit_language=ctx.params.get("language"), - ): - return False - - return True - def _partial_display_text(self, ctx: ConnectionContext, text: str) -> str: stripped = self._sanitize_candidate_text(text) if not stripped: @@ -2017,12 +1949,6 @@ class Qwen3ASRService: if holdback_chars <= 0: return stripped - if ctx.partial_stable_rounds >= 1: - return stripped - - if self._sentence_count(stripped) >= 1 and stripped[-1:] in "。!?.!?": - return stripped - if len(stripped) <= max(holdback_chars + 2, 8): return stripped @@ -2040,10 +1966,9 @@ class Qwen3ASRService: previous = ctx.partial_preview_text if native_stream_snapshot: candidate_text = self._sanitize_candidate_text(text) - # Qwen emits full snapshots rather than deltas. Accept equal or longer - # snapshots (including revisions) so a frozen prefix cannot stall output; - # ignore only transient shorter rollbacks to avoid collapsing the bubble. - if candidate_text and len(candidate_text) >= len(previous): + # Qwen emits full snapshots: replace the draft and allow shorter + # corrections to reach the client instead of freezing the old text. + if candidate_text: ctx.partial_preview_text = candidate_text else: candidate = self._partial_display_text(ctx, text) @@ -2276,59 +2201,6 @@ class Qwen3ASRService: return assembler.text() - def _should_force_stable_segment(self, ctx: ConnectionContext, text: str) -> bool: - force_sec = float( - max( - float( - ctx.params.get( - "force_stable_segment_sec", - getattr(settings, "REALTIME_FORCE_STABLE_SEGMENT_SEC", 0.0), - ) - or 0.0 - ), - 0.0, - ) - ) - if force_sec <= 0: - return False - - duration_sec = float(ctx.segment_audio_buffer.size) / 16000.0 - if duration_sec < force_sec: - return False - - partial_text = text.strip() - min_chars = max( - int( - ctx.params.get( - "force_stable_min_chars", - getattr(settings, "REALTIME_FORCE_STABLE_MIN_CHARS", 24), - ) - or 0 - ), - 8, - ) - if len(partial_text) < min_chars: - return False - - # 只因为前文某处已经出现过句号,就立刻截断整段,容易把后半句切坏。 - # 这里收紧条件:优先要求“当前尾部已经自然收句”。 - if self._ends_with_sentence_punctuation(partial_text): - return True - - # 如果句尾还没闭合,只在明显超时并且 partial 连续稳定时才软提交, - # 避免把“我们需要…”这类正在继续的从句过早切成两段。 - force_overtime_sec = max(force_sec + 2.0, force_sec * 1.35) - soft_limit_samples = self._soft_segment_limit_samples(ctx) - if soft_limit_samples > 0 and ctx.segment_audio_buffer.size >= soft_limit_samples: - force_overtime_sec = min( - force_overtime_sec, - float(ctx.segment_audio_buffer.size) / 16000.0, - ) - if duration_sec >= force_overtime_sec and ctx.partial_stable_rounds >= 1: - return True - - return ctx.partial_stable_rounds >= 2 - def _choose_committed_segment_text( self, *, @@ -2917,7 +2789,7 @@ class Qwen3ASRService: "silence_duration_ms": payload.get("silence_duration_ms", 800), "min_partial_sec": payload.get( "min_partial_sec", - 0.9, + settings.REALTIME_MIN_PARTIAL_SEC, ), "partial_window_sec": payload.get( "partial_window_sec", @@ -2957,18 +2829,6 @@ class Qwen3ASRService: "enable_realtime_vad_split", settings.REALTIME_ENABLE_VAD, ), - "force_stable_segment_sec": payload.get( - "force_stable_segment_sec", - settings.REALTIME_FORCE_STABLE_SEGMENT_SEC, - ), - "force_stable_min_chars": payload.get( - "force_stable_min_chars", - settings.REALTIME_FORCE_STABLE_MIN_CHARS, - ), - "soft_limit_sec": payload.get( - "soft_limit_sec", - settings.REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC, - ), } ctx.vad_cache = {} @@ -3204,15 +3064,25 @@ class Qwen3ASRService: # This records whether the partial reached the WebSocket send # path, without logging transcript contents or changing events. + partial_source = "window_fallback" + if has_native_stream_snapshot: + partial_source = ( + "qwen_vllm_window_snapshot" + if getattr(engine, "backend", "") == "vllm" + else "qwen_native_stream" + ) logger.info( "[%s] Realtime partial result: segment=%d source=%s outcome=%s " - "partial_resolve_ms=%.1f turn_audio_sec=%.2f window_audio_sec=%.2f " + "vad_push_ms=%.1f stream_push_ms=%.1f partial_resolve_ms=%.1f " + "turn_audio_sec=%.2f window_audio_sec=%.2f " "stream_chunk_id=%s window_rolled=%s candidate_chars=%d observed_chars=%d " "preview_input_chars=%d preview_changed=%s display_chars=%d", task_id, ctx.segment_index, - "native_stream" if has_native_stream_snapshot else "window_fallback", + partial_source, partial_outcome, + ctx.vad_push_ms_since_partial, + ctx.last_stream_push_ms, partial_decode_ms, len(ctx.segment_audio_buffer) / 16000.0, len(ctx.stream_window_buffer) / 16000.0, @@ -3228,6 +3098,7 @@ class Qwen3ASRService: preview_changed, len(partial_display), ) + ctx.vad_push_ms_since_partial = 0.0 # Match FunASR's streaming lifecycle: the active turn ends at a VAD # endpoint, not because a separate wall-clock segment timer elapsed. diff --git a/test_web/index.html b/test_web/index.html index 39a8083..e8db14e 100644 --- a/test_web/index.html +++ b/test_web/index.html @@ -2197,11 +2197,11 @@
- +
- - + +
@@ -2224,28 +2224,16 @@
- - + +
- - + +
- - -
-
- - -
-
- - -
-
- - + +
@@ -3898,7 +3886,9 @@ ${transcript} function flushLivePcm(force = false) { if (!state.liveSocket || state.liveSocket.readyState !== WebSocket.OPEN) return; - const frameSamples = Math.round(state.liveTargetSampleRate * 0.2); + // FunASR's WebSocket client sends 60 ms PCM frames; Qwen ASR accumulates + // ten of these frames before refreshing its 600 ms partial snapshot. + const frameSamples = Math.round(state.liveTargetSampleRate * 0.06); while (state.livePcmPending.length >= frameSamples) { const frame = state.livePcmPending.slice(0, frameSamples); state.liveSocket.send(frame.buffer); @@ -3934,21 +3924,23 @@ ${transcript} context: $("liveContext").value.trim(), enable_inverse_text_normalization: $("liveEnableItn").checked, // Favor a quick provisional preview; the final segment still replaces it with re-transcribed text. - chunk_size_sec: Math.max(Number($("liveStreamChunkSec").value || 0.4), 0.4), - unfixed_chunk_num: Math.max(Number($("liveUnfixedChunkNum").value || 1), 1), - unfixed_token_num: Math.max(Number($("liveUnfixedTokenNum").value || 1), 1), + chunk_size_sec: Math.max(Number($("liveStreamChunkSec").value || 0.6), 0.6), + unfixed_chunk_num: Math.max(Number($("liveUnfixedChunkNum").value || 2), 1), + unfixed_token_num: Math.max(Number($("liveUnfixedTokenNum").value || 5), 1), silence_duration_ms: Number($("liveSilenceDurationMs").value || 800), - min_partial_sec: Number($("liveMinPartialSec").value || 0.3), - pre_roll_ms: Number($("livePreRollMs").value || 240), - partial_holdback_chars: Number($("livePartialHoldbackChars").value || 2), + min_partial_sec: Number($("liveMinPartialSec").value || 0.6), + pre_roll_ms: Number($("livePreRollMs").value || 600), + // FunASR emits the current non-empty partial without a tail holdback. + partial_holdback_chars: 0, enable_native_partial_stream: $("liveEnableNativePartialStream").checked, enable_speaker: $("liveEnableSpeaker").checked, match_speaker_registry: $("liveMatchSpeakerRegistry").checked, speaker_threshold: Number($("liveSpeakerThreshold").value || 0.6), enable_realtime_longform: $("liveEnableRealtimeLongform").checked, enable_realtime_vad_split: $("liveEnableRealtimeVadSplit").checked, - force_stable_segment_sec: Number($("liveForceStableSegmentSec").value || 6), - force_stable_min_chars: Number($("liveForceStableMinChars").value || 24), + // Kept for request compatibility; the real-time route does not force time-based cuts. + force_stable_segment_sec: 0, + force_stable_min_chars: 24, }, }; }