This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
forked from floneum/floneum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscribe.rs
53 lines (48 loc) · 1.72 KB
/
transcribe.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use futures_util::StreamExt;
use kalosm::audio::*;
use tokio::time::{Duration, Instant};
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Create a new small whisper model.
let model = Whisper::new().await?;
let mut current_time_stamp = 0.0;
// Record audio and add them to a queue in the background
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
std::thread::spawn(move || {
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
loop {
let audio = kalosm_sound::MicInput::default()
.record_until(Instant::now() + Duration::from_secs(5))
.await
.unwrap();
let _ = tx.send(audio);
}
});
});
loop {
let Some(audio) = rx.recv().await else {
break Ok(());
};
// Transcribe the audio.
let mut transcribed = model.transcribe(audio)?;
// As the model transcribes the audio, print the text to the console.
while let Some(transcribed) = transcribed.next().await {
let start = current_time_stamp + transcribed.start();
let end = start + transcribed.duration();
if transcribed.probability_of_no_speech() < 0.10 {
let text = transcribed.text();
println!("({:01} - {:01}): {}", start, end, text);
} else {
println!(
"({:01} - {:01}): <no speech> ({})",
start,
end,
transcribed.text()
);
}
current_time_stamp = end;
}
}
}