Compare commits
9 Commits
0.2.5
...
b08e055dac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b08e055dac | ||
|
|
9d993cfc8a | ||
|
|
f4f8ea429a | ||
|
|
38bea3735f | ||
|
|
1c48026690 | ||
|
|
a57312786a | ||
|
|
55e3bf0a26 | ||
|
|
743c8c5f67 | ||
|
|
d32f7a4c40 |
@@ -24,6 +24,7 @@ pub fn wav_channel<F>(
|
|||||||
handler_fn: F
|
handler_fn: F
|
||||||
) where F: FnMut(Result<rust_whisper_lib::Transcribed, String>) + Send + 'static {
|
) where F: FnMut(Result<rust_whisper_lib::Transcribed, String>) + Send + 'static {
|
||||||
flags.model_path = None;
|
flags.model_path = None;
|
||||||
|
flags.model_buffer = Some(include_bytes!("../../models/ggml-distil-medium.en.bin").to_vec());
|
||||||
flags.model_buffer = Some(include_bytes!("../../models/ggml-base.en.bin").to_vec());
|
flags.model_buffer = Some(include_bytes!("../../models/ggml-base.en.bin").to_vec());
|
||||||
rust_whisper_lib::wav_channel(flags, handler_fn);
|
rust_whisper_lib::wav_channel(flags, handler_fn);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,51 +84,64 @@ fn channel(flags: rust_whisper_lib::Flags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Destutterer {
|
struct Destutterer {
|
||||||
prev: Option<String>,
|
prevs: Vec<Word>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_destutterer() -> Destutterer {
|
fn new_destutterer() -> Destutterer {
|
||||||
Destutterer{prev: None}
|
Destutterer{prevs: vec![]}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Destutterer {
|
impl Destutterer {
|
||||||
fn step(&mut self, next: String) -> String {
|
fn step(&mut self, next: String) -> String {
|
||||||
let next = next.trim().to_string();
|
|
||||||
if next.len() == 0 {
|
if next.len() == 0 {
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
match &self.prev {
|
|
||||||
None => {
|
let nexts = Word::from_string(next.clone());
|
||||||
self.prev = Some(next.clone());
|
|
||||||
next
|
let mut n = self.prevs.len().clamp(0, nexts.len());
|
||||||
},
|
while n > 0 {
|
||||||
Some(prev) => {
|
let prev_s = Word::to_comparable_string(self.prevs[self.prevs.len() - n..].to_vec());
|
||||||
let without_trailing_punctuation = {
|
let next_s = Word::to_comparable_string(nexts[..n].to_vec());
|
||||||
let mut next = next.clone();
|
if prev_s == next_s {
|
||||||
while next.ends_with("?") || next.ends_with(".") {
|
break;
|
||||||
next = next[..next.len()-1].to_string();
|
}
|
||||||
}
|
n -= 1;
|
||||||
next
|
|
||||||
};
|
|
||||||
let trailing_punctuation = next[without_trailing_punctuation.len() ..].to_string();
|
|
||||||
let next = without_trailing_punctuation;
|
|
||||||
let next = {
|
|
||||||
let mut n = prev.len().clamp(0, next.len());
|
|
||||||
while n > 0 {
|
|
||||||
if prev[prev.len() - n..] == next[..n] {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
n -= 1;
|
|
||||||
}
|
|
||||||
next[n..].to_string()
|
|
||||||
};
|
|
||||||
if next.len() == 0 {
|
|
||||||
return "".to_string();
|
|
||||||
}
|
|
||||||
self.prev = Some(next.clone());
|
|
||||||
next + &trailing_punctuation
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
self.prevs = nexts.clone();
|
||||||
|
Word::to_string(nexts[n..].to_vec())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Word {
|
||||||
|
raw: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Word {
|
||||||
|
fn from_string(s: String) -> Vec<Word> {
|
||||||
|
let mut result = vec![];
|
||||||
|
for word in s.split(" ") {
|
||||||
|
let word = word.trim();
|
||||||
|
if word.len() > 0 {
|
||||||
|
result.push(Word{raw: word.to_string()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_comparable_string(v: Vec<Word>) -> String {
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x.raw.chars().filter(|c| c.is_ascii_alphanumeric()).collect())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_string(v: Vec<Word>) -> String {
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x.raw.clone())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,11 +150,19 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_destutterer() {
|
fn test_destutterer_punctuation() {
|
||||||
let mut w = new_destutterer();
|
let mut w = new_destutterer();
|
||||||
assert_eq!("abcde".to_string(), w.step("abcde".to_string()));
|
assert_eq!("a, b. c? d!".to_string(), w.step("a, b. c? d!".to_string()));
|
||||||
assert_eq!("fg".to_string(), w.step("cdefg".to_string()));
|
assert_eq!("e! f g".to_string(), w.step("d, e! f g".to_string()));
|
||||||
assert_eq!("hij".to_string(), w.step("fghij".to_string()));
|
assert_eq!("hij".to_string(), w.step("f g hij".to_string()));
|
||||||
assert_eq!("fghij".to_string(), w.step("fghij".to_string()));
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_destutterer_letters() {
|
||||||
|
let mut w = new_destutterer();
|
||||||
|
assert_eq!("a b c d e".to_string(), w.step("a b c d e".to_string()));
|
||||||
|
assert_eq!("f g".to_string(), w.step(" c d e f g".to_string()));
|
||||||
|
assert_eq!("h i j".to_string(), w.step("f g h i j ".to_string()));
|
||||||
|
assert_eq!("a g h i j".to_string(), w.step("a g h i j".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ pub struct Flags {
|
|||||||
#[arg(long, default_value = "8")]
|
#[arg(long, default_value = "8")]
|
||||||
pub threads: i32,
|
pub threads: i32,
|
||||||
|
|
||||||
#[arg(long, default_value = "5")]
|
#[arg(long, default_value = "30")]
|
||||||
pub stream_step: u64,
|
pub stream_step: u64,
|
||||||
#[arg(long, default_value = "0.6")]
|
#[arg(long, default_value = "28.0")]
|
||||||
pub stream_retain: f32,
|
pub stream_retain: f32,
|
||||||
#[arg(long, default_value = "0.3")]
|
#[arg(long, default_value = "0.1")]
|
||||||
pub stream_head: f32,
|
pub stream_head: f32,
|
||||||
#[arg(long, default_value = "0.3")]
|
#[arg(long, default_value = "0.1")]
|
||||||
pub stream_tail: f32,
|
pub stream_tail: f32,
|
||||||
|
|
||||||
#[arg(long, default_value = "false")]
|
#[arg(long, default_value = "false")]
|
||||||
|
|||||||
Reference in New Issue
Block a user