Skip to content

Commit

Permalink
Merge pull request #100 from codesoda/whispercpp-fixes
Browse files Browse the repository at this point in the history
Expose WhisperContextParameters and update examples
  • Loading branch information
tazz4843 authored Nov 20, 2023
2 parents bd197a3 + 81e2b04 commit 2a17add
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ cargo run --example audio_transcription
```

```rust
use whisper_rs::{WhisperContext, FullParams, SamplingStrategy};
use whisper_rs::{WhisperContext, WhisperContextParameters, FullParams, SamplingStrategy};

fn main() {
let path_to_model = std::env::args().nth(1).unwrap();

// 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
let params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 });
Expand Down
9 changes: 6 additions & 3 deletions examples/audio_transcription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
use hound;
use std::fs::File;
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.
fn main() -> Result<(), &'static str> {
// Load a context and model.
let ctx = WhisperContext::new("example/path/to/model/whisper.cpp/models/ggml-base.en.bin")
.expect("failed to load model");
let ctx = WhisperContext::new_with_params(
"example/path/to/model/whisper.cpp/models/ggml-base.en.bin",
WhisperContextParameters::default(),
)
.expect("failed to load model");
// Create a state
let mut state = ctx.create_state().expect("failed to create key");

Expand Down
5 changes: 3 additions & 2 deletions examples/basic_use.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![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
// demonstration of how to use the library, and actual usage requires
// more dependencies than the base library.
pub fn usage() -> Result<(), &'static str> {
// 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
let mut state = ctx.create_state().expect("failed to create state");

Expand Down
7 changes: 5 additions & 2 deletions examples/full_usage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use hound::{SampleFormat, WavReader};
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> {
let reader = WavReader::open(path).expect("failed to read file");
Expand Down Expand Up @@ -45,7 +45,10 @@ fn main() {
let original_samples = parse_wav_file(audio_path);
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 params = FullParams::new(SamplingStrategy::default());
params.set_progress_callback_safe(|progress| println!("Progress callback: {}%", progress));
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use error::WhisperError;
pub use standalone::*;
pub use utilities::*;
pub use whisper_ctx::WhisperContext;
pub use whisper_ctx::WhisperContextParameters;
pub use whisper_params::{FullParams, SamplingStrategy};
pub use whisper_state::WhisperState;

Expand Down

0 comments on commit 2a17add

Please sign in to comment.