mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
403 lines
14 KiB
Rust
403 lines
14 KiB
Rust
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
use crate::transcription::models::{
|
|
ModelPreparationStage, ModelRuntime, TranscriptionModelId, TranscriptionSelection,
|
|
};
|
|
|
|
type Prepare = dyn Fn(&TranscriptionSelection, bool, &AtomicBool, &AtomicU64, &AtomicU8) -> 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>,
|
|
/// What the worker is doing for `model`.
|
|
pub stage: Option<ModelPreparationStage>,
|
|
pub downloaded_bytes: u64,
|
|
pub error: Option<String>,
|
|
}
|
|
|
|
struct Request {
|
|
selection: TranscriptionSelection,
|
|
installed_only: bool,
|
|
}
|
|
|
|
impl Request {
|
|
/// The worker's first stage: an installed-only request never downloads.
|
|
fn first_stage(&self) -> ModelPreparationStage {
|
|
if self.installed_only {
|
|
ModelPreparationStage::Verifying
|
|
} else {
|
|
ModelPreparationStage::Downloading
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Task {
|
|
request: Request,
|
|
canceled: Arc<AtomicBool>,
|
|
progress: Arc<AtomicU64>,
|
|
stage: Arc<AtomicU8>,
|
|
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)
|
|
});
|
|
let active = state.active.as_ref().filter(|_| state.pending.is_none());
|
|
PreparationStatus {
|
|
model: request.map(|request| request.selection.model),
|
|
stage: request.map(|request| {
|
|
active.map_or(request.first_stage(), |task| {
|
|
ModelPreparationStage::load(&task.stage)
|
|
})
|
|
}),
|
|
downloaded_bytes: match (request, active) {
|
|
(Some(_), Some(task)) => task.progress.load(Ordering::Relaxed),
|
|
_ => 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 stage = Arc::new(AtomicU8::new(request.first_stage() as u8));
|
|
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();
|
|
let worker_stage = stage.clone();
|
|
match thread::Builder::new()
|
|
.name("transcription-preparation".into())
|
|
.spawn(move || {
|
|
prepare(
|
|
&selection,
|
|
installed_only,
|
|
&worker_canceled,
|
|
&worker_progress,
|
|
&worker_stage,
|
|
)
|
|
}) {
|
|
Ok(worker) => {
|
|
self.active = Some(Task {
|
|
request,
|
|
canceled,
|
|
progress,
|
|
stage,
|
|
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,
|
|
stage: &AtomicU8,
|
|
) -> 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 {
|
|
ModelPreparationStage::Verifying.store(stage);
|
|
crate::transcription::models::verify_installed(model, canceled)?;
|
|
progress.store(model.download_bytes().unwrap_or(0), Ordering::Relaxed);
|
|
} else {
|
|
crate::transcription::models::download_with_stage_progress(
|
|
model, canceled, progress, stage,
|
|
)?;
|
|
}
|
|
}
|
|
check_canceled()?;
|
|
ModelPreparationStage::Loading.store(stage);
|
|
crate::transcription::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()
|
|
})()
|
|
// The chain reaches the controller's warning and the row's reason, so a
|
|
// failed prewarm names its cause instead of only "could not prewarm".
|
|
.map_err(|error| format!("{error:#}"))
|
|
}
|
|
|
|
#[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 status_reports_the_worker_s_own_stage() {
|
|
let (stored, stores) = mpsc::channel();
|
|
let (release, releases) = mpsc::channel::<()>();
|
|
let releases = Mutex::new(releases);
|
|
let preparation = TranscriptionPreparation::with_prepare(Arc::new(
|
|
move |_, _, _, _, stage: &AtomicU8| {
|
|
let releases = releases.lock().unwrap();
|
|
releases.recv().unwrap();
|
|
ModelPreparationStage::Loading.store(stage);
|
|
stored.send(()).unwrap();
|
|
releases.recv().unwrap();
|
|
Ok(())
|
|
},
|
|
));
|
|
// An installed model is never downloaded.
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV2), true);
|
|
assert_eq!(
|
|
preparation.status().stage,
|
|
Some(ModelPreparationStage::Verifying)
|
|
);
|
|
release.send(()).unwrap();
|
|
stores.recv_timeout(Duration::from_secs(3)).unwrap();
|
|
assert_eq!(
|
|
preparation.status().stage,
|
|
Some(ModelPreparationStage::Loading)
|
|
);
|
|
// A download waiting behind it starts by downloading.
|
|
preparation.start(selection(TranscriptionModelId::ParakeetV3), false);
|
|
assert_eq!(
|
|
preparation.status().stage,
|
|
Some(ModelPreparationStage::Downloading)
|
|
);
|
|
preparation.cancel();
|
|
assert_eq!(preparation.status().stage, None);
|
|
release.send(()).unwrap();
|
|
}
|
|
|
|
#[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
|
|
);
|
|
}
|
|
}
|