Compare commits
9 Commits
d05287fa3d
...
v0.1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11b5091872 | ||
|
|
03370f362e | ||
|
|
ec6a71d38c | ||
|
|
1b96b132e1 | ||
|
|
839487b99e | ||
|
|
a2fee32fbc | ||
|
|
091958e08d | ||
|
|
5f47b2c88b | ||
|
|
367838ac23 |
7
models/testme/Cargo.lock
generated
Normal file
7
models/testme/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "testme"
|
||||||
|
version = "0.1.0"
|
||||||
8
models/testme/Cargo.toml
Normal file
8
models/testme/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "testme"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
4
models/testme/src/main.rs
Normal file
4
models/testme/src/main.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
fn main() {
|
||||||
|
let bytes = include_bytes!("./test.txt");
|
||||||
|
println!("{}", String::from_utf8_lossy(bytes));
|
||||||
|
}
|
||||||
1
models/testme/src/test.txt
Symbolic link
1
models/testme/src/test.txt
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../../ggml-tiny.en.bin
|
||||||
139
src/main.rs
139
src/main.rs
@@ -34,11 +34,21 @@ struct Flags {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let flags = Flags::parse();
|
let flags = Flags::parse();
|
||||||
|
|
||||||
let handler = Handler{
|
let w = new_whisper_service(
|
||||||
head: flags.stream_head,
|
flags.model,
|
||||||
tail: flags.stream_tail,
|
flags.threads,
|
||||||
|
flags.stream_head,
|
||||||
|
flags.stream_tail,
|
||||||
|
|result: Result<Whispered, String>| {
|
||||||
|
match result {
|
||||||
|
Ok(whispered) => {
|
||||||
|
eprintln!("{}: {:?}", chrono::Local::now(), whispered);
|
||||||
|
println!("{}", whispered.to_string());
|
||||||
|
},
|
||||||
|
Err(msg) => { eprintln!("Error whispering: {}", msg); },
|
||||||
};
|
};
|
||||||
let w = new_whisper(flags.model, flags.threads, handler).unwrap();
|
},
|
||||||
|
).unwrap();
|
||||||
let stream_retain = (flags.stream_retain * 16_000.0) as usize;
|
let stream_retain = (flags.stream_retain * 16_000.0) as usize;
|
||||||
let stream_step = Duration::new(flags.stream_step, 0);
|
let stream_step = Duration::new(flags.stream_step, 0);
|
||||||
match flags.wav {
|
match flags.wav {
|
||||||
@@ -63,10 +73,7 @@ fn main() {
|
|||||||
new_listener().listen(move |data: Vec<f32>| {
|
new_listener().listen(move |data: Vec<f32>| {
|
||||||
data.iter().for_each(|x| buffer.push(*x));
|
data.iter().for_each(|x| buffer.push(*x));
|
||||||
if Instant::now() - last > stream_step {
|
if Instant::now() - last > stream_step {
|
||||||
match w.transcribe_async(&buffer) {
|
w.transcribe_async(&buffer).unwrap();
|
||||||
Ok(_) => (),
|
|
||||||
Err(msg) => eprintln!("{}", msg),
|
|
||||||
};
|
|
||||||
|
|
||||||
match &flags.debug {
|
match &flags.debug {
|
||||||
true => {
|
true => {
|
||||||
@@ -93,28 +100,23 @@ fn main() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Whisper {
|
struct WhisperService {
|
||||||
jobs: std::sync::mpsc::SyncSender<AWhisper>,
|
jobs: std::sync::mpsc::SyncSender<AWhisper>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WhisperEngine {
|
fn new_whisper_service<F>(model_path: String, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result<WhisperService, String> where F: FnMut(Result<Whispered, String>) + Send + 'static {
|
||||||
ctx: WhisperContext,
|
match new_whisper_engine(model_path, threads) {
|
||||||
threads: i32,
|
|
||||||
handler: Handler,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new_whisper(model_path: String, threads: i32, handler: Handler) -> Result<Whisper, String> {
|
|
||||||
match new_whisper_engine(model_path, threads, handler) {
|
|
||||||
Ok(engine) => {
|
Ok(engine) => {
|
||||||
|
let mut whisper = new_whisper_impl(engine, stream_head, stream_tail, handler_fn);
|
||||||
let (send, recv) = std::sync::mpsc::sync_channel(100);
|
let (send, recv) = std::sync::mpsc::sync_channel(100);
|
||||||
thread::spawn(move || { engine.transcribe_asyncs(recv); });
|
thread::spawn(move || { whisper.transcribe_asyncs(recv); });
|
||||||
Ok(Whisper{jobs: send})
|
Ok(WhisperService{jobs: send})
|
||||||
},
|
},
|
||||||
Err(msg) => Err(format!("failed to initialize engine: {}", msg)),
|
Err(msg) => Err(format!("failed to initialize engine: {}", msg)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Whisper {
|
impl WhisperService {
|
||||||
fn transcribe(&self, data: &Vec<f32>) {
|
fn transcribe(&self, data: &Vec<f32>) {
|
||||||
let (send, recv) = std::sync::mpsc::sync_channel(0);
|
let (send, recv) = std::sync::mpsc::sync_channel(0);
|
||||||
self._transcribe_async(data, Some(send)).unwrap();
|
self._transcribe_async(data, Some(send)).unwrap();
|
||||||
@@ -136,40 +138,78 @@ impl Whisper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_whisper_engine(model_path: String, threads: i32, handler: Handler) -> Result<WhisperEngine, String> {
|
struct WhisperImpl {
|
||||||
match WhisperContext::new(&model_path) {
|
engine: WhisperEngine,
|
||||||
Ok(ctx) => Ok(WhisperEngine{ctx: ctx, threads: threads, handler: handler}),
|
stream_head: f32,
|
||||||
Err(msg) => Err(format!("failed to load {}: {}", model_path, msg)),
|
stream_tail: f32,
|
||||||
|
handler_fn: Option<Box<dyn FnMut(Result<Whispered, String>) + Send + 'static>>
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_whisper_impl<F>(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: F) -> WhisperImpl where F: FnMut(Result<Whispered, String>) + Send + 'static {
|
||||||
|
WhisperImpl {
|
||||||
|
engine: engine,
|
||||||
|
stream_head: stream_head,
|
||||||
|
stream_tail: stream_tail,
|
||||||
|
handler_fn: Some(Box::new(handler_fn)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WhisperEngine {
|
impl WhisperImpl {
|
||||||
fn transcribe_asyncs(&self, recv: std::sync::mpsc::Receiver<AWhisper>) {
|
fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<AWhisper>) {
|
||||||
loop {
|
loop {
|
||||||
match recv.recv() {
|
match recv.recv() {
|
||||||
Ok(job) => {
|
Ok(job) => {
|
||||||
match self.transcribe(&job.data) {
|
let result = self.transcribe(&job).is_ok();
|
||||||
Ok(result) => {
|
|
||||||
self.handler.on_success(result);
|
|
||||||
match job.ack {
|
match job.ack {
|
||||||
Some(ack) => { let _ = ack.send(true); },
|
Some(ack) => {
|
||||||
|
ack.send(result).unwrap();
|
||||||
|
},
|
||||||
None => (),
|
None => (),
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
Err(msg) => {
|
|
||||||
self.handler.on_error(format!("failed to transcribe: {}", msg));
|
|
||||||
match job.ack {
|
|
||||||
Some(ack) => { let _ = ack.send(false); },
|
|
||||||
None => (),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn transcribe(&mut self, a_whisper: &AWhisper) -> Result<(), ()> {
|
||||||
|
match self.engine.transcribe(&a_whisper.data) {
|
||||||
|
Ok(result) => {
|
||||||
|
self.on_success(&result);
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
Err(msg) => {
|
||||||
|
self.on_error(msg.to_string());
|
||||||
|
Err(())
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_success(&mut self, whispered: &Whispered) {
|
||||||
|
let result = whispered
|
||||||
|
.after(&(self.stream_head * 100.0))
|
||||||
|
.before(&(self.stream_tail * 100.0));
|
||||||
|
(self.handler_fn.as_mut().unwrap())(Ok(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_error(&mut self, msg: String) {
|
||||||
|
(self.handler_fn.as_mut().unwrap())(Err(format!("failed to transcribe: {}", &msg)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct WhisperEngine {
|
||||||
|
ctx: WhisperContext,
|
||||||
|
threads: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_whisper_engine(model_path: String, threads: i32) -> Result<WhisperEngine, String> {
|
||||||
|
match WhisperContext::new(&model_path) {
|
||||||
|
Ok(ctx) => Ok(WhisperEngine{ctx: ctx, threads: threads}),
|
||||||
|
Err(msg) => Err(format!("failed to load {}: {}", model_path, msg)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WhisperEngine {
|
||||||
fn transcribe(&self, data: &Vec<f32>) -> Result<Whispered, WhisperError> {
|
fn transcribe(&self, data: &Vec<f32>) -> Result<Whispered, WhisperError> {
|
||||||
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 });
|
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 });
|
||||||
params.set_no_context(true);
|
params.set_no_context(true);
|
||||||
@@ -270,25 +310,6 @@ impl Whispered {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Handler {
|
|
||||||
head: f32,
|
|
||||||
tail: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Handler {
|
|
||||||
fn on_success(&self, result: Whispered) {
|
|
||||||
eprintln!("{}: {:?}", chrono::Local::now(), &result);
|
|
||||||
println!("{}", result
|
|
||||||
.after(&(self.head * 100.0))
|
|
||||||
.before(&(self.tail * 100.0))
|
|
||||||
.to_string(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
fn on_error(&self, msg: String) {
|
|
||||||
eprintln!("error: {}", msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Listener {
|
struct Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user