Fix up full_usage example
This commit is contained in:
parent
a9dea32c81
commit
527bb059dc
1 changed files with 22 additions and 27 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
#![allow(clippy::uninlined_format_args)]
|
#![allow(clippy::uninlined_format_args)]
|
||||||
|
|
||||||
use hound::{SampleFormat, WavReader};
|
use hound::{SampleFormat, WavReader};
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
|
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
|
||||||
|
|
||||||
fn parse_wav_file(path: &Path) -> Vec<i16> {
|
fn parse_wav_file(path: PathBuf) -> Vec<i16> {
|
||||||
let reader = WavReader::open(path).expect("failed to read file");
|
let reader = WavReader::open(path).expect("failed to read file");
|
||||||
|
|
||||||
if reader.spec().channels != 1 {
|
if reader.spec().channels != 1 {
|
||||||
|
|
@ -27,20 +27,22 @@ fn parse_wav_file(path: &Path) -> Vec<i16> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let arg1 = std::env::args()
|
let whisper_path = PathBuf::from(
|
||||||
.nth(1)
|
std::env::args()
|
||||||
.expect("first argument should be path to WAV file");
|
.nth(1)
|
||||||
let audio_path = Path::new(&arg1);
|
.expect("first argument should be path to audio file"),
|
||||||
if !audio_path.exists() {
|
);
|
||||||
panic!("audio file doesn't exist");
|
|
||||||
}
|
|
||||||
let arg2 = std::env::args()
|
|
||||||
.nth(2)
|
|
||||||
.expect("second argument should be path to Whisper model");
|
|
||||||
let whisper_path = Path::new(&arg2);
|
|
||||||
if !whisper_path.exists() {
|
if !whisper_path.exists() {
|
||||||
panic!("whisper file doesn't exist")
|
panic!("whisper file doesn't exist")
|
||||||
}
|
}
|
||||||
|
let audio_path = PathBuf::from(
|
||||||
|
std::env::args()
|
||||||
|
.nth(2)
|
||||||
|
.expect("second argument should be path to whisper model file"),
|
||||||
|
);
|
||||||
|
if !audio_path.exists() {
|
||||||
|
panic!("audio file doesn't exist");
|
||||||
|
}
|
||||||
|
|
||||||
let original_samples = parse_wav_file(audio_path);
|
let original_samples = parse_wav_file(audio_path);
|
||||||
let mut samples = vec![0.0f32; original_samples.len()];
|
let mut samples = vec![0.0f32; original_samples.len()];
|
||||||
|
|
@ -53,7 +55,10 @@ fn main() {
|
||||||
)
|
)
|
||||||
.expect("failed to open model");
|
.expect("failed to open model");
|
||||||
let mut state = ctx.create_state().expect("failed to create key");
|
let mut state = ctx.create_state().expect("failed to create key");
|
||||||
let mut params = FullParams::new(SamplingStrategy::default());
|
let mut params = FullParams::new(SamplingStrategy::BeamSearch {
|
||||||
|
beam_size: 5,
|
||||||
|
patience: -1.0,
|
||||||
|
});
|
||||||
params.set_initial_prompt("experience");
|
params.set_initial_prompt("experience");
|
||||||
params.set_progress_callback_safe(|progress| println!("Progress callback: {}%", progress));
|
params.set_progress_callback_safe(|progress| println!("Progress callback: {}%", progress));
|
||||||
|
|
||||||
|
|
@ -63,19 +68,9 @@ fn main() {
|
||||||
.expect("failed to convert samples");
|
.expect("failed to convert samples");
|
||||||
let et = std::time::Instant::now();
|
let et = std::time::Instant::now();
|
||||||
|
|
||||||
let num_segments = state
|
for segment in state.as_iter() {
|
||||||
.full_n_segments()
|
let start_timestamp = segment.start_timestamp();
|
||||||
.expect("failed to get number of segments");
|
let end_timestamp = segment.end_timestamp();
|
||||||
for i in 0..num_segments {
|
|
||||||
let segment = state
|
|
||||||
.full_get_segment_text(i)
|
|
||||||
.expect("failed to get segment");
|
|
||||||
let start_timestamp = state
|
|
||||||
.full_get_segment_t0(i)
|
|
||||||
.expect("failed to get start timestamp");
|
|
||||||
let end_timestamp = state
|
|
||||||
.full_get_segment_t1(i)
|
|
||||||
.expect("failed to get end timestamp");
|
|
||||||
println!("[{} - {}]: {}", start_timestamp, end_timestamp, segment);
|
println!("[{} - {}]: {}", start_timestamp, end_timestamp, segment);
|
||||||
}
|
}
|
||||||
println!("took {}ms", (et - st).as_millis());
|
println!("took {}ms", (et - st).as_millis());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue