Rename away from whisper

master
Bel LaPointe 2023-12-20 09:09:48 -05:00
parent 26f3ccad37
commit fd77e19b60
1 changed files with 42 additions and 35 deletions

View File

@ -33,8 +33,8 @@ pub struct Flags {
pub debug: bool, pub debug: bool,
} }
pub fn main<F>(flags: Flags, handler_fn: F) where F: FnMut(Result<Whispered, String>) + Send + 'static { pub fn main<F>(flags: Flags, handler_fn: F) where F: FnMut(Result<Transcribed, String>) + Send + 'static {
let w = new_whisper_service( let w = new_service(
flags.model_path, flags.model_path,
flags.model_buffer, flags.model_buffer,
flags.threads, flags.threads,
@ -93,23 +93,23 @@ pub fn main<F>(flags: Flags, handler_fn: F) where F: FnMut(Result<Whispered, Str
}; };
} }
struct WhisperService { struct Service {
jobs: std::sync::mpsc::SyncSender<AWhisper>, jobs: std::sync::mpsc::SyncSender<ATranscribe>,
} }
fn new_whisper_service<F>(model_path: Option<String>, model_buffer: Option<Vec<u8>>, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result<WhisperService, String> where F: FnMut(Result<Whispered, String>) + Send + 'static { fn new_service<F>(model_path: Option<String>, model_buffer: Option<Vec<u8>>, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result<Service, String> where F: FnMut(Result<Transcribed, String>) + Send + 'static {
match new_whisper_engine(model_path, model_buffer, threads) { match new_engine(model_path, model_buffer, threads) {
Ok(engine) => { Ok(engine) => {
let mut whisper = new_whisper_impl(engine, stream_head, stream_tail, handler_fn); let mut whisper = new_whisper_impl(engine, stream_head, stream_tail, handler_fn);
let (send, recv) = std::sync::mpsc::sync_channel(100); let (send, recv) = std::sync::mpsc::sync_channel(100);
thread::spawn(move || { whisper.transcribe_asyncs(recv); }); thread::spawn(move || { whisper.transcribe_asyncs(recv); });
Ok(WhisperService{jobs: send}) Ok(Service{jobs: send})
}, },
Err(msg) => Err(format!("failed to initialize engine: {}", msg)), Err(msg) => Err(format!("failed to initialize engine: {}", msg)),
} }
} }
impl WhisperService { impl Service {
fn transcribe(&self, data: &Vec<f32>) { fn transcribe(&self, data: &Vec<f32>) {
let (send, recv) = std::sync::mpsc::sync_channel(0); let (send, recv) = std::sync::mpsc::sync_channel(0);
self._transcribe_async(data, Some(send)).unwrap(); self._transcribe_async(data, Some(send)).unwrap();
@ -121,7 +121,7 @@ impl WhisperService {
} }
fn _transcribe_async(&self, data: &Vec<f32>, ack: Option<std::sync::mpsc::SyncSender<bool>>) -> Result<(), String> { fn _transcribe_async(&self, data: &Vec<f32>, ack: Option<std::sync::mpsc::SyncSender<bool>>) -> Result<(), String> {
match self.jobs.try_send(AWhisper{ match self.jobs.try_send(ATranscribe{
data: data.clone().to_vec(), data: data.clone().to_vec(),
ack: ack, ack: ack,
}) { }) {
@ -131,15 +131,15 @@ impl WhisperService {
} }
} }
struct WhisperImpl { struct Impl {
engine: WhisperEngine, engine: Engine,
stream_head: f32, stream_head: f32,
stream_tail: f32, stream_tail: f32,
handler_fn: Option<Box<dyn FnMut(Result<Whispered, String>) + Send + 'static>> handler_fn: Option<Box<dyn FnMut(Result<Transcribed, String>) + Send + 'static>>
} }
fn new_whisper_impl<F>(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: F) -> WhisperImpl where F: FnMut(Result<Whispered, String>) + Send + 'static { fn new_whisper_impl<F>(engine: Engine, stream_head: f32, stream_tail: f32, handler_fn: F) -> Impl where F: FnMut(Result<Transcribed, String>) + Send + 'static {
WhisperImpl { Impl {
engine: engine, engine: engine,
stream_head: stream_head, stream_head: stream_head,
stream_tail: stream_tail, stream_tail: stream_tail,
@ -147,8 +147,8 @@ fn new_whisper_impl<F>(engine: WhisperEngine, stream_head: f32, stream_tail: f32
} }
} }
impl WhisperImpl { impl Impl {
fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<AWhisper>) { fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<ATranscribe>) {
loop { loop {
match recv.recv() { match recv.recv() {
Ok(job) => { 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) { match self.engine.transcribe(&a_whisper.data) {
Ok(result) => { Ok(result) => {
self.on_success(&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 let result = whispered
.after(&(self.stream_head * 100.0)) .after(&(self.stream_head * 100.0))
.before(&(self.stream_tail * 100.0)); .before(&(self.stream_tail * 100.0));
@ -190,31 +190,38 @@ impl WhisperImpl {
} }
} }
struct WhisperEngine { struct Engine {
ctx: WhisperContext, ctx: WhisperContext,
threads: i32, threads: i32,
} }
fn new_whisper_engine(model_path: Option<String>, model_buffer: Option<Vec<u8>>, threads: i32) -> Result<WhisperEngine, String> { fn new_engine(model_path: Option<String>, model_buffer: Option<Vec<u8>>, threads: i32) -> Result<Engine, String> {
if model_path.is_some() { if model_path.is_some() {
let model_path = model_path.unwrap(); let model_path = model_path.unwrap();
return match WhisperContext::new(&model_path.clone()) { 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)), Err(msg) => Err(format!("failed to load {}: {}", model_path, msg)),
}; };
} }
if model_buffer.is_some() { if model_buffer.is_some() {
let model_buffer = model_buffer.unwrap(); let model_buffer = model_buffer.unwrap();
return match WhisperContext::new_from_buffer(&model_buffer) { 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(msg) => Err(format!("failed to load buffer: {}", msg)),
}; };
} }
Err("neither model path nor buffer provided".to_string()) Err("neither model path nor buffer provided".to_string())
} }
impl WhisperEngine { impl Engine {
fn transcribe(&self, data: &Vec<f32>) -> Result<Whispered, WhisperError> { fn transcribe(&self, data: &Vec<f32>) -> Result<Transcribed, String> {
match self._transcribe(data) {
Ok(transcribed) => Ok(transcribed),
Err(msg) => Err(format!("{}", msg)),
}
}
fn _transcribe(&self, data: &Vec<f32>) -> Result<Transcribed, WhisperError> {
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 }); let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 });
params.set_no_context(true); params.set_no_context(true);
params.set_n_threads(self.threads); params.set_n_threads(self.threads);
@ -242,36 +249,36 @@ impl WhisperEngine {
} }
} }
struct AWhisper { struct ATranscribe {
data: Vec<f32>, data: Vec<f32>,
ack: Option<std::sync::mpsc::SyncSender<bool>>, ack: Option<std::sync::mpsc::SyncSender<bool>>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Whispered { pub struct Transcribed {
data: Vec<AWhispered>, data: Vec<ATranscribed>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct AWhispered { struct ATranscribed {
pub data: String, pub data: String,
pub offset: i64, pub offset: i64,
pub length: i64, pub length: i64,
} }
fn new_whispered() -> Whispered { fn new_whispered() -> Transcribed {
Whispered{data: vec![]} Transcribed{data: vec![]}
} }
fn new_a_whispered(data: String, start: i64, stop: i64) -> AWhispered { fn new_a_whispered(data: String, start: i64, stop: i64) -> ATranscribed {
AWhispered{ ATranscribed{
data: data, data: data,
offset: start.clone(), offset: start.clone(),
length: stop - start, length: stop - start,
} }
} }
impl Whispered { impl Transcribed {
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
let mut result = "".to_string(); let mut result = "".to_string();
for i in 0..self.data.len() { for i in 0..self.data.len() {
@ -280,7 +287,7 @@ impl Whispered {
result result
} }
fn after(&self, t: &f32) -> Whispered { fn after(&self, t: &f32) -> Transcribed {
let mut result = new_whispered(); let mut result = new_whispered();
self.data self.data
.iter() .iter()
@ -289,7 +296,7 @@ impl Whispered {
result result
} }
fn before(&self, t: &f32) -> Whispered { fn before(&self, t: &f32) -> Transcribed {
let mut result = new_whispered(); let mut result = new_whispered();
let end = match self.data.iter().map(|x| x.offset + x.length).max() { let end = match self.data.iter().map(|x| x.offset + x.length).max() {
Some(x) => x, Some(x) => x,