|
| 1 | +#![allow(clippy::uninlined_format_args)] |
| 2 | + |
| 3 | +use std::path::Path; |
| 4 | +use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters}; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + let arg1 = std::env::args() |
| 8 | + .nth(1) |
| 9 | + .expect("first argument should be path to WAV file"); |
| 10 | + let audio_path = Path::new(&arg1); |
| 11 | + if !audio_path.exists() { |
| 12 | + panic!("audio file doesn't exist"); |
| 13 | + } |
| 14 | + let arg2 = std::env::args() |
| 15 | + .nth(2) |
| 16 | + .expect("second argument should be path to Whisper model"); |
| 17 | + let whisper_path = Path::new(&arg2); |
| 18 | + if !whisper_path.exists() { |
| 19 | + panic!("whisper file doesn't exist") |
| 20 | + } |
| 21 | + |
| 22 | + let original_samples = examples_common::parse_wav_file(audio_path); |
| 23 | + let mut samples = vec![0.0f32; original_samples.len()]; |
| 24 | + whisper_rs::convert_integer_to_float_audio(&original_samples, &mut samples) |
| 25 | + .expect("failed to convert samples"); |
| 26 | + |
| 27 | + let ctx = WhisperContext::new_with_params( |
| 28 | + &whisper_path.to_string_lossy(), |
| 29 | + WhisperContextParameters::default(), |
| 30 | + ) |
| 31 | + .expect("failed to open model"); |
| 32 | + let mut state = ctx.create_state().expect("failed to create a model state"); |
| 33 | + |
| 34 | + // Enable OpenVINO now |
| 35 | + // We're expecting the OpenVINO file sitting right next to the model |
| 36 | + state |
| 37 | + .init_openvino_encoder(None, "GPU", None) |
| 38 | + .expect("failed to enable openvino"); |
| 39 | + |
| 40 | + let mut params = FullParams::new(SamplingStrategy::default()); |
| 41 | + params.set_initial_prompt("experience"); |
| 42 | + params.set_progress_callback_safe(|progress| println!("Progress callback: {}%", progress)); |
| 43 | + |
| 44 | + let st = std::time::Instant::now(); |
| 45 | + state |
| 46 | + .full(params, &samples) |
| 47 | + .expect("failed to convert samples"); |
| 48 | + let et = std::time::Instant::now(); |
| 49 | + |
| 50 | + let num_segments = state |
| 51 | + .full_n_segments() |
| 52 | + .expect("failed to get number of segments"); |
| 53 | + for i in 0..num_segments { |
| 54 | + let segment = state |
| 55 | + .full_get_segment_text(i) |
| 56 | + .expect("failed to get segment"); |
| 57 | + let start_timestamp = state |
| 58 | + .full_get_segment_t0(i) |
| 59 | + .expect("failed to get start timestamp"); |
| 60 | + let end_timestamp = state |
| 61 | + .full_get_segment_t1(i) |
| 62 | + .expect("failed to get end timestamp"); |
| 63 | + println!("[{} - {}]: {}", start_timestamp, end_timestamp, segment); |
| 64 | + } |
| 65 | + println!("took {}ms", (et - st).as_millis()); |
| 66 | +} |
0 commit comments