@@ -3,34 +3,6 @@ mod error;
33use error:: WhisperCppError ;
44use whisper_rs:: { FullParams , SamplingStrategy , WhisperContext , WhisperContextParameters } ;
55use std:: io:: Write ;
6- use serde:: Serialize ;
7-
8- #[ derive( Serialize ) ]
9- pub struct GpuInfo {
10- pub platform : String ,
11- pub expected_backend : String ,
12- pub gpu_enabled_in_settings : bool ,
13- }
14-
15- /// Get information about the expected GPU backend for the current platform
16- #[ tauri:: command]
17- pub fn get_gpu_info ( use_gpu : bool ) -> GpuInfo {
18- let ( platform, expected_backend) = if cfg ! ( target_os = "windows" ) {
19- ( "Windows" , "CUDA (NVIDIA GPU)" )
20- } else if cfg ! ( target_os = "macos" ) {
21- ( "macOS" , "Metal/CoreML (Apple Silicon)" )
22- } else if cfg ! ( target_os = "linux" ) {
23- ( "Linux" , "CUDA (NVIDIA GPU)" )
24- } else {
25- ( "Unknown" , "CPU only" )
26- } ;
27-
28- GpuInfo {
29- platform : platform. to_string ( ) ,
30- expected_backend : expected_backend. to_string ( ) ,
31- gpu_enabled_in_settings : use_gpu,
32- }
33- }
346
357/// Check if audio is already in whisper-compatible format (16kHz, mono, 16-bit PCM)
368fn is_valid_wav_format ( audio_data : & [ u8 ] ) -> bool {
@@ -113,48 +85,25 @@ fn convert_audio_for_whisper(audio_data: Vec<u8>) -> Result<Vec<u8>, WhisperCppE
11385 } )
11486}
11587
116- /// Load Whisper model with automatic GPU fallback to CPU if needed
117- fn load_whisper_model ( model_path : & str , use_gpu : bool ) -> Result < WhisperContext , WhisperCppError > {
118- let mut params = WhisperContextParameters :: default ( ) ;
119- params. use_gpu = use_gpu;
120-
121- // Try loading with requested settings
122- match WhisperContext :: new_with_params ( model_path, params) {
123- Ok ( context) => {
124- let backend = if use_gpu { "GPU" } else { "CPU" } ;
125- tracing:: info!( "Whisper model loaded with {} backend" , backend) ;
126- Ok ( context)
127- }
128- Err ( first_error) => {
129- // If GPU was requested and failed, try CPU fallback
130- if use_gpu {
131- tracing:: warn!( "GPU failed, trying CPU fallback" ) ;
132- params. use_gpu = false ;
133-
134- WhisperContext :: new_with_params ( model_path, params)
135- . map ( |ctx| {
136- tracing:: info!( "Successfully fell back to CPU" ) ;
137- ctx
138- } )
139- . map_err ( |_| WhisperCppError :: ModelLoadError {
140- message : format ! ( "Failed to load model (GPU and CPU both failed)" )
141- } )
142- } else {
143- // CPU failed with no fallback option
144- Err ( WhisperCppError :: ModelLoadError {
145- message : first_error. to_string ( )
146- } )
147- }
148- }
149- }
88+ /// Load Whisper model with automatic GPU support based on compiled features
89+ fn load_whisper_model ( model_path : & str ) -> Result < WhisperContext , WhisperCppError > {
90+ // GPU acceleration is automatically enabled based on compile-time features:
91+ // - macOS: Metal + CoreML
92+ // - Windows: CUDA + Vulkan
93+ // - Linux: CUDA + Vulkan + HipBLAS
94+ // The whisper-rs library automatically selects the best available backend
95+
96+ WhisperContext :: new_with_params ( model_path, WhisperContextParameters :: default ( ) )
97+ . map_err ( |e| WhisperCppError :: ModelLoadError {
98+ message : format ! ( "Failed to load model: {}" , e)
99+ } )
150100}
151101
152102#[ tauri:: command]
153103pub async fn transcribe_with_whisper_cpp (
154104 audio_data : Vec < u8 > ,
155105 model_path : String ,
156106 language : Option < String > ,
157- use_gpu : bool ,
158107 prompt : String ,
159108 temperature : f32 ,
160109) -> Result < String , WhisperCppError > {
@@ -182,8 +131,8 @@ pub async fn transcribe_with_whisper_cpp(
182131 return Ok ( String :: new ( ) ) ;
183132 }
184133
185- // Load model with automatic GPU fallback
186- let context = load_whisper_model ( & model_path, use_gpu ) ?;
134+ // Load model with automatic GPU acceleration based on compiled features
135+ let context = load_whisper_model ( & model_path) ?;
187136
188137 // Create state and configure parameters
189138 let mut state = context
@@ -197,7 +146,7 @@ pub async fn transcribe_with_whisper_cpp(
197146 params. set_no_timestamps ( true ) ;
198147 params. set_temperature ( temperature) ;
199148 params. set_no_speech_thold ( 0.2 ) ; // Better silence detection
200- params. set_suppress_non_speech_tokens ( true ) ; // Prevent hallucinations
149+ params. set_suppress_nst ( true ) ; // Prevent hallucinations (non-speech tokens)
201150
202151 // Set language if specified
203152 if let Some ( ref lang) = language {
@@ -218,18 +167,19 @@ pub async fn transcribe_with_whisper_cpp(
218167 } ) ?;
219168
220169 // Collect transcribed text from all segments
221- let num_segments = state. full_n_segments ( )
222- . map_err ( |e| WhisperCppError :: TranscriptionError {
223- message : format ! ( "Failed to get segments: {}" , e) ,
224- } ) ?;
170+ let num_segments = state. full_n_segments ( ) ;
225171
226172 let mut text = String :: new ( ) ;
227173 for i in 0 ..num_segments {
228- let segment = state. full_get_segment_text ( i)
174+ let segment = state. get_segment ( i)
175+ . ok_or_else ( || WhisperCppError :: TranscriptionError {
176+ message : format ! ( "Failed to get segment {}" , i) ,
177+ } ) ?;
178+ let segment_text = segment. to_str ( )
229179 . map_err ( |e| WhisperCppError :: TranscriptionError {
230- message : format ! ( "Failed to get segment {}: {}" , i, e) ,
180+ message : format ! ( "Failed to get segment {} text : {}" , i, e) ,
231181 } ) ?;
232- text. push_str ( & segment ) ;
182+ text. push_str ( segment_text ) ;
233183 }
234184
235185 Ok ( text. trim ( ) . to_string ( ) )
0 commit comments