File size: 2,119 Bytes
b371670
 
f33df9c
b371670
 
81301f1
b371670
81301f1
b371670
 
 
81301f1
b371670
 
 
 
 
8b6063a
b371670
 
 
81301f1
8b6063a
b371670
 
81301f1
b371670
81301f1
8b6063a
 
f33df9c
b371670
 
 
 
f33df9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81301f1
b371670
 
 
 
81301f1
b371670
81301f1
b371670
8b6063a
b371670
 
 
81301f1
b371670
 
81301f1
 
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
use std::fmt::{Debug, Formatter};
use async_trait::async_trait;
use tokio::{spawn};
use tokio::sync::broadcast::Receiver;
use lazy_static::lazy_static;

extern crate whisper;

use whisper::handler::{Error, Output, WhisperHandler, Context};
use crate::asr::{ASR, Event};
use crate::config::SETTINGS;

lazy_static! {
    pub static ref CONTEXT: Context = Context::new(&SETTINGS.whisper.model)
        .expect("Failed to initialize whisper context");
}

pub struct WhisperAsr {
    whisper: WhisperHandler,
    tx: tokio::sync::broadcast::Sender<Event>,
}

impl Debug for WhisperAsr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Whisper_ASR")
    }
}

impl WhisperAsr {
    pub async fn from_config() -> Result<WhisperAsr, Error> {
        let whisper = CONTEXT.create_handler(SETTINGS.whisper.clone(), "".to_string());
        let mut output_rx = whisper.subscribe();
        let (tx, _) = tokio::sync::broadcast::channel(64);
        let shared_tx = tx.clone();
        let fut = async move {
            while let Ok(outputs) = output_rx.recv().await {
                for output in outputs {
                    let evt = match output {
                        Output::Stable(segment) => Event {
                            transcript: segment.text,
                            is_final: true,
                        },
                        Output::Unstable(segment) => Event {
                            transcript: segment.text,
                            is_final: false,
                        },
                    };
                    if let Err(e) = tx.send(evt) {
                        tracing::warn!("Failed to send whisper event: {}", e);
                        break
                    }
                }
            }
        };
        spawn(fut);
        Ok(Self { whisper, tx: shared_tx })
    }
}

#[async_trait]
impl ASR for WhisperAsr {
    async fn frame(&mut self, frame: Vec<i16>) -> anyhow::Result<()> {
        Ok(self.whisper.send_i16(frame).await?)
    }

    fn subscribe(&mut self) -> Receiver<Event> {
        self.tx.subscribe()
    }
}