From fd77e19b60ff76e4e2fee8ba8dbf7b315f54e25e Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:09:48 -0500 Subject: [PATCH] Rename away from whisper --- rust-whisper-lib/src/lib.rs | 77 ++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/rust-whisper-lib/src/lib.rs b/rust-whisper-lib/src/lib.rs index e949f85..ed97f18 100644 --- a/rust-whisper-lib/src/lib.rs +++ b/rust-whisper-lib/src/lib.rs @@ -33,8 +33,8 @@ pub struct Flags { pub debug: bool, } -pub fn main(flags: Flags, handler_fn: F) where F: FnMut(Result) + Send + 'static { - let w = new_whisper_service( +pub fn main(flags: Flags, handler_fn: F) where F: FnMut(Result) + Send + 'static { + let w = new_service( flags.model_path, flags.model_buffer, flags.threads, @@ -93,23 +93,23 @@ pub fn main(flags: Flags, handler_fn: F) where F: FnMut(Result, +struct Service { + jobs: std::sync::mpsc::SyncSender, } -fn new_whisper_service(model_path: Option, model_buffer: Option>, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result where F: FnMut(Result) + Send + 'static { - match new_whisper_engine(model_path, model_buffer, threads) { +fn new_service(model_path: Option, model_buffer: Option>, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result where F: FnMut(Result) + Send + 'static { + match new_engine(model_path, model_buffer, threads) { Ok(engine) => { let mut whisper = new_whisper_impl(engine, stream_head, stream_tail, handler_fn); let (send, recv) = std::sync::mpsc::sync_channel(100); thread::spawn(move || { whisper.transcribe_asyncs(recv); }); - Ok(WhisperService{jobs: send}) + Ok(Service{jobs: send}) }, Err(msg) => Err(format!("failed to initialize engine: {}", msg)), } } -impl WhisperService { +impl Service { fn transcribe(&self, data: &Vec) { let (send, recv) = std::sync::mpsc::sync_channel(0); self._transcribe_async(data, Some(send)).unwrap(); @@ -121,7 +121,7 @@ impl WhisperService { } fn _transcribe_async(&self, data: &Vec, ack: Option>) -> Result<(), String> { - match self.jobs.try_send(AWhisper{ + match self.jobs.try_send(ATranscribe{ data: data.clone().to_vec(), ack: ack, }) { @@ -131,15 +131,15 @@ impl WhisperService { } } -struct WhisperImpl { - engine: WhisperEngine, +struct Impl { + engine: Engine, stream_head: f32, stream_tail: f32, - handler_fn: Option) + Send + 'static>> + handler_fn: Option) + Send + 'static>> } -fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: F) -> WhisperImpl where F: FnMut(Result) + Send + 'static { - WhisperImpl { +fn new_whisper_impl(engine: Engine, stream_head: f32, stream_tail: f32, handler_fn: F) -> Impl where F: FnMut(Result) + Send + 'static { + Impl { engine: engine, stream_head: stream_head, stream_tail: stream_tail, @@ -147,8 +147,8 @@ fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32 } } -impl WhisperImpl { - fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver) { +impl Impl { + fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver) { loop { match recv.recv() { Ok(job) => { @@ -165,7 +165,7 @@ impl WhisperImpl { } } - fn transcribe(&mut self, a_whisper: &AWhisper) -> Result<(), ()> { + fn transcribe(&mut self, a_whisper: &ATranscribe) -> Result<(), ()> { match self.engine.transcribe(&a_whisper.data) { Ok(result) => { self.on_success(&result); @@ -178,7 +178,7 @@ impl WhisperImpl { } } - fn on_success(&mut self, whispered: &Whispered) { + fn on_success(&mut self, whispered: &Transcribed) { let result = whispered .after(&(self.stream_head * 100.0)) .before(&(self.stream_tail * 100.0)); @@ -190,31 +190,38 @@ impl WhisperImpl { } } -struct WhisperEngine { +struct Engine { ctx: WhisperContext, threads: i32, } -fn new_whisper_engine(model_path: Option, model_buffer: Option>, threads: i32) -> Result { +fn new_engine(model_path: Option, model_buffer: Option>, threads: i32) -> Result { if model_path.is_some() { let model_path = model_path.unwrap(); return match WhisperContext::new(&model_path.clone()) { - Ok(ctx) => Ok(WhisperEngine{ctx: ctx, threads: threads}), + Ok(ctx) => Ok(Engine{ctx: ctx, threads: threads}), Err(msg) => Err(format!("failed to load {}: {}", model_path, msg)), }; } if model_buffer.is_some() { let model_buffer = model_buffer.unwrap(); return match WhisperContext::new_from_buffer(&model_buffer) { - Ok(ctx) => Ok(WhisperEngine{ctx: ctx, threads: threads}), + Ok(ctx) => Ok(Engine{ctx: ctx, threads: threads}), Err(msg) => Err(format!("failed to load buffer: {}", msg)), }; } Err("neither model path nor buffer provided".to_string()) } -impl WhisperEngine { - fn transcribe(&self, data: &Vec) -> Result { +impl Engine { + fn transcribe(&self, data: &Vec) -> Result { + match self._transcribe(data) { + Ok(transcribed) => Ok(transcribed), + Err(msg) => Err(format!("{}", msg)), + } + } + + fn _transcribe(&self, data: &Vec) -> Result { let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 }); params.set_no_context(true); params.set_n_threads(self.threads); @@ -242,36 +249,36 @@ impl WhisperEngine { } } -struct AWhisper { +struct ATranscribe { data: Vec, ack: Option>, } #[derive(Clone, Debug)] -pub struct Whispered { - data: Vec, +pub struct Transcribed { + data: Vec, } #[derive(Clone, Debug)] -struct AWhispered { +struct ATranscribed { pub data: String, pub offset: i64, pub length: i64, } -fn new_whispered() -> Whispered { - Whispered{data: vec![]} +fn new_whispered() -> Transcribed { + Transcribed{data: vec![]} } -fn new_a_whispered(data: String, start: i64, stop: i64) -> AWhispered { - AWhispered{ +fn new_a_whispered(data: String, start: i64, stop: i64) -> ATranscribed { + ATranscribed{ data: data, offset: start.clone(), length: stop - start, } } -impl Whispered { +impl Transcribed { pub fn to_string(&self) -> String { let mut result = "".to_string(); for i in 0..self.data.len() { @@ -280,7 +287,7 @@ impl Whispered { result } - fn after(&self, t: &f32) -> Whispered { + fn after(&self, t: &f32) -> Transcribed { let mut result = new_whispered(); self.data .iter() @@ -289,7 +296,7 @@ impl Whispered { result } - fn before(&self, t: &f32) -> Whispered { + fn before(&self, t: &f32) -> Transcribed { let mut result = new_whispered(); let end = match self.data.iter().map(|x| x.offset + x.length).max() { Some(x) => x,