mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* labs: transcribe dropped audio files
Add Labs › Transcribe dropped audio (off by default) and its backend:
WAV and MP3 on every OS, M4A/AIFF/CAF on macOS through the meeting import
bridge, decoded in 30 s pieces, transcribed in-process with the selected
model, and written as <stem>.txt beside the file with a Downloads fallback,
collision numbering, atomic writes, cancellation, and progress. Move the
shared pieces reader out of the transcribe CLI so both use it.
The window's drop target lands in a later change.
* labs: drop audio files on the window to save a transcript
The window root takes files with the extensions this build reads while
Labs › Transcribe dropped audio is on: a file drag shows "Drop to save a
transcript" over the window, a drag Hex can't read says what it reads, and
a drop queues one job per file behind the one running, all at the dictation
model and language in use. A worker transcribes each file and the toast
shows its name, a progress ring and percentage, how many wait behind it,
and Cancel; a saved file shows its name, the whole path, and Show in folder
(meeting::reveal), and a failure shows the backend's own words. The lab's
card offers Transcribe a file…, which opens the same queue's native file
dialog, for Wayland, where nothing can be dropped.
file_transcript exposes its extension table for the drop filter and dialog,
and finds every format from that one list. New goldens cover the hint and
the transcribing toast, and the drop-transcribe card now leads with a wave.
* meeting: use the renamed bridge in the import test
* transcription: find a temp folder through a symlinked path
* transcription: finish the audio file drop's review fixes
A drop right after Cancel (or the lab turned off and on) started a second
worker and a second transcription service while the first was still loading
or running. The running job now stays tracked as cancelling, its toast says
Cancelling… with no Cancel, and the next queued file starts only when its
worker reports Done or its channel closes. A cancel before the save also
stops a cancelled file from writing a transcript.
Also: validate a WAV whole before a model loads, report an unknown MP3 length
as indeterminate progress, map a bridge decode failure to Unreadable, keep a
writable source folder's transcript beside it without Downloads, name this
build's formats in the Labs card, use the kit's 16 pt toast lead, route Show
in folder through the host, derive the Audio folder through app_paths, and
correct the stale comments and feature map.
* transcription: read dropped files through the shared pieces reader
The rebased tree already decodes every container through Pieces, so the
dropped-file backend now opens, validates and reads one Pieces instance
instead of its own WAV, rodio MP3 and macOS bridge readers. Progress comes
from the reader's position and duration, and a compressed file with no
header duration reports no fraction. The retired Labs › Expire meeting audio
gate and the removed Transcribe files lab leave the tree too.
* labs: refresh the goldens for the dropped audio card
The card's row and the drop hint and toast change the Labs images and the
two drop scenes; only those eight goldens changed.
* labs: finish the dropped audio card's translations and goldens
The string tables already translate Audio files and Transcription stopped,
so the lab reuses them; the card's English line gains translations, the
website catalog lists the lab, and the what's new goldens show it.
* labs: offer every audio format the shared reader decodes
the dropped audio lab offered wav and mp3 everywhere, and the old macOS decoder's extras only on macOS, though every OS reads through the shared pieces reader. it now offers one list: wav, mp3, m4a, mp4, aiff/aif, caf, flac, ogg/oga/opus, webm, and mkv. every copy, translation, and golden names it, and a fixture proves the reader takes each one. also names the codec or missing track on a failure, keeps a late cancel's saved toast, and waits for a cancelled worker before the next file.
* labs: offer raw AAC and MOV files too
* labs: offer the reader's other common extensions
the shared pieces reader detects formats by content, so m4b, m4v, 3gp,
aifc, mka, weba, and wave decode too. the extension list is now described
as the common names it offers rather than an exhaustive set, empty files
and unreadable wavs say why, and a lab-off poll drains the worker's
channel instead of reading one event.
* transcription: free the audio slot before a result is sent
A long file is transcribed one piece after another, and each piece admits
the slot as soon as the previous result arrives. The worker released the
slot only after sending that result, so the next admit could see it busy
and fail mid-file with "Another transcription is running".
Also fixes the codec refusal's grammar and two stale comments.
* transcription: say where a damaged file stopped
a compressed file that stops decoding partway now says where it stopped
("talk.m4a is damaged at 12:34") instead of only that it is corrupt; a wav
refused up front keeps the old words. the slot-order test uses a rendezvous
response channel and waits for the slot to read free before receiving, so
sending before the admission drops fails every time instead of occasionally.
a locked drop-transcribe card keeps the lock's words instead of replacing
them with its formats line.
366 lines
13 KiB
Rust
366 lines
13 KiB
Rust
pub(crate) mod app_model_switching;
|
|
pub(crate) mod artifacts;
|
|
pub(crate) mod background_priority;
|
|
#[cfg(target_os = "macos")]
|
|
pub(crate) mod benchmark;
|
|
#[cfg(all(
|
|
target_arch = "x86_64",
|
|
any(target_os = "linux", target_os = "windows")
|
|
))]
|
|
pub(crate) mod cpu_floor;
|
|
pub(crate) mod custom_models;
|
|
pub(crate) mod engine;
|
|
pub(crate) mod file_transcript;
|
|
pub(crate) mod found_models;
|
|
pub(crate) mod gguf_header;
|
|
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
|
|
pub(crate) mod gguf_session;
|
|
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
|
pub(crate) mod gpu_health;
|
|
pub(crate) mod keyboard_model_switching;
|
|
pub(crate) mod languages;
|
|
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
|
pub(crate) mod local_transcriber;
|
|
pub(crate) mod model_benchmarks;
|
|
pub(crate) mod model_details;
|
|
pub(crate) mod models;
|
|
pub(crate) mod pause_segmenter;
|
|
pub(crate) mod pieces;
|
|
#[cfg(any(target_os = "macos", target_os = "windows", test))]
|
|
pub(crate) mod preparation;
|
|
pub(crate) mod service;
|
|
pub(crate) mod speech_detector;
|
|
pub(crate) mod stream;
|
|
pub(crate) mod subtitle_credits;
|
|
pub(crate) mod transcript;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
use color_eyre::eyre::Result;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
use crate::apple_speech::AppleSpeech;
|
|
#[cfg(target_os = "macos")]
|
|
use crate::parakeet::Parakeet;
|
|
#[cfg(not(target_os = "macos"))]
|
|
use crate::transcription::models::TranscriptionModelId;
|
|
#[cfg(target_os = "macos")]
|
|
use crate::transcription::models::{
|
|
ModelRuntime, TranscriptionModelId, TranscriptionSelection, validate,
|
|
};
|
|
#[cfg(target_os = "macos")]
|
|
pub use crate::transcription::transcript::{Transcript, TranscriptSegment};
|
|
|
|
/// Silence after a Unified English clip (400 ms at 16 kHz), so the model
|
|
/// has the context to emit the final word and its closing punctuation
|
|
/// (issue 657: sentences lost their full stop or question mark).
|
|
pub(crate) const UNIFIED_ENGLISH_TRAILING_SILENCE_SAMPLES: usize = 6_400;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
pub enum Transcriber {
|
|
Gguf(Box<Parakeet>),
|
|
AppleSpeech(AppleSpeech),
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
#[derive(Default)]
|
|
pub struct WarmTranscriber {
|
|
active: Option<Transcriber>,
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
impl Transcriber {
|
|
pub fn load() -> Result<Self> {
|
|
let (_, selection) = crate::app_settings::transcription_selection();
|
|
Self::load_selection(&selection)
|
|
}
|
|
|
|
pub fn load_selection(selection: &TranscriptionSelection) -> Result<Self> {
|
|
match validate(selection)?.runtime {
|
|
ModelRuntime::Gguf(_) => Parakeet::load_selection(selection)
|
|
.map(Box::new)
|
|
.map(Self::Gguf),
|
|
ModelRuntime::AppleSpeech => AppleSpeech::load(selection).map(Self::AppleSpeech),
|
|
}
|
|
}
|
|
|
|
pub fn matches_selection(&self, selection: &TranscriptionSelection) -> bool {
|
|
match self {
|
|
Self::Gguf(model) => model.matches_selection(selection),
|
|
Self::AppleSpeech(model) => model.matches_selection(selection),
|
|
}
|
|
}
|
|
|
|
/// Brings an idle GGUF model's weights back before audio arrives (see
|
|
/// [`Parakeet::prewarm`]). Apple Speech has no weights of Hex's to warm.
|
|
pub fn prewarm(&mut self) -> Result<()> {
|
|
match self {
|
|
Self::Gguf(model) => model.prewarm(),
|
|
Self::AppleSpeech(_) => Ok(()),
|
|
}
|
|
}
|
|
|
|
/// Transcribe normalized 16 kHz audio. Backend padding is applied once here,
|
|
/// before GGUF chunking or any hex retranscription.
|
|
pub fn transcribe(&mut self, mut samples: Vec<f32>) -> Result<String> {
|
|
match self {
|
|
Self::Gguf(model) => {
|
|
pad_gguf_samples(&mut samples, model.model_id());
|
|
model.transcribe(&samples)
|
|
}
|
|
Self::AppleSpeech(model) => model.transcribe(&samples).map(|result| result.text),
|
|
}
|
|
}
|
|
|
|
/// Transcribe one normalized segment with the same input preparation as
|
|
/// plain dictation. The caller retains ownership of segment offsets.
|
|
pub fn transcribe_segments(&mut self, mut samples: Vec<f32>) -> Result<Transcript> {
|
|
match self {
|
|
Self::Gguf(model) => {
|
|
pad_gguf_samples(&mut samples, model.model_id());
|
|
model
|
|
.transcribe_segments(&samples)
|
|
.map(|result| Transcript {
|
|
text: result.text,
|
|
segments: result
|
|
.segments
|
|
.into_iter()
|
|
.map(|segment| TranscriptSegment {
|
|
start_ms: segment.t0_ms,
|
|
end_ms: segment.t1_ms,
|
|
text: segment.text,
|
|
})
|
|
.collect(),
|
|
words: result
|
|
.words
|
|
.into_iter()
|
|
.map(|word| TranscriptSegment {
|
|
start_ms: word.t0_ms,
|
|
end_ms: word.t1_ms,
|
|
text: word.text,
|
|
})
|
|
.collect(),
|
|
})
|
|
}
|
|
Self::AppleSpeech(model) => model.transcribe(&samples),
|
|
}
|
|
}
|
|
|
|
/// A complete clip with its native timings, padded exactly as
|
|
/// [`Self::transcribe`] pads it so the text matches an untimed request.
|
|
pub fn transcribe_timed(&mut self, mut samples: Vec<f32>) -> Result<Transcript> {
|
|
match self {
|
|
Self::Gguf(model) => {
|
|
pad_gguf_samples(&mut samples, model.model_id());
|
|
model.transcribe_timed(&samples)
|
|
}
|
|
// Apple Speech segments the whole clip itself.
|
|
Self::AppleSpeech(model) => model.transcribe(&samples),
|
|
}
|
|
}
|
|
|
|
/// The finest native timings this backend gives: Apple Speech segments,
|
|
/// and a GGUF model what its metadata advertises.
|
|
pub fn timestamps(&self) -> crate::transcription::transcript::TimestampLevel {
|
|
match self {
|
|
Self::Gguf(model) => model.timestamps(),
|
|
Self::AppleSpeech(_) => crate::transcription::transcript::TimestampLevel::Segment,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Pads a GGUF clip as dictation prepares it on every OS: the backend's
|
|
/// minimum duration, then the model's trailing silence. Applied after any
|
|
/// pause cut, so a trailing pause cannot trim that silence.
|
|
pub(crate) fn pad_gguf_samples(samples: &mut Vec<f32>, model: Option<TranscriptionModelId>) {
|
|
crate::dictation::pad_for_parakeet(samples);
|
|
samples.resize(samples.len() + trailing_silence_samples(model), 0.0);
|
|
}
|
|
|
|
/// Silence a clip of `model` gets after its audio, whole clip or stream.
|
|
pub(crate) fn trailing_silence_samples(model: Option<TranscriptionModelId>) -> usize {
|
|
if model == Some(TranscriptionModelId::ParakeetUnifiedEnglish) {
|
|
UNIFIED_ENGLISH_TRAILING_SILENCE_SAMPLES
|
|
} else {
|
|
0
|
|
}
|
|
}
|
|
|
|
/// Whether a live stream of `model` must end its text with closing sentence
|
|
/// punctuation before it is trusted: Unified English's chunked decode drops
|
|
/// the mark its whole-clip run keeps (issue 1933).
|
|
pub(crate) fn requires_closing_mark(model: Option<TranscriptionModelId>) -> bool {
|
|
model == Some(TranscriptionModelId::ParakeetUnifiedEnglish)
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
impl WarmTranscriber {
|
|
/// A warm transcriber already serving `model`, for opt-in harnesses.
|
|
#[cfg(test)]
|
|
pub(crate) fn serving(model: Transcriber) -> Self {
|
|
Self {
|
|
active: Some(model),
|
|
}
|
|
}
|
|
|
|
pub fn load() -> Result<Self> {
|
|
Ok(Self {
|
|
active: Some(Transcriber::load()?),
|
|
})
|
|
}
|
|
|
|
/// The selection of the warm model when it is a GGUF model.
|
|
pub(crate) fn gguf_selection(&self) -> Option<&TranscriptionSelection> {
|
|
match &self.active {
|
|
Some(Transcriber::Gguf(model)) => model.selection(),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Silence a live stream of the warm model hears before it finalizes,
|
|
/// the same trailing silence its whole-clip runs get.
|
|
pub(crate) fn stream_trailing_silence(&self) -> usize {
|
|
match &self.active {
|
|
Some(Transcriber::Gguf(model)) => trailing_silence_samples(model.model_id()),
|
|
_ => 0,
|
|
}
|
|
}
|
|
|
|
/// Whether a live stream of the warm model must end its text with closing
|
|
/// sentence punctuation before it is trusted, as Unified English's does
|
|
/// (issue 1933).
|
|
pub(crate) fn stream_requires_closing_mark(&self) -> bool {
|
|
match &self.active {
|
|
Some(Transcriber::Gguf(model)) => requires_closing_mark(model.model_id()),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// Stream options of the warm model, when it can stream on its device.
|
|
pub(crate) fn stream_options(&self) -> Option<&transcribe_cpp::StreamOptions> {
|
|
match &self.active {
|
|
Some(Transcriber::Gguf(model)) => model.stream_options(),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
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> {
|
|
match &mut self.active {
|
|
Some(Transcriber::Gguf(model)) => model.with_stream(stream_options, cancel, drive),
|
|
_ => Err(transcribe_cpp::Error::NotImplemented(
|
|
"the active transcriber cannot stream".into(),
|
|
)),
|
|
}
|
|
}
|
|
|
|
/// The loaded model, without switching or loading one.
|
|
pub(crate) fn active(&mut self) -> Option<&mut Transcriber> {
|
|
self.active.as_mut()
|
|
}
|
|
|
|
pub fn activate(&mut self, selection: &TranscriptionSelection) -> Result<&mut Transcriber> {
|
|
if !self
|
|
.active
|
|
.as_ref()
|
|
.is_some_and(|model| model.matches_selection(selection))
|
|
{
|
|
let candidate = Transcriber::load_selection(selection)?;
|
|
self.active = Some(candidate);
|
|
}
|
|
Ok(self
|
|
.active
|
|
.as_mut()
|
|
.expect("activated transcriber must be available"))
|
|
}
|
|
}
|
|
|
|
#[cfg(all(test, target_os = "macos"))]
|
|
mod apple_speech_tests {
|
|
use super::Transcript;
|
|
|
|
#[test]
|
|
fn apple_bridge_transcript_uses_the_canonical_wire_shape() {
|
|
let transcript: Transcript = serde_json::from_str(
|
|
r#"{"text":"hello","segments":[{"startMs":10,"endMs":40,"text":"hello"}]}"#,
|
|
)
|
|
.unwrap();
|
|
|
|
assert_eq!(transcript.text, "hello");
|
|
assert_eq!(transcript.segments.len(), 1);
|
|
assert_eq!(transcript.segments[0].start_ms, 10);
|
|
assert_eq!(transcript.segments[0].end_ms, 40);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod padding_tests {
|
|
use super::{UNIFIED_ENGLISH_TRAILING_SILENCE_SAMPLES, pad_gguf_samples};
|
|
use crate::transcription::models::TranscriptionModelId;
|
|
|
|
#[test]
|
|
fn unified_english_gets_trailing_silence_for_final_token_context() {
|
|
let mut samples = vec![0.5; 32_000];
|
|
|
|
pad_gguf_samples(
|
|
&mut samples,
|
|
Some(TranscriptionModelId::ParakeetUnifiedEnglish),
|
|
);
|
|
|
|
assert_eq!(
|
|
samples.len(),
|
|
32_000 + UNIFIED_ENGLISH_TRAILING_SILENCE_SAMPLES
|
|
);
|
|
assert!(samples[..32_000].iter().all(|sample| *sample == 0.5));
|
|
assert!(samples[32_000..].iter().all(|sample| *sample == 0.0));
|
|
}
|
|
|
|
#[test]
|
|
fn other_gguf_models_do_not_get_trailing_silence() {
|
|
let mut samples = vec![0.5; 32_000];
|
|
|
|
pad_gguf_samples(&mut samples, Some(TranscriptionModelId::ParakeetV2));
|
|
|
|
assert_eq!(samples.len(), 32_000);
|
|
}
|
|
|
|
#[test]
|
|
fn short_unified_input_is_minimum_padded_before_trailing_context() {
|
|
let mut samples = vec![0.5; 1_600];
|
|
pad_gguf_samples(
|
|
&mut samples,
|
|
Some(TranscriptionModelId::ParakeetUnifiedEnglish),
|
|
);
|
|
|
|
// The 400 ms context is additional to the 1.5 s minimum, not part of it.
|
|
assert_eq!(
|
|
samples.len(),
|
|
24_000 + UNIFIED_ENGLISH_TRAILING_SILENCE_SAMPLES
|
|
);
|
|
assert!(samples[..1_600].iter().all(|sample| *sample == 0.5));
|
|
assert!(samples[1_600..].iter().all(|sample| *sample == 0.0));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod closing_mark_tests {
|
|
use super::requires_closing_mark;
|
|
use crate::transcription::models::{CustomModelId, TranscriptionModelId};
|
|
|
|
#[test]
|
|
fn only_unified_english_streams_require_a_closing_mark() {
|
|
assert!(requires_closing_mark(Some(
|
|
TranscriptionModelId::ParakeetUnifiedEnglish
|
|
)));
|
|
assert!(!requires_closing_mark(None));
|
|
assert!(!requires_closing_mark(Some(
|
|
TranscriptionModelId::ParakeetV2
|
|
)));
|
|
assert!(!requires_closing_mark(Some(TranscriptionModelId::Custom(
|
|
CustomModelId::parse("custom-0123456789abcdef").expect("16 hex digits")
|
|
))));
|
|
}
|
|
}
|