Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(remote): load speech recognition when a phone opens a chat with the microphone (GH #212)

purpleffdeb3531023
diff --git a/mobile-client/client.js b/mobile-client/client.js
index 424fd9ea..8240387a 100644
--- a/mobile-client/client.js
+++ b/mobile-client/client.js
@@ -2396,6 +2396,8 @@ import {
var f = s && Array.isArray(s.features) ? s.features : [];
voiceFeature = f.indexOf('voice') >= 0;
voiceSlot();
+ // Asking now also lets the computer load its speech recognition before the first take.
+ if(voiceFeature && voiceSecure()) voiceCaps().catch(function(){});
})
.catch(function(){});
diff --git a/src-tauri/src/commands/remote_voice.rs b/src-tauri/src/commands/remote_voice.rs
index af65303a..eaddf182 100644
--- a/src-tauri/src/commands/remote_voice.rs
+++ b/src-tauri/src/commands/remote_voice.rs
@@ -78,6 +78,8 @@ pub(crate) trait VoiceEngine: Send + Sync {
fn transcribe(&self, wav: Vec<u8>) -> EngineFuture<String>;
/// A WAV of `text`.
fn speak(&self, text: String) -> EngineFuture<Vec<u8>>;
+ /// Load the speech recognition now, so the first take does not wait for it. Returns at once.
+ fn warm(&self) {}
}
/// The status of the engines, and when it was asked.
@@ -160,7 +162,14 @@ pub(crate) async fn handle_status(AxumState(ctx): AxumState<VoiceCtx>, caller: O
return StatusCode::UNAUTHORIZED.into_response();
}
let here = ctx.engine.is_some();
- Json(status_body(ctx.abilities().await, here)).into_response()
+ let abilities = ctx.abilities().await;
+ // A phone asks this when a chat with the microphone opens: load the model now, not at the first take.
+ if abilities.listen {
+ if let Some(engine) = &ctx.engine {
+ engine.warm();
+ }
+ }
+ Json(status_body(abilities, here)).into_response()
}
/// A RIFF/WAVE header, nothing more: the decoder on the other side checks the rest.
@@ -283,6 +292,10 @@ impl VoiceEngine for DesktopVoice {
})
}
+ fn warm(&self) {
+ super::whisper::warm_up(self.app.clone());
+ }
+
fn transcribe(&self, wav: Vec<u8>) -> EngineFuture<String> {
let app = self.app.clone();
Box::pin(async move {
@@ -319,6 +332,7 @@ mod tests {
struct Fake {
abilities: Abilities,
asked: AtomicUsize,
+ warmed: AtomicUsize,
heard: Mutex<Vec<usize>>,
}
@@ -335,10 +349,13 @@ mod tests {
fn speak(&self, text: String) -> EngineFuture<Vec<u8>> {
Box::pin(async move { Ok(format!("RIFF....WAVE{text}").into_bytes()) })
}
+ fn warm(&self) {
+ self.warmed.fetch_add(1, Ordering::SeqCst);
+ }
}
fn fake(listen: bool, speak: bool) -> Arc<Fake> {
- Arc::new(Fake { abilities: Abilities { listen, speak }, asked: AtomicUsize::new(0), heard: Mutex::new(vec![]) })
+ Arc::new(Fake { abilities: Abilities { listen, speak }, asked: AtomicUsize::new(0), warmed: AtomicUsize::new(0), heard: Mutex::new(vec![]) })
}
fn router(engine: Option<Arc<dyn VoiceEngine>>, signed_in: bool) -> Router {
@@ -404,6 +421,17 @@ mod tests {
assert_eq!(v["why"], NOT_HERE);
}
+ #[tokio::test]
+ async fn the_status_loads_the_speech_recognition_ahead_of_the_first_take() {
+ let ready = fake(true, false);
+ call(router(Some(ready.clone()), true), "GET", STATUS_PATH, "", vec![]).await;
+ assert_eq!(ready.warmed.load(Ordering::SeqCst), 1);
+ // Nothing to load where nothing is installed.
+ let none = fake(false, false);
+ call(router(Some(none.clone()), true), "GET", STATUS_PATH, "", vec![]).await;
+ assert_eq!(none.warmed.load(Ordering::SeqCst), 0);
+ }
+
#[tokio::test]
async fn the_status_is_kept_a_little_while() {
let engine = fake(true, true);
diff --git a/src-tauri/src/commands/whisper.rs b/src-tauri/src/commands/whisper.rs
index b8c1430f..f2e3acdd 100644
--- a/src-tauri/src/commands/whisper.rs
+++ b/src-tauri/src/commands/whisper.rs
@@ -528,6 +528,31 @@ fn ensure_whisper_running(app: &AppHandle, state: &State<'_, AppState>) -> Resul
auto_start_whisper_sync(app, &python_bin, &state.whisper)
}
+/// One warm-up at a time.
+static WARMING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// Start speech recognition ahead of the first take, in the background
+/// (voice over remote, #212). A phone asks for the voice status when a chat
+/// with the microphone opens; loading the model then takes up to a minute,
+/// and without this the first take waited for all of it. Does nothing while
+/// the server runs or another warm-up is under way; a failure is said again
+/// by the take itself.
+pub fn warm_up(app: AppHandle) {
+ use std::sync::atomic::Ordering;
+ let state = app.state::<AppState>();
+ let running = state.whisper.lock().map(|w| w.is_running()).unwrap_or(true);
+ if running || WARMING.swap(true, Ordering::SeqCst) {
+ return;
+ }
+ std::thread::spawn(move || {
+ let state = app.state::<AppState>();
+ if let Err(why) = ensure_whisper_running(&app, &state) {
+ println!("[Whisper] warm-up for a phone did not start: {}", why);
+ }
+ WARMING.store(false, Ordering::SeqCst);
+ });
+}
+
/// Tear the server down and bring it back up, once.
fn restart_whisper(app: &AppHandle, state: &State<'_, AppState>) -> Result<(), String> {
if let Ok(mut w) = state.whisper.lock() {