调整WebSocket partial处理逻辑

main
Bifang 2026-10-09 16:15:43 +08:00
parent f27d844ed7
commit 3561802cc8
1 changed files with 14 additions and 115 deletions

View File

@ -281,12 +281,8 @@ class ConnectionContext:
last_partial_text: str = ""
last_partial_language: str = ""
last_partial_chunk_id: int = 0
last_partial_raw_text: str = ""
last_partial_display_text: str = ""
partial_preview_text: str = ""
stable_partial_prefix: str = ""
pending_partial_revision_text: str = ""
pending_partial_revision_rounds: int = 0
segment_observed_text: str = ""
segment_observed_language: str = ""
best_partial_text: str = ""
@ -1630,12 +1626,8 @@ class Qwen3ASRService:
ctx.last_partial_text = ""
ctx.last_partial_language = ""
ctx.last_partial_chunk_id = 0
ctx.last_partial_raw_text = ""
ctx.last_partial_display_text = ""
ctx.partial_preview_text = ""
ctx.stable_partial_prefix = ""
ctx.pending_partial_revision_text = ""
ctx.pending_partial_revision_rounds = 0
ctx.segment_observed_text = ""
ctx.segment_observed_language = ""
ctx.best_partial_text = ""
@ -1926,9 +1918,9 @@ class Qwen3ASRService:
candidate = self._sanitize_candidate_text(text)
if not candidate:
return ctx.segment_observed_text
if self._is_unstable_expansion(ctx.segment_observed_text, candidate):
return ctx.segment_observed_text
# Native Qwen partials are full snapshots. Keep the newest snapshot here;
# the final transcription will replace this provisional text at VAD end.
ctx.segment_observed_text = candidate
if language:
ctx.segment_observed_language = language
@ -1964,81 +1956,6 @@ class Qwen3ASRService:
idx += 1
return idx
def _stable_prefix_cutoff(self, text: str, max_commit_len: int) -> int:
if max_commit_len <= 0:
return 0
candidate = text[:max_commit_len]
if not candidate:
return 0
for idx in range(len(candidate) - 1, -1, -1):
ch = candidate[idx]
if ch.isspace() or unicodedata.category(ch).startswith("P"):
return idx + 1
return len(candidate)
def _stabilize_partial_text(self, ctx: ConnectionContext, raw_text: str) -> str:
text = self._sanitize_candidate_text(raw_text)
if not text:
ctx.last_partial_raw_text = ""
ctx.stable_partial_prefix = ""
ctx.pending_partial_revision_text = ""
ctx.pending_partial_revision_rounds = 0
return ""
previous_raw = ctx.last_partial_raw_text
stable_prefix = ctx.stable_partial_prefix
previous_emitted = ctx.last_partial_text.strip()
if previous_raw:
common_len = self._common_prefix_len(previous_raw, text)
keep_tail_chars = max(int(settings.REALTIME_STREAM_STABLE_TAIL_CHARS), 2)
max_commit_len = max(0, common_len - keep_tail_chars)
stable_cutoff = self._stable_prefix_cutoff(text, max_commit_len)
if stable_cutoff - len(stable_prefix) >= max(
int(settings.REALTIME_STREAM_STABLE_MIN_GROW_CHARS),
1,
):
stable_prefix = text[:stable_cutoff]
if stable_prefix and not text.startswith(stable_prefix):
stable_common = self._common_prefix_len(stable_prefix, text)
tolerated_divergence = max(
int(settings.REALTIME_STREAM_DIVERGENCE_TOLERANCE_CHARS),
1,
)
if len(stable_prefix) - stable_common >= tolerated_divergence and previous_emitted:
# A single shorter or divergent snapshot may be a transient model
# revision. Hold the visible text until the same correction repeats.
if text == ctx.pending_partial_revision_text:
ctx.pending_partial_revision_rounds += 1
else:
ctx.pending_partial_revision_text = text
ctx.pending_partial_revision_rounds = 1
if ctx.pending_partial_revision_rounds < 2:
ctx.last_partial_raw_text = text
return previous_emitted
# The repeated revision is stable enough to replace the old prefix.
stable_prefix = text[: self._stable_prefix_cutoff(text, stable_common)]
ctx.pending_partial_revision_text = ""
ctx.pending_partial_revision_rounds = 0
else:
ctx.pending_partial_revision_text = ""
ctx.pending_partial_revision_rounds = 0
stable_prefix = text[:stable_common]
stable_prefix = text[: self._stable_prefix_cutoff(text, len(stable_prefix))]
else:
ctx.pending_partial_revision_text = ""
ctx.pending_partial_revision_rounds = 0
ctx.stable_partial_prefix = stable_prefix
ctx.last_partial_raw_text = text
unstable_suffix = text[len(stable_prefix):]
return f"{stable_prefix}{unstable_suffix}".strip()
def _should_emit_partial(
self,
ctx: ConnectionContext,
@ -2112,38 +2029,27 @@ class Qwen3ASRService:
stable_prefix = stripped[:-holdback_chars].rstrip()
return stable_prefix or stripped
def _append_only_partial_preview(
def _update_partial_preview(
self,
ctx: ConnectionContext,
text: str,
*,
native_stream_snapshot: bool = False,
) -> str:
"""Keep the visible prefix steady while allowing only a short native tail to revise."""
"""Show each growing native snapshot; let the final transcript replace this draft."""
previous = ctx.partial_preview_text
if native_stream_snapshot:
# Qwen returns replaceable snapshots. Preserve everything before the
# small mutable tail so a temporary short snapshot cannot shrink the bubble.
candidate_text = self._sanitize_candidate_text(text)
stable_prefix = ctx.stable_partial_prefix.strip()
tail_chars = max(int(settings.REALTIME_STREAM_STABLE_TAIL_CHARS), 2)
if stable_prefix and candidate_text.startswith(stable_prefix):
suffix = candidate_text[len(stable_prefix):]
candidate = stable_prefix + suffix[-tail_chars:]
else:
# Before a stable prefix exists, keep the first preview intact; later
# updates may revise only its last tail_chars characters.
candidate = candidate_text
immutable_prefix = previous[:-tail_chars] if len(previous) > tail_chars else ""
if candidate and (not immutable_prefix or candidate.startswith(immutable_prefix)):
ctx.partial_preview_text = candidate
# 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):
ctx.partial_preview_text = candidate_text
else:
candidate = self._partial_display_text(ctx, text)
if candidate and (not previous or candidate.startswith(previous)):
ctx.partial_preview_text = candidate
# Revisions outside the mutable tail stay frozen until the final segment arrives.
return ctx.partial_preview_text
def _is_suspicious_segment_text(
@ -3203,7 +3109,7 @@ class Qwen3ASRService:
ctx.last_partial_decode_samples = int(ctx.segment_audio_buffer.size)
current = self._sanitize_candidate_text(current)
observed_chars = 0
stabilized_chars = 0
preview_input_chars = 0
preview_changed = False
partial_display = ""
partial_outcome = "empty"
@ -3217,16 +3123,9 @@ class Qwen3ASRService:
observed = update_observed_text(ctx, current, current_language)
observed_chars = len(observed)
visible_observed = self._trim_previous_segment_overlap(ctx, observed)
if has_native_stream_snapshot:
# Qwen returns full-utterance snapshots; stabilize the
# displayed prefix while allowing repeated corrections.
visible_observed = self._stabilize_partial_text(
ctx,
visible_observed,
)
stabilized_chars = len(visible_observed)
preview_input_chars = len(visible_observed)
previous_preview = ctx.partial_preview_text
partial_display = self._append_only_partial_preview(
partial_display = self._update_partial_preview(
ctx,
visible_observed,
native_stream_snapshot=has_native_stream_snapshot,
@ -3309,7 +3208,7 @@ class Qwen3ASRService:
"[%s] Realtime partial result: segment=%d source=%s outcome=%s "
"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 "
"stabilized_chars=%d preview_changed=%s display_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",
@ -3325,7 +3224,7 @@ class Qwen3ASRService:
),
len(current),
observed_chars,
stabilized_chars,
preview_input_chars,
preview_changed,
len(partial_display),
)