Skip to content

Commit e9ce3c1

Browse files
committed
faster tauri dev
1 parent a29b11e commit e9ce3c1

File tree

8 files changed

+169
-111
lines changed

8 files changed

+169
-111
lines changed

crates/whisper-local/Cargo.toml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ edition = "2021"
55

66
# https://github.com/tazz4843/whisper-rs/blob/e3d67d5/Cargo.toml
77
[features]
8-
default = []
9-
coreml = ["whisper-rs/coreml"]
10-
cuda = ["whisper-rs/cuda"]
11-
hipblas = ["whisper-rs/hipblas"]
12-
openblas = ["whisper-rs/openblas"]
13-
metal = ["whisper-rs/metal"]
14-
vulkan = ["whisper-rs/vulkan"]
15-
openmp = ["whisper-rs/openmp"]
8+
default = ["actual"]
9+
actual = ["dep:whisper-rs"]
10+
11+
coreml = ["actual", "whisper-rs/coreml"]
12+
cuda = ["actual", "whisper-rs/cuda"]
13+
hipblas = ["actual", "whisper-rs/hipblas"]
14+
openblas = ["actual", "whisper-rs/openblas"]
15+
metal = ["actual", "whisper-rs/metal"]
16+
vulkan = ["actual", "whisper-rs/vulkan"]
17+
openmp = ["actual", "whisper-rs/openmp"]
1618

1719
[dependencies]
1820
hypr-audio-utils = { workspace = true }
@@ -23,7 +25,7 @@ hound = { workspace = true }
2325

2426
kalosm-sound = { workspace = true, default-features = false }
2527
rodio = { workspace = true }
26-
whisper-rs = { git = "https://codeberg.org/tazz4843/whisper-rs", rev = "0c509ec", features = ["raw-api", "tracing_backend"] }
28+
whisper-rs = { git = "https://codeberg.org/tazz4843/whisper-rs", rev = "0c509ec", features = ["raw-api", "tracing_backend"], optional = true }
2729

2830
futures-util = { workspace = true }
2931
tracing = { workspace = true }

crates/whisper-local/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde::{ser::Serializer, Serialize};
44
pub enum Error {
55
#[error("model_not_found")]
66
ModelNotFound,
7+
#[cfg(feature = "actual")]
78
#[error(transparent)]
89
LocalWhisperError(#[from] whisper_rs::WhisperError),
910
}

crates/whisper-local/src/ggml.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, specta::Type)]
2+
pub struct GgmlBackend {
3+
pub kind: String,
4+
pub name: String,
5+
pub description: String,
6+
pub total_memory_mb: usize,
7+
pub free_memory_mb: usize,
8+
}
9+
10+
#[cfg(any(feature = "actual", debug_assertions))]
11+
pub fn list_ggml_backends() -> Vec<GgmlBackend> {
12+
vec![]
13+
}
14+
15+
// https://github.com/ggml-org/llama.cpp/blob/3a9457d/common/arg.cpp#L2300
16+
#[cfg(all(feature = "actual", not(debug_assertions)))]
17+
pub fn list_ggml_backends() -> Vec<GgmlBackend> {
18+
use whisper_rs::whisper_rs_sys::{
19+
ggml_backend_dev_count, ggml_backend_dev_description, ggml_backend_dev_get,
20+
ggml_backend_dev_memory, ggml_backend_dev_name, ggml_backend_dev_type,
21+
ggml_backend_dev_type_GGML_BACKEND_DEVICE_TYPE_ACCEL as GGML_BACKEND_DEVICE_TYPE_ACCEL,
22+
ggml_backend_dev_type_GGML_BACKEND_DEVICE_TYPE_CPU as GGML_BACKEND_DEVICE_TYPE_CPU,
23+
ggml_backend_dev_type_GGML_BACKEND_DEVICE_TYPE_GPU as GGML_BACKEND_DEVICE_TYPE_GPU,
24+
};
25+
26+
let count = unsafe { ggml_backend_dev_count() };
27+
let mut devices = Vec::with_capacity(count);
28+
29+
for i in 0..count {
30+
unsafe {
31+
let dev = ggml_backend_dev_get(i);
32+
33+
let kind: String = match ggml_backend_dev_type(dev) {
34+
GGML_BACKEND_DEVICE_TYPE_CPU => "CPU".into(),
35+
GGML_BACKEND_DEVICE_TYPE_ACCEL => "ACCEL".into(),
36+
GGML_BACKEND_DEVICE_TYPE_GPU => "GPU".into(),
37+
_ => "UNKNOWN".into(),
38+
};
39+
40+
let name = std::ffi::CStr::from_ptr(ggml_backend_dev_name(dev))
41+
.to_string_lossy()
42+
.into_owned();
43+
let description = std::ffi::CStr::from_ptr(ggml_backend_dev_description(dev))
44+
.to_string_lossy()
45+
.into_owned();
46+
47+
let mut free_mem: usize = 0;
48+
let mut total_mem: usize = 0;
49+
ggml_backend_dev_memory(dev, &mut free_mem, &mut total_mem);
50+
51+
devices.push(GgmlBackend {
52+
kind,
53+
name,
54+
description,
55+
total_memory_mb: total_mem / 1024 / 1024,
56+
free_memory_mb: free_mem / 1024 / 1024,
57+
});
58+
}
59+
}
60+
61+
devices
62+
}

crates/whisper-local/src/lib.rs

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// https://github.com/tazz4843/whisper-rs/blob/master/examples/audio_transcription.rs
1+
mod ggml;
2+
pub use ggml::*;
23

34
mod stream;
45
pub use stream::*;
@@ -8,60 +9,3 @@ pub use model::*;
89

910
mod error;
1011
pub use error::*;
11-
12-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, specta::Type)]
13-
pub struct GgmlBackend {
14-
pub kind: String,
15-
pub name: String,
16-
pub description: String,
17-
pub total_memory_mb: usize,
18-
pub free_memory_mb: usize,
19-
}
20-
21-
// https://github.com/ggml-org/llama.cpp/blob/3a9457d/common/arg.cpp#L2300
22-
pub fn list_ggml_backends() -> Vec<GgmlBackend> {
23-
use whisper_rs::whisper_rs_sys::{
24-
ggml_backend_dev_count, ggml_backend_dev_description, ggml_backend_dev_get,
25-
ggml_backend_dev_memory, ggml_backend_dev_name, ggml_backend_dev_type,
26-
ggml_backend_dev_type_GGML_BACKEND_DEVICE_TYPE_ACCEL as GGML_BACKEND_DEVICE_TYPE_ACCEL,
27-
ggml_backend_dev_type_GGML_BACKEND_DEVICE_TYPE_CPU as GGML_BACKEND_DEVICE_TYPE_CPU,
28-
ggml_backend_dev_type_GGML_BACKEND_DEVICE_TYPE_GPU as GGML_BACKEND_DEVICE_TYPE_GPU,
29-
};
30-
31-
let count = unsafe { ggml_backend_dev_count() };
32-
let mut devices = Vec::with_capacity(count);
33-
34-
for i in 0..count {
35-
unsafe {
36-
let dev = ggml_backend_dev_get(i);
37-
38-
let kind: String = match ggml_backend_dev_type(dev) {
39-
GGML_BACKEND_DEVICE_TYPE_CPU => "CPU".into(),
40-
GGML_BACKEND_DEVICE_TYPE_ACCEL => "ACCEL".into(),
41-
GGML_BACKEND_DEVICE_TYPE_GPU => "GPU".into(),
42-
_ => "UNKNOWN".into(),
43-
};
44-
45-
let name = std::ffi::CStr::from_ptr(ggml_backend_dev_name(dev))
46-
.to_string_lossy()
47-
.into_owned();
48-
let description = std::ffi::CStr::from_ptr(ggml_backend_dev_description(dev))
49-
.to_string_lossy()
50-
.into_owned();
51-
52-
let mut free_mem: usize = 0;
53-
let mut total_mem: usize = 0;
54-
ggml_backend_dev_memory(dev, &mut free_mem, &mut total_mem);
55-
56-
devices.push(GgmlBackend {
57-
kind,
58-
name,
59-
description,
60-
total_memory_mb: total_mem / 1024 / 1024,
61-
free_memory_mb: free_mem / 1024 / 1024,
62-
});
63-
}
64-
}
65-
66-
devices
67-
}

crates/whisper-local/src/model.rs renamed to crates/whisper-local/src/model/actual.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use whisper_rs::{
1010

1111
use hypr_whisper::Language;
1212

13+
use crate::Segment;
14+
1315
lazy_static! {
1416
static ref TRAILING_DOTS: Regex = Regex::new(r"\.{2,}$").unwrap();
1517
}
@@ -89,7 +91,7 @@ impl Whisper {
8991
WhisperBuilder::default()
9092
}
9193

92-
pub fn transcribe(&mut self, audio: &[f32]) -> Result<Vec<Segment>, super::Error> {
94+
pub fn transcribe(&mut self, audio: &[f32]) -> Result<Vec<Segment>, crate::Error> {
9395
#[cfg(debug_assertions)]
9496
self.debug(audio);
9597

@@ -187,7 +189,7 @@ impl Whisper {
187189
Ok(segments)
188190
}
189191

190-
fn get_language(&mut self, audio: &[f32]) -> Result<Option<String>, super::Error> {
192+
fn get_language(&mut self, audio: &[f32]) -> Result<Option<String>, crate::Error> {
191193
if self.languages.len() == 0 {
192194
tracing::info!("no_language_specified");
193195
return Ok(None);
@@ -292,47 +294,6 @@ impl Whisper {
292294
}
293295
}
294296

295-
// https://github.com/floneum/floneum/blob/52967ae/models/rwhisper/src/lib.rs#L116
296-
#[derive(Debug, Default)]
297-
pub struct Segment {
298-
pub text: String,
299-
pub language: Option<String>,
300-
pub start: f64,
301-
pub end: f64,
302-
pub confidence: f32,
303-
pub meta: Option<serde_json::Value>,
304-
}
305-
306-
impl Segment {
307-
pub fn text(&self) -> &str {
308-
&self.text
309-
}
310-
311-
pub fn language(&self) -> Option<&str> {
312-
self.language.as_deref()
313-
}
314-
315-
pub fn start(&self) -> f64 {
316-
self.start
317-
}
318-
319-
pub fn end(&self) -> f64 {
320-
self.end
321-
}
322-
323-
pub fn duration(&self) -> f64 {
324-
self.end - self.start
325-
}
326-
327-
pub fn confidence(&self) -> f32 {
328-
self.confidence
329-
}
330-
331-
pub fn meta(&self) -> Option<serde_json::Value> {
332-
self.meta.clone()
333-
}
334-
}
335-
336297
#[cfg(test)]
337298
mod tests {
338299
use super::*;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::Segment;
2+
use hypr_whisper::Language;
3+
4+
#[derive(Default)]
5+
pub struct WhisperBuilder {}
6+
7+
#[derive(Default)]
8+
pub struct Whisper {}
9+
10+
impl WhisperBuilder {
11+
pub fn model_path(self, _model_path: impl Into<String>) -> Self {
12+
self
13+
}
14+
15+
pub fn languages(self, _languages: Vec<Language>) -> Self {
16+
self
17+
}
18+
19+
pub fn build(self) -> Result<Whisper, crate::Error> {
20+
Ok(Whisper {})
21+
}
22+
}
23+
24+
impl Whisper {
25+
pub fn builder() -> WhisperBuilder {
26+
WhisperBuilder::default()
27+
}
28+
29+
pub fn transcribe(&mut self, _samples: &[f32]) -> Result<Vec<Segment>, crate::Error> {
30+
Ok(vec![Segment {
31+
text: "mock".to_string(),
32+
language: None,
33+
start: 0.0,
34+
end: 1.0,
35+
confidence: 1.0,
36+
meta: None,
37+
}])
38+
}
39+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#[cfg(feature = "actual")]
2+
mod actual;
3+
#[cfg(feature = "actual")]
4+
pub use actual::*;
5+
6+
#[cfg(not(feature = "actual"))]
7+
mod mock;
8+
#[cfg(not(feature = "actual"))]
9+
pub use mock::*;
10+
11+
#[derive(Debug, Default)]
12+
pub struct Segment {
13+
pub text: String,
14+
pub language: Option<String>,
15+
pub start: f64,
16+
pub end: f64,
17+
pub confidence: f32,
18+
pub meta: Option<serde_json::Value>,
19+
}
20+
21+
impl Segment {
22+
pub fn text(&self) -> &str {
23+
&self.text
24+
}
25+
26+
pub fn language(&self) -> Option<&str> {
27+
self.language.as_deref()
28+
}
29+
30+
pub fn start(&self) -> f64 {
31+
self.start
32+
}
33+
34+
pub fn end(&self) -> f64 {
35+
self.end
36+
}
37+
38+
pub fn duration(&self) -> f64 {
39+
self.end - self.start
40+
}
41+
42+
pub fn confidence(&self) -> f32 {
43+
self.confidence
44+
}
45+
46+
pub fn meta(&self) -> Option<serde_json::Value> {
47+
self.meta.clone()
48+
}
49+
}

crates/whisper-local/src/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use dasp::sample::FromSample;
88
use futures_util::{Stream, StreamExt};
99
use rodio::Source;
1010

11-
use super::{Segment, Whisper};
11+
use crate::{Segment, Whisper};
1212

1313
pub struct TranscriptionTask<S, T> {
1414
stream: S,

0 commit comments

Comments
 (0)