From 4f6e70fa7ce36892d8307181b9e01eaac90c6279 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 07:06:21 +0000 Subject: [PATCH 1/3] Collapse looping phrases and repeated segments in whisper output (cast-veeq) Greedy temperature-0 decoding can loop ("Thank you. Thank you. ..."). A looping phrase is collapsed to one occurrence and a segment that repeats the previous one is dropped. Real repeats ("very very good") are left alone. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011eiBNKnoczJNGAAcUAjESt --- .../crates/castcorder-transcribe/src/lib.rs | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/castcorder/crates/castcorder-transcribe/src/lib.rs b/castcorder/crates/castcorder-transcribe/src/lib.rs index cb5096e..69b64c9 100644 --- a/castcorder/crates/castcorder-transcribe/src/lib.rs +++ b/castcorder/crates/castcorder-transcribe/src/lib.rs @@ -252,12 +252,112 @@ impl Whisper { text, }); } - Ok(out) + Ok(squash_repeats(out)) } } +/// Words of `text` for comparing repeats: lowercase, punctuation dropped. +fn words(text: &str) -> Vec { + text.split_whitespace() + .map(|w| { + w.chars() + .filter(|c| c.is_alphanumeric()) + .collect::() + .to_lowercase() + }) + .filter(|w| !w.is_empty()) + .collect() +} + +/// Collapse a phrase the model repeats in a loop (greedy decoding at +/// temperature 0 can get stuck: "Thank you. Thank you. Thank you. ...") to one +/// occurrence. A run of the same 1..=12 words is only a loop from 3 repeats on +/// (or 2 for 3 words or more, which people do not say twice by accident). +fn squash_text(text: &str) -> String { + let toks: Vec<&str> = text.split_whitespace().collect(); + let keys: Vec = toks.iter().map(|w| words(w).concat()).collect(); + let mut out: Vec<&str> = Vec::new(); + let mut i = 0; + while i < toks.len() { + let mut skipped = false; + for n in 1..=12usize.min((toks.len() - i) / 2) { + let mut reps = 1; + while i + (reps + 1) * n <= toks.len() + && keys[i..i + n] == keys[i + reps * n..i + (reps + 1) * n] + { + reps += 1; + } + if reps >= if n >= 3 { 2 } else { 3 } { + out.extend(&toks[i..i + n]); + i += reps * n; + skipped = true; + break; + } + } + if !skipped { + out.push(toks[i]); + i += 1; + } + } + out.join(" ") +} + +/// Loops within a segment collapsed, and a segment that says what the one +/// before it said dropped (its time joins the earlier one). +fn squash_repeats(segs: Vec) -> Vec { + let mut out: Vec = Vec::new(); + for mut s in segs { + s.text = squash_text(&s.text); + if s.text.is_empty() { + continue; + } + match out.last_mut() { + Some(prev) if words(&prev.text) == words(&s.text) => prev.end_ms = s.end_ms, + _ => out.push(s), + } + } + out +} + #[cfg(test)] mod tests { + use super::{Segment, squash_repeats, squash_text}; + + #[test] + fn a_looping_phrase_is_said_once_and_real_repeats_stay() { + assert_eq!( + squash_text("Thank you. Thank you. Thank you. Thank you."), + "Thank you." + ); + assert_eq!(squash_text("No no no no no way"), "No way"); + assert_eq!( + squash_text("Well, I think so. I think so. Maybe."), + "Well, I think so. Maybe." + ); + // Said twice on purpose, or just a stutter: untouched. + assert_eq!(squash_text("very very good"), "very very good"); + assert_eq!( + squash_text("Hello there, welcome back"), + "Hello there, welcome back" + ); + } + + #[test] + fn a_segment_repeating_the_one_before_is_dropped() { + let seg = |a, b, t: &str| Segment { + start_ms: a, + end_ms: b, + text: t.into(), + }; + let got = squash_repeats(vec![ + seg(0, 1000, "Thank you."), + seg(1000, 2000, "thank you"), + seg(2000, 3000, "Thank you."), + seg(3000, 4000, "Next topic."), + ]); + assert_eq!(got.len(), 2); + assert_eq!((got[0].start_ms, got[0].end_ms), (0, 3000)); + } #[test] fn the_gpu_model_is_tried_first_and_either_rather_than_none() { let dir = std::env::temp_dir().join(format!("castcorder-pick-{}", std::process::id())); From 9129d692ce37096df4f18fe008e8be5228016f8e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 07:30:39 +0000 Subject: [PATCH 2/3] Transcribe: collapse a loop only from three repeats, never a phrase said twice Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011eiBNKnoczJNGAAcUAjESt --- .../crates/castcorder-transcribe/src/lib.rs | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/castcorder/crates/castcorder-transcribe/src/lib.rs b/castcorder/crates/castcorder-transcribe/src/lib.rs index 69b64c9..351e8ad 100644 --- a/castcorder/crates/castcorder-transcribe/src/lib.rs +++ b/castcorder/crates/castcorder-transcribe/src/lib.rs @@ -271,8 +271,9 @@ fn words(text: &str) -> Vec { /// Collapse a phrase the model repeats in a loop (greedy decoding at /// temperature 0 can get stuck: "Thank you. Thank you. Thank you. ...") to one -/// occurrence. A run of the same 1..=12 words is only a loop from 3 repeats on -/// (or 2 for 3 words or more, which people do not say twice by accident). +/// occurrence. A run of the same 1..=12 words is only a loop from 3 repeats on: +/// people do say a phrase twice on purpose ("I think so. I think so."), and a +/// wrong guess here deletes what they said. fn squash_text(text: &str) -> String { let toks: Vec<&str> = text.split_whitespace().collect(); let keys: Vec = toks.iter().map(|w| words(w).concat()).collect(); @@ -287,7 +288,7 @@ fn squash_text(text: &str) -> String { { reps += 1; } - if reps >= if n >= 3 { 2 } else { 3 } { + if reps >= 3 { out.extend(&toks[i..i + n]); i += reps * n; skipped = true; @@ -302,18 +303,30 @@ fn squash_text(text: &str) -> String { out.join(" ") } -/// Loops within a segment collapsed, and a segment that says what the one -/// before it said dropped (its time joins the earlier one). +/// Loops within a segment collapsed, and a run of three or more segments that +/// all say the same thing reduced to one (its time spans the run). Two in a +/// row stay: people do repeat themselves ("Yes. Yes."). fn squash_repeats(segs: Vec) -> Vec { - let mut out: Vec = Vec::new(); + // Each kept segment with the segments that said the same, in a row. + let mut runs: Vec<(Segment, Vec)> = Vec::new(); for mut s in segs { s.text = squash_text(&s.text); if s.text.is_empty() { continue; } - match out.last_mut() { - Some(prev) if words(&prev.text) == words(&s.text) => prev.end_ms = s.end_ms, - _ => out.push(s), + match runs.last_mut() { + Some((first, rest)) if words(&first.text) == words(&s.text) => rest.push(s), + _ => runs.push((s, Vec::new())), + } + } + let mut out = Vec::new(); + for (mut first, rest) in runs { + if rest.len() >= 2 { + first.end_ms = rest.last().map_or(first.end_ms, |l| l.end_ms); + out.push(first); + } else { + out.push(first); + out.extend(rest); } } out @@ -331,11 +344,15 @@ mod tests { ); assert_eq!(squash_text("No no no no no way"), "No way"); assert_eq!( - squash_text("Well, I think so. I think so. Maybe."), + squash_text("Well, I think so. I think so. I think so. Maybe."), "Well, I think so. Maybe." ); // Said twice on purpose, or just a stutter: untouched. assert_eq!(squash_text("very very good"), "very very good"); + assert_eq!( + squash_text("Well, I think so. I think so. Maybe."), + "Well, I think so. I think so. Maybe." + ); assert_eq!( squash_text("Hello there, welcome back"), "Hello there, welcome back" @@ -343,7 +360,7 @@ mod tests { } #[test] - fn a_segment_repeating_the_one_before_is_dropped() { + fn a_run_of_three_identical_segments_becomes_one() { let seg = |a, b, t: &str| Segment { start_ms: a, end_ms: b, @@ -357,6 +374,9 @@ mod tests { ]); assert_eq!(got.len(), 2); assert_eq!((got[0].start_ms, got[0].end_ms), (0, 3000)); + // Twice in a row is the speaker, not a loop. + let two = squash_repeats(vec![seg(0, 1000, "Yes."), seg(1000, 2000, "yes")]); + assert_eq!(two.len(), 2); } #[test] fn the_gpu_model_is_tried_first_and_either_rather_than_none() { From f3de3d032ff334cf68718e18c68214b8a661abae Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 07:34:16 +0000 Subject: [PATCH 3/3] Transcribe: let whisper retry a looping decode warmer, instead of filtering its output Greedy decoding at temperature 0 with fallback disabled can loop. whisper.cpp's own guard (token entropy under 2.4 -> retry at 0.2, 0.4 ... 1.0) was switched off by temperature_inc = 0. Switch it on, and keep the log-probability trigger off so ordinary quiet speech still decodes once at temperature 0. Replaces the text filter. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011eiBNKnoczJNGAAcUAjESt --- .../crates/castcorder-transcribe/src/lib.rs | 144 +++--------------- 1 file changed, 21 insertions(+), 123 deletions(-) diff --git a/castcorder/crates/castcorder-transcribe/src/lib.rs b/castcorder/crates/castcorder-transcribe/src/lib.rs index 351e8ad..ea25f02 100644 --- a/castcorder/crates/castcorder-transcribe/src/lib.rs +++ b/castcorder/crates/castcorder-transcribe/src/lib.rs @@ -5,7 +5,8 @@ //! nothing is downloaded). With the `vulkan` feature it uses the GPU when one //! is usable and the CPU otherwise. Each utterance is transcribed on its own //! (no context carried over), greedy, English, at temperature 0, so the same -//! audio gives the same text. Non-speech is dropped: segments whisper itself +//! audio gives the same text, except that a decode that loops is retried +//! warmer (whisper.cpp's repetition check, see `transcribe`). Non-speech is dropped: segments whisper itself //! rates as probably not speech, and markers such as "[BLANK_AUDIO]" or //! "(music)". //! @@ -92,6 +93,15 @@ fn hiss(secs: f32) -> Vec { /// A segment whisper rates at least this likely to be non-speech is dropped. const NO_SPEECH: f32 = 0.6; +/// How much warmer each retry of a decode that looped is (whisper.cpp's own +/// default); a loop is retried at 0.2, 0.4, ... up to 1.0. +const TEMPERATURE_STEP: f32 = 0.2; +/// A decode whose token entropy is under this is a loop (whisper.cpp's +/// default, the same as OpenAI's compression ratio check of 2.4). +const REPETITION_ENTROPY: f32 = 2.4; +/// Average log probability under which whisper.cpp would also retry warmer: +/// set so low it never fires, so only a loop causes a retry. +const NEVER_BY_CONFIDENCE: f32 = -100.0; /// Audio quieter than this (RMS, about -50 dBFS) is not given to the model: /// whisper invents words for near-silence. const QUIET_RMS: f32 = 0.003; @@ -212,8 +222,16 @@ impl Whisper { p.set_language(Some("en")); p.set_translate(false); p.set_no_context(true); + // Greedy at temperature 0 can get stuck in a loop ("Thank you. Thank + // you. ..."). whisper.cpp's own guard is on: a decode whose tokens are + // too repetitive (entropy under the threshold) is run again at a + // warmer temperature, up to 1.0. The other trigger, a low average log + // probability, is off (threshold far below any real value), so plain + // quiet or unclear speech still decodes once, at temperature 0. p.set_temperature(0.0); - p.set_temperature_inc(0.0); + p.set_temperature_inc(TEMPERATURE_STEP); + p.set_entropy_thold(REPETITION_ENTROPY); + p.set_logprob_thold(NEVER_BY_CONFIDENCE); p.set_suppress_blank(true); p.set_suppress_nst(true); p.set_print_progress(false); @@ -252,132 +270,12 @@ impl Whisper { text, }); } - Ok(squash_repeats(out)) + Ok(out) } } -/// Words of `text` for comparing repeats: lowercase, punctuation dropped. -fn words(text: &str) -> Vec { - text.split_whitespace() - .map(|w| { - w.chars() - .filter(|c| c.is_alphanumeric()) - .collect::() - .to_lowercase() - }) - .filter(|w| !w.is_empty()) - .collect() -} - -/// Collapse a phrase the model repeats in a loop (greedy decoding at -/// temperature 0 can get stuck: "Thank you. Thank you. Thank you. ...") to one -/// occurrence. A run of the same 1..=12 words is only a loop from 3 repeats on: -/// people do say a phrase twice on purpose ("I think so. I think so."), and a -/// wrong guess here deletes what they said. -fn squash_text(text: &str) -> String { - let toks: Vec<&str> = text.split_whitespace().collect(); - let keys: Vec = toks.iter().map(|w| words(w).concat()).collect(); - let mut out: Vec<&str> = Vec::new(); - let mut i = 0; - while i < toks.len() { - let mut skipped = false; - for n in 1..=12usize.min((toks.len() - i) / 2) { - let mut reps = 1; - while i + (reps + 1) * n <= toks.len() - && keys[i..i + n] == keys[i + reps * n..i + (reps + 1) * n] - { - reps += 1; - } - if reps >= 3 { - out.extend(&toks[i..i + n]); - i += reps * n; - skipped = true; - break; - } - } - if !skipped { - out.push(toks[i]); - i += 1; - } - } - out.join(" ") -} - -/// Loops within a segment collapsed, and a run of three or more segments that -/// all say the same thing reduced to one (its time spans the run). Two in a -/// row stay: people do repeat themselves ("Yes. Yes."). -fn squash_repeats(segs: Vec) -> Vec { - // Each kept segment with the segments that said the same, in a row. - let mut runs: Vec<(Segment, Vec)> = Vec::new(); - for mut s in segs { - s.text = squash_text(&s.text); - if s.text.is_empty() { - continue; - } - match runs.last_mut() { - Some((first, rest)) if words(&first.text) == words(&s.text) => rest.push(s), - _ => runs.push((s, Vec::new())), - } - } - let mut out = Vec::new(); - for (mut first, rest) in runs { - if rest.len() >= 2 { - first.end_ms = rest.last().map_or(first.end_ms, |l| l.end_ms); - out.push(first); - } else { - out.push(first); - out.extend(rest); - } - } - out -} - #[cfg(test)] mod tests { - use super::{Segment, squash_repeats, squash_text}; - - #[test] - fn a_looping_phrase_is_said_once_and_real_repeats_stay() { - assert_eq!( - squash_text("Thank you. Thank you. Thank you. Thank you."), - "Thank you." - ); - assert_eq!(squash_text("No no no no no way"), "No way"); - assert_eq!( - squash_text("Well, I think so. I think so. I think so. Maybe."), - "Well, I think so. Maybe." - ); - // Said twice on purpose, or just a stutter: untouched. - assert_eq!(squash_text("very very good"), "very very good"); - assert_eq!( - squash_text("Well, I think so. I think so. Maybe."), - "Well, I think so. I think so. Maybe." - ); - assert_eq!( - squash_text("Hello there, welcome back"), - "Hello there, welcome back" - ); - } - - #[test] - fn a_run_of_three_identical_segments_becomes_one() { - let seg = |a, b, t: &str| Segment { - start_ms: a, - end_ms: b, - text: t.into(), - }; - let got = squash_repeats(vec![ - seg(0, 1000, "Thank you."), - seg(1000, 2000, "thank you"), - seg(2000, 3000, "Thank you."), - seg(3000, 4000, "Next topic."), - ]); - assert_eq!(got.len(), 2); - assert_eq!((got[0].start_ms, got[0].end_ms), (0, 3000)); - // Twice in a row is the speaker, not a loop. - let two = squash_repeats(vec![seg(0, 1000, "Yes."), seg(1000, 2000, "yes")]); - assert_eq!(two.len(), 2); - } #[test] fn the_gpu_model_is_tried_first_and_either_rather_than_none() { let dir = std::env::temp_dir().join(format!("castcorder-pick-{}", std::process::id()));