Merge pull request #100 from codesoda/whispercpp-fixes

Expose WhisperContextParameters and update examples
This commit is contained in:
Niko 2023-11-20 22:24:57 +00:00 committed by GitHub
commit 2a17adde85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 9 deletions

View file

@ -15,13 +15,16 @@ cargo run --example audio_transcription
``` ```
```rust ```rust
use whisper_rs::{WhisperContext, FullParams, SamplingStrategy}; use whisper_rs::{WhisperContext, WhisperContextParameters, FullParams, SamplingStrategy};
fn main() { fn main() {
let path_to_model = std::env::args().nth(1).unwrap(); let path_to_model = std::env::args().nth(1).unwrap();
// load a context and model // load a context and model
let ctx = WhisperContext::new(&path_to_model).expect("failed to load model"); let ctx = WhisperContext::new_with_params(
path_to_model,
WhisperContextParameters::default()
).expect("failed to load model");
// create a params object // create a params object
let params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 }); let params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 });

View file

@ -4,13 +4,16 @@
use hound; use hound;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext}; use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
/// Loads a context and model, processes an audio file, and prints the resulting transcript to stdout. /// Loads a context and model, processes an audio file, and prints the resulting transcript to stdout.
fn main() -> Result<(), &'static str> { fn main() -> Result<(), &'static str> {
// Load a context and model. // Load a context and model.
let ctx = WhisperContext::new("example/path/to/model/whisper.cpp/models/ggml-base.en.bin") let ctx = WhisperContext::new_with_params(
.expect("failed to load model"); "example/path/to/model/whisper.cpp/models/ggml-base.en.bin",
WhisperContextParameters::default(),
)
.expect("failed to load model");
// Create a state // Create a state
let mut state = ctx.create_state().expect("failed to create key"); let mut state = ctx.create_state().expect("failed to create key");

View file

@ -1,13 +1,14 @@
#![allow(clippy::uninlined_format_args)] #![allow(clippy::uninlined_format_args)]
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext}; use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
// note that running this example will not do anything, as it is just a // note that running this example will not do anything, as it is just a
// demonstration of how to use the library, and actual usage requires // demonstration of how to use the library, and actual usage requires
// more dependencies than the base library. // more dependencies than the base library.
pub fn usage() -> Result<(), &'static str> { pub fn usage() -> Result<(), &'static str> {
// load a context and model // load a context and model
let ctx = WhisperContext::new("path/to/model").expect("failed to load model"); let ctx = WhisperContext::new_with_params("path/to/model", WhisperContextParameters::default())
.expect("failed to load model");
// make a state // make a state
let mut state = ctx.create_state().expect("failed to create state"); let mut state = ctx.create_state().expect("failed to create state");

View file

@ -2,7 +2,7 @@
use hound::{SampleFormat, WavReader}; use hound::{SampleFormat, WavReader};
use std::path::Path; use std::path::Path;
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext}; use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
fn parse_wav_file(path: &Path) -> Vec<i16> { fn parse_wav_file(path: &Path) -> Vec<i16> {
let reader = WavReader::open(path).expect("failed to read file"); let reader = WavReader::open(path).expect("failed to read file");
@ -45,7 +45,10 @@ fn main() {
let original_samples = parse_wav_file(audio_path); let original_samples = parse_wav_file(audio_path);
let samples = whisper_rs::convert_integer_to_float_audio(&original_samples); let samples = whisper_rs::convert_integer_to_float_audio(&original_samples);
let ctx = WhisperContext::new(&whisper_path.to_string_lossy()).expect("failed to open model"); let ctx = WhisperContext::new_with_params(
&whisper_path.to_string_lossy(),
WhisperContextParameters::default()
).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::default());
params.set_progress_callback_safe(|progress| println!("Progress callback: {}%", progress)); params.set_progress_callback_safe(|progress| println!("Progress callback: {}%", progress));

View file

@ -13,6 +13,7 @@ pub use error::WhisperError;
pub use standalone::*; pub use standalone::*;
pub use utilities::*; pub use utilities::*;
pub use whisper_ctx::WhisperContext; pub use whisper_ctx::WhisperContext;
pub use whisper_ctx::WhisperContextParameters;
pub use whisper_params::{FullParams, SamplingStrategy}; pub use whisper_params::{FullParams, SamplingStrategy};
pub use whisper_state::WhisperState; pub use whisper_state::WhisperState;