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 fn main<F>(flags: Flags, handler_fn: F) where F: FnMut(Result<Whispered, String>) + Send + 'static {
let w = new_whisper_service(
pub fn main<F>(flags: Flags, handler_fn: F) where F: FnMut(Result<Transcribed, String>) + Send + 'static {
let w = new_service(
flags.model_path,
flags.model_buffer,
flags.threads,
@ -93,23 +93,23 @@ pub fn main<F>(flags: Flags, handler_fn: F) where F: FnMut(Result<Whispered, Str
};
}
struct WhisperService {
jobs: std::sync::mpsc::SyncSender<AWhisper>,
struct Service {
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 {
match new_whisper_engine(model_path, model_buffer, threads) {
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_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<f32>) {
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<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(),
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<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 {
WhisperImpl {
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 {
Impl {
engine: engine,
stream_head: stream_head,
stream_tail: stream_tail,
@ -147,8 +147,8 @@ fn new_whisper_impl<F>(engine: WhisperEngine, stream_head: f32, stream_tail: f32
}
}
impl WhisperImpl {
fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<AWhisper>) {
impl Impl {
fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<ATranscribe>) {
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<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() {
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<f32>) -> Result<Whispered, WhisperError> {
impl Engine {
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 });
params.set_no_context(true);
params.set_n_threads(self.threads);
@ -242,36 +249,36 @@ impl WhisperEngine {
}
}
struct AWhisper {
struct ATranscribe {
data: Vec<f32>,
ack: Option<std::sync::mpsc::SyncSender<bool>>,
}
#[derive(Clone, Debug)]
pub struct Whispered {
data: Vec<AWhispered>,
pub struct Transcribed {
data: Vec<ATranscribed>,
}
#[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,