Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(desktop-gpui): run caption transcription engines through an injectable seam

Richie McIlroy0fd0fab9653c
diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs
index f75b189e1..3240295a9 100644
--- a/apps/desktop-gpui/src/transcription.rs
+++ b/apps/desktop-gpui/src/transcription.rs
@@ -1613,10 +1613,22 @@ fn process_with_parakeet(
tracing::info!("Parakeet produced {} timed tokens", result.tokens.len());
+ caption_segments_from_tokens(result.tokens.iter().map(|token| CaptionWord {
+ text: token.text.clone(),
+ start: token.start,
+ end: token.end,
+ }))
+}
+
+/// Parakeet's timed word tokens -> caption segments: drop empty tokens,
+/// normalize the words and chunk them.
+#[cfg(any(test, not(all(target_os = "macos", target_arch = "x86_64"))))]
+fn caption_segments_from_tokens(
+ tokens: impl IntoIterator<Item = CaptionWord>,
+) -> Result<Vec<CaptionSegment>, String> {
let words = normalize_caption_words(
- result
- .tokens
- .iter()
+ tokens
+ .into_iter()
.filter(|token| !token.text.trim().is_empty())
.map(|token| CaptionWord {
text: token.text.trim().to_string(),
@@ -1679,32 +1691,42 @@ pub fn transcribe_blocking(
));
}
- let temp_dir = std::env::temp_dir().join(format!(
- "cap-gpui-transcription-{}-{}",
- std::process::id(),
- TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
- ));
- std::fs::create_dir_all(&temp_dir)
- .map_err(|e| format!("Failed to create temporary directory: {e}"))?;
- let audio_path = temp_dir.join("audio.wav");
-
- let result = (|| {
- extract_audio_from_video(project_path, &audio_path)?;
-
+ transcribe_project_with(project_path, |audio_path| {
let _guard = lock_transcription_worker_slot();
if is_parakeet_model(model) {
tracing::info!("Using Parakeet TDT engine");
- let result = process_with_parakeet(&audio_path, model_file.to_string_lossy().as_ref());
+ let result = process_with_parakeet(audio_path, model_file.to_string_lossy().as_ref());
schedule_parakeet_release();
result
} else {
tracing::info!("Using Whisper engine");
let hints = transcription_hints();
let context = get_whisper_context(model_file.to_string_lossy().as_ref())?;
- let result = process_with_whisper(&audio_path, context, language, &hints);
+ let result = process_with_whisper(audio_path, context, language, &hints);
release_whisper_context_after_transcription();
result
}
+ })
+}
+
+/// Extracts the project's transcription audio to a temporary 16 kHz mono WAV
+/// and runs `engine` on it.
+fn transcribe_project_with(
+ project_path: &Path,
+ engine: impl FnOnce(&Path) -> Result<Vec<CaptionSegment>, String>,
+) -> Result<Vec<CaptionSegment>, String> {
+ let temp_dir = std::env::temp_dir().join(format!(
+ "cap-gpui-transcription-{}-{}",
+ std::process::id(),
+ TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
+ ));
+ std::fs::create_dir_all(&temp_dir)
+ .map_err(|e| format!("Failed to create temporary directory: {e}"))?;
+ let audio_path = temp_dir.join("audio.wav");
+
+ let result = (|| {
+ extract_audio_from_video(project_path, &audio_path)?;
+ engine(&audio_path)
})();
let _ = std::fs::remove_dir_all(&temp_dir);
@@ -2175,6 +2197,9 @@ pub fn caption_generation_error_message(message: &str) -> String {
message.to_string()
}
+#[cfg(test)]
+mod verify_tests;
+
#[cfg(test)]
mod tests {
use super::*;