feat: declarative pipeline config, non-blocking Scene VLM, rust_asrx
Consolidated snapshot (previously WIP in working tree). - core/pipeline_config: new module loading config/pipeline_flows.json as the source of truth for stage execution mode (parallel/blocking) and dependencies - worker/job_worker: dispatch Scene VLM to a background task when the stage is declared parallel, unblocking the worker poll loop; resolve processor dependencies from pipeline_flows.json (fallback to built-in) - config/pipeline_flows.json: asrx depends on [cut, asr] - scripts/fix_stuck_jobs: fix UTC timezone comparison, raise default threshold to 6h, skip jobs with recent processor activity - rust_asrx: Rust speaker diarization CLI + processor wiring - processor metadata helper; fast_face_clustering script; file_progress pipeline API; scan API; VAD updates
This commit is contained in:
@@ -8,34 +8,31 @@ pub struct AudioReader {
|
||||
|
||||
impl AudioReader {
|
||||
pub fn from_wav(path: &str) -> Result<Self> {
|
||||
let reader = WavReader::open(path)
|
||||
.context("Failed to open WAV file")?;
|
||||
|
||||
let reader = WavReader::open(path).context("Failed to open WAV file")?;
|
||||
|
||||
let spec = reader.spec();
|
||||
let sample_rate = spec.sample_rate;
|
||||
|
||||
|
||||
// 轉換為 f32,範圍 [-1.0, 1.0]
|
||||
let samples: Vec<f32> = reader.into_samples::<i16>()
|
||||
.map(|s| {
|
||||
s.map(|v| v as f32 / 32768.0)
|
||||
.unwrap_or(0.0)
|
||||
})
|
||||
let samples: Vec<f32> = reader
|
||||
.into_samples::<i16>()
|
||||
.map(|s| s.map(|v| v as f32 / 32768.0).unwrap_or(0.0))
|
||||
.collect();
|
||||
|
||||
|
||||
Ok(Self {
|
||||
samples,
|
||||
sample_rate,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
pub fn samples(&self) -> &[f32] {
|
||||
&self.samples
|
||||
}
|
||||
|
||||
|
||||
pub fn sample_rate(&self) -> u32 {
|
||||
self.sample_rate
|
||||
}
|
||||
|
||||
|
||||
pub fn duration(&self) -> f32 {
|
||||
self.samples.len() as f32 / self.sample_rate as f32
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pub mod onnx;
|
||||
pub mod audio;
|
||||
pub mod onnx;
|
||||
pub mod segment;
|
||||
|
||||
pub use onnx::SileroVad;
|
||||
pub use audio::AudioReader;
|
||||
pub use onnx::SileroVad;
|
||||
pub use segment::SpeechSegment;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use anyhow::{Context, Result};
|
||||
use ort::session::{Session, builder::GraphOptimizationLevel};
|
||||
use ort::value::Value;
|
||||
use ndarray::IxDyn;
|
||||
use ort::session::{builder::GraphOptimizationLevel, Session};
|
||||
use ort::value::Value;
|
||||
|
||||
pub struct SileroVad {
|
||||
session: Session,
|
||||
state: Vec<f32>, // [2, 1, 128] = 256 elements
|
||||
context: Vec<f32>, // [1, 64] for 16kHz
|
||||
state: Vec<f32>, // [2, 1, 128] = 256 elements
|
||||
context: Vec<f32>, // [1, 64] for 16kHz
|
||||
sample_rate: i64,
|
||||
context_size: usize, // 64 for 16kHz, 32 for 8kHz
|
||||
context_size: usize, // 64 for 16kHz, 32 for 8kHz
|
||||
}
|
||||
|
||||
impl SileroVad {
|
||||
@@ -18,81 +18,82 @@ impl SileroVad {
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set optimization level: {}", e))?
|
||||
.commit_from_file(model_path)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to load model: {}", e))?;
|
||||
|
||||
|
||||
let sample_rate = 16000;
|
||||
let context_size = if sample_rate == 16000 { 64 } else { 32 };
|
||||
|
||||
|
||||
Ok(Self {
|
||||
session,
|
||||
state: vec![0.0; 256], // [2, 1, 128] = 256
|
||||
context: vec![0.0; context_size], // [1, 64]
|
||||
state: vec![0.0; 256], // [2, 1, 128] = 256
|
||||
context: vec![0.0; context_size], // [1, 64]
|
||||
sample_rate,
|
||||
context_size,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
pub fn detect(&mut self, audio_chunk: &[f32]) -> Result<f32> {
|
||||
// 拼接 context 和當前塊
|
||||
let mut input_with_context = Vec::with_capacity(self.context_size + audio_chunk.len());
|
||||
input_with_context.extend_from_slice(&self.context);
|
||||
input_with_context.extend_from_slice(audio_chunk);
|
||||
|
||||
|
||||
// 準備輸入
|
||||
let input_array = ndarray::Array::from_shape_vec(
|
||||
IxDyn(&[1, input_with_context.len()]),
|
||||
input_with_context.clone()
|
||||
).context("Failed to create input array")?;
|
||||
|
||||
let state_array = ndarray::Array::from_shape_vec(
|
||||
IxDyn(&[2, 1, 128]),
|
||||
self.state.clone()
|
||||
).context("Failed to create state array")?;
|
||||
|
||||
input_with_context.clone(),
|
||||
)
|
||||
.context("Failed to create input array")?;
|
||||
|
||||
let state_array = ndarray::Array::from_shape_vec(IxDyn(&[2, 1, 128]), self.state.clone())
|
||||
.context("Failed to create state array")?;
|
||||
|
||||
let sr_array = ndarray::Array::from_shape_vec(
|
||||
IxDyn(&[]), // 標量
|
||||
vec![self.sample_rate]
|
||||
).context("Failed to create sr array")?;
|
||||
|
||||
IxDyn(&[]), // 標量
|
||||
vec![self.sample_rate],
|
||||
)
|
||||
.context("Failed to create sr array")?;
|
||||
|
||||
// 創建 ONNX Value
|
||||
let input_value = Value::from_array(input_array)
|
||||
.context("Failed to create input value")?;
|
||||
let state_value = Value::from_array(state_array)
|
||||
.context("Failed to create state value")?;
|
||||
let sr_value = Value::from_array(sr_array)
|
||||
.context("Failed to create sr value")?;
|
||||
|
||||
let input_value = Value::from_array(input_array).context("Failed to create input value")?;
|
||||
let state_value = Value::from_array(state_array).context("Failed to create state value")?;
|
||||
let sr_value = Value::from_array(sr_array).context("Failed to create sr value")?;
|
||||
|
||||
// 運行推理
|
||||
let inputs = ort::inputs![
|
||||
"input" => input_value,
|
||||
"state" => state_value,
|
||||
"sr" => sr_value,
|
||||
];
|
||||
|
||||
let outputs = self.session.run(inputs)
|
||||
|
||||
let outputs = self
|
||||
.session
|
||||
.run(inputs)
|
||||
.context("Failed to run inference")?;
|
||||
|
||||
|
||||
// 提取輸出
|
||||
let output = outputs["output"].try_extract_tensor::<f32>()
|
||||
let output = outputs["output"]
|
||||
.try_extract_tensor::<f32>()
|
||||
.context("Failed to extract output")?;
|
||||
|
||||
|
||||
// output 是 (&Shape, &[f32]) tuple
|
||||
let output_data = output.1;
|
||||
let speech_prob = output_data[0];
|
||||
|
||||
|
||||
// 更新隱藏狀態
|
||||
let state_n = outputs["stateN"].try_extract_tensor::<f32>()
|
||||
let state_n = outputs["stateN"]
|
||||
.try_extract_tensor::<f32>()
|
||||
.context("Failed to extract stateN")?;
|
||||
|
||||
|
||||
// state_n 是 (&Shape, &[f32]) tuple
|
||||
let state_data = state_n.1;
|
||||
self.state = state_data.to_vec();
|
||||
|
||||
|
||||
// 更新 context(當前輸入的最後 context_size 個樣本)
|
||||
self.context = input_with_context[input_with_context.len() - self.context_size..].to_vec();
|
||||
|
||||
|
||||
Ok(speech_prob)
|
||||
}
|
||||
|
||||
|
||||
pub fn reset_state(&mut self) {
|
||||
self.state = vec![0.0; 256];
|
||||
self.context = vec![0.0; self.context_size];
|
||||
|
||||
@@ -10,7 +10,7 @@ impl SpeechSegment {
|
||||
pub fn new(start_sec: f32, end_sec: f32) -> Self {
|
||||
Self { start_sec, end_sec }
|
||||
}
|
||||
|
||||
|
||||
pub fn duration(&self) -> f32 {
|
||||
self.end_sec - self.start_sec
|
||||
}
|
||||
@@ -36,7 +36,7 @@ impl SegmentCollector {
|
||||
silence_start: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn process(&mut self, timestamp: f32, speech_prob: f32) {
|
||||
if speech_prob >= self.threshold {
|
||||
// 語音
|
||||
@@ -50,7 +50,7 @@ impl SegmentCollector {
|
||||
if self.silence_start.is_none() {
|
||||
self.silence_start = Some(timestamp);
|
||||
}
|
||||
|
||||
|
||||
// 檢查是否結束語音段
|
||||
if let Some(silence_start) = self.silence_start {
|
||||
let silence_duration = timestamp - silence_start;
|
||||
@@ -69,7 +69,7 @@ impl SegmentCollector {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn finalize(&mut self, end_timestamp: f32) {
|
||||
// 處理最後一個段
|
||||
if let Some(start) = self.current_start {
|
||||
@@ -79,7 +79,7 @@ impl SegmentCollector {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn segments(&self) -> &[SpeechSegment] {
|
||||
&self.segments
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user