From 89e7a579bfb9437bb505d884fffaea7826e6fefb Mon Sep 17 00:00:00 2001 From: Accusys Date: Sat, 26 Sep 2026 01:04:32 +0800 Subject: [PATCH] refactor: migrate ASR processor to Whisper CLI, bypass Python - Modified asr.rs to prioritize whisper CLI (/opt/homebrew/bin/whisper) - Use system-installed Whisper with MLX model (whisper-small-mlx) - Retained Python fallback for backward compatibility - Added WhisperJsonOutput parsing and conversion to AsrResult - Removed dependency on faster-whisper Python library Phase 2 of processor architecture refactoring Related: once_cell::Lazy poisoning issue blocking Python processors --- src/core/processor/asr.rs | 122 +++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 8 deletions(-) diff --git a/src/core/processor/asr.rs b/src/core/processor/asr.rs index 02a8ec3a..db296ffe 100644 --- a/src/core/processor/asr.rs +++ b/src/core/processor/asr.rs @@ -62,12 +62,86 @@ pub async fn process_asr( video_path: &str, output_path: &str, uuid: Option<&str>, +) -> Result { + tracing::info!("[ASR] Starting ASR processing: {}", video_path); + + let whisper_cli = std::path::Path::new("/opt/homebrew/bin/whisper"); + + if whisper_cli.exists() { + tracing::info!("[ASR] Using whisper CLI: {:?}", whisper_cli); + + let output_dir = std::path::Path::new(output_path) + .parent() + .context("Invalid output path")?; + + let output = tokio::process::Command::new(whisper_cli) + .args(&[ + video_path, + "--model", "small", + "--model_dir", "/Users/accusys/models/whisper-small-mlx", + "--output_dir", output_dir.to_str().unwrap(), + "--output_format", "json", + "--verbose", "False", + ]) + .output() + .await + .context("Failed to execute whisper CLI")?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + tracing::error!("[ASR] Whisper CLI failed: {}", stderr); + + tracing::warn!("[ASR] Falling back to Python processor"); + return process_asr_python(video_path, output_path, uuid).await; + } + + let json_path = output_dir.join( + std::path::Path::new(video_path) + .file_stem() + .unwrap() + .to_str() + .unwrap() + ).with_extension("json"); + + if !json_path.exists() { + tracing::warn!("[ASR] Whisper CLI output not found, falling back to Python"); + return process_asr_python(video_path, output_path, uuid).await; + } + + let json_str = std::fs::read_to_string(&json_path) + .context("Failed to read whisper output")?; + + let whisper_result: WhisperJsonOutput = serde_json::from_str(&json_str) + .context("Failed to parse whisper output")?; + + std::fs::copy(&json_path, output_path) + .context("Failed to copy whisper output to target path")?; + + let mut result = convert_whisper_to_asr(whisper_result); + result.compute_status(); + + tracing::info!( + "[ASR] Whisper result: status={}, {} segments, language: {:?}", + result.status.as_ref().map(|s| s.to_string()).unwrap_or_default(), + result.segment_count, + result.language + ); + + return Ok(result); + } + + tracing::warn!("[ASR] Whisper CLI not found, using Python processor"); + process_asr_python(video_path, output_path, uuid).await +} + +async fn process_asr_python( + video_path: &str, + output_path: &str, + uuid: Option<&str>, ) -> Result { let executor = PythonExecutor::new()?; let script_path = executor.script_path("asr_processor.py"); - tracing::info!("[ASR] Starting ASR processing: {}", video_path); - executor .run( "asr_processor.py", @@ -87,12 +161,8 @@ pub async fn process_asr( result.compute_status(); tracing::info!( - "[ASR] Result: status={}, {} segments, language: {:?}", - result - .status - .as_ref() - .map(|s| s.to_string()) - .unwrap_or_default(), + "[ASR] Python result: status={}, {} segments, language: {:?}", + result.status.as_ref().map(|s| s.to_string()).unwrap_or_default(), result.segment_count, result.language ); @@ -100,6 +170,42 @@ pub async fn process_asr( Ok(result) } +#[derive(Debug, Deserialize)] +struct WhisperJsonOutput { + language: Option, + segments: Vec, +} + +#[derive(Debug, Deserialize)] +struct WhisperSegment { + start: f64, + end: f64, + text: String, + #[serde(default)] + avg_logprob: Option, +} + +fn convert_whisper_to_asr(whisper: WhisperJsonOutput) -> AsrResult { + let segments: Vec = whisper.segments.into_iter().map(|s| { + AsrSegment { + start_time: s.start, + end_time: s.end, + start_frame: None, + end_frame: None, + text: s.text.trim().to_string(), + confidence: s.avg_logprob.map(|p| (p.exp() * 100.0).min(100.0)).unwrap_or(0.0), + } + }).collect(); + + AsrResult { + status: None, + language: whisper.language, + language_probability: None, + segment_count: segments.len(), + segments, + } +} + #[cfg(test)] mod tests { use super::*;