forked from Zackriya-Solutions/meetily
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhisper_engine.rs
More file actions
1137 lines (953 loc) · 49.2 KB
/
Copy pathwhisper_engine.rs
File metadata and controls
1137 lines (953 loc) · 49.2 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Commit name to recover the serial whisper engine processing for smaller meetings [Slower processing but dooes not fail] - "before parallel processing implementation"
use std::path::{PathBuf};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use whisper_rs::{WhisperContext, WhisperContextParameters, FullParams, SamplingStrategy};
use serde::{Serialize, Deserialize};
use anyhow::{Result, anyhow};
use reqwest::Client;
use tokio::fs;
use tokio::io::AsyncWriteExt;
use crate::config::WHISPER_MODEL_CATALOG;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ModelStatus {
Available,
Missing,
Downloading { progress: u8 },
Error(String),
Corrupted { file_size: u64, expected_min_size: u64 },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
pub name: String,
pub path: PathBuf,
pub size_mb: u32,
pub accuracy: String,
pub speed: String,
pub status: ModelStatus,
pub description: String,
}
pub struct WhisperEngine {
models_dir: PathBuf,
current_context: Arc<RwLock<Option<WhisperContext>>>,
current_model: Arc<RwLock<Option<String>>>,
available_models: Arc<RwLock<HashMap<String, ModelInfo>>>,
// State tracking for smart logging
last_transcription_was_short: Arc<RwLock<bool>>,
short_audio_warning_logged: Arc<RwLock<bool>>,
// Performance optimization: reduce logging frequency
transcription_count: Arc<RwLock<u64>>,
// Download cancellation tracking
cancel_download_flag: Arc<RwLock<Option<String>>>, // Model name being cancelled
// Active downloads tracking to prevent concurrent downloads
active_downloads: Arc<RwLock<HashSet<String>>>, // Set of models currently being downloaded
}
impl WhisperEngine {
/// Detect available GPU acceleration capabilities
fn detect_gpu_acceleration() -> bool {
// On macOS, prefer Metal GPU acceleration
if cfg!(target_os = "macos") {
log::info!("macOS detected - attempting to enable Metal GPU acceleration");
return true; // Enable GPU by default on macOS, whisper-rs will fallback if needed
}
// Check for CUDA support on other platforms
if cfg!(feature = "cuda") {
log::info!("CUDA feature enabled - attempting GPU acceleration");
return true;
}
// Check for Vulkan support on other platforms
if cfg!(feature = "vulkan") {
log::info!("Vulkan feature enabled - attempting GPU acceleration");
return true;
}
// Fall back to CPU
log::info!("No GPU acceleration features detected - using CPU processing");
false
}
pub fn new() -> Result<Self> {
Self::new_with_models_dir(None)
}
/// Create a new WhisperEngine with optional custom models directory
/// If models_dir is None, uses default location (app data dir for production, local for dev)
pub fn new_with_models_dir(models_dir: Option<PathBuf>) -> Result<Self> {
// PERFORMANCE: Suppress verbose whisper.cpp and Metal logs
// These C library logs bypass Rust logging and clutter output
// Set environment variables to reduce C library verbosity
std::env::set_var("GGML_METAL_LOG_LEVEL", "1"); // 0=off, 1=error, 2=warn, 3=info
std::env::set_var("WHISPER_LOG_LEVEL", "1"); // Reduce whisper.cpp verbosity
let models_dir = if let Some(dir) = models_dir {
// Use provided directory (for production with app_data_dir)
dir
} else {
// Fallback: determine based on debug/release mode
let current_dir = std::env::current_dir()
.map_err(|e| anyhow!("Failed to get current directory: {}", e))?;
// Development: Use frontend/models or backend directories
// Production: Use system directories (should be overridden by caller)
if cfg!(debug_assertions) {
// Development mode - try frontend and backend directories
if current_dir.join("models").exists() {
current_dir.join("models")
} else if current_dir.join("../models").exists() {
current_dir.join("../models")
} else if current_dir.join("backend/whisper-server-package/models").exists() {
current_dir.join("backend/whisper-server-package/models")
} else if current_dir.join("../backend/whisper-server-package/models").exists() {
current_dir.join("../backend/whisper-server-package/models")
} else {
// Create models directory in current directory for development
current_dir.join("models")
}
} else {
// Production mode fallback (shouldn't reach here, caller should provide path)
log::warn!("WhisperEngine: No models directory provided, using fallback path");
dirs::data_dir()
.or_else(|| dirs::home_dir())
.ok_or_else(|| anyhow!("Could not find system data directory"))?
.join("Meetily")
.join("models")
}
};
log::info!("WhisperEngine using models directory: {}", models_dir.display());
log::info!("Debug mode: {}", cfg!(debug_assertions));
// Log acceleration capabilities
let gpu_support = Self::detect_gpu_acceleration();
log::info!("Hardware acceleration support: {}", if gpu_support { "enabled" } else { "disabled" });
#[cfg(feature = "metal")]
log::info!("Apple Metal GPU support: enabled");
#[cfg(feature = "openblas")]
log::info!("OpenBLAS CPU optimization: enabled");
#[cfg(feature = "coreml")]
log::info!("Apple CoreML support: enabled");
#[cfg(feature = "cuda")]
log::info!("NVIDIA CUDA support: enabled");
#[cfg(feature = "vulkan")]
log::info!("Vulkan GPU support: enabled");
#[cfg(feature = "openmp")]
log::info!("OpenMP parallel processing: enabled");
let engine = Self {
models_dir,
current_context: Arc::new(RwLock::new(None)),
current_model: Arc::new(RwLock::new(None)),
available_models: Arc::new(RwLock::new(HashMap::new())),
// Initialize state tracking
last_transcription_was_short: Arc::new(RwLock::new(false)),
short_audio_warning_logged: Arc::new(RwLock::new(false)),
// Performance optimization: reduce logging frequency
transcription_count: Arc::new(RwLock::new(0)),
// Initialize cancellation tracking
cancel_download_flag: Arc::new(RwLock::new(None)),
// Initialize active downloads tracking
active_downloads: Arc::new(RwLock::new(HashSet::new())),
};
Ok(engine)
}
pub async fn discover_models(&self) -> Result<Vec<ModelInfo>> {
let models_dir = &self.models_dir;
let mut models = Vec::new();
// Use centralized model catalog from config.rs
let model_configs = WHISPER_MODEL_CATALOG;
for &(name, filename, size_mb, accuracy, speed, description) in model_configs {
let model_path = models_dir.join(filename);
let status = if model_path.exists() {
// Check if file size is reasonable (at least 1MB for a valid model)
match std::fs::metadata(&model_path) {
Ok(metadata) => {
let file_size_bytes = metadata.len();
let file_size_mb = file_size_bytes / (1024 * 1024);
let expected_min_size_mb = (size_mb as f64 * 0.9) as u64; // Allow 90% of expected size as minimum for more accurate corruption detection
if file_size_mb >= expected_min_size_mb && file_size_mb > 1 {
// File size looks good, but let's also check if it's a valid GGML file
match self.validate_model_file(&model_path).await {
Ok(_) => ModelStatus::Available,
Err(_) => {
log::warn!("Model file {} has correct size but appears corrupted (failed validation)",
filename);
ModelStatus::Corrupted {
file_size: file_size_bytes,
expected_min_size: (expected_min_size_mb * 1024 * 1024) as u64
}
}
}
} else if file_size_mb > 0 {
// File exists but is smaller than expected
// Check if this model is currently being downloaded
let models_guard = self.available_models.read().await;
if let Some(existing_model) = models_guard.get(name) {
match &existing_model.status {
ModelStatus::Downloading { progress } => {
log::debug!("Model {} appears to be downloading ({} MB so far, {}% complete)",
filename, file_size_mb, progress);
ModelStatus::Downloading { progress: *progress }
}
_ => {
log::warn!("Model file {} exists but is corrupted ({} MB, expected ~{} MB)",
filename, file_size_mb, size_mb);
ModelStatus::Corrupted {
file_size: file_size_bytes,
expected_min_size: (expected_min_size_mb * 1024 * 1024) as u64
}
}
}
} else {
log::warn!("Model file {} exists but is corrupted ({} MB, expected ~{} MB)",
filename, file_size_mb, size_mb);
ModelStatus::Corrupted {
file_size: file_size_bytes,
expected_min_size: (expected_min_size_mb * 1024 * 1024) as u64
}
}
} else {
ModelStatus::Missing
}
}
Err(_) => ModelStatus::Missing
}
} else {
ModelStatus::Missing
};
let model_info = ModelInfo {
name: name.to_string(),
path: model_path,
size_mb: size_mb as u32,
accuracy: accuracy.to_string(),
speed: speed.to_string(),
status,
description: description.to_string(),
};
models.push(model_info);
}
// Update internal cache
let mut available_models = self.available_models.write().await;
available_models.clear();
for model in &models {
available_models.insert(model.name.clone(), model.clone());
}
Ok(models)
}
pub async fn load_model(&self, model_name: &str) -> Result<()> {
let models = self.available_models.read().await;
let model_info = models.get(model_name)
.ok_or_else(|| anyhow!("Model {} not found", model_name))?;
match model_info.status {
ModelStatus::Available => {
// FIX 5: Check if this model is already loaded
if let Some(current_model) = self.current_model.read().await.as_ref() {
if current_model == model_name {
log::info!("Model {} is already loaded, skipping reload", model_name);
return Ok(());
}
// FIX 5: Unload current model before loading new one
log::info!("Unloading current model '{}' before loading '{}'", current_model, model_name);
self.unload_model().await;
}
log::info!("Loading model: {}", model_name);
// PERFORMANCE OPTIMIZATION: Use comprehensive hardware profile for optimal GPU configuration
let hardware_profile = crate::audio::HardwareProfile::detect();
let adaptive_config = hardware_profile.get_whisper_config();
// Enable flash attention for high-end GPUs (Metal on Apple Silicon, CUDA on NVIDIA)
// Flash attention provides 20-40% speedup but requires stable GPU drivers
let flash_attn_enabled = match (&hardware_profile.gpu_type, &hardware_profile.performance_tier) {
(crate::audio::GpuType::Metal, crate::audio::PerformanceTier::Ultra | crate::audio::PerformanceTier::High) => true,
(crate::audio::GpuType::Cuda, crate::audio::PerformanceTier::Ultra | crate::audio::PerformanceTier::High) => true,
_ => false, // Conservative: disable for other GPU types and lower tiers
};
let context_param = WhisperContextParameters {
use_gpu: adaptive_config.use_gpu,
gpu_device: 0,
flash_attn: flash_attn_enabled,
..Default::default()
};
// PERFORMANCE: Suppress verbose C library logs during model loading
// This hides the excessive Metal/GGML initialization logs in release builds
let ctx = {
// let _suppressor = crate::whisper_engine::StderrSuppressor::new();
// Load whisper context with hardware-optimized parameters
WhisperContext::new_with_params(&model_info.path.to_string_lossy(), context_param)
.map_err(|e| anyhow!("Failed to load model {}: {}", model_name, e))?
// Suppressor dropped here, stderr restored
};
// Update current context and model
*self.current_context.write().await = Some(ctx);
*self.current_model.write().await = Some(model_name.to_string());
// Enhanced acceleration status reporting
let acceleration_status = match (&hardware_profile.gpu_type, flash_attn_enabled) {
(crate::audio::GpuType::Metal, true) => "Metal GPU with Flash Attention (Ultra-Fast)",
(crate::audio::GpuType::Metal, false) => "Metal GPU acceleration",
(crate::audio::GpuType::Cuda, true) => "CUDA GPU with Flash Attention (Ultra-Fast)",
(crate::audio::GpuType::Cuda, false) => "CUDA GPU acceleration",
(crate::audio::GpuType::Vulkan, _) => "Vulkan GPU acceleration",
(crate::audio::GpuType::OpenCL, _) => "OpenCL GPU acceleration",
(crate::audio::GpuType::None, _) => "CPU processing only",
};
log::info!("Successfully loaded model: {} with {} (Performance Tier: {:?}, Beam Size: {}, Threads: {:?})",
model_name, acceleration_status, hardware_profile.performance_tier,
adaptive_config.beam_size, adaptive_config.max_threads);
Ok(())
},
ModelStatus::Missing => {
Err(anyhow!("Model {} is not downloaded", model_name))
},
ModelStatus::Downloading { .. } => {
Err(anyhow!("Model {} is currently downloading", model_name))
},
ModelStatus::Error(ref err) => {
Err(anyhow!("Model {} has error: {}", model_name, err))
},
ModelStatus::Corrupted { .. } => {
Err(anyhow!("Model {} is corrupted and cannot be loaded", model_name))
}
}
}
pub async fn unload_model(&self) -> bool {
let mut ctx_guard = self.current_context.write().await;
let unloaded = ctx_guard.take().is_some();
if unloaded {
log::info!("📉Whisper model unloaded");
}
let mut model_name_guard = self.current_model.write().await;
model_name_guard.take();
unloaded
}
pub async fn get_current_model(&self) -> Option<String> {
self.current_model.read().await.clone()
}
pub async fn is_model_loaded(&self) -> bool {
self.current_context.read().await.is_some()
}
// Enhanced function to clean repetitive text patterns and meaningless outputs
fn clean_repetitive_text(text: &str) -> String {
if text.is_empty() {
return String::new();
}
// Check for obviously meaningless patterns first
if Self::is_meaningless_output(text) {
// Performance optimization: reduce meaningless output logging to debug level
perf_debug!("Detected meaningless output, returning empty: '{}'", text);
return String::new();
}
let words: Vec<&str> = text.split_whitespace().collect();
if words.len() < 3 {
return text.to_string();
}
// Enhanced repetition detection with sliding window
let cleaned_words = Self::remove_word_repetitions(&words);
// Remove phrase repetitions with more sophisticated detection
let cleaned_words = Self::remove_phrase_repetitions(&cleaned_words);
// Check for overall repetition ratio
let final_text = cleaned_words.join(" ");
if Self::calculate_repetition_ratio(&final_text) > 0.7 {
// Performance optimization: reduce repetition ratio logging to debug level
perf_debug!("High repetition ratio detected, filtering out: '{}'", final_text);
return String::new();
}
final_text
}
// Check for obviously meaningless patterns
fn is_meaningless_output(text: &str) -> bool {
let text_lower = text.to_lowercase();
// Check for common meaningless patterns
let meaningless_patterns = [
"thank you for watching",
"thanks for watching",
"like and subscribe",
"music playing",
"applause",
"laughter",
"um um um",
"uh uh uh",
"ah ah ah",
];
for pattern in &meaningless_patterns {
if text_lower.contains(pattern) {
return true;
}
}
// Check if text is mostly the same character or very short repetitive patterns
let unique_chars: HashSet<char> = text.chars().collect();
if unique_chars.len() <= 3 && text.len() > 10 {
return true;
}
false
}
// Enhanced word repetition removal
fn remove_word_repetitions<'a>(words: &'a [&'a str]) -> Vec<&'a str> {
let mut cleaned_words = Vec::new();
let mut i = 0;
while i < words.len() {
let current_word = words[i];
let mut repeat_count = 1;
// Count consecutive repetitions of the same word
while i + repeat_count < words.len() && words[i + repeat_count] == current_word {
repeat_count += 1;
}
// Be more aggressive: if word is repeated 2+ times, only keep one instance
if repeat_count >= 2 {
cleaned_words.push(current_word);
i += repeat_count;
} else {
cleaned_words.push(current_word);
i += 1;
}
}
cleaned_words
}
// Enhanced phrase repetition removal with variable length detection
fn remove_phrase_repetitions<'a>(words: &'a [&'a str]) -> Vec<&'a str> {
if words.len() < 4 {
return words.to_vec();
}
let mut final_words = Vec::new();
let mut i = 0;
while i < words.len() {
let mut phrase_found = false;
// Check for 2-word to 5-word phrase repetitions
for phrase_len in 2..=std::cmp::min(5, (words.len() - i) / 2) {
if i + phrase_len * 2 <= words.len() {
let phrase1 = &words[i..i + phrase_len];
let phrase2 = &words[i + phrase_len..i + phrase_len * 2];
if phrase1 == phrase2 {
// Add the phrase once and skip the repetition
final_words.extend_from_slice(phrase1);
i += phrase_len * 2;
phrase_found = true;
break;
}
}
}
if !phrase_found {
final_words.push(words[i]);
i += 1;
}
}
final_words
}
// Calculate repetition ratio in text
fn calculate_repetition_ratio(text: &str) -> f32 {
let words: Vec<&str> = text.split_whitespace().collect();
if words.len() < 4 {
return 0.0;
}
let mut word_counts = HashMap::new();
for word in &words {
*word_counts.entry(word.to_lowercase()).or_insert(0) += 1;
}
let total_words = words.len() as f32;
let repeated_words: usize = word_counts.values().map(|&count| if count > 1 { count - 1 } else { 0 }).sum();
repeated_words as f32 / total_words
}
/// Transcribe audio with streaming support for partial results and adaptive quality
pub async fn transcribe_audio_with_confidence(&self, audio_data: Vec<f32>, language: Option<String>) -> Result<(String, f32, bool)> {
let ctx_lock = self.current_context.read().await;
let ctx = ctx_lock.as_ref()
.ok_or_else(|| anyhow!("No model loaded. Please load a model first."))?;
// Get adaptive configuration based on hardware
let hardware_profile = crate::audio::HardwareProfile::detect();
let adaptive_config = hardware_profile.get_whisper_config();
// ADAPTIVE parameters - optimized for current hardware
let mut params = FullParams::new(SamplingStrategy::BeamSearch {
beam_size: adaptive_config.beam_size as i32,
patience: 1.0
});
// Configure with adaptive settings
// If language is "auto" or None, use automatic language detection (pass None)
// If language is "auto-translate", enable translation to English
// Otherwise, use the specified language code
let (language_code, should_translate) = match language.as_deref() {
Some("auto") | None => (None, false),
Some("auto-translate") => (None, true),
Some(lang) => (Some(lang), false),
};
params.set_language(language_code);
params.set_translate(should_translate);
// CRITICAL: Disable timestamp tokens to prevent whisper.cpp chunking heuristics
// The "single timestamp ending - skip entire chunk" optimization incorrectly discards
// complete, valid transcriptions. Disabling timestamps forces whisper to return ALL text.
params.set_no_timestamps(true); // Prevent timestamp-based segment skipping
params.set_token_timestamps(true); // Keep for any timestamp-aware features
// PERFORMANCE: Disable ALL whisper.cpp internal printing
// This reduces C library log spam significantly
params.set_print_special(false); // Don't print special tokens
params.set_print_progress(false); // Don't print progress
params.set_print_realtime(false); // Don't print realtime info
params.set_print_timestamps(false); // Don't print timestamps
// Additional suppression to reduce C library verbosity
params.set_suppress_blank(true);
params.set_suppress_non_speech_tokens(true);
params.set_temperature(adaptive_config.temperature);
params.set_max_initial_ts(1.0);
params.set_entropy_thold(2.4);
params.set_logprob_thold(-1.0);
// BALANCED FIX: Lowered from 0.75 to 0.55 to allow quiet speech detection
// Previous value was too aggressive and rejected valid quiet speech
// 0.55 is balanced - prevents hallucinations while preserving quiet speech
params.set_no_speech_thold(0.55);
params.set_max_len(200);
params.set_single_segment(false);
// Set thread count based on hardware (if supported by whisper.cpp)
if let Some(_max_threads) = adaptive_config.max_threads {
// Note: whisper.cpp may or may not expose thread control through params
// Removed debug log to reduce I/O overhead in transcription hot path
}
let duration_seconds = audio_data.len() as f64 / 16000.0;
let is_partial = duration_seconds < 15.0; // Consider chunks under 15s as partial
// PERFORMANCE: Suppress verbose C library logs during transcription
// This hides whisper_full_with_state debug logs and beam search details
let (num_segments, state) = {
// let _suppressor = crate::whisper_engine::StderrSuppressor::new();
let mut state = ctx.create_state()?;
state.full(params, &audio_data)?;
let num_segments = state.full_n_segments();
(num_segments, state)
// Suppressor dropped here, stderr restored
};
let mut result = String::new();
let mut total_confidence = 0.0;
let mut segment_count = 0;
let num_segments = num_segments?;
for i in 0..num_segments {
let segment_text = match state.full_get_segment_text_lossy(i) {
Ok(text) => text,
Err(_) => continue,
};
// Calculate confidence based on segment length and duration (simplified approach)
let segment_length = segment_text.len() as f32;
let segment_confidence = if segment_length > 0.0 {
(segment_length / 100.0).min(0.9) + 0.1 // 0.1 to 1.0 confidence based on text length
} else {
0.1
};
total_confidence += segment_confidence;
segment_count += 1;
let cleaned_text = segment_text.trim();
if !cleaned_text.is_empty() {
if !result.is_empty() {
result.push(' ');
}
result.push_str(cleaned_text);
}
}
let final_result = result.trim().to_string();
let cleaned_result = Self::clean_repetitive_text(&final_result);
let avg_confidence = if segment_count > 0 {
total_confidence / segment_count as f32
} else {
0.0
};
Ok((cleaned_result, avg_confidence, is_partial))
}
pub async fn transcribe_audio(&self, audio_data: Vec<f32>, language: Option<String>) -> Result<String> {
let ctx_lock = self.current_context.read().await;
let ctx = ctx_lock.as_ref()
.ok_or_else(|| anyhow!("No model loaded. Please load a model first."))?;
// Get adaptive configuration based on hardware
let hardware_profile = crate::audio::HardwareProfile::detect();
let adaptive_config = hardware_profile.get_whisper_config();
// ADAPTIVE parameters - optimized for current hardware
let mut params = FullParams::new(SamplingStrategy::BeamSearch {
beam_size: adaptive_config.beam_size as i32,
patience: 1.0
});
// Configure for good quality
// If language is "auto" or None, use automatic language detection (pass None)
// If language is "auto-translate", enable translation to English
// Otherwise, use the specified language code
let (language_code, should_translate) = match language.as_deref() {
Some("auto") | None => (None, false),
Some("auto-translate") => (None, true),
Some(lang) => (Some(lang), false),
};
params.set_language(language_code);
params.set_translate(should_translate);
// CRITICAL: Disable timestamp tokens to prevent whisper.cpp chunking heuristics
// The "single timestamp ending - skip entire chunk" optimization incorrectly discards
// complete, valid transcriptions. Disabling timestamps forces whisper to return ALL text.
params.set_no_timestamps(true); // Prevent timestamp-based segment skipping
params.set_token_timestamps(true); // Keep for any timestamp-aware features
params.set_print_special(false);
params.set_print_progress(false);
params.set_print_realtime(false);
params.set_print_timestamps(false);
// BALANCED settings - good quality with reasonable speed
params.set_suppress_blank(true);
params.set_suppress_non_speech_tokens(true);
params.set_temperature(0.3); // Lower than 0.4 for consistency, higher than 0.0 for quality
params.set_max_initial_ts(1.0);
params.set_entropy_thold(2.4);
params.set_logprob_thold(-1.0);
// BALANCED FIX: Lowered from 0.75 to 0.55 to allow quiet speech detection
// Previous value was too aggressive and rejected valid quiet speech
// 0.55 is balanced - prevents hallucinations while preserving quiet speech
params.set_no_speech_thold(0.55);
// Reasonable length limits
params.set_max_len(200); // Reasonable length
params.set_single_segment(false); // Allow multiple segments for better accuracy
// Note: compression_ratio_threshold would be ideal but not available in current whisper-rs
// This would help detect repetitive outputs: params.set_compression_ratio_threshold(2.4);
// Duration-based optimization is handled by beam search parameters
let duration_seconds = audio_data.len() as f64 / 16000.0; // Assuming 16kHz
let is_short_audio = duration_seconds < 1.0;
// Smart logging based on audio duration and previous states
let mut should_log_transcription = true;
let mut should_log_short_warning = false;
if is_short_audio {
let last_was_short = *self.last_transcription_was_short.read().await;
let warning_logged = *self.short_audio_warning_logged.read().await;
if !warning_logged {
should_log_short_warning = true;
*self.short_audio_warning_logged.write().await = true;
}
// Only log transcription start if it's the first short audio or previous wasn't short
should_log_transcription = !last_was_short;
*self.last_transcription_was_short.write().await = true;
} else {
let last_was_short = *self.last_transcription_was_short.read().await;
// Always log when transitioning from short to normal audio
if last_was_short {
log::info!("Audio duration normalized, resuming transcription");
*self.short_audio_warning_logged.write().await = false;
}
*self.last_transcription_was_short.write().await = false;
}
if should_log_short_warning {
log::warn!("Audio duration is short ({:.1}s < 1.0s). Consider padding the input audio with silence. Further short audio warnings will be suppressed.", duration_seconds);
}
// Performance optimization: reduce transcription start logging frequency
let transcription_count = {
let mut count = self.transcription_count.write().await;
*count += 1;
*count
};
// Only log every 10th transcription or significant audio (>10s) to reduce I/O overhead
if should_log_transcription && (transcription_count % 10 == 0 || duration_seconds > 10.0) {
log::info!("Starting transcription #{} of {} samples ({:.1}s duration)",
transcription_count, audio_data.len(), duration_seconds);
}
let mut state = ctx.create_state()?;
state.full(params, &audio_data)?;
// Extract text with improved segment handling
let num_segments = state.full_n_segments()?;
// Performance optimization: reduce segment completion logging
// Only log for significant transcriptions to avoid I/O overhead
if (should_log_transcription || num_segments > 0) && (num_segments > 3 || duration_seconds > 5.0) {
perf_debug!("Transcription #{} completed with {} segments ({:.1}s)", transcription_count, num_segments, duration_seconds);
}
let mut result = String::new();
for i in 0..num_segments {
let segment_text = match state.full_get_segment_text_lossy(i) {
Ok(text) => text,
Err(_) => continue,
};
let _start_time = state.full_get_segment_t0(i).unwrap_or(0);
let _end_time = state.full_get_segment_t1(i).unwrap_or(0);
// Performance optimization: remove per-segment debug logging
// This was causing significant I/O overhead during transcription
// Only log segments for very long audio (>30s) or when explicitly debugging
if duration_seconds > 30.0 {
perf_trace!("Segment {} ({:.2}s-{:.2}s): '{}'",
i, _start_time as f64 / 100.0, _end_time as f64 / 100.0, segment_text);
}
// Clean and append segment text
let cleaned_text = segment_text.trim();
if !cleaned_text.is_empty() {
if !result.is_empty() {
result.push(' ');
}
result.push_str(cleaned_text);
}
}
let final_result = result.trim().to_string();
// Check for repetition loops and clean them up
let cleaned_result = Self::clean_repetitive_text(&final_result);
// Performance optimization: smart logging for transcription results
if cleaned_result.is_empty() {
// Only log empty results occasionally to reduce spam
if should_log_transcription && transcription_count % 20 == 0 {
perf_debug!("Transcription #{} result is empty - no speech detected", transcription_count);
}
} else {
if cleaned_result != final_result {
log::info!("Cleaned repetitive transcription #{}: '{}' -> '{}'", transcription_count, final_result, cleaned_result);
}
// Reduce successful transcription logging frequency
// Only log every 5th result or significant results (>50 chars) to reduce I/O overhead
if transcription_count % 5 == 0 || cleaned_result.len() > 50 || duration_seconds > 10.0 {
log::info!("Transcription #{} result: '{}'", transcription_count, cleaned_result);
} else {
perf_debug!("Transcription #{} result: '{}'", transcription_count, cleaned_result);
}
}
Ok(cleaned_result)
}
pub async fn get_models_directory(&self) -> PathBuf {
self.models_dir.clone()
}
/// Validate if a model file is a valid GGML file by checking its header
async fn validate_model_file(&self, model_path: &PathBuf) -> Result<()> {
use tokio::io::AsyncReadExt;
let mut file = fs::File::open(model_path).await
.map_err(|e| anyhow!("Failed to open model file: {}", e))?;
// Read the first 8 bytes to check for GGML magic number
let mut buffer = [0u8; 8];
file.read_exact(&mut buffer).await
.map_err(|e| anyhow!("Failed to read model file header: {}", e))?;
// Check for GGML magic number (various versions and endianness)
if buffer.starts_with(b"ggml") || buffer.starts_with(b"GGUF") || buffer.starts_with(b"ggmf") ||
buffer.starts_with(b"lmgg") || buffer.starts_with(b"FUGU") || buffer.starts_with(b"fmgg") {
Ok(())
} else {
Err(anyhow!("Invalid model file: missing GGML/GGUF magic number. Found: {:?}",
String::from_utf8_lossy(&buffer[..4])))
}
}
pub async fn delete_model(&self, model_name: &str) -> Result<String> {
log::info!("Attempting to delete model: {}", model_name);
// Get model info to find the file path
let model_info = {
let models = self.available_models.read().await;
models.get(model_name).cloned()
};
let model_info = model_info.ok_or_else(|| anyhow!("Model '{}' not found", model_name))?;
// Check if model is corrupted before allowing deletion
log::info!("Model '{}' has status: {:?}", model_name, model_info.status);
match &model_info.status {
ModelStatus::Corrupted { file_size, expected_min_size } => {
log::info!("Deleting corrupted model '{}' (file size: {} bytes, expected min: {} bytes)",
model_name, file_size, expected_min_size);
// Delete the file
if model_info.path.exists() {
fs::remove_file(&model_info.path).await
.map_err(|e| anyhow!("Failed to delete file '{}': {}", model_info.path.display(), e))?;
log::info!("Successfully deleted corrupted file: {}", model_info.path.display());
} else {
log::warn!("File '{}' does not exist, nothing to delete", model_info.path.display());
}
// Update model status to Missing
{
let mut models = self.available_models.write().await;
if let Some(model) = models.get_mut(model_name) {
model.status = ModelStatus::Missing;
}
}
Ok(format!("Successfully deleted corrupted model '{}'", model_name))
}
ModelStatus::Available => {
// Allow deletion of available models for testing/cleanup
log::info!("Deleting available model '{}' (for cleanup)", model_name);
if model_info.path.exists() {
fs::remove_file(&model_info.path).await
.map_err(|e| anyhow!("Failed to delete file '{}': {}", model_info.path.display(), e))?;
log::info!("Successfully deleted available model file: {}", model_info.path.display());
} else {
log::warn!("File '{}' does not exist, nothing to delete", model_info.path.display());
}
// Update model status to Missing
{
let mut models = self.available_models.write().await;
if let Some(model) = models.get_mut(model_name) {
model.status = ModelStatus::Missing;
}
}
Ok(format!("Successfully deleted model '{}'", model_name))
}
_ => {
Err(anyhow!("Can only delete corrupted or available models. Model '{}' has status: {:?}", model_name, model_info.status))
}
}
}
pub async fn download_model(&self, model_name: &str, progress_callback: Option<Box<dyn Fn(u8) + Send>>) -> Result<()> {
log::info!("Starting download for model: {}", model_name);
// Check if download is already in progress for this model
{
let active = self.active_downloads.read().await;
if active.contains(model_name) {
log::warn!("Download already in progress for model: {}", model_name);
return Err(anyhow!("Download already in progress for model: {}", model_name));
}
}
// Add to active downloads
{
let mut active = self.active_downloads.write().await;
active.insert(model_name.to_string());
}
// Clear any previous cancellation flag for this model
{
let mut cancel_flag = self.cancel_download_flag.write().await;
*cancel_flag = None;
}
// Official ggerganov/whisper.cpp model URLs from Hugging Face
let model_url = match model_name {
// Standard f16 models
"tiny" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.bin",
"base" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin",
"small" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin",
"medium" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.bin",
"large-v3-turbo" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo.bin",
"large-v3" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3.bin",
// Q5_1 quantized models
"tiny-q5_1" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny-q5_1.bin",
"base-q5_1" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-q5_1.bin",
"small-q5_1" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small-q5_1.bin",
// Q5_0 quantized models
"medium-q5_0" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium-q5_0.bin",
"large-v3-turbo-q5_0" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo-q5_0.bin",
"large-v3-q5_0" => "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-q5_0.bin",
_ => return Err(anyhow!("Unsupported model: {}", model_name))
};
log::info!("Model URL for {}: {}", model_name, model_url);
// Generate correct filename - all models follow ggml-{model_name}.bin pattern
let filename = format!("ggml-{}.bin", model_name);
let file_path = self.models_dir.join(&filename);
log::info!("Downloading to file path: {}", file_path.display());
// Create models directory if it doesn't exist
if !self.models_dir.exists() {
fs::create_dir_all(&self.models_dir).await
.map_err(|e| anyhow!("Failed to create models directory: {}", e))?;
}
// Update model status to downloading
{
let mut models = self.available_models.write().await;
if let Some(model_info) = models.get_mut(model_name) {
model_info.status = ModelStatus::Downloading { progress: 0 };
}
}
log::info!("Creating HTTP client and starting request...");
let client = Client::new();
log::info!("Sending GET request to: {}", model_url);
let response = client.get(model_url).send().await
.map_err(|e| anyhow!("Failed to start download: {}", e))?;
log::info!("Received response with status: {}", response.status());
if !response.status().is_success() {
// Remove from active downloads on error
let mut active = self.active_downloads.write().await;
active.remove(model_name);
return Err(anyhow!("Download failed with status: {}", response.status()));
}
let total_size = response.content_length().unwrap_or(0);
log::info!("Response successful, content length: {} bytes ({:.1} MB)", total_size, total_size as f64 / (1024.0 * 1024.0));
if total_size == 0 {
log::warn!("Content length is 0 or unknown - download may not show accurate progress");
}
let mut file = fs::File::create(&file_path).await
.map_err(|e| anyhow!("Failed to create file: {}", e))?;
log::info!("File created successfully at: {}", file_path.display());
// Stream download with real progress reporting
log::info!("Starting streaming download...");
log::info!("Expected size: {:.1} MB", total_size as f64 / (1024.0 * 1024.0));
use futures_util::StreamExt;
let mut stream = response.bytes_stream();
let mut downloaded = 0u64;
let mut last_progress_report = 0u8;