zeph-llm 0.22.4

LLM provider abstraction with Ollama, Claude, OpenAI, and Candle backends
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::future::Future;
use std::io::Cursor;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use candle_core::{Device, IndexOp, Tensor};
use candle_nn::VarBuilder;
use candle_transformers::models::whisper::{self as m, Config};
use tokenizers::Tokenizer;

use crate::error::LlmError;
use crate::stt::{SpeechToText, Transcription};

#[derive(Clone)]
pub struct CandleWhisperProvider {
    model: Arc<Mutex<m::model::Whisper>>,
    config: Config,
    mel_filters: Vec<f32>,
    tokenizer: Arc<Tokenizer>,
    device: Device,
    language: String,
}

impl std::fmt::Debug for CandleWhisperProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CandleWhisperProvider")
            .field("device", &device_name(&self.device))
            .finish_non_exhaustive()
    }
}

fn device_name(d: &Device) -> &'static str {
    match d {
        Device::Cpu => "cpu",
        Device::Cuda(_) => "cuda",
        Device::Metal(_) => "metal",
    }
}

impl CandleWhisperProvider {
    /// Load a Whisper model from a `HuggingFace` repo.
    ///
    /// When `expected_sha256` is set, the downloaded safetensors file is verified against
    /// it before loading; a mismatch aborts the load without ever memory-mapping the file.
    ///
    /// # Errors
    ///
    /// Returns `LlmError::ModelLoad` if downloading, hash verification, or loading fails.
    #[allow(unsafe_code)]
    pub fn load(
        repo_id: &str,
        device: Option<Device>,
        language: &str,
        expected_sha256: Option<&str>,
    ) -> Result<Self, LlmError> {
        let device = device.unwrap_or_else(crate::device::detect_device);
        tracing::info!(
            repo = repo_id,
            device = device_name(&device),
            "loading candle whisper model"
        );

        let client = hf_hub::HFClientSync::new()
            .map_err(|e| LlmError::ModelLoad(format!("hf-hub init: {e}")))?;
        let (owner, name) = hf_hub::split_id(repo_id);
        let repo = client.model(owner, name);

        let config_path = repo
            .download_file()
            .filename("config.json")
            .send()
            .map_err(|e| LlmError::ModelLoad(format!("config.json: {e}")))?;
        let tokenizer_path = repo
            .download_file()
            .filename("tokenizer.json")
            .send()
            .map_err(|e| LlmError::ModelLoad(format!("tokenizer.json: {e}")))?;
        let weights_path = repo
            .download_file()
            .filename("model.safetensors")
            .send()
            .map_err(|e| LlmError::ModelLoad(format!("model.safetensors: {e}")))?;

        if let Some(expected_hash) = expected_sha256 {
            crate::classifier::verify_sha256(&weights_path, expected_hash)?;
        }

        let config: Config = serde_json::from_reader(std::io::BufReader::new(
            std::fs::File::open(&config_path)
                .map_err(|e| LlmError::ModelLoad(format!("open config: {e}")))?,
        ))?;

        let tokenizer = Tokenizer::from_file(&tokenizer_path)
            .map_err(|e| LlmError::ModelLoad(format!("tokenizer: {e}")))?;

        // SAFETY: safetensors file is validated before mmap; file is not modified during VarBuilder lifetime.
        let vb = unsafe {
            VarBuilder::from_mmaped_safetensors(&[weights_path], candle_core::DType::F32, &device)
                .map_err(|e| LlmError::ModelLoad(format!("weights: {e}")))?
        };

        let model = m::model::Whisper::load(&vb, config.clone())?;

        let mel_bytes = match config.num_mel_bins {
            80 => include_bytes!("melfilters.bytes").as_slice(),
            128 => include_bytes!("melfilters128.bytes").as_slice(),
            n => {
                return Err(LlmError::ModelLoad(format!(
                    "unsupported num_mel_bins: {n}"
                )));
            }
        };
        let mut mel_filters = vec![0f32; mel_bytes.len() / 4];
        for (i, chunk) in mel_bytes.chunks_exact(4).enumerate() {
            mel_filters[i] = f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
        }

        tracing::info!("candle whisper model loaded");

        Ok(Self {
            model: Arc::new(Mutex::new(model)),
            config,
            mel_filters,
            tokenizer: Arc::new(tokenizer),
            device,
            language: language.to_string(),
        })
    }

    fn transcribe_sync(&self, audio: &[u8]) -> Result<Transcription, LlmError> {
        const MAX_DECODE_TOKENS: usize = 224;
        let pcm = decode_audio(audio)?;
        let mel = m::audio::pcm_to_mel(&self.config, &pcm, &self.mel_filters);
        let mel_len = mel.len();
        let n_mel = self.config.num_mel_bins;

        let mel = Tensor::from_vec(mel, (1, n_mel, mel_len / n_mel), &self.device)?;

        let sot = self
            .tokenizer
            .token_to_id(m::SOT_TOKEN)
            .ok_or_else(|| LlmError::TranscriptionFailed("missing SOT token".into()))?;
        let transcribe = self
            .tokenizer
            .token_to_id(m::TRANSCRIBE_TOKEN)
            .ok_or_else(|| LlmError::TranscriptionFailed("missing TRANSCRIBE token".into()))?;
        let no_timestamps = self
            .tokenizer
            .token_to_id(m::NO_TIMESTAMPS_TOKEN)
            .ok_or_else(|| LlmError::TranscriptionFailed("missing NO_TIMESTAMPS token".into()))?;
        let eot = self
            .tokenizer
            .token_to_id(m::EOT_TOKEN)
            .ok_or_else(|| LlmError::TranscriptionFailed("missing EOT token".into()))?;

        let lang_tag = if self.language == "auto" {
            "<|en|>".to_string()
        } else {
            format!("<|{}|>", self.language)
        };
        let language_token = self.tokenizer.token_to_id(&lang_tag).ok_or_else(|| {
            LlmError::TranscriptionFailed(format!(
                "language token {lang_tag} not found in tokenizer"
            ))
        })?;

        let mut model = self
            .model
            .lock()
            .map_err(|e| LlmError::TranscriptionFailed(format!("lock: {e}")))?;
        model.reset_kv_cache();

        let audio_features = model.encoder.forward(&mel, true)?;

        let mut tokens = vec![sot, language_token, transcribe, no_timestamps];

        for _ in 0..MAX_DECODE_TOKENS {
            let token_tensor = Tensor::new(tokens.as_slice(), &self.device)?.unsqueeze(0)?;
            let logits =
                model
                    .decoder
                    .forward(&token_tensor, &audio_features, tokens.len() == 4)?;

            let (_, seq_len, _) = logits.dims3()?;
            let next_logits = logits.i((0, seq_len - 1))?;
            let next_token = next_logits
                .argmax(candle_core::D::Minus1)?
                .to_scalar::<u32>()?;

            if next_token == eot {
                break;
            }
            tokens.push(next_token);
        }

        // Decode only generated tokens (skip prompt tokens)
        let generated = &tokens[4..];
        let text = self
            .tokenizer
            .decode(generated, true)
            .map_err(|e| LlmError::TranscriptionFailed(format!("decode: {e}")))?;

        Ok(Transcription {
            text: text.trim().to_string(),
            language: Some(
                if self.language == "auto" {
                    "en"
                } else {
                    &self.language
                }
                .into(),
            ),
            #[allow(clippy::cast_precision_loss)]
            duration_secs: Some(pcm.len() as f32 / m::SAMPLE_RATE as f32),
        })
    }
}

impl SpeechToText for CandleWhisperProvider {
    fn transcribe(
        &self,
        audio: &[u8],
        _filename: Option<&str>,
    ) -> Pin<Box<dyn Future<Output = Result<Transcription, LlmError>> + Send + '_>> {
        let audio = audio.to_vec();
        Box::pin(async move {
            let provider = self.clone();
            tokio::task::spawn_blocking(move || provider.transcribe_sync(&audio))
                .await
                .map_err(|e| LlmError::TranscriptionFailed(e.to_string()))?
        })
    }
}

fn decode_audio(bytes: &[u8]) -> Result<Vec<f32>, LlmError> {
    use symphonia::core::codecs::CodecParameters;
    use symphonia::core::codecs::audio::AudioDecoderOptions;
    use symphonia::core::formats::probe::Hint;
    use symphonia::core::formats::{FormatOptions, TrackType};
    use symphonia::core::io::{MediaSourceStream, MediaSourceStreamOptions};
    use symphonia::core::meta::MetadataOptions;

    let cursor = Cursor::new(bytes.to_vec());
    let mss = MediaSourceStream::new(Box::new(cursor), MediaSourceStreamOptions::default());

    let mut format = symphonia::default::get_probe()
        .probe(
            &Hint::new(),
            mss,
            FormatOptions::default(),
            MetadataOptions::default(),
        )
        .map_err(|e| LlmError::TranscriptionFailed(format!("probe: {e}")))?;

    let track = format
        .default_track(TrackType::Audio)
        .ok_or_else(|| LlmError::TranscriptionFailed("no audio track".into()))?;
    let audio_params = match track.codec_params.as_ref() {
        Some(CodecParameters::Audio(p)) => p.clone(),
        _ => return Err(LlmError::TranscriptionFailed("non-audio track".into())),
    };
    let sample_rate = audio_params
        .sample_rate
        .ok_or_else(|| LlmError::TranscriptionFailed("unknown sample rate".into()))?;
    let channels = audio_params
        .channels
        .as_ref()
        .map_or(1, symphonia::core::audio::Channels::count);
    let track_id = track.id;

    let mut decoder = symphonia::default::get_codecs()
        .make_audio_decoder(&audio_params, &AudioDecoderOptions::default())
        .map_err(|e| LlmError::TranscriptionFailed(format!("decoder: {e}")))?;

    let mut pcm = Vec::new();

    while let Ok(Some(packet)) = format.next_packet() {
        if packet.track_id != track_id {
            continue;
        }
        let audio_buf = match decoder.decode(&packet) {
            Ok(d) => d,
            Err(e) => {
                tracing::trace!("skipping packet decode error: {e}");
                continue;
            }
        };
        let n_samples = audio_buf.frames() * audio_buf.spec().channels().count();
        let mut samples: Vec<f32> = vec![0f32; n_samples];
        audio_buf.copy_to_slice_interleaved(&mut samples[..]);

        if channels > 1 {
            for chunk in samples.chunks(channels) {
                #[allow(clippy::cast_precision_loss)]
                let avg = chunk.iter().sum::<f32>() / channels as f32;
                pcm.push(avg);
            }
        } else {
            pcm.extend_from_slice(&samples);
        }
    }

    if pcm.is_empty() {
        return Err(LlmError::TranscriptionFailed("no audio decoded".into()));
    }

    // Guard against pathological inputs: max 5 minutes at the source sample rate
    let max_samples = 5 * 60 * sample_rate as usize;
    if pcm.len() > max_samples {
        return Err(LlmError::TranscriptionFailed(format!(
            "audio too long: {} samples exceeds {max_samples} limit (5 min)",
            pcm.len()
        )));
    }

    // Resample to 16kHz if needed
    #[allow(clippy::cast_possible_truncation)]
    let target_rate = m::SAMPLE_RATE as u32;
    if sample_rate != target_rate {
        pcm = resample(&pcm, sample_rate, target_rate)?;
    }

    Ok(pcm)
}

fn resample(input: &[f32], from_rate: u32, to_rate: u32) -> Result<Vec<f32>, LlmError> {
    use rubato::{
        Async, FixedAsync, Resampler, SincInterpolationParameters, SincInterpolationType,
        WindowFunction,
    };

    let params = SincInterpolationParameters {
        sinc_len: 256,
        f_cutoff: Some(0.95),
        interpolation: SincInterpolationType::Linear,
        oversampling_factor: 256,
        window: WindowFunction::BlackmanHarris2,
    };

    let ratio = f64::from(to_rate) / f64::from(from_rate);
    let mut resampler =
        Async::<f32>::new_sinc(ratio, 2.0, &params, input.len(), 1, FixedAsync::Input)
            .map_err(|e| LlmError::TranscriptionFailed(format!("resampler init: {e}")))?;

    let input_adapter = audioadapter_buffers::direct::InterleavedSlice::new(input, 1, input.len())
        .map_err(|e| LlmError::TranscriptionFailed(format!("input buffer: {e}")))?;

    let output = resampler
        .process(&input_adapter, None)
        .map_err(|e| LlmError::TranscriptionFailed(format!("resample: {e}")))?;

    Ok(output.take_data())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::device::detect_device;
    use std::assert_matches;

    #[test]
    fn device_detection_returns_cpu_by_default() {
        let d = detect_device();
        // On CI without GPU, should be CPU
        assert_matches!(d, Device::Cpu | Device::Metal(_) | Device::Cuda(_));
    }

    #[test]
    fn debug_format() {
        let d = detect_device();
        let name = device_name(&d);
        assert!(!name.is_empty());
    }

    #[test]
    fn decode_audio_rejects_invalid_bytes() {
        let result = decode_audio(&[0, 1, 2, 3, 4]);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("probe"), "expected probe error, got: {err}");
    }

    #[test]
    fn decode_audio_rejects_empty_input() {
        let result = decode_audio(&[]);
        assert!(result.is_err());
    }

    #[test]
    fn resample_zeros_preserves_silence() {
        let input = vec![0.0_f32; 1000];
        let output = resample(&input, 44100, 16000).unwrap();
        assert!(!output.is_empty());
        for &s in &output {
            assert!(s.abs() < 1e-6, "expected silence, got {s}");
        }
    }

    #[test]
    fn resample_changes_length() {
        let input = vec![0.5_f32; 44100];
        let output = resample(&input, 44100, 16000).unwrap();
        let expected_len = 16000_usize; // 44100 * 16000 / 44100 = 16000 exactly
        let tolerance = expected_len / 10;
        assert!(
            output.len().abs_diff(expected_len) < tolerance,
            "expected ~{expected_len} samples, got {}",
            output.len()
        );
    }
}