Skip to content

Commit

Permalink
Merge pull request #163 from arizhih/fix-lang-detect
Browse files Browse the repository at this point in the history
fix: lang_detect not working properly
  • Loading branch information
tazz4843 authored Jul 17, 2024
2 parents 7634d31 + bbdc8a0 commit 744804a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exclude = ["examples/full_usage"]

[package]
name = "whisper-rs"
version = "0.12.0"
version = "0.12.1"
edition = "2021"
description = "Rust bindings for whisper.cpp"
license = "Unlicense"
Expand All @@ -14,7 +14,7 @@ repository = "https://github.com/tazz4843/whisper-rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
whisper-rs-sys = { path = "sys", version = "0.10.0" }
whisper-rs-sys = { path = "sys", version = "0.10.1" }
log = { version = "0.4", optional = true }
tracing = { version = "0.1", optional = true }

Expand Down
30 changes: 10 additions & 20 deletions src/whisper_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,16 @@ impl WhisperState {
/// * n_threads: How many threads to use. Defaults to 1. Must be at least 1, returns an error otherwise.
///
/// # Returns
/// `Ok(Vec<f32>)` on success, `Err(WhisperError)` on failure.
/// `Ok((i32, Vec<f32>))` on success where the i32 is detected language id and Vec<f32>
/// is array with the probabilities of all languages, `Err(WhisperError)` on failure.
///
/// # C++ equivalent
/// `int whisper_lang_auto_detect(struct whisper_context * ctx, int offset_ms, int n_threads, float * lang_probs)`
pub fn lang_detect(&self, offset_ms: usize, threads: usize) -> Result<Vec<f32>, WhisperError> {
pub fn lang_detect(
&self,
offset_ms: usize,
threads: usize,
) -> Result<(i32, Vec<f32>), WhisperError> {
if threads < 1 {
return Err(WhisperError::InvalidThreadCount);
}
Expand All @@ -244,25 +249,10 @@ impl WhisperState {
lang_probs.as_mut_ptr(),
)
};
if ret == -1 {
Err(WhisperError::UnableToCalculateEvaluation)
if ret < 0 {
Err(WhisperError::GenericError(ret))
} else {
assert_eq!(
ret as usize,
lang_probs.len(),
"lang_probs length mismatch: this is a bug in whisper.cpp"
);
// if we're still running, double check that the length is correct, otherwise print to stderr
// and abort, as this will cause Undefined Behavior
// might get here due to the unwind being caught by a user-installed panic handler
if lang_probs.len() != ret as usize {
eprintln!(
"lang_probs length mismatch: this is a bug in whisper.cpp, \
aborting to avoid Undefined Behavior"
);
std::process::abort();
}
Ok(lang_probs)
Ok((ret as i32, lang_probs))
}
}

Expand Down
2 changes: 1 addition & 1 deletion sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whisper-rs-sys"
version = "0.10.0"
version = "0.10.1"
edition = "2021"
description = "Rust bindings for whisper.cpp (FFI bindings)"
license = "Unlicense"
Expand Down
1 change: 1 addition & 0 deletions sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ fn main() {
// debug builds are too slow to even remotely be usable,
// so we build with optimizations even in debug mode
config.define("CMAKE_BUILD_TYPE", "RelWithDebInfo");
config.cxxflag("-DWHISPER_DEBUG");
}

// Allow passing any WHISPER or CMAKE compile flags
Expand Down

0 comments on commit 744804a

Please sign in to comment.