use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Instant;
use crate::error::Error;
use crate::output::Segment;
pub const LANGUAGES: &[&str] = &[
"auto", "en", "zh", "de", "es", "ru", "ko", "fr", "ja", "pt", "tr", "pl", "ca", "nl", "ar",
"sv", "it", "id", "hi", "fi", "vi", "he", "uk", "el", "ms", "cs", "ro", "da", "hu", "ta", "no",
"th", "ur", "hr", "bg", "lt", "la", "mi", "ml", "cy", "sk", "te", "fa", "lv", "bn", "sr", "az",
"sl", "kn", "et", "mk", "br", "eu", "is", "hy", "ne", "mn", "bs", "kk", "sq", "sw", "gl", "mr",
"pa", "si", "km", "sn", "yo", "so", "af", "oc", "ka", "be", "tg", "sd", "gu", "am", "yi", "lo",
"uz", "fo", "ht", "ps", "tk", "nn", "mt", "sa", "lb", "my", "bo", "tl", "mg", "as", "tt",
"haw", "ln", "ha", "ba", "jw", "su", "yue",
];
pub fn is_known_language(code: &str) -> bool {
LANGUAGES.contains(&code)
}
#[derive(Debug, Clone, Copy)]
pub struct TranscribeRequest<'a> {
pub model_path: &'a Path,
pub samples: &'a [f32],
pub language: &'a str,
pub beam: i32,
pub device: u32,
pub deadline: Option<Instant>,
}
#[derive(Debug, Clone)]
pub struct EngineOutput {
pub text: String,
pub segments: Vec<Segment>,
pub language: String,
pub language_detected: bool,
pub cold_load: bool,
}
#[allow(dead_code)]
type ContextKey = (PathBuf, u32);
#[allow(dead_code)]
fn lock_through_poison<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(|e| e.into_inner())
}
#[allow(dead_code)]
fn load_or_reuse<T, E, F>(
cache: &Mutex<HashMap<ContextKey, Arc<T>>>,
key: ContextKey,
build: F,
) -> Result<(Arc<T>, bool), E>
where
F: FnOnce() -> Result<T, E>,
{
let mut guard = lock_through_poison(cache);
if let Some(found) = guard.get(&key) {
return Ok((Arc::clone(found), false));
}
let built = Arc::new(build()?);
guard.insert(key, Arc::clone(&built));
Ok((built, true))
}
#[cfg(all(feature = "stub-engine", any(feature = "cuda", feature = "cpu")))]
compile_error!("`stub-engine` cannot be combined with `cuda` or `cpu`");
pub const fn backend() -> &'static str {
#[cfg(feature = "cuda")]
{
"cuda"
}
#[cfg(feature = "cpu")]
{
"cpu"
}
#[cfg(feature = "stub-engine")]
{
"stub"
}
#[cfg(not(any(feature = "cuda", feature = "cpu", feature = "stub-engine")))]
{
"none"
}
}
pub fn transcribe(request: &TranscribeRequest<'_>) -> Result<EngineOutput, Error> {
#[cfg(any(feature = "cuda", feature = "cpu"))]
{
native(request)
}
#[cfg(feature = "stub-engine")]
{
stub(request)
}
#[cfg(not(any(feature = "cuda", feature = "cpu", feature = "stub-engine")))]
{
let _ = request;
Err(Error::EngineUnavailable)
}
}
#[cfg(feature = "stub-engine")]
static STUB_CONTEXTS: std::sync::OnceLock<Mutex<HashMap<ContextKey, Arc<()>>>> =
std::sync::OnceLock::new();
#[cfg(feature = "stub-engine")]
const STUB_COST_PER_SECOND: std::time::Duration = std::time::Duration::from_millis(20);
#[cfg(feature = "stub-engine")]
fn stub(request: &TranscribeRequest<'_>) -> Result<EngineOutput, Error> {
if !request.model_path.exists() {
return Err(Error::WhisperInference(format!(
"model not readable: {}",
request.model_path.display()
)));
}
let cache = STUB_CONTEXTS.get_or_init(|| Mutex::new(HashMap::new()));
let key = (request.model_path.to_path_buf(), request.device);
let (_ctx, cold_load) = load_or_reuse::<(), Error, _>(cache, key, || Ok(()))?;
let seconds = request.samples.len() as f64 / 16_000.0;
let slices = (seconds.ceil() as u64).max(1);
for _ in 0..slices {
if crate::signal::is_shutdown_requested() {
return Err(crate::signal::shutdown_error().unwrap_or(Error::Interrupted));
}
if request
.deadline
.map(|d| Instant::now() > d)
.unwrap_or(false)
{
return Err(Error::Timeout);
}
std::thread::sleep(STUB_COST_PER_SECOND);
}
let mut segments = Vec::new();
let mut cursor = 0.0f64;
while cursor < seconds {
let end = (cursor + 1.0).min(seconds);
segments.push(Segment {
start: cursor,
end,
text: format!("trecho de transcrição número {}", segments.len()),
});
cursor = end;
}
if segments.is_empty() {
segments.push(Segment {
start: 0.0,
end: seconds,
text: "trecho de transcrição número 0".to_string(),
});
}
let text = segments
.iter()
.map(|s| s.text.as_str())
.collect::<Vec<_>>()
.join("\n");
let (language, language_detected) = if request.language == "auto" {
("en".to_string(), true)
} else {
(request.language.to_string(), false)
};
Ok(EngineOutput {
text,
segments,
language,
language_detected,
cold_load,
})
}
pub fn silence_native_logs() {
#[cfg(any(feature = "cuda", feature = "cpu"))]
{
whisper_rs::install_logging_hooks();
}
}
#[cfg(any(feature = "cuda", feature = "cpu"))]
static CONTEXTS: std::sync::OnceLock<Mutex<HashMap<ContextKey, Arc<whisper_rs::WhisperContext>>>> =
std::sync::OnceLock::new();
#[cfg(any(feature = "cuda", feature = "cpu"))]
fn native(request: &TranscribeRequest<'_>) -> Result<EngineOutput, Error> {
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
let cache = CONTEXTS.get_or_init(|| Mutex::new(HashMap::new()));
let key = (request.model_path.to_path_buf(), request.device);
let (ctx, cold_load) = load_or_reuse(cache, key, || {
let mut ctx_params = WhisperContextParameters::default();
ctx_params.gpu_device(request.device as i32);
WhisperContext::new_with_params(request.model_path.display().to_string(), ctx_params)
.map_err(|e| Error::WhisperInference(e.to_string()))
})?;
let mut state = ctx
.create_state()
.map_err(|e| Error::WhisperInference(e.to_string()))?;
let mut params = if request.beam <= 1 {
FullParams::new(SamplingStrategy::Greedy { best_of: 1 })
} else {
FullParams::new(SamplingStrategy::BeamSearch {
beam_size: request.beam,
patience: -1.0,
})
};
params.set_print_progress(false);
params.set_print_special(false);
params.set_print_realtime(false);
params.set_print_timestamps(false);
if request.language != "auto" {
params.set_language(Some(request.language));
}
let deadline = request.deadline;
params.set_abort_callback_safe(move || {
crate::signal::is_shutdown_requested()
|| deadline.map(|d| Instant::now() > d).unwrap_or(false)
});
let inference = state.full(params, request.samples);
if let Some(d) = request.deadline {
if Instant::now() > d {
return Err(Error::Timeout);
}
}
if let Some(err) = crate::signal::shutdown_error() {
return Err(err);
}
inference.map_err(|e| Error::WhisperInference(e.to_string()))?;
let (language, language_detected) = if request.language == "auto" {
let id = state.full_lang_id_from_state();
match whisper_rs::get_lang_str(id) {
Some(code) => (code.to_string(), true),
None => (request.language.to_string(), false),
}
} else {
(request.language.to_string(), false)
};
let count = state.full_n_segments();
let mut segments = Vec::new();
let mut text = String::new();
for i in 0..count {
let Some(segment) = state.get_segment(i) else {
continue;
};
let body = segment
.to_str_lossy()
.map_err(|e| Error::WhisperInference(e.to_string()))?;
let body = body.trim();
if !text.is_empty() {
text.push('\n');
}
text.push_str(body);
segments.push(Segment {
start: segment.start_timestamp() as f64 / 100.0,
end: segment.end_timestamp() as f64 / 100.0,
text: body.to_string(),
});
}
Ok(EngineOutput {
text,
segments,
language,
language_detected,
cold_load,
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
fn request<'a>(samples: &'a [f32], path: &'a Path) -> TranscribeRequest<'a> {
TranscribeRequest {
model_path: path,
samples,
language: "auto",
beam: 5,
device: 0,
deadline: None,
}
}
#[test]
fn request_carries_device_and_deadline() {
let samples = [0.0f32; 4];
let path = Path::new("/nonexistent/model.bin");
let mut req = request(&samples, path);
req.device = 3;
req.deadline = Some(Instant::now() + Duration::from_secs(5));
assert_eq!(req.device, 3);
assert!(req.deadline.is_some());
}
#[test]
fn known_languages_cover_auto_and_portuguese() {
assert!(is_known_language("auto"));
assert!(is_known_language("pt"));
assert!(is_known_language("en"));
assert!(!is_known_language("xx"));
assert!(!is_known_language(""));
}
#[test]
fn language_list_has_no_duplicates() {
let mut sorted = LANGUAGES.to_vec();
sorted.sort_unstable();
let before = sorted.len();
sorted.dedup();
assert_eq!(before, sorted.len());
}
#[test]
fn only_the_building_pass_is_a_cold_load() {
use std::sync::atomic::{AtomicUsize, Ordering};
let cache: Mutex<HashMap<ContextKey, Arc<u32>>> = Mutex::new(HashMap::new());
let builds = AtomicUsize::new(0);
let key = (PathBuf::from("/models/small.bin"), 0u32);
let (first, cold) = load_or_reuse::<u32, Error, _>(&cache, key.clone(), || {
builds.fetch_add(1, Ordering::SeqCst);
Ok(7)
})
.expect("build");
assert!(cold, "the pass that builds must report a cold load");
let (again, cold) = load_or_reuse::<u32, Error, _>(&cache, key, || {
builds.fetch_add(1, Ordering::SeqCst);
Ok(9)
})
.expect("reuse");
assert!(!cold, "a reused context is not a cold load");
assert_eq!(
builds.load(Ordering::SeqCst),
1,
"weights load exactly once"
);
assert!(
Arc::ptr_eq(&first, &again),
"the same handle must be reused"
);
}
#[test]
fn each_model_and_device_is_its_own_context() {
let cache: Mutex<HashMap<ContextKey, Arc<u32>>> = Mutex::new(HashMap::new());
let small = (PathBuf::from("/models/small.bin"), 0u32);
let large = (PathBuf::from("/models/large.bin"), 0u32);
let second_card = (PathBuf::from("/models/small.bin"), 1u32);
for key in [small.clone(), large, second_card] {
let (_, cold) = load_or_reuse::<u32, Error, _>(&cache, key, || Ok(1)).expect("build");
assert!(cold, "a new key must load its own context");
}
let (_, cold) = load_or_reuse::<u32, Error, _>(&cache, small, || Ok(1)).expect("reuse");
assert!(!cold, "a known key must not reload");
assert_eq!(cache.lock().expect("cache").len(), 3);
}
#[test]
fn a_failed_build_is_not_remembered() {
let cache: Mutex<HashMap<ContextKey, Arc<u32>>> = Mutex::new(HashMap::new());
let key = (PathBuf::from("/models/missing.bin"), 0u32);
let err = load_or_reuse::<u32, Error, _>(&cache, key.clone(), || {
Err(Error::WhisperInference("no such model".into()))
})
.expect_err("must fail");
assert!(matches!(err, Error::WhisperInference(_)));
assert!(cache.lock().expect("cache").is_empty());
let (_, cold) = load_or_reuse::<u32, Error, _>(&cache, key, || Ok(1)).expect("retry");
assert!(cold, "a retry after a failed build still loads cold");
}
#[test]
fn a_parallel_wave_loads_the_weights_once() {
use std::sync::atomic::{AtomicUsize, Ordering};
let cache: Mutex<HashMap<ContextKey, Arc<u32>>> = Mutex::new(HashMap::new());
let builds = AtomicUsize::new(0);
let key = (PathBuf::from("/models/small.bin"), 0u32);
let colds: Vec<bool> = std::thread::scope(|scope| {
let handles: Vec<_> = (0..8)
.map(|_| {
let key = key.clone();
let cache = &cache;
let builds = &builds;
scope.spawn(move || {
load_or_reuse::<u32, Error, _>(cache, key, || {
builds.fetch_add(1, Ordering::SeqCst);
Ok(1)
})
.expect("load")
.1
})
})
.collect();
handles
.into_iter()
.map(|h| h.join().expect("join"))
.collect()
});
assert_eq!(builds.load(Ordering::SeqCst), 1, "eight workers, one load");
assert_eq!(
colds.iter().filter(|c| **c).count(),
1,
"exactly one worker may claim the cold load"
);
}
#[cfg(not(any(feature = "cuda", feature = "cpu", feature = "stub-engine")))]
#[test]
fn no_feature_is_engine_unavailable() {
let samples = [0.0f32; 4];
let path = Path::new("/nonexistent/model.bin");
let err = transcribe(&request(&samples, path)).expect_err("must refuse");
assert!(matches!(err, Error::EngineUnavailable));
assert_eq!(err.exit_code(), 70);
}
}