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
This commit is contained in:
Accusys
2026-09-26 01:04:32 +08:00
parent e283b477d2
commit 89e7a579bf
+114 -8
View File
@@ -62,12 +62,86 @@ pub async fn process_asr(
video_path: &str,
output_path: &str,
uuid: Option<&str>,
) -> Result<AsrResult> {
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<AsrResult> {
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<String>,
segments: Vec<WhisperSegment>,
}
#[derive(Debug, Deserialize)]
struct WhisperSegment {
start: f64,
end: f64,
text: String,
#[serde(default)]
avg_logprob: Option<f64>,
}
fn convert_whisper_to_asr(whisper: WhisperJsonOutput) -> AsrResult {
let segments: Vec<AsrSegment> = 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::*;