I am trying to create a wrapper around the OpenAI Whisper Rust library whisper-rs for my usecase. I have created a Whisper
struct which holds the context, state, parameters and configs. I want these to work in a different worker thread (future implementation). I want the data to have a static lifetime.
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperState};
use std::sync::{Arc, Mutex,};
use once_cell::sync::OnceCell;
pub struct WhisperCore<'a> {
ctx: Option<Arc<WhisperContext>>,
state: Option<Arc<WhisperState<'a>>>,
}
impl WhisperCore<'_> {
pub fn new() -> Self {
WhisperCore
{
ctx: None,
state: None,
}
}
}
pub struct Whisper {
inner: Arc<Mutex<WhisperCore<'static>>>,
}
impl Whisper{
pub fn get_instance() -> Arc<Mutex<WhisperCore<'static>>> {
static INSTANCE: OnceCell<Arc<Mutex<WhisperCore<'static>>>> = OnceCell::new();
let singleton = INSTANCE.get_or_init(|| {
Arc::new(Mutex::new(WhisperCore::new()))
});
Arc::clone(singleton)
}
pub fn set_whisper_ctx(&self, path: Option<&'static str>){
let mut whisper_entity = self.inner.lock().unwrap();
match path {
Some(value) => {
let ctx = Arc::new(WhisperContext::new(value).unwrap());
whisper_entity.ctx = Some(ctx);
},
None => {
println!("Empty model path");
}
}
}
pub fn set_whisper_state(&self){
let mut whisper_entity = self.inner.lock().unwrap();
if let Some(ctx) = whisper_entity.ctx.clone() {
let state = Arc::new(ctx.create_state().expect("failed to create key"));
whisper_entity.state = Some(state);
} else {
println!("hiheiheih")
}
}
pub fn start(&self) {
self.set_whisper_ctx(Some("/path/model"));
self.set_whisper_state();
}
}
I am getting this error when I try to set_whisper_state, the error is happening on this line whisper_entity.state = Some(state). I also tried various steps such as cloning the ctx before using it to get the state in set_whisper_state function. Please help me with this issue.
error[E0597]: `ctx` does not live long enough
--> src\speech_to_text\whisper.rs:166:34
|
165 | if let Some(ctx) = whisper_entity.ctx.clone() {
| --- binding `ctx` declared here
166 | let state = Arc::new(ctx.create_state().expect("failed to create key"));
| ^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
167 | whisper_entity.state = Some(state);
168 | } else {
| - `ctx` dropped here while still borrowed
...
173 | }
| - borrow might be used here, when `whisper_entity` is dropped and runs the `Drop` code for type `MutexGuard`
|
= note: values in a scope are dropped in the opposite order they are defined