Skip to main content

any_speech_to_text/
lib.rs

1//! Local speech-to-text inference built around Candle.
2//!
3//! The crate exposes a single API for model asset resolution, runtime selection,
4//! audio decoding, and transcription across the supported ASR backends. Model
5//! weights can be supplied from directories, explicit files, typed in-memory
6//! assets, or the optional Hugging Face downloader.
7
8pub mod audio;
9pub mod config;
10pub mod device;
11#[cfg(feature = "download")]
12pub mod download;
13pub mod error;
14pub mod models;
15#[cfg(feature = "opus")]
16pub mod opus;
17pub mod traits;
18
19pub use audio::{AudioInput, AudioSamples};
20pub use config::{
21    DType, ModelAsset, ModelAssetBundle, ModelAssetDir, ModelFiles, MoonshineAssets,
22    Qwen3AsrAssets, Qwen3TokenizerAsset, RuntimeChoice, SafetensorsWeights, SttConfig,
23    WhisperAssets, preferred_runtime_choice, preferred_runtime_choices,
24};
25pub use device::DeviceSelection;
26pub use error::SttError;
27pub use models::{BackendKind, InferenceMode, ModelAssetRequirement, ModelType};
28pub use traits::{
29    ModelInfo, SttModel, TimestampGranularity, TranscriptionRequest, TranscriptionResult,
30    TranscriptionSegment, TranscriptionTask,
31};
32
33use models::moonshine::MoonshineSttModel;
34#[cfg(feature = "olmoasr")]
35use models::olmoasr::OlmoAsrModel;
36#[cfg(feature = "qwen3-asr")]
37use models::qwen3asr::Qwen3AsrModel;
38#[cfg(feature = "whisper")]
39use models::whisper::WhisperModel;
40
41/// Load a model backend based on the provided configuration.
42pub fn load_model(config: SttConfig) -> Result<Box<dyn SttModel>, SttError> {
43    match config.model_type.family() {
44        BackendKind::Whisper => {
45            #[cfg(feature = "whisper")]
46            {
47                let model = WhisperModel::load(config)?;
48                Ok(Box::new(model))
49            }
50
51            #[cfg(not(feature = "whisper"))]
52            {
53                Err(SttError::BackendDisabled(
54                    "Whisper support is disabled; enable the `whisper` feature".to_string(),
55                ))
56            }
57        }
58        BackendKind::OlmoAsr => {
59            #[cfg(feature = "olmoasr")]
60            {
61                let model = OlmoAsrModel::load(config)?;
62                Ok(Box::new(model))
63            }
64
65            #[cfg(not(feature = "olmoasr"))]
66            {
67                Err(SttError::BackendDisabled(
68                    "OLMoASR support is disabled; enable the `olmoasr` feature".to_string(),
69                ))
70            }
71        }
72        BackendKind::Qwen3Asr => {
73            #[cfg(feature = "qwen3-asr")]
74            {
75                let model = Qwen3AsrModel::load(config)?;
76                Ok(Box::new(model))
77            }
78
79            #[cfg(not(feature = "qwen3-asr"))]
80            {
81                Err(SttError::BackendDisabled(
82                    "Qwen3-ASR support is disabled; enable the `qwen3-asr` feature".to_string(),
83                ))
84            }
85        }
86        BackendKind::Moonshine => {
87            let model = MoonshineSttModel::load(config)?;
88            Ok(Box::new(model))
89        }
90    }
91}