bunsen-tool 0.43.0

bunsen's models on the command line: speech to text (Parakeet, Qwen3-ASR, Voxtral Realtime, Whisper) and who spoke when (Sortformer), text to speech (ZONOS3, Qwen3-TTS), chat (Qwen3, Qwen3.5, Gemma, nanochat) and typed decisions (Laya). The chat driver is also a library, `bunsen_tool::chat`.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! The model families and kits, and loading a model with its info card.
use std::{
    io::Write,
    sync::Arc,
    time::Instant,
};

use bunsen::{
    data::{
        builds::{
            BuildFactory,
            BuildProvider,
            BuildSummary,
            ConstructInit,
            DeferredBuild,
            Loaded,
        },
        cache::verify_sha256,
        catalog::{
            ResolvedBuild,
            lookup::LeafMatch,
        },
        formats::LoadPolicy,
        resources::{
            ResourceCache,
            ResourceCacheOptions,
        },
    },
    errors::{
        BunsenResult,
        sys_at,
    },
    kits::speech::{
        asr::{
            parakeet::{
                ParakeetConstruct,
                pretrained::{
                    DEFAULT_PARAKEET_MODEL,
                    default_parakeet_factory,
                },
            },
            qwen3_asr::{
                Qwen3AsrConstruct,
                pretrained::{
                    DEFAULT_QWEN3_ASR_MODEL,
                    default_qwen3_asr_factory,
                },
            },
            voxtral::{
                VoxtralConstruct,
                pretrained::{
                    DEFAULT_VOXTRAL_MODEL,
                    default_voxtral_factory,
                },
            },
            whisper::{
                driver::WhisperConstruct,
                pretrained::default_whisper_factory,
            },
        },
        diarization::sortformer::pretrained::{
            DEFAULT_SORTFORMER_MODEL,
            default_sortformer_factory,
        },
    },
};
use burn::tensor::Device;

use crate::asr::{
    parakeet::ParakeetFlags,
    qwen3_asr::Qwen3AsrFlags,
    voxtral::VoxtralFlags,
    whisper,
};

/// A model family: one kit, its factory, and its default model.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum AsrFamily {
    /// NVIDIA's Parakeet TDT.
    Parakeet,

    /// Qwen's Qwen3-ASR.
    Qwen3Asr,

    /// Mistral's Voxtral Realtime.
    Voxtral,

    /// `OpenAI`'s Whisper.
    Whisper,
}

/// What `list` and `fetch` serve: a speech recognizer's family, or the
/// diarizer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AsrKit {
    /// A speech recognizer's family.
    Asr(AsrFamily),

    /// The speaker diarizer: Sortformer.
    Diarizer,
}

impl From<AsrFamily> for AsrKit {
    fn from(family: AsrFamily) -> Self {
        AsrKit::Asr(family)
    }
}

/// What a family's factory offers: its listed rows, with their providers,
/// and the providers that list nothing but resolve a ref (`hf`).
#[derive(Clone, Debug)]
pub struct AsrFamilyListing {
    /// The listed rows, by provider.
    pub rows: Vec<(String, ResolvedBuild)>,

    /// Every provider, in search order.
    pub providers: Vec<Arc<dyn BuildProvider>>,

    /// The providers that list nothing.
    pub unlisted: Vec<String>,
}

/// A loaded model of one family.
#[derive(Clone, Debug)]
pub enum AsrConstructWrapper {
    /// A Parakeet TDT construct.
    Parakeet(Arc<ParakeetConstruct>),

    /// A Qwen3-ASR construct.
    Qwen3Asr(Arc<Qwen3AsrConstruct>),

    /// A Voxtral Realtime construct.
    Voxtral(Arc<VoxtralConstruct>),

    /// A Whisper construct, which `transcribe` drives through a stream driver.
    Whisper(Arc<WhisperConstruct>),
}

impl<'de> serde::Deserialize<'de> for AsrFamily {
    /// By [`name`](AsrFamily::name): `parakeet`, `qwen3-asr`, `voxtral`,
    /// `whisper`.
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let name = String::deserialize(deserializer)?;
        AsrFamily::from_name(&name).ok_or_else(|| {
            let names: Vec<&str> = AsrFamily::ALL.iter().map(|f| f.name()).collect();
            serde::de::Error::custom(format!("`{name}` is not a family: {}", names.join(", ")))
        })
    }
}

impl AsrFamily {
    /// Every family.
    pub const ALL: [AsrFamily; 4] = [
        AsrFamily::Parakeet,
        AsrFamily::Qwen3Asr,
        AsrFamily::Voxtral,
        AsrFamily::Whisper,
    ];

    /// The family whose command name is `name`.
    pub fn from_name(name: &str) -> Option<AsrFamily> {
        AsrFamily::ALL.into_iter().find(|f| f.name() == name)
    }

    /// The family's command name.
    pub fn name(self) -> &'static str {
        match self {
            AsrFamily::Parakeet => "parakeet",
            AsrFamily::Qwen3Asr => "qwen3-asr",
            AsrFamily::Voxtral => "voxtral",
            AsrFamily::Whisper => "whisper",
        }
    }

    /// The model `--model` names when it is not given.
    pub fn default_model(self) -> &'static str {
        match self {
            AsrFamily::Parakeet => DEFAULT_PARAKEET_MODEL,
            AsrFamily::Qwen3Asr => DEFAULT_QWEN3_ASR_MODEL,
            AsrFamily::Voxtral => DEFAULT_VOXTRAL_MODEL,
            AsrFamily::Whisper => "openai/base",
        }
    }

    /// Loads `spec` through `cache`, and logs its info card at `INFO`:
    /// a row of the family's factory, or for Whisper a checkpoint's path,
    /// with `flags`' `--vocab` over it. Also returns the build that answered
    /// `spec`, for the command to report
    /// ([`log_build`](crate::support::log_build)).
    pub fn load<F: FamilyFlags>(
        self,
        spec: &str,
        flags: &F,
        cache: &ResourceCache,
        device: &Device,
        policy: LoadPolicy,
    ) -> BunsenResult<(AsrConstructWrapper, BuildSummary)> {
        fn split<T>(
            loaded: Loaded<T>,
            wrap: impl FnOnce(Arc<T>) -> AsrConstructWrapper,
        ) -> (AsrConstructWrapper, BuildSummary) {
            (wrap(loaded.handle), loaded.summary)
        }
        Ok(match self {
            AsrFamily::Parakeet => split(
                load_logged(
                    default_parakeet_factory()?.resolve(spec, cache)?,
                    cache,
                    device,
                    policy,
                )?,
                AsrConstructWrapper::Parakeet,
            ),
            AsrFamily::Qwen3Asr => split(
                load_logged(
                    default_qwen3_asr_factory()?.resolve(spec, cache)?,
                    cache,
                    device,
                    policy,
                )?,
                AsrConstructWrapper::Qwen3Asr,
            ),
            AsrFamily::Voxtral => split(
                load_logged(
                    default_voxtral_factory()?.resolve(spec, cache)?,
                    cache,
                    device,
                    policy,
                )?,
                AsrConstructWrapper::Voxtral,
            ),
            AsrFamily::Whisper => split(
                load_logged(
                    whisper::resolve(spec, flags.whisper(), cache)?,
                    cache,
                    device,
                    policy,
                )?,
                AsrConstructWrapper::Whisper,
            ),
        })
    }
}

impl AsrKit {
    /// The kit's command name.
    pub fn name(self) -> &'static str {
        match self {
            AsrKit::Asr(family) => family.name(),
            AsrKit::Diarizer => "diarize",
        }
    }

    /// The model `--model` names when it is not given.
    pub fn default_model(self) -> &'static str {
        match self {
            AsrKit::Asr(family) => family.default_model(),
            AsrKit::Diarizer => DEFAULT_SORTFORMER_MODEL,
        }
    }

    /// Whether the kit's factory lists `name`, a row's name or alias, with
    /// or without its provider (`catalog:openai/small`), case aside: a
    /// row named in the build catalog's form answers its shorter spellings too
    /// (`openai/small` for `openai/whisper/small`), as its provider's lookup
    /// takes them.
    ///
    /// # Errors
    /// As [`listing`](Self::listing).
    pub fn lists(
        self,
        name: &str,
    ) -> BunsenResult<bool> {
        let listing = self.listing()?;
        Ok(listing.rows.iter().any(|(provider, row)| {
            let bare = match name.split_once(':') {
                Some((p, bare)) if p.eq_ignore_ascii_case(provider) => bare,
                Some(_) => return false,
                None => name,
            };
            row.name.matches_query(bare, LeafMatch::Exact) || row.answers_to(bare)
        }))
    }

    /// What the kit's factory offers.
    pub fn listing(self) -> BunsenResult<AsrFamilyListing> {
        fn split<H: ConstructInit>(factory: BuildFactory<H>) -> AsrFamilyListing {
            let rows = factory.list();
            let listed: Vec<&str> = rows.iter().map(|(p, _)| p.as_str()).collect();
            let unlisted = factory
                .providers()
                .iter()
                .map(|p| p.name().to_string())
                .filter(|p| !listed.contains(&p.as_str()))
                .collect();
            AsrFamilyListing {
                rows,
                providers: factory.providers().to_vec(),
                unlisted,
            }
        }
        Ok(match self {
            AsrKit::Asr(AsrFamily::Parakeet) => split(default_parakeet_factory()?),
            AsrKit::Asr(AsrFamily::Qwen3Asr) => split(default_qwen3_asr_factory()?),
            AsrKit::Asr(AsrFamily::Voxtral) => split(default_voxtral_factory()?),
            AsrKit::Asr(AsrFamily::Whisper) => split(default_whisper_factory()?),
            AsrKit::Diarizer => split(default_sortformer_factory()?),
        })
    }

    /// Brings every resource of each of `names` into `cache`, fetching what
    /// is not local, and writes where each one is to `out`.
    ///
    /// # Arguments
    /// * `verify` - re-hash each pinned file once it is local, the cached ones
    ///   included.
    ///
    /// # Errors
    /// As the kit's factory resolving a name, its plan, and the cache's
    /// load; [`DigestMismatch`](bunsen::errors::DigestMismatch) when a file
    /// verified does not hash to its pin.
    pub fn fetch(
        self,
        names: &[String],
        cache: &ResourceCache,
        verify: bool,
        out: &mut impl Write,
    ) -> BunsenResult<()> {
        match self {
            AsrKit::Asr(AsrFamily::Parakeet) => {
                fetch_with(default_parakeet_factory()?, names, cache, verify, out)
            }
            AsrKit::Asr(AsrFamily::Qwen3Asr) => {
                fetch_with(default_qwen3_asr_factory()?, names, cache, verify, out)
            }
            AsrKit::Asr(AsrFamily::Voxtral) => {
                fetch_with(default_voxtral_factory()?, names, cache, verify, out)
            }
            AsrKit::Asr(AsrFamily::Whisper) => {
                fetch_with(default_whisper_factory()?, names, cache, verify, out)
            }
            AsrKit::Diarizer => {
                fetch_with(default_sortformer_factory()?, names, cache, verify, out)
            }
        }
    }
}

/// [`AsrKit::fetch`] through one kit's factory: each name's plan (a
/// path's vocabulary is the one its checkpoint's layout selects, and a
/// name's checkpoint must scan as its prefab), then every resource of it.
pub fn fetch_with<H: ConstructInit>(
    factory: BuildFactory<H>,
    names: &[String],
    cache: &ResourceCache,
    verify: bool,
    out: &mut impl Write,
) -> BunsenResult<()> {
    let w = |e| sys_at("write to", "stdout")(e);
    for name in names {
        let model = factory.resolve(name, cache)?;
        let map = model.plan(cache)?;
        let loaded = cache.load(&map)?;
        writeln!(out, "{}:", model.id()).map_err(w)?;
        for (key, part) in loaded.iter() {
            writeln!(
                out,
                "  {key}: {} ({})",
                part.path.display(),
                part.provenance
            )
            .map_err(w)?;
            if verify {
                match map.get(key).and_then(|r| r.sha256.as_deref()) {
                    Some(sha256) => {
                        verify_sha256(&part.path, sha256)?;
                        writeln!(out, "    sha256 {sha256} ok").map_err(w)?;
                    }
                    None => {
                        writeln!(out, "    (unpinned; no digest to verify against)").map_err(w)?
                    }
                }
            }
        }
    }
    Ok(())
}

/// Loads `deferred` under `policy`, logging what it is and where its files
/// came from: the model's info card. The [`Loaded`] keeps the build's
/// [`summary`](Loaded::summary), for the command to report.
pub fn load_logged<H: ConstructInit>(
    deferred: DeferredBuild<H>,
    cache: &ResourceCache,
    device: &Device,
    policy: LoadPolicy,
) -> BunsenResult<Loaded<H::Construct>> {
    let t0 = Instant::now();
    let loaded = deferred.load(cache, device, policy)?;
    log::info!("model: {}", loaded.summary);
    if let Some((_, row)) = deferred.model.build() {
        log::info!("  {}", row.description);
        if let Some(license) = &row.license {
            log::info!("  license: {license}");
        }
        if let Some(origin) = &row.origin {
            log::info!("  origin: {origin}");
        }
    }
    for (key, part) in &loaded.resources.parts {
        log::info!("  {key}: {} ({})", part.path.display(), part.provenance);
    }
    log::info!("  device: {device:?}; loaded in {:.1?}", t0.elapsed());
    Ok(loaded)
}

/// A family's own transcription flags: paths, not policy.
pub trait FamilyFlags: std::fmt::Debug {
    /// Whisper's, when these are Whisper's.
    fn whisper(&self) -> Option<&whisper::WhisperFlags> {
        None
    }

    /// The cache options these flags call for, over `options`.
    fn cache_options(
        &self,
        options: ResourceCacheOptions,
    ) -> ResourceCacheOptions {
        match self.whisper() {
            Some(whisper) => whisper.cache_options(options),
            None => options,
        }
    }
}

impl FamilyFlags for whisper::WhisperFlags {
    fn whisper(&self) -> Option<&whisper::WhisperFlags> {
        Some(self)
    }
}

impl FamilyFlags for ParakeetFlags {}

impl FamilyFlags for Qwen3AsrFlags {}

impl FamilyFlags for VoxtralFlags {}

/// `$body` with `$flags` the family `$family`'s own flags: their defaults, as
/// a preset sets no paths. A macro, as each family's flags are their own
/// type.
macro_rules! with_flags {
    ($family:expr, $flags:ident => $body:expr) => {
        match $family {
            $crate::asr::AsrFamily::Parakeet => {
                let $flags = $crate::asr::parakeet::ParakeetFlags::default();
                $body
            }
            $crate::asr::AsrFamily::Qwen3Asr => {
                let $flags = $crate::asr::qwen3_asr::Qwen3AsrFlags::default();
                $body
            }
            $crate::asr::AsrFamily::Voxtral => {
                let $flags = $crate::asr::voxtral::VoxtralFlags::default();
                $body
            }
            $crate::asr::AsrFamily::Whisper => {
                let $flags = $crate::asr::whisper::WhisperFlags::default();
                $body
            }
        }
    };
}
pub(crate) use with_flags;

#[cfg(test)]
mod tests {
    use bunsen_app::cache::WeightsCacheArgs;

    use super::*;

    /// Offline, over an empty cache, a name the family does not have is a
    /// lookup error, and one it has cannot be fetched; nothing is written.
    #[test]
    fn test_fetch_offline() {
        let dir = tempfile::tempdir().unwrap();
        let cache = WeightsCacheArgs {
            cache_dir: Some(dir.path().to_path_buf()),
            offline: true,
        }
        .init()
        .unwrap();
        let mut out = Vec::new();
        for kit in [
            AsrKit::Asr(AsrFamily::Parakeet),
            AsrKit::Asr(AsrFamily::Qwen3Asr),
            AsrKit::Asr(AsrFamily::Voxtral),
            AsrKit::Asr(AsrFamily::Whisper),
            AsrKit::Diarizer,
        ] {
            let unknown = kit.fetch(&["no/such-model".into()], &cache, false, &mut out);
            assert!(unknown.is_err(), "{}", kit.name());
            let known = kit.fetch(&[kit.default_model().into()], &cache, false, &mut out);
            assert!(known.is_err(), "{} fetched offline", kit.name());
        }
        assert!(cache.cached_files(&Default::default()).unwrap().is_empty());
    }
}