aboutsummaryrefslogtreecommitdiffstats
path: root/crates/asist-asr/src/lib.rs
blob: e6ecb36c27ae8addbebffe7af4966a14fdcb5cc8 (plain)
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
//! Speech recognition on top of Canary (ONNX).
//!
//! The fact that shapes this crate: **decoding a window costs more than
//! recording it**. On this machine a 6 s window takes about 800 ms to decode
//! and only advances 400 ms of audio. Any design that processes every window
//! in order keeps falling behind the speaker without limit.
//!
//! The way out is dropping work: the decoding thread drains its whole queue,
//! keeps only the most recent window and discards the rest. Latency stays
//! bounded by one decode instead of growing unchecked, and what is lost are
//! partial transcriptions that were going to be overwritten anyway.

use std::time::{Duration, Instant};

use crossbeam_channel::{Receiver, Sender};

use asist_core::config::AsrConfig;
use asist_core::error::{Error, Result};
use asist_core::event::TurnId;

pub use canary_rs::{Canary, CanarySession, ExecutionConfig, ExecutionProvider, StreamConfig};

/// Work arriving at the decoder.
#[derive(Debug)]
pub enum AsrJob {
    /// New audio for the sliding window.
    Window {
        turn: TurnId,
        samples: Vec<f32>,
        at: Instant,
    },
    /// Closed utterance, to be transcribed in full.
    Utterance {
        turn: TurnId,
        samples: Vec<f32>,
        at: Instant,
    },
    /// The utterance was empty: resets the window state.
    Reset { turn: TurnId },
}

/// What the decoder returns.
#[derive(Debug, Clone)]
pub enum AsrResult {
    Partial {
        turn: TurnId,
        committed: String,
        volatile: String,
        /// Windows discarded as stale before this one.
        dropped: usize,
        decode: Duration,
    },
    Final {
        turn: TurnId,
        text: String,
        audio_secs: f32,
        decode: Duration,
        /// Instant the user stopped talking. It is the origin perceived latency
        /// is measured from, and it cannot be taken here: by the time the
        /// transcription is ready almost a second has passed.
        spoken_at: Instant,
    },
    Empty {
        turn: TurnId,
    },
    Error {
        turn: TurnId,
        message: String,
    },
}

/// Engine loaded and ready to decode.
pub struct Recognizer {
    model: Canary,
    config: AsrConfig,
}

impl Recognizer {
    /// Loads the model from `config.model_dir`.
    pub fn load(config: &AsrConfig) -> Result<Self> {
        if !config.model_dir.is_dir() {
            return Err(Error::Asr(format!(
                "the model directory does not exist: {}. Run scripts/bootstrap.sh",
                config.model_dir.display()
            )));
        }
        let started = Instant::now();
        let model = Canary::from_pretrained(
            config.model_dir.to_string_lossy().as_ref(),
            Some(execution_config(config)),
        )
        .map_err(|e| Error::Asr(format!("could not load Canary: {e}")))?;

        tracing::info!(
            target: "asr",
            dir = %config.model_dir.display(),
            provider = %config.execution_provider,
            ms = started.elapsed().as_millis(),
            "modelo cargado"
        );
        Ok(Self {
            model,
            config: config.clone(),
        })
    }

    /// Standalone session to transcribe in one go (tests and checks).
    pub fn transcribe(&self, samples: &[f32], sample_rate: u32) -> Result<String> {
        let mut session = self.model.session();
        session
            .transcribe_samples(
                samples,
                sample_rate as usize,
                1,
                &self.config.source_lang,
                &self.config.target_lang,
            )
            .map(|r| normalize(&r.text))
            .map_err(|e| Error::Asr(e.to_string()))
    }

    /// Decoder loop. Runs on its own thread until `jobs` is closed.
    pub fn run(self, jobs: Receiver<AsrJob>, out: Sender<AsrResult>) {
        let mut stream = match self.model.stream(
            self.config.source_lang.clone(),
            self.config.target_lang.clone(),
            stream_config(&self.config),
        ) {
            Ok(stream) => stream,
            Err(err) => {
                let _ = out.send(AsrResult::Error {
                    turn: TurnId::default(),
                    message: format!("could not open the stream: {err}"),
                });
                return;
            }
        };
        let mut session = self.model.session();
        let mut committed = String::new();
        let step_samples = (self.config.step * 16_000.0).max(1.0) as usize;

        while let Some(batch) = drain(&jobs) {
            // All the audio left behind an utterance close belongs to a turn that
            // is about to be transcribed in full: spending a window on it would
            // be work doomed to be overwritten.
            let mut pending: Vec<f32> = Vec::new();
            let mut pending_turn = TurnId::default();
            let mut newest = Instant::now();
            let mut dropped = 0usize;

            for job in batch {
                match job {
                    AsrJob::Window { turn, samples, at } => {
                        pending_turn = turn;
                        pending.extend_from_slice(&samples);
                        newest = at;
                    }
                    AsrJob::Reset { turn } => {
                        dropped += pending.len() / step_samples;
                        pending.clear();
                        stream.reset();
                        committed.clear();
                        if out.send(AsrResult::Empty { turn }).is_err() {
                            return;
                        }
                    }
                    AsrJob::Utterance { turn, samples, at } => {
                        dropped += pending.len() / step_samples;
                        pending.clear();
                        stream.reset();
                        committed.clear();
                        let result = decode_final(&mut session, &self.config, turn, &samples, at);
                        if out.send(result).is_err() {
                            return;
                        }
                    }
                }
            }

            if pending.is_empty() || !self.config.partials {
                continue;
            }
            // Only the newest window survives; the older ones no longer describe
            // what is being said now.
            dropped += (pending.len() / step_samples).saturating_sub(1);

            let started = Instant::now();
            let chunks = match stream.push_samples(&pending, 16_000, 1) {
                Ok(chunks) => chunks,
                Err(err) => {
                    let _ = out.send(AsrResult::Error {
                        turn: pending_turn,
                        message: err.to_string(),
                    });
                    continue;
                }
            };
            let Some(chunk) = chunks.last() else { continue };
            if is_degenerate(&chunk.result.text) {
                continue;
            }

            append_delta(&mut committed, chunk.delta_text.trim());
            let volatile = volatile_tail(&committed, chunk.result.text.trim());
            let decode = started.elapsed();
            tracing::debug!(
                target: "asr",
                turn = pending_turn.0,
                ms = decode.as_millis(),
                delay_ms = newest.elapsed().as_millis(),
                dropped = dropped,
                "ventana"
            );
            if out
                .send(AsrResult::Partial {
                    turn: pending_turn,
                    committed: committed.clone(),
                    volatile,
                    dropped,
                    decode,
                })
                .is_err()
            {
                return;
            }
        }
    }
}

fn decode_final(
    session: &mut CanarySession,
    config: &AsrConfig,
    turn: TurnId,
    samples: &[f32],
    at: Instant,
) -> AsrResult {
    let started = Instant::now();
    let audio_secs = samples.len() as f32 / 16_000.0;
    match session.transcribe_samples(samples, 16_000, 1, &config.source_lang, &config.target_lang) {
        Ok(result) => {
            let text = normalize(&result.text);
            let decode = started.elapsed();
            tracing::debug!(
                target: "asr",
                turn = turn.0,
                ms = decode.as_millis(),
                audio_s = audio_secs,
                rtf = decode.as_secs_f32() / audio_secs.max(0.001),
                delay_ms = at.elapsed().as_millis(),
                "final transcription"
            );
            if text.is_empty() {
                AsrResult::Empty { turn }
            } else {
                AsrResult::Final <