移除单句12秒硬切逻辑,对齐FunASR由VAD控制结束思路

main
Bifang 2026-10-08 16:38:07 +08:00
parent e43d13b729
commit 5a02302587
3 changed files with 29 additions and 205 deletions

View File

@ -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",

View File

@ -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:

View File

@ -2231,10 +2231,6 @@
<label for="liveForceStableMinChars">软提交最少字数(force_stable_min_chars)</label>
<input id="liveForceStableMinChars" type="number" min="8" step="1" value="24">
</div>
<div class="field">
<label for="liveMaxSegmentSec">单段最长秒数(max_segment_sec)</label>
<input id="liveMaxSegmentSec" type="number" min="3" step="1" value="9">
</div>
</div>
<div class="mini-grid" style="margin-top: 10px;">
<label class="switch">原生流式 partial <input id="liveEnableNativePartialStream" type="checkbox" checked></label>
@ -3937,7 +3933,6 @@ ${transcript}
enable_realtime_vad_split: $("liveEnableRealtimeVadSplit").checked,
force_stable_segment_sec: Number($("liveForceStableSegmentSec").value || 6),
force_stable_min_chars: Number($("liveForceStableMinChars").value || 24),
max_segment_sec: Number($("liveMaxSegmentSec").value || 12),
},
};
}
@ -4116,12 +4111,33 @@ ${transcript}
function groupLiveTranscriptItems(items) {
const groups = [];
items.forEach((item) => {
// Late speaker updates can arrive after newer segments; sort by sentence id before grouping.
const orderedItems = items
.map((item, index) => ({ item, index }))
.sort((left, right) => {
const leftId = left.item.segmentIndex;
const rightId = right.item.segmentIndex;
const leftHasId = leftId !== null && leftId !== undefined && Number.isFinite(Number(leftId));
const rightHasId = rightId !== null && rightId !== undefined && Number.isFinite(Number(rightId));
if (leftHasId && rightHasId && Number(leftId) !== Number(rightId)) {
return Number(leftId) - Number(rightId);
}
if (leftHasId !== rightHasId) return leftHasId ? -1 : 1;
return left.index - right.index;
})
.map(({ item }) => item);
orderedItems.forEach((item) => {
const speakerKey = String(item.speakerKey || "");
const canJoinPrevious = !item.partial && !item.speakerPending && speakerKey && groups.length > 0
&& !groups[groups.length - 1].partial
&& !groups[groups.length - 1].speakerPending
&& groups[groups.length - 1].speakerKey === speakerKey;
const speakerName = String(item.speaker || "").trim();
const previous = groups[groups.length - 1];
const sameSpeaker = previous && (
(speakerKey && previous.speakerKey === speakerKey)
|| (speakerName && previous.speaker === speakerName)
);
const canJoinPrevious = !item.partial && !item.speakerPending && speakerName && sameSpeaker
&& !previous.partial
&& !previous.speakerPending;
if (!canJoinPrevious) {
groups.push({ ...item });
@ -4130,7 +4146,6 @@ ${transcript}
// Segment cuts are transport/recognition boundaries, not bubble boundaries.
// Keep adjacent finalized segments from the same speaker together even across silence.
const previous = groups[groups.length - 1];
previous.text = joinLiveTranscriptText(previous.text, item.text);
previous.endTimeMs = Math.max(Number(previous.endTimeMs || 0), Number(item.endTimeMs || 0));
});