From 5a0230258796d108754fa1e3aedd4bcccfc40858 Mon Sep 17 00:00:00 2001 From: Bifang <915779419@qq.com> Date: Thu, 8 Oct 2026 16:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=8D=95=E5=8F=A512=E7=A7=92?= =?UTF-8?q?=E7=A1=AC=E5=88=87=E9=80=BB=E8=BE=91=EF=BC=8C=E5=AF=B9=E9=BD=90?= =?UTF-8?q?FunASR=E7=94=B1VAD=E6=8E=A7=E5=88=B6=E7=BB=93=E6=9D=9F=E6=80=9D?= =?UTF-8?q?=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/config.py | 21 ---- app/services/qwen3_websocket_asr.py | 176 +--------------------------- test_web/index.html | 37 ++++-- 3 files changed, 29 insertions(+), 205 deletions(-) diff --git a/app/core/config.py b/app/core/config.py index 2c8f794..dc2fa7e 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -100,8 +100,6 @@ 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_MAX_SEGMENT_SEC: float = 12.0 - REALTIME_MAX_SEGMENT_TAIL_SEC: float = 1.6 REALTIME_FORCE_STABLE_SEGMENT_SEC: float = 8.0 REALTIME_FORCE_STABLE_MIN_CHARS: int = 24 REALTIME_MIN_PARTIAL_SEC: float = 0.45 @@ -125,7 +123,6 @@ class Settings: REALTIME_VAD_CHUNK_MS: int = 200 REALTIME_VAD_PRE_ROLL_MS: int = 600 REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC: float = 8.0 - REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC: float = 12.0 REALTIME_ENABLE_DIARIZATION: bool = True REALTIME_ENABLE_SEGMENT_REFINE: bool = False REALTIME_DIARIZATION_MIN_SEC: float = 3.0 @@ -274,18 +271,6 @@ class Settings: self.REALTIME_SV_MODEL_REVISION, ) self.SV_THRESHOLD = float(os.getenv("SV_THRESHOLD", str(self.SV_THRESHOLD))) - self.REALTIME_MAX_SEGMENT_SEC = float( - os.getenv( - "REALTIME_MAX_SEGMENT_SEC", - str(self.REALTIME_MAX_SEGMENT_SEC), - ) - ) - self.REALTIME_MAX_SEGMENT_TAIL_SEC = float( - os.getenv( - "REALTIME_MAX_SEGMENT_TAIL_SEC", - str(self.REALTIME_MAX_SEGMENT_TAIL_SEC), - ) - ) self.REALTIME_FORCE_STABLE_SEGMENT_SEC = float( os.getenv( "REALTIME_FORCE_STABLE_SEGMENT_SEC", @@ -404,12 +389,6 @@ class Settings: str(self.REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC), ) ) - self.REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC = float( - os.getenv( - "REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC", - str(self.REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC), - ) - ) self.REALTIME_ENABLE_DIARIZATION = ( os.getenv( "REALTIME_ENABLE_DIARIZATION", diff --git a/app/services/qwen3_websocket_asr.py b/app/services/qwen3_websocket_asr.py index 3a6dd78..6dfd6de 100644 --- a/app/services/qwen3_websocket_asr.py +++ b/app/services/qwen3_websocket_asr.py @@ -311,7 +311,6 @@ class SessionEntry: class Qwen3ASRService: _MAX_SPEAKER_HISTORY_RECORDS = 128 _MAX_SPEAKER_FINAL_RECLUSTER = 128 - _MIN_HARD_SEGMENT_SEC = 12.0 _SEGMENT_EVENT_TEXT_TAIL_CHARS = 2000 _SEGMENT_EVENT_TAIL_COUNT = 8 _LIGHTWEIGHT_RECENT_SENTENCE_COUNT = 3 @@ -1158,18 +1157,6 @@ class Qwen3ASRService: 200, ) - def _max_segment_samples(self, ctx: ConnectionContext) -> int: - configured_sec = float( - ctx.params.get( - "hard_limit_sec", - ctx.params.get("max_segment_sec", settings.REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC), - ) - or 0.0 - ) - if configured_sec <= 0: - return 0 - return int(max(configured_sec, self._MIN_HARD_SEGMENT_SEC, 1.0) * 16000) - def _soft_segment_limit_samples(self, ctx: ConnectionContext) -> int: limit_sec = float( ctx.params.get( @@ -1368,51 +1355,6 @@ class Qwen3ASRService: stripped = str(text or "").rstrip() return bool(stripped) and stripped[-1] in "。!??!" - def _last_sentence_body_len(self, text: str) -> int: - stripped = str(text or "").rstrip() - if not stripped: - return 0 - if self._ends_with_sentence_punctuation(stripped): - stripped = stripped[:-1].rstrip() - last_boundary = max(stripped.rfind(ch) for ch in "。!??!") - body = stripped[last_boundary + 1:] if last_boundary >= 0 else stripped - return sum(1 for ch in body if ch.isalnum() or "\u4e00" <= ch <= "\u9fff") - - def _should_commit_complete_sentence(self, ctx: ConnectionContext, text: str) -> bool: - candidate = self._sanitize_candidate_text(text) - if not candidate or not self._ends_with_sentence_punctuation(candidate): - return False - - duration_sec = float(ctx.segment_audio_buffer.size) / 16000.0 - min_sec = max( - float( - ctx.params.get( - "complete_sentence_commit_sec", - max(float(ctx.params.get("force_stable_segment_sec", 8.0) or 8.0), 12.0), - ) - or 12.0 - ), - 6.0, - ) - if duration_sec < min_sec: - return False - - min_chars = max( - int( - ctx.params.get( - "complete_sentence_commit_min_chars", - max(int(ctx.params.get("force_stable_min_chars", 24) or 24), 48), - ) - or 48 - ), - 12, - ) - if len(candidate) < min_chars: - return False - - # Avoid committing on tiny unstable tails such as "可。" or "啊。". - return self._last_sentence_body_len(candidate) >= 6 - def _stream_chunk_size_sec(self, ctx: ConnectionContext) -> float: return max( float( @@ -2287,45 +2229,6 @@ class Qwen3ASRService: finalized_audio = np.asarray(audio[:finalized_end_sample], dtype=np.float32) return finalized_audio, np.array([], dtype=np.float32) - async def _find_completed_segment_split_sample( - self, - audio: np.ndarray, - ) -> Optional[int]: - if audio.size < int(3.0 * 16000): - return None - - vad_segments = await self._run_vad_segments(audio) - if not vad_segments: - return None - - valid_segments = [ - (int(seg[0]), int(seg[1])) - for seg in vad_segments - if len(seg) >= 2 and int(seg[1]) > int(seg[0]) - ] - if len(valid_segments) < 2: - return None - - total_ms = int(audio.size / 16) - finalize_silence_ms = int(max(settings.REALTIME_VAD_FINALIZE_SILENCE_SEC, 0.2) * 1000) - - last_start_ms, last_end_ms = valid_segments[-1] - if total_ms - last_end_ms >= finalize_silence_ms: - return min(audio.size, last_end_ms * 16) - - # 接近 max_duration 时,优先把倒数第二个已完成语音段刷出去,保留最后一段继续等上下文。 - hard_limit_sec = max( - float(getattr(settings, "REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC", 12.0) or 12.0), - 1.0, - ) - near_limit_ms = int(max(hard_limit_sec * 0.85, 4.0) * 1000) - if total_ms >= near_limit_ms: - _, prev_end_ms = valid_segments[-2] - if prev_end_ms > 0: - return min(audio.size, prev_end_ms * 16) - - return None - async def _transcribe_audio_text( self, engine: Qwen3ASREngine, @@ -2408,18 +2311,6 @@ class Qwen3ASRService: return assembler.text() - def _split_max_duration_audio( - self, - audio: np.ndarray, - ) -> tuple[np.ndarray, np.ndarray]: - tail_samples = int(max(settings.REALTIME_MAX_SEGMENT_TAIL_SEC, 0.0) * 16000) - min_flush_samples = int(max(settings.REALTIME_SPEAKER_MIN_SEC, 1.0) * 16000) - if tail_samples <= 0 or audio.size <= tail_samples + min_flush_samples: - return audio, np.array([], dtype=np.float32) - flush_audio = np.asarray(audio[:-tail_samples], dtype=np.float32) - carry_audio = np.asarray(audio[-tail_samples:], dtype=np.float32) - return flush_audio, carry_audio - def _should_force_stable_segment(self, ctx: ConnectionContext, text: str) -> bool: force_sec = float( max( @@ -2655,7 +2546,7 @@ class Qwen3ASRService: ) ): return fallback_text - if not force and duration_sec < 6.0 and reason != "max_duration": + if not force and duration_sec < 6.0: return fallback_text temp_path: Optional[str] = None @@ -2716,9 +2607,6 @@ class Qwen3ASRService: ) elif reason == "silence" and self._enable_realtime_vad_split(ctx): finalized_audio, carry_audio = await self._split_silence_audio_by_vad(current_audio) - elif reason in {"max_duration", "long_speech"} and self._enable_realtime_vad_split(ctx): - finalized_audio, carry_audio = self._split_max_duration_audio(current_audio) - has_native_stream_snapshot = ( carry_audio.size == 0 and self._should_use_native_partial_stream(ctx, engine) @@ -3098,13 +2986,6 @@ class Qwen3ASRService: "soft_limit_sec", settings.REALTIME_FINAL_SEGMENT_SOFT_LIMIT_SEC, ), - "hard_limit_sec": payload.get( - "hard_limit_sec", - payload.get( - "max_segment_sec", - settings.REALTIME_FINAL_SEGMENT_HARD_LIMIT_SEC, - ), - ), } ctx.vad_cache = {} @@ -3334,17 +3215,8 @@ class Qwen3ASRService: ) ctx.state = ConnectionState.STREAMING - # Follow FunASR's VAD-driven segment lifecycle; punctuation count - # should not force an otherwise active realtime speech segment to commit. - if self._should_commit_complete_sentence(ctx, visible_observed): - await self._commit_retranscribe_turn( - websocket, - ctx, - task_id, - "complete_sentence", - ) - continue - + # Match FunASR's streaming lifecycle: the active turn ends at a VAD + # endpoint, not because a separate wall-clock segment timer elapsed. if vad_boundaries and ctx.sentence_active and not bulk_audio_frame: if await self._commit_vad_boundary( websocket, @@ -3406,48 +3278,6 @@ class Qwen3ASRService: ctx.state = ConnectionState.STREAMING continue - max_segment_samples = self._max_segment_samples(ctx) - if max_segment_samples > 0 and ctx.segment_audio_buffer.size >= max_segment_samples: - if not self._enable_realtime_vad_split(ctx): - await self._commit_retranscribe_turn( - websocket, - ctx, - task_id, - "max_duration", - ) - ctx.state = ConnectionState.STREAMING - continue - - split_sample = await self._find_completed_segment_split_sample( - ctx.segment_audio_buffer, - ) - if split_sample is not None and 0 < split_sample < ctx.segment_audio_buffer.size: - finalized_audio = np.asarray( - ctx.segment_audio_buffer[:split_sample], - dtype=np.float32, - ) - carry_audio = np.asarray( - ctx.segment_audio_buffer[split_sample:], - dtype=np.float32, - ) - await self._commit_retranscribe_turn( - websocket, - ctx, - task_id, - "long_speech", - finalized_audio_override=finalized_audio, - carry_audio_override=carry_audio, - ) - else: - await self._commit_retranscribe_turn( - websocket, - ctx, - task_id, - "max_duration", - ) - ctx.state = ConnectionState.STREAMING - continue - except WebSocketDisconnect: logger.info("[%s] WebSocket disconnected", task_id) except Exception as exc: diff --git a/test_web/index.html b/test_web/index.html index 3ef8be3..c950ac5 100644 --- a/test_web/index.html +++ b/test_web/index.html @@ -2231,10 +2231,6 @@ -