but it is soooo sloooow

master
Bel LaPointe 2023-12-21 14:34:22 -05:00
parent 0d8315550a
commit de099d9917
1 changed files with 20 additions and 18 deletions

View File

@ -42,18 +42,10 @@ pub fn wav<F>(flags: Flags, handler_fn: F, wav_path: String) where F: FnMut(Resu
flags.stream_tail,
handler_fn,
).unwrap();
let (header, data) = wav::read(
&mut std::fs::File::open(wav_path).expect("failed to open $WAV"),
).expect("failed to decode $WAV");
assert!(header.channel_count == 1);
assert!(header.sampling_rate == 16_000);
let data16 = data.as_sixteen().expect("wav is not 32bit floats");
let audio_data = &whisper_rs::convert_integer_to_float_audio(&data16);
w.transcribe(&audio_data);
w.transcribe(&f32_from_wav_file(&wav_path).unwrap())
}
fn f32_from_wav_file(path: String) -> Result<Vec<f32>, String> {
fn f32_from_wav_file(path: &String) -> Result<Vec<f32>, String> {
let f = std::fs::File::open(path);
if let Some(err) = f.as_ref().err() {
return Err(format!("failed to open wav file: {}", err));
@ -267,10 +259,18 @@ impl Engine {
}
struct Engine2 {
model: rwhisper::Whisper,
}
fn new_engine2(model_path: Option<String>, model_buffer: Option<Vec<u8>>, threads: i32) -> Result<Engine2, String> {
Ok(Engine2{})
match rwhisper::WhisperBuilder::default()
.with_cpu(true)
.with_language(Some(rwhisper::WhisperLanguage::English))
.with_source(rwhisper::WhisperSource::TinyEn)
.build() {
Ok(model) => Ok(Engine2{model: model}),
Err(msg) => Err(format!("failed to create model: {}", msg)),
}
}
impl Engine2 {
@ -279,15 +279,15 @@ impl Engine2 {
}
fn _transcribe(&self, data: &Vec<f32>) -> Result<Transcribed, String> {
let model = rwhisper::WhisperBuilder::default()
.with_cpu(true)
.with_language(Some(rwhisper::WhisperLanguage::English))
.with_source(rwhisper::WhisperSource::TinyEn)
.build().unwrap();
let buffer = rodio::buffer::SamplesBuffer::new(1, 16_000, data.clone());
let stream = self.model.transcribe(buffer);
if stream.as_ref().is_err() {
return Err(format!("failed to start transcribing: {}", stream.err().unwrap()));
}
let stream = stream.unwrap();
let future = async {
let mut w: Vec<u8> = vec![];
let mut stream = model.transcribe(buffer).unwrap();
stream.write_to(&mut w).await.unwrap();
w
};
@ -412,8 +412,10 @@ mod tests {
None,
4,
).expect("failed to make new engine2");
let data = f32_from_wav_file("../gitea-whisper-rs/sys/whisper.cpp/bindings/go/samples/jfk.wav".to_string()).expect("failed to read jfk.wav");
let data = f32_from_wav_file(&"../gitea-whisper-rs/sys/whisper.cpp/bindings/go/samples/jfk.wav".to_string()).expect("failed to read jfk.wav");
let start = std::time::Instant::now();
let result = engine_2.transcribe(&data).expect("failed to transcribe");
println!("rwhisper = {}s", start.elapsed().as_secs_f32());
assert_eq!(" And so my fellow American asked not what your country can do for you, ask what you can do for your country.".to_string(), result.to_string());
}
}