Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Whisper-repeat-penalty (#528)

Ezra Ellette929ed254447f
diff --git a/screenpipe-audio/src/audio_processing.rs b/screenpipe-audio/src/audio_processing.rs
index b2037bd68..16ab789f1 100644
--- a/screenpipe-audio/src/audio_processing.rs
+++ b/screenpipe-audio/src/audio_processing.rs
@@ -29,11 +29,9 @@ pub fn spectral_subtraction(audio: &[f32], d: f32) -> Result<Vec<f32>> {
let mut y = r2c.make_output_vec();
- let mut padded_audio: [f32; 1600] = [0.0; 1600];
+ let mut padded_audio = audio.to_vec();
- for i in 0..audio.len() {
- padded_audio[i] = audio[i];
- }
+ padded_audio.append(&mut vec![0.0f32; window_size - audio.len()]);
let mut indata = padded_audio;
r2c.process(&mut indata, &mut y)?;
diff --git a/screenpipe-audio/src/stt.rs b/screenpipe-audio/src/stt.rs
index bc0763a88..d08f36fa0 100644
--- a/screenpipe-audio/src/stt.rs
+++ b/screenpipe-audio/src/stt.rs
@@ -379,7 +379,7 @@ pub async fn stt(
// Deepgram implementation
let api_key = deepgram_api_key
.clone()
- .unwrap_or_else(|| get_deepgram_api_key());
+ .unwrap_or_else(get_deepgram_api_key);
info!(
"device: {}, using deepgram api key: {}...",
audio_input.device,
diff --git a/screenpipe-audio/src/vad_engine.rs b/screenpipe-audio/src/vad_engine.rs
index 604e13464..8b3251db8 100644
--- a/screenpipe-audio/src/vad_engine.rs
+++ b/screenpipe-audio/src/vad_engine.rs
@@ -19,9 +19,9 @@ pub enum VadSensitivity {
impl VadSensitivity {
pub fn min_speech_ratio(&self) -> f32 {
match self {
- VadSensitivity::Low => 0.01, // 1% of frames must be speech
+ VadSensitivity::Low => 0.01, // 1% of frames must be speech
VadSensitivity::Medium => 0.05, // 5% of frames must be speech
- VadSensitivity::High => 0.2, // 20% of frames must be speech
+ VadSensitivity::High => 0.2, // 20% of frames must be speech
}
}
}
@@ -91,7 +91,7 @@ impl VadEngine for WebRtcVad {
.map_err(|e| anyhow::anyhow!("WebRTC VAD error: {:?}", e))?;
if !result {
- return Ok(VadStatus::Silence)
+ return Ok(VadStatus::Silence);
}
Ok(VadStatus::Speech)
@@ -269,9 +269,7 @@ impl VadEngine for SileroVad {
}
match status {
- VadStatus::Unknown => {
- Ok(VadStatus::Unknown)
- },
+ VadStatus::Unknown => Ok(VadStatus::Unknown),
// this is super misleading
_ => Ok(VadStatus::Silence),
}
diff --git a/screenpipe-audio/src/whisper.rs b/screenpipe-audio/src/whisper.rs
index e8bdb649b..0e2854777 100644
--- a/screenpipe-audio/src/whisper.rs
+++ b/screenpipe-audio/src/whisper.rs
@@ -217,6 +217,8 @@ impl<'a> Decoder<'a> {
let mut sum_logprob = 0f64;
let mut last_token_was_timestamp = false;
+ let mut token_history = Vec::new(); // Track recent tokens
+
for i in 0..sample_len {
let tokens_t = Tensor::new(tokens.as_slice(), mel.device())?;
let tokens_t = tokens_t.unsqueeze(0)?;
@@ -247,13 +249,19 @@ impl<'a> Decoder<'a> {
} else {
logits
};
+
+ // Apply repetition penalty
+ let mut logits_v: Vec<f32> = logits.to_vec1()?;
+ apply_repetition_penalty(&mut logits_v, &token_history, 1.0); // Adjust penalty as needed
+
let next_token = if t > 0f64 {
- let prs = softmax(&(&logits / t)?, 0)?;
- let logits_v: Vec<f32> = prs.to_vec1()?;
- let distr = rand::distributions::WeightedIndex::new(&logits_v)?;
+ let logits_tensor = Tensor::new(logits_v.as_slice(), logits.device())?;
+ let scaled_logits = (&logits_tensor / t)?;
+ let prs = softmax(&scaled_logits, 0)?;
+ let prs_vec: Vec<f32> = prs.to_vec1()?;
+ let distr = rand::distributions::WeightedIndex::new(&prs_vec)?;
distr.sample(&mut self.rng) as u32
} else {
- let logits_v: Vec<f32> = logits.to_vec1()?;
logits_v
.iter()
.enumerate()
@@ -263,6 +271,7 @@ impl<'a> Decoder<'a> {
};
tokens.push(next_token);
+ token_history.push(next_token); // Add to history
let prob = softmax(&logits, candle::D::Minus1)?
.i(next_token as usize)?
@@ -402,4 +411,12 @@ pub enum Task {
Translate,
}
+fn apply_repetition_penalty(logits: &mut [f32], token_history: &[u32], penalty: f32) {
+ for &token in token_history {
+ if let Some(logit) = logits.get_mut(token as usize) {
+ *logit -= penalty;
+ }
+ }
+}
+
// ... (keep any other helper functions or structs that are specific to Whisper)