mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* chore(deps): update transcribe-cpp to 0.4.0 Model::capabilities() now returns a Result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(models): download a pinned Silero VAD with every transcription model Downloads and verification now work on a GGUF artifact, so the speech detector reuses the same resumable, checksummed path. Its download is best effort and never fails the model download. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(dictation): add a Silero speech detector Loads the pinned Silero VAD on the CPU on first use, answers whether 16 kHz audio contains speech, and returns None while the detector is missing or broken so callers keep their level-only silence check. A missing file starts one background download per process. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(dictation): skip captures with no detected speech A capture above the -50 dBFS level gate now also goes through the Silero speech detector when it is installed; a key click, the start tone or a noisy room no longer reaches the model and comes back as an invented phrase. Without the detector the gate works as before. Preparation prewarms an installed detector and never downloads it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(meetings): skip chunks with no detected speech Rolling transcription, the Stop & Transcribe fallback and meeting echo now ask the Silero speech detector before each chunk. A chunk without speech becomes an empty transcript without inference, and accepting it still advances the track's coverage, so the draft and echo suppression keep moving. On a 63-minute call the detector rejected all 152 invented microphone entries and kept 32 of 33 real ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(transcription): drop Whisper output that only repeats the recognition hints Whisper reads the hints as its initial prompt and returns them verbatim on silence or noise. Segments equal to the hints after normalization are removed together with their words, so meetings do not rebuild them into sentences; speech that contains a hint is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(speech): address review of the speech detector gate - Keep a single recognition hint: the echo filter needs at least two hints, and commas, semicolons and line breaks now separate words. - Lower the Silero threshold to 0.4 so a whisper at -43 dBFS counts as speech. - Log audio_ms and vad_ms at debug level when a dictation passes the detector. - Download the detector in the background when a model downloads or activates, so an installed model activates at once offline. - Fix documentation drift (local API, Linux stop tone, prewarm, figures). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Speech detector: fix Linux dead code, prewarm on Linux, retry a failed download * Drop the log-capture test that races other tests over tracing interest --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Dax Raad <kit.langton@gmail.com>
337 lines
11 KiB
Rust
337 lines
11 KiB
Rust
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
use crate::transcription_models::{ModelRuntime, TranscriptionModelId, TranscriptionSelection};
|
|
|
|
type Prepare = dyn Fn(&TranscriptionSelection, bool, &AtomicBool, &AtomicU64) -> Result<(), String>
|
|
+ Send
|
|
+ Sync;
|
|
|
|
/// Application-owned preparation shared by Settings and the menu bar.
|
|
/// At most one worker runs; a newer request replaces the single pending request.
|
|
#[derive(Clone)]
|
|
pub struct TranscriptionPreparation(Rc<RefCell<State>>);
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
pub struct PreparationStatus {
|
|
pub model: Option<TranscriptionModelId>,
|
|
pub downloaded_bytes: u64,
|
|
pub error: Option<String>,
|
|
}
|
|
|
|
struct Request {
|
|
selection: TranscriptionSelection,
|
|
installed_only: bool,
|
|
}
|
|
|
|
struct Task {
|
|
request: Request,
|
|
canceled: Arc<AtomicBool>,
|
|
progress: Arc<AtomicU64>,
|
|
worker: JoinHandle<Result<(), String>>,
|
|
}
|
|
|
|
struct State {
|
|
prepare: Arc<Prepare>,
|
|
active: Option<Task>,
|
|
pending: Option<Request>,
|
|
error: Option<String>,
|
|
}
|
|
|
|
impl Default for TranscriptionPreparation {
|
|
fn default() -> Self {
|
|
Self::with_prepare(Arc::new(prepare_selection))
|
|
}
|
|
}
|
|
|
|
impl TranscriptionPreparation {
|
|
pub(crate) fn with_prepare(prepare: Arc<Prepare>) -> Self {
|
|
Self(Rc::new(RefCell::new(State {
|
|
prepare,
|
|
active: None,
|
|
pending: None,
|
|
error: None,
|
|
})))
|
|
}
|
|
|
|
pub fn start(&self, selection: TranscriptionSelection, installed_only: bool) {
|
|
self.cancel();
|
|
let mut state = self.0.borrow_mut();
|
|
state.error = None;
|
|
state.pending = Some(Request {
|
|
selection,
|
|
installed_only,
|
|
});
|
|
state.start_pending();
|
|
}
|
|
|
|
pub fn cancel(&self) {
|
|
let mut state = self.0.borrow_mut();
|
|
state.pending = None;
|
|
if let Some(task) = &state.active {
|
|
task.canceled.store(true, Ordering::Release);
|
|
}
|
|
}
|
|
|
|
/// Only the application controller consumes completions and commits settings.
|
|
pub fn poll(&self) -> Option<Result<TranscriptionSelection, String>> {
|
|
let mut state = self.0.borrow_mut();
|
|
if !state.active.as_ref()?.worker.is_finished() {
|
|
return None;
|
|
}
|
|
let task = state.active.take().expect("finished preparation exists");
|
|
let result = match task.worker.join() {
|
|
Ok(result) => result,
|
|
Err(_) => Err("Model preparation worker failed.".into()),
|
|
};
|
|
let accepted = !task.canceled.load(Ordering::Acquire);
|
|
state.start_pending();
|
|
accepted.then(|| result.map(|()| task.request.selection))
|
|
}
|
|
|
|
pub fn status(&self) -> PreparationStatus {
|
|
let state = self.0.borrow();
|
|
let request = state.pending.as_ref().or_else(|| {
|
|
state
|
|
.active
|
|
.as_ref()
|
|
.filter(|task| !task.canceled.load(Ordering::Acquire))
|
|
.map(|task| &task.request)
|
|
});
|
|
PreparationStatus {
|
|
model: request.map(|request| request.selection.model),
|
|
downloaded_bytes: if state.pending.is_none() && request.is_some() {
|
|
state
|
|
.active
|
|
.as_ref()
|
|
.map_or(0, |task| task.progress.load(Ordering::Relaxed))
|
|
} else {
|
|
0
|
|
},
|
|
error: state.error.clone(),
|
|
}
|
|
}
|
|
|
|
pub fn set_error(&self, error: Option<String>) {
|
|
self.0.borrow_mut().error = error;
|
|
}
|
|
}
|
|
|
|
impl State {
|
|
fn start_pending(&mut self) {
|
|
if self.active.is_some() {
|
|
return;
|
|
}
|
|
let Some(request) = self.pending.take() else {
|
|
return;
|
|
};
|
|
let canceled = Arc::new(AtomicBool::new(false));
|
|
let progress = Arc::new(AtomicU64::new(0));
|
|
let prepare = self.prepare.clone();
|
|
let selection = request.selection.clone();
|
|
let installed_only = request.installed_only;
|
|
let worker_canceled = canceled.clone();
|
|
let worker_progress = progress.clone();
|
|
match thread::Builder::new()
|
|
.name("transcription-preparation".into())
|
|
.spawn(move || {
|
|
prepare(
|
|
&selection,
|
|
installed_only,
|
|
&worker_canceled,
|
|
&worker_progress,
|
|
)
|
|
}) {
|
|
Ok(worker) => {
|
|
self.active = Some(Task {
|
|
request,
|
|
canceled,
|
|
progress,
|
|
worker,
|
|
});
|
|
}
|
|
Err(error) => self.error = Some(format!("Could not start model preparation: {error}")),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for State {
|
|
fn drop(&mut self) {
|
|
if let Some(task) = &self.active {
|
|
task.canceled.store(true, Ordering::Release);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn prepare_selection(
|
|
selection: &TranscriptionSelection,
|
|
installed_only: bool,
|
|
canceled: &AtomicBool,
|
|
progress: &AtomicU64,
|
|
) -> Result<(), String> {
|
|
let check_canceled = || {
|
|
if canceled.load(Ordering::Acquire) {
|
|
color_eyre::eyre::bail!("Model preparation canceled.");
|
|
}
|
|
Ok(())
|
|
};
|
|
(|| -> color_eyre::Result<()> {
|
|
let model = crate::transcription_models::validate(selection)?;
|
|
if matches!(model.runtime, ModelRuntime::Gguf(_)) {
|
|
if installed_only {
|
|
crate::transcription_models::verify_installed(model, canceled)?;
|
|
progress.store(model.download_bytes().unwrap_or(0), Ordering::Relaxed);
|
|
} else {
|
|
crate::transcription_models::download_with_progress(model, canceled, progress)?;
|
|
}
|
|
}
|
|
check_canceled()?;
|
|
crate::speech_detector::prewarm();
|
|
// macOS warms the model to prove it loads. Windows and Linux load it
|
|
// once, when the listener restarts with the new selection.
|
|
#[cfg(target_os = "macos")]
|
|
crate::transcription::Transcriber::load_selection(selection).map(drop)?;
|
|
check_canceled()
|
|
})()
|
|
.map_err(|error| error.to_string())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::sync::Mutex;
|
|
use std::sync::mpsc;
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn selection(model: TranscriptionModelId) -> TranscriptionSelection {
|
|
TranscriptionSelection {
|
|
model,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn completion(
|
|
preparation: &TranscriptionPreparation,
|
|
) -> Result<TranscriptionSelection, String> {
|
|
let deadline = Instant::now() + Duration::from_secs(3);
|
|
loop {
|
|
if let Some(result) = preparation.poll() {
|
|
return result;
|
|
}
|
|
assert!(Instant::now() < deadline, "preparation did not complete");
|
|
thread::sleep(Duration::from_millis(1));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn preparation_has_one_worker_and_only_keeps_the_latest_pending_choice() {
|
|
let (started, starts) = mpsc::channel();
|
|
let (release, releases) = mpsc::channel();
|
|
let releases = Mutex::new(releases);
|
|
let preparation = TranscriptionPreparation::with_prepare(Arc::new(
|
|
move |selection, installed, _, progress| {
|
|
assert!(installed);
|
|
progress.store(123, Ordering::Relaxed);
|
|
started.send(selection.model).unwrap();
|
|
releases.lock().unwrap().recv().unwrap();
|
|
Ok(())
|
|
},
|
|
));
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV2), true);
|
|
assert_eq!(
|
|
starts.recv_timeout(Duration::from_secs(3)).unwrap(),
|
|
TranscriptionModelId::ParakeetV2
|
|
);
|
|
assert_eq!(preparation.status().downloaded_bytes, 123);
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV3), true);
|
|
preparation.start(selection(TranscriptionModelId::WhisperLargeV3Turbo), true);
|
|
assert_eq!(
|
|
preparation.status().model,
|
|
Some(TranscriptionModelId::WhisperLargeV3Turbo)
|
|
);
|
|
assert_eq!(preparation.status().downloaded_bytes, 0);
|
|
assert!(starts.try_recv().is_err());
|
|
release.send(()).unwrap();
|
|
let deadline = Instant::now() + Duration::from_secs(3);
|
|
loop {
|
|
assert!(preparation.poll().is_none(), "superseded selection escaped");
|
|
if let Ok(model) = starts.try_recv() {
|
|
assert_eq!(model, TranscriptionModelId::WhisperLargeV3Turbo);
|
|
break;
|
|
}
|
|
assert!(Instant::now() < deadline);
|
|
thread::sleep(Duration::from_millis(1));
|
|
}
|
|
release.send(()).unwrap();
|
|
assert_eq!(
|
|
completion(&preparation).unwrap().model,
|
|
TranscriptionModelId::WhisperLargeV3Turbo
|
|
);
|
|
assert!(preparation.status().model.is_none());
|
|
assert!(starts.try_recv().is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn cancelling_a_completed_unpolled_preparation_never_commits_it() {
|
|
let preparation = TranscriptionPreparation::with_prepare(Arc::new(|_, _, _, _| Ok(())));
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV2), true);
|
|
let deadline = Instant::now() + Duration::from_secs(3);
|
|
while !preparation
|
|
.0
|
|
.borrow()
|
|
.active
|
|
.as_ref()
|
|
.unwrap()
|
|
.worker
|
|
.is_finished()
|
|
{
|
|
assert!(Instant::now() < deadline);
|
|
thread::sleep(Duration::from_millis(1));
|
|
}
|
|
preparation.cancel();
|
|
assert!(preparation.poll().is_none());
|
|
assert!(preparation.0.borrow().active.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn preparation_failure_and_worker_panic_allow_a_later_selection() {
|
|
let preparation =
|
|
TranscriptionPreparation::with_prepare(Arc::new(|selection, _, _, _| match selection
|
|
.model
|
|
{
|
|
TranscriptionModelId::ParakeetV2 => Err("fixture load failed".into()),
|
|
TranscriptionModelId::ParakeetV3 => panic!("fixture worker panic"),
|
|
_ => Ok(()),
|
|
}));
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV2), true);
|
|
assert_eq!(completion(&preparation).unwrap_err(), "fixture load failed");
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV3), true);
|
|
assert_eq!(
|
|
completion(&preparation).unwrap_err(),
|
|
"Model preparation worker failed."
|
|
);
|
|
preparation.set_error(Some("save failed".into()));
|
|
assert_eq!(preparation.status().error.as_deref(), Some("save failed"));
|
|
preparation.start(selection(TranscriptionModelId::WhisperLargeV3Turbo), true);
|
|
assert!(preparation.status().error.is_none());
|
|
assert_eq!(
|
|
completion(&preparation).unwrap().model,
|
|
TranscriptionModelId::WhisperLargeV3Turbo
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn dropping_a_settings_reference_does_not_cancel_application_preparation() {
|
|
let preparation = TranscriptionPreparation::with_prepare(Arc::new(|_, _, _, _| Ok(())));
|
|
let settings_reference = preparation.clone();
|
|
settings_reference.start(selection(TranscriptionModelId::ParakeetV2), true);
|
|
drop(settings_reference);
|
|
assert_eq!(
|
|
completion(&preparation).unwrap().model,
|
|
TranscriptionModelId::ParakeetV2
|
|
);
|
|
}
|
|
}
|