hex/src/local_transcriber.rs
Dax Raad 0eeeb2bde3 fix(linux, windows): apply saved settings to the running listener in place
Changing a setting stopped the listener and started it again, so the
Status card flashed Disabling/Enabling. The host now hands the running
listener its saved settings: paste, indicator, audio behavior, sounds,
and modes apply at once; shortcut changes register again, a microphone
change reopens the stream, and a model or hint change loads beside the
current model and swaps it in once nothing is pending (ModelSwap).
Recording a shortcut suspends only global matching. Windows drops its
'Applying settings' restart the same way.
2026-10-22 19:50:34 -04:00

374 lines
13 KiB
Rust

use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::Instant;
use color_eyre::eyre::{Result, WrapErr, eyre};
use transcribe_cpp::{Backend, Model, ModelOptions, RunOptions, TimestampKind};
use crate::gguf_session::OfflineGgufSession;
#[cfg(target_os = "linux")]
use crate::transcription_models::{ModelDefinition, TranscriptionModelId, definition};
use crate::transcription_models::{
TranscriptionSelection, download_with_progress, model_path, validate,
};
pub struct LocalTranscriber {
session: OfflineGgufSession,
options: RunOptions,
device_label: String,
max_audio_samples: Option<usize>,
stream: Option<transcribe_cpp::StreamOptions>,
}
#[cfg(target_os = "linux")]
pub fn default_model() -> &'static ModelDefinition {
definition(TranscriptionModelId::default())
}
#[cfg(target_os = "linux")]
pub fn install_default(canceled: &AtomicBool) -> Result<()> {
install(&TranscriptionSelection::default(), canceled)
}
pub fn install(selection: &TranscriptionSelection, canceled: &AtomicBool) -> Result<()> {
let model = validate(selection)?;
let downloaded = AtomicU64::new(0);
println!("Installing {} ({})...", model.name, model.size_label());
let path = download_with_progress(model, canceled, &downloaded)?;
println!(
"Installed {} bytes at {}",
downloaded.load(Ordering::Relaxed),
path.display()
);
Ok(())
}
pub fn devices() -> Result<Vec<String>> {
transcribe_cpp::init_logging();
transcribe_cpp::init_backends_default()
.wrap_err("could not initialize transcription backends")?;
Ok(transcribe_cpp::devices()
.into_iter()
.map(|device| {
format!(
"{}\t{}\t{} MB\t{}",
device
.index
.map_or_else(|| "-".into(), |index| index.to_string()),
device.kind,
device.memory_total / (1024 * 1024),
if device.description.is_empty() {
device.name
} else {
device.description
}
)
})
.collect())
}
/// Initializes transcribe.cpp's backends, which creates its Vulkan instance,
/// on the calling thread. The Linux process calls this before its listener
/// or window can start: two Vulkan instances created at once in one process
/// (ggml's here, wgpu's for the window) crash inside the Vulkan loader.
/// Later model loads reuse the instance.
#[cfg(target_os = "linux")]
pub fn initialize_backends() {
transcribe_cpp::init_logging();
match transcribe_cpp::init_backends_default() {
// Enumerating the devices makes sure the Vulkan backend exists now.
Ok(()) => drop(transcribe_cpp::devices()),
Err(error) => tracing::warn!(%error, "could not initialize transcription backends"),
}
}
#[cfg(target_os = "linux")]
pub fn transcribe_wav(path: &Path) -> Result<String> {
transcribe_wav_with_selection(path, &TranscriptionSelection::default())
}
pub fn transcribe_wav_with_selection(
path: &Path,
selection: &TranscriptionSelection,
) -> Result<String> {
let samples = read_wav_for_parakeet(path)?;
LocalTranscriber::load(selection)?.transcribe(&samples)
}
pub fn read_wav_for_parakeet(path: &Path) -> Result<Vec<f32>> {
let mut reader = hound::WavReader::open(path)
.wrap_err_with(|| format!("could not open {}", path.display()))?;
let spec = reader.spec();
let samples = match spec.sample_format {
hound::SampleFormat::Float => reader
.samples::<f32>()
.collect::<std::result::Result<Vec<_>, _>>()?,
hound::SampleFormat::Int if spec.bits_per_sample <= 16 => reader
.samples::<i16>()
.map(|sample| sample.map(|sample| sample as f32 / i16::MAX as f32))
.collect::<std::result::Result<Vec<_>, _>>()?,
hound::SampleFormat::Int => reader
.samples::<i32>()
.map(|sample| sample.map(|sample| sample as f32 / i32::MAX as f32))
.collect::<std::result::Result<Vec<_>, _>>()?,
};
let channels = usize::from(spec.channels);
let mono = samples
.chunks(channels)
.map(|frame| frame.iter().sum::<f32>() / channels as f32)
.collect::<Vec<_>>();
let mut samples = crate::dictation::resample_for_parakeet(&mono, spec.sample_rate);
crate::dictation::pad_for_parakeet(&mut samples);
Ok(samples)
}
impl LocalTranscriber {
#[cfg(target_os = "linux")]
pub fn load_default() -> Result<Self> {
Self::load(&TranscriptionSelection::default())
}
pub fn load(selection: &TranscriptionSelection) -> Result<Self> {
transcribe_cpp::init_logging();
transcribe_cpp::init_backends_default()
.wrap_err("could not initialize transcription backends")?;
let definition = validate(selection)?;
if !crate::transcription_models::is_installed(definition, &selection.language) {
return Err(eyre!(
"{} is not installed; run `cargo run -- model install`",
definition.name
));
}
let path = model_path(definition)?;
let model = Model::load_with(
&path,
&ModelOptions {
backend: Backend::Auto,
device: None,
},
)
.wrap_err_with(|| format!("could not load transcription model from {}", path.display()))?;
let device = model.device()?;
let variant = model.variant();
let architecture = model.arch();
let capabilities = model.capabilities();
let language = crate::gguf_session::validate_gguf_artifact(
&path,
definition,
&architecture,
&variant,
selection,
capabilities.supports_language_detect,
)?;
// Recognition hints softly prime Whisper's decoder as its initial
// prompt; validate() already rejects hints on unsupported models.
let recognition_hints = selection.recognition_hints.trim();
let family =
(definition.supports_recognition_hints && !recognition_hints.is_empty()).then(|| {
transcribe_cpp::RunExtension::Whisper(transcribe_cpp::WhisperRunOptions {
initial_prompt: Some(recognition_hints.to_string()),
..Default::default()
})
});
let device_label = format!("{} ({})", device.description, device.kind);
tracing::info!(
backend = device.kind,
device = device.description,
variant,
"loaded local transcription model"
);
let session = OfflineGgufSession::new(model)?;
let mut transcriber = Self {
session,
options: RunOptions {
timestamps: if capabilities.max_timestamp_kind == TimestampKind::None {
TimestampKind::None
} else {
TimestampKind::Segment
},
language,
family,
..Default::default()
},
device_label,
max_audio_samples: crate::transcription_models::max_audio_chunk_samples(
&architecture,
capabilities.max_audio_ms,
),
stream: None,
};
let started = Instant::now();
transcriber.transcribe(&vec![0.0; 24_000])?;
tracing::info!(
prewarm_ms = started.elapsed().as_millis(),
"prewarmed local transcription model"
);
let stream_run = transcriber.stream_run_options();
transcriber.stream = transcriber.session.probe_stream(&stream_run, &architecture);
Ok(transcriber)
}
/// Streams decode exactly as offline; a run-slot family extension
/// (Whisper's prompt) does not apply to a stream.
fn stream_run_options(&self) -> RunOptions {
RunOptions {
family: None,
..self.options.clone()
}
}
pub(crate) fn stream_options(&self) -> Option<&transcribe_cpp::StreamOptions> {
self.stream.as_ref()
}
pub(crate) fn with_stream<R>(
&mut self,
stream_options: &transcribe_cpp::StreamOptions,
cancel: &transcribe_cpp::CancelToken,
drive: impl FnOnce(&mut transcribe_cpp::Stream<'_>) -> R,
) -> transcribe_cpp::Result<R> {
let options = self.stream_run_options();
self.session
.with_stream(&options, stream_options, cancel, drive)
}
pub fn device_label(&self) -> &str {
&self.device_label
}
pub fn transcribe(&mut self, samples: &[f32]) -> Result<String> {
self.session
.transcribe_chunked(samples, &self.options, self.max_audio_samples)
}
}
/// A running listener's model: the one it transcribes with, and the one it
/// changes to. A new selection loads on a worker thread while dictation goes
/// on with the old model; the listener swaps it in once nothing records or
/// waits on the old one. A model the host already prepared skips the load.
pub(crate) struct ModelSwap {
loaded: TranscriptionSelection,
loading: Option<(
TranscriptionSelection,
std::sync::mpsc::Receiver<std::result::Result<LocalTranscriber, String>>,
)>,
ready: Option<(TranscriptionSelection, LocalTranscriber)>,
}
impl ModelSwap {
pub(crate) fn new(loaded: &TranscriptionSelection) -> Self {
Self {
loaded: runnable(loaded),
loading: None,
ready: None,
}
}
/// The selection the listener will transcribe with once swaps settle.
fn target(&self) -> &TranscriptionSelection {
self.ready
.as_ref()
.map(|(selection, _)| selection)
.or(self.loading.as_ref().map(|(selection, _)| selection))
.unwrap_or(&self.loaded)
}
/// A model the host loaded for `selection` (Linux prepares models
/// beside the listener; Windows lets the listener load them).
#[cfg_attr(target_os = "windows", allow(dead_code))]
pub(crate) fn offer(
&mut self,
selection: &TranscriptionSelection,
transcriber: LocalTranscriber,
) {
self.loading = None;
self.ready = Some((runnable(selection), transcriber));
}
/// Settings now select `selection`; loads it unless it is already the
/// target.
pub(crate) fn want(&mut self, selection: &TranscriptionSelection) {
let selection = runnable(selection);
if &selection == self.target() {
return;
}
self.ready = None;
if selection == self.loaded {
self.loading = None;
return;
}
let (sender, receiver) = std::sync::mpsc::channel();
let load = selection.clone();
let spawned = std::thread::Builder::new()
.name("hex-model-swap".into())
.spawn(move || {
let _ = sender
.send(LocalTranscriber::load(&load).map_err(|error| format!("{error:#}")));
});
match spawned {
Ok(_) => self.loading = Some((selection, receiver)),
Err(error) => tracing::warn!(%error, "could not load the new transcription model"),
}
}
/// Collects a finished load; a failure keeps the current model.
pub(crate) fn poll(&mut self) -> Option<String> {
let (_, receiver) = self.loading.as_ref()?;
let result = match receiver.try_recv() {
Ok(result) => result,
Err(std::sync::mpsc::TryRecvError::Empty) => return None,
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
Err("the model loader stopped unexpectedly".into())
}
};
let (selection, _) = self.loading.take()?;
match result {
Ok(transcriber) => {
self.ready = Some((selection, transcriber));
None
}
Err(error) => Some(format!("could not switch the transcription model: {error}")),
}
}
/// The model to swap in now, if one is ready.
pub(crate) fn take_ready(&mut self) -> Option<LocalTranscriber> {
let (selection, transcriber) = self.ready.take()?;
self.loaded = selection;
Some(transcriber)
}
}
/// `selection` without recognition hints a model cannot use, which
/// `validate` would reject.
fn runnable(selection: &TranscriptionSelection) -> TranscriptionSelection {
let mut selection = selection.clone();
if !crate::transcription_models::definition(selection.model).supports_recognition_hints {
selection.recognition_hints = String::new();
}
selection
}
#[cfg(test)]
mod model_swap_tests {
use super::*;
use crate::transcription_models::TranscriptionModelId;
#[test]
fn an_unchanged_or_hint_only_change_on_a_hintless_model_loads_nothing() {
let selection = TranscriptionSelection {
model: TranscriptionModelId::default(),
..Default::default()
};
let mut swap = ModelSwap::new(&selection);
swap.want(&selection);
let mut hinted = selection.clone();
hinted.recognition_hints = "Hex".into();
if !crate::transcription_models::definition(hinted.model).supports_recognition_hints {
swap.want(&hinted);
}
assert!(swap.loading.is_none());
assert!(swap.take_ready().is_none());
assert!(swap.poll().is_none());
}
}