Spaces:
Sleeping
Sleeping
File size: 2,894 Bytes
b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 b371670 81301f1 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 70 71 72 73 74 75 76 77 78 79 80 81 82 |
use std::fmt::{Debug, Formatter};
use async_trait::async_trait;
use tokio::{select, spawn};
use tokio::sync::broadcast::Receiver;
use tokio::sync::broadcast::error::RecvError;
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 Whisper_ASR {
whisper: WhisperHandler,
tx: tokio::sync::broadcast::Sender<Event>,
}
impl Debug for Whisper_ASR {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Whisper_ASR")
}
}
impl Whisper_ASR {
pub async fn from_config() -> Result<Whisper_ASR, Error> {
let whisper = CONTEXT.create_handler(&SETTINGS.whisper, "".to_string())?;
let mut output_rx = whisper.subscribe();
let (tx, _) = tokio::sync::broadcast::channel(64);
let shared_tx = tx.clone();
let fut = async move {
loop {
select! {
poll = output_rx.recv() => {
match poll {
Ok(outputs) => {
for output in outputs {
let res = match output {
Output::Stable(segment) => tx.send(Event {
transcript: segment.text,
is_final: true,
}),
Output::Unstable(segment) => tx.send(Event {
transcript: segment.text,
is_final: false,
}),
};
if let Err(e) = res {
tracing::warn!("Failed to send whisper event: {}", e);
break
}
}
},
Err(RecvError::Closed) => break,
Err(RecvError::Lagged(lagged)) => {
tracing::warn!("Whisper ASR output lagged: {}", lagged);
}
}
},
}
}
};
spawn(fut);
Ok(Self { whisper, tx: shared_tx })
}
}
#[async_trait]
impl ASR for Whisper_ASR {
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()
}
} |