diff --git a/app/src-tauri/Cargo.lock b/app/src-tauri/Cargo.lock index e9d3472..f73fd6b 100644 --- a/app/src-tauri/Cargo.lock +++ b/app/src-tauri/Cargo.lock @@ -2137,6 +2137,7 @@ name = "kindtech-transcribe-app" version = "0.3.0" dependencies = [ "anyhow", + "base64 0.22.1", "chrono", "cpal", "ed25519-dalek", @@ -2146,7 +2147,6 @@ dependencies = [ "objc2-core-audio", "serde", "serde_json", - "sha2 0.10.9", "sysinfo", "tauri", "tauri-build", diff --git a/app/src-tauri/Cargo.toml b/app/src-tauri/Cargo.toml index 305a8af..6c8c28f 100644 --- a/app/src-tauri/Cargo.toml +++ b/app/src-tauri/Cargo.toml @@ -17,6 +17,9 @@ ed25519-dalek = { version = "2", features = ["pem"] } [dev-dependencies] ed25519-dalek = { version = "2", features = ["pem"] } +# Tests sign packs with the committed test key (ed25519-dalek above). Already in +# the tree through `modelpack`, with the same features. +base64 = "0.22" [dependencies] # In-process engine via a PATH dependency — NOT a registry fetch. Pulls @@ -44,7 +47,6 @@ serde_json = "1" # Verified-pack resolution: extract verified packs' `weights.bin` zips into a # content-addressed cache. Network-free (scanned by the zero-egress gate). zip = "2" -sha2 = "0.10" # In-app recording: microphone capture (CoreAudio / WASAPI / ALSA backends), # WAV writing, and local-time file names. All network-free. diff --git a/app/src-tauri/src/commands.rs b/app/src-tauri/src/commands.rs index 62ed713..4f64118 100644 --- a/app/src-tauri/src/commands.rs +++ b/app/src-tauri/src/commands.rs @@ -2,7 +2,7 @@ //! leaves the machine and no network call is made. use engine::{ - decode_to_16k_mono_with_progress, merge, AsrSegment, EngineConfig, SherpaEngine, + decode_to_16k_mono_with_progress, merge, AsrSegment, Diarizer, EngineConfig, SherpaEngine, TranscriptionEngine, }; use serde::Serialize; @@ -52,37 +52,53 @@ pub(crate) fn finish_transcript( NewTranscript { transcript, speakers_labelled, note } } +/// Speakers were asked for, but no speaker-detection pack is installed. +#[derive(Debug)] +struct NoSpeakerPack; + +impl std::fmt::Display for NoSpeakerPack { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("no speaker-detection pack is installed") + } +} + +impl std::error::Error for NoSpeakerPack {} + /// The note for a transcript whose speakers couldn't be labelled because of `e`. fn unlabelled_note(e: &anyhow::Error) -> String { - match e.downcast_ref::() { - Some(engine::EngineError::ModelNotFound(_)) => NO_SPEAKER_PACK.to_string(), - _ => format!("Speakers couldn't be labelled, so this transcript has none. ({e})"), + if e.is::() { + NO_SPEAKER_PACK.to_string() + } else { + format!("Speakers couldn't be labelled, so this transcript has none. ({e})") } } -/// The engine to label speakers with. Building it checks for the speaker -/// models, so build it before reading any audio. -/// -/// `cfg.models_dir` is a models dir resolved for transcription. The resolver -/// composes every verified pack into it, so the speaker models are there only -/// if their pack is installed; if not, this fails with -/// [`engine::EngineError::ModelNotFound`]. -pub(crate) fn speaker_engine(cfg: &EngineConfig) -> anyhow::Result { - let mut cfg = cfg.clone(); - cfg.diarize = true; - SherpaEngine::new(&cfg) +/// The diarizer to label speakers with, from the speaker-detection pack. Get +/// it before reading any audio: it fails fast when the pack is missing. +pub(crate) fn speaker_diarizer(app: &AppHandle, num_threads: i32) -> anyhow::Result { + diarizer_for(resolve_speaker_dir(app), num_threads) } -/// Diarize `samples` with `engine` (from [`speaker_engine`]) and attach +/// The diarizer for a resolved speaker-models folder; `Ok(None)` means no +/// speaker-detection pack is installed. +fn diarizer_for( + dir: Result, String>, + num_threads: i32, +) -> anyhow::Result { + let dir = dir.map_err(anyhow::Error::msg)?.ok_or(NoSpeakerPack)?; + Diarizer::new(dir, num_threads) +} + +/// Diarize `samples` with `diarizer` (from [`speaker_diarizer`]) and attach /// speakers to the transcribed `segments`. pub(crate) fn label_speakers( - engine: &SherpaEngine, + diarizer: &Diarizer, samples: &[f32], sample_rate: u32, segments: &[AsrSegment], num_speakers: Option, ) -> anyhow::Result { - let spans = engine.diarize(samples, sample_rate, num_speakers)?; + let spans = diarizer.diarize(samples, sample_rate, num_speakers)?; Ok(merge::merge(segments, &spans)) } @@ -107,6 +123,12 @@ fn debug_models_dir_override() -> Option { std::env::var("KINDTECH_MODELS_DIR").ok().map(std::path::PathBuf::from) } +/// Whether every one of `files` (relative paths) is a file under `dir`. +#[cfg(debug_assertions)] +fn holds_all(dir: &std::path::Path, files: Vec) -> bool { + files.iter().all(|file| dir.join(file).is_file()) +} + /// Report a usable raw-model override to the frontend in development builds. /// /// This keeps the import preflight honest when developers run with @@ -123,10 +145,7 @@ pub fn debug_asr_model_override() -> Option { (engine::AsrModel::SmallEn, "small.en"), (engine::AsrModel::MediumEn, "medium.en"), ] { - if engine::required_model_files(model, false) - .iter() - .all(|file| dir.join(file).is_file()) - { + if holds_all(&dir, engine::transcription_model_files(model)) { return Some(name.to_string()); } } @@ -151,6 +170,17 @@ pub(crate) fn resolve_models_dir( models::resolve_verified_models_dir(app, asr_model) } +/// Resolve the speaker-detection models' folder, or `None` when no verified +/// speaker-detection pack is installed (debug-only raw override: the override +/// folder, if it holds the speaker models). Blocking: may unpack the pack. +fn resolve_speaker_dir(app: &AppHandle) -> Result, String> { + #[cfg(debug_assertions)] + if let Some(raw) = debug_models_dir_override() { + return Ok(holds_all(&raw, engine::speaker_model_files()).then_some(raw)); + } + models::resolve_verified_speaker_dir(app) +} + /// Transcribe an audio file (wav/mp3/m4a/mp4/flac/ogg…) into the project's JSON /// contract. /// @@ -186,10 +216,9 @@ pub async fn transcribe( tauri::async_runtime::spawn_blocking(move || { // Resolve the models dir, enforcing verified-pack trust (debug-only raw // override: see `debug_models_dir_override`). Transcription models - // only: speakers are labelled as a separate step below, so missing - // speaker models, or a failure to load or run them, don't cost the - // transcript. (Resolving still unpacks every verified pack, so a pack - // that fails to unpack stops this too: #38.) + // only: speakers are labelled as a separate step below, from their own + // pack, so a speaker pack that is missing or fails to unpack, load or + // run doesn't cost the transcript. cfg.models_dir = resolve_models_dir(&resolve_app, cfg.asr_model)?; // Timed from model load to the finished transcript: what the person @@ -216,8 +245,8 @@ pub async fn transcribe( let segments = sherpa.transcribe(&samples, sample_rate, &progress).map_err(|e| e.to_string())?; let label = diarize.then_some(|| { - let eng = speaker_engine(&cfg)?; - label_speakers(&eng, &samples, sample_rate, &segments, num_speakers) + let diarizer = speaker_diarizer(&run_app, cfg.num_threads)?; + label_speakers(&diarizer, &samples, sample_rate, &segments, num_speakers) }); let result = finish_transcript(&run_app, &segments, label, Vec::new()); crate::device::record_run( @@ -233,3 +262,42 @@ pub async fn transcribe( .await .map_err(|e| format!("transcription task failed: {e}"))? } + +#[cfg(test)] +mod tests { + use super::*; + use crate::models::resolve_verified_speaker_dir_in; + use crate::models::tests::{broken_speaker_pack, copy_fixture_pack, fixtures_context, unique_tmp}; + use modelpack::PublicKey; + use std::path::Path; + + /// The speaker step's note says why a transcript has no speakers: the pack + /// isn't installed, or it couldn't be used. + #[test] + fn the_speaker_step_says_why_a_transcript_has_no_speakers() { + let (pem, versions) = fixtures_context(); + let pubkey = PublicKey::SpkiPem(&pem); + let packs = unique_tmp("note-packs"); + copy_fixture_pack("asr-small-en", &packs); + let cache_base = unique_tmp("note-cache"); + let note = |packs: &Path| { + let dir = resolve_verified_speaker_dir_in(packs, &cache_base, &pubkey, &versions); + let err = diarizer_for(dir, 4).err().expect("no speaker models to label with"); + unlabelled_note(&err) + }; + + assert_eq!( + note(&packs), + "The speaker-detection pack isn't installed, so this transcript has no speakers. Add it on the Models page." + ); + broken_speaker_pack(&packs); + let broken = note(&packs); + assert!( + broken.starts_with("Speakers couldn't be labelled, so this transcript has none. ("), + "got: {broken}" + ); + + let _ = std::fs::remove_dir_all(&packs); + let _ = std::fs::remove_dir_all(&cache_base); + } +} diff --git a/app/src-tauri/src/models.rs b/app/src-tauri/src/models.rs index 60953f9..46d9984 100644 --- a/app/src-tauri/src/models.rs +++ b/app/src-tauri/src/models.rs @@ -14,7 +14,6 @@ use std::path::{Component, Path, PathBuf}; use modelpack::{compare_semver, verify_model_pack_reader, PublicKey}; use serde::Serialize; use serde_json::Value; -use sha2::{Digest, Sha256}; use tauri::Manager; /// Committed dev/test SPKI key; never the production signing key (release @@ -46,6 +45,10 @@ fn engine_versions() -> BTreeMap { /// The manifest `task` of a language-model pack (a GGUF for llama.cpp). const LLM_TASK: &str = "llm"; +/// The manifest `task` of a transcription pack (ASR models + Silero VAD). +const ASR_TASK: &str = "asr"; +/// The manifest `task` of the speaker-detection pack (segmentation + embedding). +const DIARIZATION_TASK: &str = "diarization"; /// The verified/rejected status of a single model pack, surfaced to the UI. #[derive(Debug, Clone, Serialize)] @@ -153,11 +156,7 @@ fn packs_dir(app: &tauri::AppHandle) -> Result { if let Ok(dir) = std::env::var("KINDTECH_PACKS_DIR") { return Ok(PathBuf::from(dir)); } - let base = app - .path() - .app_data_dir() - .map_err(|e| format!("cannot resolve app data dir: {e}"))?; - Ok(base.join("packs")) + Ok(app_cache_base(app)?.join("packs")) } /// List + verify every pack in the packs dir. Returns an empty list (not an @@ -217,16 +216,6 @@ pub fn import_model_pack(app: tauri::AppHandle, src_dir: String) -> Result String { - let digest = Sha256::digest(bytes); - let mut out = String::with_capacity(digest.len() * 2); - for b in digest { - out.push_str(&format!("{b:02x}")); - } - out -} - /// True iff `name` (a zip entry path) stays within the extraction root: no /// absolute paths, no `..` components, no root/prefix components. Defends /// against zip path-traversal ("zip slip") regardless of `ZipArchive::extract`'s @@ -280,13 +269,6 @@ fn extract_zip_into(weights: &Path, dest: &Path) -> Result<(), String> { Ok(()) } -/// True iff `path` is a readable zip archive. -fn is_zip_file(path: &Path) -> bool { - std::fs::File::open(path) - .map(|f| zip::ZipArchive::new(std::io::BufReader::new(f)).is_ok()) - .unwrap_or(false) -} - /// The `sha256` string of the (already verified) manifest in `dir`. fn manifest_sha256(dir: &Path) -> Result { let bytes = std::fs::read(dir.join("manifest.json")) @@ -310,127 +292,177 @@ fn declared_task(dir: &Path) -> Option { value.get("task").and_then(Value::as_str).map(str::to_string) } -/// Verify every pack directory in `packs_dir` whose declared task passes -/// `wanted`, returning the verified ones. Packs with an unreadable manifest are -/// still verified (and so rejected). +/// Verify every pack directory in `packs_dir` that declares `task`, returning +/// the verified ones, newest first: highest version, ties going to the last +/// directory name. Packs with an unreadable manifest are still verified (and so +/// rejected). fn verified_packs( packs_dir: &Path, pubkey: &PublicKey<'_>, engine_versions: &BTreeMap, - wanted: impl Fn(&str) -> bool, + task: &str, ) -> Vec<(PathBuf, PackStatus)> { let mut out: Vec<(PathBuf, PackStatus)> = match std::fs::read_dir(packs_dir) { Ok(entries) => entries .filter_map(|e| e.ok()) .map(|e| e.path()) .filter(|p| p.is_dir()) - .filter(|p| declared_task(p).is_none_or(|t| wanted(&t))) + .filter(|p| declared_task(p).is_none_or(|t| t == task)) .map(|p| { let status = verify_pack_dir(&p, pubkey, engine_versions); (p, status) }) - .filter(|(_, status)| status.verified) + .filter(|(_, status)| status.verified && status.task == task) .collect(), Err(_) => Vec::new(), }; - out.sort_by(|a, b| a.0.cmp(&b.0)); + out.sort_by(|a, b| compare_semver(&b.1.version, &a.1.version).then(b.0.cmp(&a.0))); out } -/// Verified-pack resolver. Enumerates `packs_dir`, VERIFIES every -/// pack, extracts ONLY verified speech packs' `weights.bin` zips into a content- -/// addressed cache under `cache_base`, and returns the cache dir IFF it provides -/// every model file the engine requires for `(asr_model, diarize)`. Fails closed -/// at every step: no verified pack, or any missing required file, is an `Err`. -/// Language-model packs (`task: "llm"`) are left out; see [`resolve_verified_llm_in`]. -/// -/// The cache dir is keyed by the SHA-256 of the verified packs' `sha256` manifest -/// strings (sorted ascending, `\n`-joined), so the same verified set always maps -/// to the same dir and re-extraction is skipped via a `.complete` marker. +/// True iff the zip at `weights` lists every one of `files`. Reads only the +/// zip's directory, so choosing a pack never unpacks the others. +fn zip_lists(weights: &Path, files: &[String]) -> bool { + let Ok(file) = std::fs::File::open(weights) else { + return false; + }; + let Ok(archive) = zip::ZipArchive::new(std::io::BufReader::new(file)) else { + return false; + }; + let names: std::collections::HashSet<&str> = archive.file_names().collect(); + files.iter().all(|f| names.contains(f.as_str())) +} + +/// `pack` unpacked into its own content-addressed cache folder, +/// `cache_base/verified-models/-/`. A `.complete` +/// marker skips the unpack on later runs; a partial folder from an interrupted +/// run is cleared first. +fn unpacked(pack: &Path, cache_base: &Path, prefix: &str) -> Result { + let dest = cache_base + .join("verified-models") + .join(format!("{prefix}-{}", manifest_sha256(pack)?)); + let complete_marker = dest.join(".complete"); + if complete_marker.exists() { + return Ok(dest); + } + if dest.exists() { + std::fs::remove_dir_all(&dest) + .map_err(|e| format!("clearing partial cache {}: {e}", dest.display()))?; + } + std::fs::create_dir_all(&dest) + .map_err(|e| format!("creating cache dir {}: {e}", dest.display()))?; + extract_zip_into(&pack.join("weights.bin"), &dest)?; + std::fs::write(&complete_marker, b"") + .map_err(|e| format!("writing completion marker {}: {e}", complete_marker.display()))?; + Ok(dest) +} + +/// The verified speech pack `pack`, unpacked (see [`unpacked`]) into +/// `verified-models/pack-/` and checked to hold every one of +/// `files`. +fn unpacked_speech_pack(pack: &Path, cache_base: &Path, files: &[String]) -> Result { + let dest = unpacked(pack, cache_base, "pack")?; + let missing: Vec<&String> = files.iter().filter(|rel| !dest.join(rel).exists()).collect(); + if !missing.is_empty() { + return Err(format!("verified pack {} is missing {missing:?}", pack.display())); + } + Ok(dest) +} + +/// Remove the folders of the old layout, where every verified speech pack was +/// unpacked into one shared folder named by a 64-hex hash of the whole set. +/// Best effort: nothing uses them any more. +fn remove_old_shared_caches(cache_base: &Path) { + let Ok(entries) = std::fs::read_dir(cache_base.join("verified-models")) else { + return; + }; + for entry in entries.filter_map(|e| e.ok()) { + let name = entry.file_name(); + let name = name.to_string_lossy(); + if name.len() == 64 && name.bytes().all(|b| b.is_ascii_hexdigit()) && entry.path().is_dir() { + let _ = std::fs::remove_dir_all(entry.path()); + } + } +} + +/// Verified transcription-pack resolver. Enumerates `packs_dir`, VERIFIES every +/// `task: "asr"` pack, picks one holding every model file the engine requires +/// for `asr_model` (each ASR pack carries Silero VAD too), newest first, and +/// unpacks only that pack into its own cache folder (see [`unpacked_speech_pack`]). Fails closed: no +/// verified pack, or none providing the required files, is an `Err`. Other +/// packs, the speaker-detection pack included, are never unpacked here, so one +/// that fails to unpack can't stop transcription (#38). /// -/// The app relies on the dir holding EVERY verified pack's files: it resolves -/// for transcription only, and `commands::speaker_engine` then finds the speaker -/// models there if their pack is installed. Caching packs separately (#38) must -/// keep that working. +/// Also removes the old shared cache folders (see [`remove_old_shared_caches`]). pub fn resolve_verified_models_dir_in( packs_dir: &Path, cache_base: &Path, pubkey: &PublicKey<'_>, engine_versions: &BTreeMap, asr_model: engine::AsrModel, - diarize: bool, ) -> Result { - let verified_dirs: Vec = - verified_packs(packs_dir, pubkey, engine_versions, |task| task != LLM_TASK) - .into_iter() - .filter(|(_, status)| status.task != LLM_TASK) - .map(|(dir, _)| dir) - .collect(); - if verified_dirs.is_empty() { - return Err(format!( - "no verified model pack found in {}", - packs_dir.display() - )); - } - - let mut sha_list: Vec = Vec::new(); - for dir in &verified_dirs { - sha_list.push(manifest_sha256(dir)?); + remove_old_shared_caches(cache_base); + let files = engine::transcription_model_files(asr_model); + let packs = verified_packs(packs_dir, pubkey, engine_versions, ASR_TASK); + if packs.is_empty() { + return Err(format!("no verified model pack found in {}", packs_dir.display())); } - sha_list.sort(); - let combined_key = sha256_hex(sha_list.join("\n").as_bytes()); - - let composed = cache_base.join("verified-models").join(&combined_key); - let complete_marker = composed.join(".complete"); - if !complete_marker.exists() { - // Fresh extraction: clear any partial dir, recreate, extract each - // verified pack's zip-valid weights.bin (skip non-zip packs). - if composed.exists() { - std::fs::remove_dir_all(&composed) - .map_err(|e| format!("clearing partial cache {}: {e}", composed.display()))?; - } - std::fs::create_dir_all(&composed) - .map_err(|e| format!("creating cache dir {}: {e}", composed.display()))?; - for dir in &verified_dirs { - let weights = dir.join("weights.bin"); - // A non-zip weights.bin contributes no files — skip rather than abort. - if !is_zip_file(&weights) { - continue; - } - extract_zip_into(&weights, &composed)?; - } - std::fs::write(&complete_marker, b"") - .map_err(|e| format!("writing completion marker {}: {e}", complete_marker.display()))?; - } - - // Fail closed unless EVERY required file is present. - let missing: Vec = engine::required_model_files(asr_model, diarize) + // The zip's directory names its files, so this unpacks nothing. + let (pack, _) = packs .into_iter() - .filter(|rel| !composed.join(rel).exists()) - .collect(); - if !missing.is_empty() { - return Err(format!( - "no verified pack provides the required model file(s): {missing:?}" - )); - } - Ok(composed) + .find(|(dir, _)| zip_lists(&dir.join("weights.bin"), &files)) + .ok_or_else(|| format!("no verified pack provides the required model file(s): {files:?}"))?; + unpacked_speech_pack(&pack, cache_base, &files) } /// AppHandle wrapper around [`resolve_verified_models_dir_in`]. -/// Resolves for transcription; the speaker step finds the speaker models in the -/// same dir (see [`resolve_verified_models_dir_in`]). pub fn resolve_verified_models_dir( app: &tauri::AppHandle, asr_model: engine::AsrModel, ) -> Result { - let packs = packs_dir(app)?; - let cache_base = app - .path() + with_installed(app, |packs, cache, key, versions| { + resolve_verified_models_dir_in(packs, cache, key, versions, asr_model) + }) +} + +/// Verified speaker-detection-pack resolver: the newest verified +/// `task: "diarization"` pack, unpacked on its own (see +/// [`unpacked_speech_pack`]) and checked to hold [`engine::speaker_model_files`]. +/// `Ok(None)` when no verified speaker pack is installed; `Err` when it can't be +/// unpacked or lacks those files. +pub fn resolve_verified_speaker_dir_in( + packs_dir: &Path, + cache_base: &Path, + pubkey: &PublicKey<'_>, + engine_versions: &BTreeMap, +) -> Result, String> { + let newest = verified_packs(packs_dir, pubkey, engine_versions, DIARIZATION_TASK).into_iter().next(); + newest + .map(|(pack, _)| unpacked_speech_pack(&pack, cache_base, &engine::speaker_model_files())) + .transpose() +} + +/// AppHandle wrapper around [`resolve_verified_speaker_dir_in`]. +pub fn resolve_verified_speaker_dir(app: &tauri::AppHandle) -> Result, String> { + with_installed(app, resolve_verified_speaker_dir_in) +} + +/// The per-user app-data dir: where packs are installed and unpacked. +fn app_cache_base(app: &tauri::AppHandle) -> Result { + app.path() .app_data_dir() - .map_err(|e| format!("cannot resolve app data dir: {e}"))?; - let pubkey = PublicKey::SpkiPem(vendor_pubkey_pem()); - let versions = engine_versions(); - resolve_verified_models_dir_in(&packs, &cache_base, &pubkey, &versions, asr_model, false) + .map_err(|e| format!("cannot resolve app data dir: {e}")) +} + +/// Run a `_in` resolver against this app's packs folder, cache folder, trusted +/// vendor key and engine versions. +fn with_installed( + app: &tauri::AppHandle, + resolve: impl FnOnce(&Path, &Path, &PublicKey<'_>, &BTreeMap) -> Result, +) -> Result { + let packs = packs_dir(app)?; + let cache_base = app_cache_base(app)?; + resolve(&packs, &cache_base, &PublicKey::SpkiPem(vendor_pubkey_pem()), &engine_versions()) } /// Verified LLM-pack resolver. Enumerates `packs_dir`, VERIFIES every pack, and @@ -448,34 +480,15 @@ pub fn resolve_verified_llm_in( pubkey: &PublicKey<'_>, engine_versions: &BTreeMap, ) -> Result { - let mut llm_packs: Vec<(PathBuf, PackStatus)> = - verified_packs(packs_dir, pubkey, engine_versions, |task| task == LLM_TASK) - .into_iter() - .filter(|(_, status)| status.task == LLM_TASK) - .collect(); - llm_packs.sort_by(|a, b| compare_semver(&a.1.version, &b.1.version).then(a.0.cmp(&b.0))); - let Some((dir, status)) = llm_packs.pop() else { + let newest = verified_packs(packs_dir, pubkey, engine_versions, LLM_TASK).into_iter().next(); + let Some((dir, status)) = newest else { return Err(format!( "no verified LLM model pack found in {}", packs_dir.display() )); }; - let dest = cache_base - .join("verified-models") - .join(format!("llm-{}", manifest_sha256(&dir)?)); - let complete_marker = dest.join(".complete"); - if !complete_marker.exists() { - if dest.exists() { - std::fs::remove_dir_all(&dest) - .map_err(|e| format!("clearing partial cache {}: {e}", dest.display()))?; - } - std::fs::create_dir_all(&dest) - .map_err(|e| format!("creating cache dir {}: {e}", dest.display()))?; - extract_zip_into(&dir.join("weights.bin"), &dest)?; - std::fs::write(&complete_marker, b"") - .map_err(|e| format!("writing completion marker {}: {e}", complete_marker.display()))?; - } + let dest = unpacked(&dir, cache_base, "llm")?; let ggufs: Vec = std::fs::read_dir(&dest) .map_err(|e| format!("reading cache dir {}: {e}", dest.display()))? @@ -501,18 +514,11 @@ pub fn resolve_verified_llm_in( /// AppHandle wrapper around [`resolve_verified_llm_in`]. #[allow(dead_code)] // Spike S5: not called by the app yet; P2 (local notes) will use it. pub fn resolve_verified_llm(app: &tauri::AppHandle) -> Result { - let packs = packs_dir(app)?; - let cache_base = app - .path() - .app_data_dir() - .map_err(|e| format!("cannot resolve app data dir: {e}"))?; - let pubkey = PublicKey::SpkiPem(vendor_pubkey_pem()); - let versions = engine_versions(); - resolve_verified_llm_in(&packs, &cache_base, &pubkey, &versions) + with_installed(app, resolve_verified_llm_in) } #[cfg(test)] -mod tests { +pub(crate) mod tests { use super::*; use std::fs; @@ -525,7 +531,7 @@ mod tests { .to_path_buf() } - fn fixtures_context() -> (String, BTreeMap) { + pub(crate) fn fixtures_context() -> (String, BTreeMap) { let fixtures = repo_root().join("fixtures/modelpack"); let v: Value = serde_json::from_str(&fs::read_to_string(fixtures.join("context.json")).unwrap()) @@ -621,7 +627,7 @@ mod tests { assert!(status.reason.unwrap().contains("weights hash mismatch")); } - fn unique_tmp(tag: &str) -> PathBuf { + pub(crate) fn unique_tmp(tag: &str) -> PathBuf { std::env::temp_dir().join(format!( "podcast-resolver-{tag}-{}-{:?}", std::process::id(), @@ -649,7 +655,6 @@ mod tests { &pubkey, &versions, engine::AsrModel::SmallEn, - false, ) .expect("committed verified pack must resolve"); @@ -669,7 +674,6 @@ mod tests { &pubkey, &versions, engine::AsrModel::SmallEn, - false, ) .expect("second resolve must succeed"); assert_eq!(dir, dir2, "resolver must be deterministic / idempotent"); @@ -703,7 +707,6 @@ mod tests { &pubkey, &versions, engine::AsrModel::SmallEn, - false, ) .expect_err("an unsigned pack must not be trusted"); assert!( @@ -715,35 +718,162 @@ mod tests { let _ = fs::remove_dir_all(&cache_base); } - /// DIARIZE-MISSING: the asr-only fixture pack provides no seg/emb model, so - /// resolving with `diarize=true` fails closed naming the missing file(s). + /// A zip holding `files` (path, bytes), stored uncompressed. + fn zip_of(files: &[(&str, &[u8])]) -> Vec { + use std::io::Write; + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let stored = zip::write::SimpleFileOptions::default() + .compression_method(zip::CompressionMethod::Stored); + for (name, bytes) in files { + zip.start_file(*name, stored).unwrap(); + zip.write_all(bytes).unwrap(); + } + zip.finish().unwrap().into_inner() + } + + /// Write `packs//` as a pack of `weights`, signed with the committed + /// test key, declaring `task`. + fn signed_pack(packs: &Path, name: &str, task: &str, weights: &[u8]) -> PathBuf { + use base64::Engine as _; + use ed25519_dalek::pkcs8::DecodePrivateKey; + use ed25519_dalek::Signer as _; + let pem = + fs::read_to_string(repo_root().join("fixtures/modelpack/keys/test-private.pkcs8.pem")) + .unwrap(); + let key = ed25519_dalek::SigningKey::from_pkcs8_pem(&pem).unwrap(); + let manifest = modelpack::Manifest { + attribution: "Test pack".into(), + engine: "sherpa-onnx".into(), + engine_min_version: "1.13.2".into(), + id: format!("test/{name}"), + license: "MIT".into(), + sha256: modelpack::sha256_hex_reader(weights).unwrap(), + task: task.into(), + version: "1.0.0".into(), + signature: String::new(), + }; + let signature = key.sign(&modelpack::canonicalize(&manifest)); + // `signature` is left out of the serialized manifest; add it back. + let mut json = serde_json::to_value(&manifest).unwrap(); + json["signature"] = + base64::engine::general_purpose::STANDARD.encode(signature.to_bytes()).into(); + let dir = packs.join(name); + fs::create_dir_all(&dir).unwrap(); + fs::write(dir.join("weights.bin"), weights).unwrap(); + fs::write(dir.join("manifest.json"), json.to_string()).unwrap(); + dir + } + + /// A speaker-detection pack that verifies but can't be unpacked: its + /// segmentation model's bytes were changed after zipping, so the entry + /// fails its CRC check. + pub(crate) fn broken_speaker_pack(packs: &Path) -> PathBuf { + let mut weights = zip_of(&[ + (engine::SEG_MODEL, b"segmentation model bytes"), + (engine::EMB_MODEL, b"embedding model bytes"), + ]); + let at = weights + .windows(b"segmentation model bytes".len()) + .position(|w| w == b"segmentation model bytes") + .unwrap(); + weights[at] ^= 0x01; + signed_pack(packs, "speakers-broken", "diarization", &weights) + } + + /// #38: a speaker pack that fails to unpack doesn't stop transcription. #[test] - fn resolve_verified_models_dir_in_fails_closed_when_diarization_models_missing() { + fn a_speaker_pack_that_fails_to_unpack_does_not_stop_transcription_models_resolving() { let (pem, versions) = fixtures_context(); let pubkey = PublicKey::SpkiPem(&pem); - let packs = repo_root().join("fixtures/modelpack/packs"); - let cache_base = unique_tmp("diarize"); - let _ = fs::remove_dir_all(&cache_base); + let packs = unique_tmp("broken-speakers-packs"); + copy_fixture_pack("asr-small-en", &packs); + broken_speaker_pack(&packs); + let cache_base = unique_tmp("broken-speakers-cache"); - let err = resolve_verified_models_dir_in( + let dir = resolve_verified_models_dir_in( &packs, &cache_base, &pubkey, &versions, engine::AsrModel::SmallEn, - true, ) - .expect_err("asr-only pack must not satisfy diarization"); - assert!( - err.contains("required model file") && err.contains("pyannote-segmentation"), - "expected missing seg/emb error, got: {err}" + .expect("transcription models must resolve"); + + for rel in engine::required_model_files(engine::AsrModel::SmallEn, false) { + assert!(dir.join(&rel).is_file(), "{rel} missing under {}", dir.display()); + } + let err = resolve_verified_speaker_dir_in(&packs, &cache_base, &pubkey, &versions) + .expect_err("the broken speaker pack can't be unpacked"); + assert!(err.contains("extracting zip entry"), "got: {err}"); + let _ = fs::remove_dir_all(&packs); + let _ = fs::remove_dir_all(&cache_base); + } + + /// The speaker models resolve from the speaker-detection pack on its own; + /// a packs folder without one has none. + #[test] + fn speaker_models_resolve_from_the_speaker_pack_alone() { + let (pem, versions) = fixtures_context(); + let pubkey = PublicKey::SpkiPem(&pem); + let packs = unique_tmp("speakers-packs"); + let weights = zip_of(&[(engine::SEG_MODEL, b""), (engine::EMB_MODEL, b"")]); + signed_pack(&packs, "speakers", "diarization", &weights); + let cache_base = unique_tmp("speakers-cache"); + + let dir = resolve_verified_speaker_dir_in(&packs, &cache_base, &pubkey, &versions) + .expect("the speaker pack must unpack") + .expect("the speaker pack provides the speaker models"); + assert!(dir.join(engine::SEG_MODEL).is_file()); + assert!(dir.join(engine::EMB_MODEL).is_file()); + + let asr_only = unique_tmp("speakers-asr-only-packs"); + copy_fixture_pack("asr-small-en", &asr_only); + assert_eq!( + resolve_verified_speaker_dir_in(&asr_only, &cache_base, &pubkey, &versions), + Ok(None) ); + for dir in [&packs, &asr_only, &cache_base] { + let _ = fs::remove_dir_all(dir); + } + } + /// Resolving removes the old layout's shared cache folders (named by a + /// 64-hex hash) and keeps the per-pack and LLM ones. + #[test] + fn resolving_removes_the_old_shared_cache_folders() { + let (pem, versions) = fixtures_context(); + let pubkey = PublicKey::SpkiPem(&pem); + let packs = unique_tmp("old-cache-packs"); + copy_fixture_pack("asr-small-en", &packs); + let cache_base = unique_tmp("old-cache"); + let cached = cache_base.join("verified-models"); + let hash = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + let old = cached.join(hash); + let other_pack = cached.join(format!("pack-{hash}")); + let llm = cached.join(format!("llm-{hash}")); + for dir in [&old, &other_pack, &llm] { + fs::create_dir_all(dir).unwrap(); + fs::write(dir.join("silero_vad.onnx"), b"").unwrap(); + } + + resolve_verified_models_dir_in( + &packs, + &cache_base, + &pubkey, + &versions, + engine::AsrModel::SmallEn, + ) + .expect("transcription models must resolve"); + + assert!(!old.exists(), "the old shared folder must be removed"); + assert!(other_pack.exists(), "a per-pack folder must stay"); + assert!(llm.exists(), "an LLM folder must stay"); + let _ = fs::remove_dir_all(&packs); let _ = fs::remove_dir_all(&cache_base); } /// Copy the committed fixture pack `name` into `packs//`. - fn copy_fixture_pack(name: &str, packs: &Path) -> PathBuf { + pub(crate) fn copy_fixture_pack(name: &str, packs: &Path) -> PathBuf { let src = repo_root().join("fixtures/modelpack/packs").join(name); let dest = packs.join(name); fs::create_dir_all(&dest).unwrap(); @@ -843,7 +973,6 @@ mod tests { &pubkey, &versions, engine::AsrModel::SmallEn, - false, ); let _ = tx.send(r); }); diff --git a/app/src-tauri/src/recorder.rs b/app/src-tauri/src/recorder.rs index 01f5793..3007da3 100644 --- a/app/src-tauri/src/recorder.rs +++ b/app/src-tauri/src/recorder.rs @@ -42,7 +42,7 @@ use serde::Serialize; use tauri::{AppHandle, Emitter, Manager}; use crate::commands::{ - finish_transcript, label_speakers, resolve_models_dir, speaker_engine, NewTranscript, + finish_transcript, label_speakers, resolve_models_dir, speaker_diarizer, NewTranscript, }; use crate::system; @@ -1324,7 +1324,8 @@ pub fn resume_recording(app: AppHandle) -> Result<(), String> { struct Finished { path: PathBuf, - cfg: EngineConfig, + /// Threads the recording's engine used; speaker labelling uses as many. + num_threads: i32, segments: Vec, /// Seconds of the record that couldn't be transcribed. unread_s: f64, @@ -1356,7 +1357,7 @@ fn finish(app: &AppHandle, discarding: bool) -> Result { let mut segments = active.segments.lock().unwrap().clone(); segments.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap_or(std::cmp::Ordering::Equal)); let unread_s = active.unread.load(Ordering::Relaxed) as f64 / SAMPLE_RATE as f64; - Ok(Finished { path: active.path, cfg: active.cfg, segments, unread_s }) + Ok(Finished { path: active.path, num_threads: active.cfg.num_threads, segments, unread_s }) } /// Stop recording and build the transcript from the record's pieces, with @@ -1394,13 +1395,11 @@ fn label_recording( done: &Finished, num_speakers: Option, ) -> anyhow::Result { - let mut cfg = done.cfg.clone(); - // Resolved again, so a pack added while recording counts. - cfg.models_dir = resolve_models_dir(app, cfg.asr_model).map_err(anyhow::Error::msg)?; - // Before reading the recording, which takes about 230 MB per hour. - let eng = speaker_engine(&cfg)?; + // Resolved at Stop, so a pack added while recording counts, and before + // reading the recording, which takes about 230 MB per hour. + let diarizer = speaker_diarizer(app, done.num_threads)?; let (samples, sr) = load_wav_16k_mono(&done.path)?; - label_speakers(&eng, &samples, sr, &done.segments, num_speakers) + label_speakers(&diarizer, &samples, sr, &done.segments, num_speakers) } /// Remove a finished recording's notes file once the transcript holding the diff --git a/engine/src/lib.rs b/engine/src/lib.rs index fcfb2a9..2dd4e56 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -16,7 +16,7 @@ pub mod vocab; pub use decode::{decode_to_16k_mono, decode_to_16k_mono_with_progress, probe_duration_s}; pub use resample::StreamResampler; -pub use sherpa::{Recognizer, SherpaEngine, Vad, VadProfile}; +pub use sherpa::{Diarizer, Recognizer, SherpaEngine, Vad, VadProfile}; pub use vocab::{SkippedLine, Vocabulary}; /// A progress update emitted during the pipeline so the UI can render a @@ -364,15 +364,27 @@ pub const EMB_MODEL: &str = "3dspeaker_speech_campplus_sv_en_voxceleb_16k.onnx"; /// [`SherpaEngine::new`]'s existence check and the verified-pack resolver, so /// the two can never drift. pub fn required_model_files(asr_model: AsrModel, diarize: bool) -> Vec { - let mut files = asr_model.asr_files(); - files.push(VAD_MODEL.to_string()); + let mut files = transcription_model_files(asr_model); if diarize { - files.push(SEG_MODEL.to_string()); - files.push(EMB_MODEL.to_string()); + files.extend(speaker_model_files()); } files } +/// The RELATIVE model-file paths transcription needs (the ASR model's files and +/// Silero VAD): what a transcription pack must provide. +pub fn transcription_model_files(asr_model: AsrModel) -> Vec { + let mut files = asr_model.asr_files(); + files.push(VAD_MODEL.to_string()); + files +} + +/// The RELATIVE model-file paths diarization needs: what [`Diarizer::new`] +/// checks for, and what a speaker-detection pack must provide. +pub fn speaker_model_files() -> Vec { + vec![SEG_MODEL.to_string(), EMB_MODEL.to_string()] +} + #[derive(Debug, thiserror::Error)] pub enum EngineError { #[error("model file not found: {0}")] diff --git a/engine/src/sherpa.rs b/engine/src/sherpa.rs index cfdf2ad..8810e59 100644 --- a/engine/src/sherpa.rs +++ b/engine/src/sherpa.rs @@ -261,12 +261,7 @@ impl SherpaEngine { let dir = &cfg.models_dir; // Build the required list from the shared source of truth so the // diarization models are required ONLY when `cfg.diarize`. - for rel in crate::required_model_files(cfg.asr_model, cfg.diarize) { - let p = dir.join(&rel); - if !p.exists() { - return Err(EngineError::ModelNotFound(p).into()); - } - } + require_files(dir, crate::required_model_files(cfg.asr_model, cfg.diarize))?; if let Some(h) = &cfg.hotwords { if !h.file.exists() { return Err(EngineError::ModelNotFound(h.file.clone()).into()); @@ -288,7 +283,7 @@ impl SherpaEngine { } fn model_path(&self, rel: impl AsRef) -> String { - self.models_dir.join(rel).to_string_lossy().into_owned() + model_path(&self.models_dir, rel) } fn build_recognizer(&self) -> anyhow::Result { @@ -617,6 +612,41 @@ impl TranscriptionEngine for SherpaEngine { samples: &[f32], sample_rate: u32, num_speakers: Option, + ) -> anyhow::Result> { + let diarizer = Diarizer { models_dir: self.models_dir.clone(), num_threads: self.num_threads }; + diarizer.diarize(samples, sample_rate, num_speakers) + } +} + +/// Speaker diarization (pyannote segmentation + 3D-Speaker embeddings + +/// FastClustering), bound to a folder holding only the speaker models: the +/// verified speaker-detection pack is unpacked on its own, apart from the +/// transcription models. +pub struct Diarizer { + models_dir: PathBuf, + num_threads: i32, +} + +impl Diarizer { + /// Bind to `models_dir`, checking up front that it holds every + /// [`crate::speaker_model_files`] file. + pub fn new(models_dir: impl AsRef, num_threads: i32) -> anyhow::Result { + let models_dir = models_dir.as_ref().to_path_buf(); + require_files(&models_dir, crate::speaker_model_files())?; + Ok(Self { models_dir, num_threads }) + } + + fn model_path(&self, rel: impl AsRef) -> String { + model_path(&self.models_dir, rel) + } + + /// Diarize 16 kHz mono `samples` into speaker spans. `num_speakers = None` + /// auto-detects. + pub fn diarize( + &self, + samples: &[f32], + sample_rate: u32, + num_speakers: Option, ) -> anyhow::Result> { let config = OfflineSpeakerDiarizationConfig { segmentation: OfflineSpeakerSegmentationModelConfig { @@ -674,6 +704,24 @@ impl TranscriptionEngine for SherpaEngine { } } +/// Fail with [`EngineError::ModelNotFound`] naming the first of `files` +/// (relative to `dir`) that is missing, so a missing file fails with a clear +/// path rather than a deep sherpa error. +fn require_files(dir: &Path, files: Vec) -> Result<(), EngineError> { + for rel in files { + let p = dir.join(&rel); + if !p.exists() { + return Err(EngineError::ModelNotFound(p)); + } + } + Ok(()) +} + +/// `rel` under `dir`, as the string sherpa-onnx configs take. +fn model_path(dir: &Path, rel: impl AsRef) -> String { + dir.join(rel).to_string_lossy().into_owned() +} + #[cfg(test)] mod new_tests { use super::*;