Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

refactor: improve performance on transcription

Gabriel Stein950418350dde
diff --git a/src/App.jsx b/src/App.jsx
index 1075535b0..a498a0f1b 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -8,14 +8,22 @@ import { Settings } from "lucide-react";
const SoundWaveIcon = ({ size = 16 }) => {
return (
<div className="flex items-center justify-center gap-1">
- <div className={`bg-white rounded-full`} style={{ width: size * 0.25, height: size * 0.6 }}></div>
- <div className={`bg-white rounded-full`} style={{ width: size * 0.25, height: size }}></div>
- <div className={`bg-white rounded-full`} style={{ width: size * 0.25, height: size * 0.6 }}></div>
+ <div
+ className={`bg-white rounded-full`}
+ style={{ width: size * 0.25, height: size * 0.6 }}
+ ></div>
+ <div
+ className={`bg-white rounded-full`}
+ style={{ width: size * 0.25, height: size }}
+ ></div>
+ <div
+ className={`bg-white rounded-full`}
+ style={{ width: size * 0.25, height: size * 0.6 }}
+ ></div>
</div>
);
};
-
// Voice Wave Animation Component (for processing state)
const VoiceWaveIndicator = ({ isListening }) => {
return (
@@ -24,13 +32,11 @@ const VoiceWaveIndicator = ({ isListening }) => {
<div
key={i}
className={`w-0.5 bg-white rounded-full transition-all duration-150 ${
- isListening
- ? 'animate-pulse h-4'
- : 'h-2'
+ isListening ? "animate-pulse h-4" : "h-2"
}`}
style={{
- animationDelay: isListening ? `${i * 0.1}s` : '0s',
- animationDuration: isListening ? `${0.6 + i * 0.1}s` : '0s'
+ animationDelay: isListening ? `${i * 0.1}s` : "0s",
+ animationDuration: isListening ? `${0.6 + i * 0.1}s` : "0s",
}}
/>
))}
@@ -41,7 +47,7 @@ const VoiceWaveIndicator = ({ isListening }) => {
// Enhanced Tooltip Component
const Tooltip = ({ children, content, emoji }) => {
const [isVisible, setIsVisible] = useState(false);
-
+
return (
<div className="relative inline-block">
<div
@@ -51,7 +57,10 @@ const Tooltip = ({ children, content, emoji }) => {
{children}
</div>
{isVisible && (
- <div className="absolute bottom-full left-1/2 transform -translate-x-1/2 mb-2 px-1 py-1 text-white bg-gradient-to-r from-neutral-800 to-neutral-700 rounded-md whitespace-nowrap z-10 transition-opacity duration-150" style={{ fontSize: '9.7px' }}>
+ <div
+ className="absolute bottom-full left-1/2 transform -translate-x-1/2 mb-2 px-1 py-1 text-white bg-gradient-to-r from-neutral-800 to-neutral-700 rounded-md whitespace-nowrap z-10 transition-opacity duration-150"
+ style={{ fontSize: "9.7px" }}
+ >
{emoji && <span className="mr-1">{emoji}</span>}
{content}
<div className="absolute top-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-l-2 border-r-2 border-t-2 border-transparent border-t-neutral-800"></div>
@@ -64,36 +73,38 @@ const Tooltip = ({ children, content, emoji }) => {
export default function App() {
const [isRecording, setIsRecording] = useState(false);
const [isProcessing, setIsProcessing] = useState(false);
- const [transcript, setTranscript] = useState('');
- const [error, setError] = useState('');
+ const [transcript, setTranscript] = useState("");
+ const [error, setError] = useState("");
const [isHovered, setIsHovered] = useState(false);
const mediaRecorderRef = useRef(null);
const audioChunksRef = useRef([]);
const startRecording = async () => {
try {
- setError('');
+ setError("");
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
-
+
mediaRecorderRef.current = new window.MediaRecorder(stream);
audioChunksRef.current = [];
-
+
mediaRecorderRef.current.ondataavailable = (event) => {
audioChunksRef.current.push(event.data);
};
-
+
mediaRecorderRef.current.onstop = async () => {
setIsProcessing(true);
- const audioBlob = new Blob(audioChunksRef.current, { type: 'audio/wav' });
+ const audioBlob = new Blob(audioChunksRef.current, {
+ type: "audio/wav",
+ });
await processAudio(audioBlob);
- stream.getTracks().forEach(track => track.stop());
+ stream.getTracks().forEach((track) => track.stop());
};
-
+
mediaRecorderRef.current.start();
setIsRecording(true);
} catch (err) {
- console.error("Recording error:", err)
- setError('Failed to access microphone: ' + err.message);
+ console.error("Recording error:", err);
+ setError("Failed to access microphone: " + err.message);
}
};
@@ -109,76 +120,77 @@ export default function App() {
try {
await window.electronAPI.pasteText(text);
} catch (err) {
- setError('Failed to paste text. Please check accessibility permissions.');
+ setError("Failed to paste text. Please check accessibility permissions.");
}
};
const processAudio = async (audioBlob) => {
try {
// Check if local Whisper is available and user preference
- const useLocalWhisper = localStorage.getItem('useLocalWhisper') === 'true';
- const whisperModel = localStorage.getItem('whisperModel') || 'base';
-
+ const useLocalWhisper =
+ localStorage.getItem("useLocalWhisper") === "true";
+ const whisperModel = localStorage.getItem("whisperModel") || "turbo"; // Default to turbo for speed
+
if (useLocalWhisper) {
await processWithLocalWhisper(audioBlob, whisperModel);
} else {
await processWithOpenAIAPI(audioBlob);
}
-
} catch (err) {
- console.error("Transcription error:", err)
- setError('Transcription failed: ' + err.message);
+ console.error("Transcription error:", err);
+ setError("Transcription failed: " + err.message);
} finally {
setIsProcessing(false);
}
};
- const processWithLocalWhisper = async (audioBlob, model = 'base') => {
+ const processWithLocalWhisper = async (audioBlob, model = "turbo") => {
try {
- // Check Whisper installation first
- const installCheck = await window.electronAPI.checkWhisperInstallation();
- if (!installCheck.installed || !installCheck.working) {
- throw new Error(`Local Whisper not available: ${installCheck.error || 'Installation check failed'}`);
- }
-
- // Convert Blob to ArrayBuffer for IPC transfer
+ // Convert Blob to ArrayBuffer for IPC transfer - do this early
const arrayBuffer = await audioBlob.arrayBuffer();
const options = { model };
- const result = await window.electronAPI.transcribeLocalWhisper(arrayBuffer, options);
-
+ const result = await window.electronAPI.transcribeLocalWhisper(
+ arrayBuffer,
+ options
+ );
+
if (result.success && result.text) {
const text = result.text.trim();
-
+
if (text) {
setTranscript(text);
-
- // Save transcription to database
- try {
- await window.electronAPI.saveTranscription(text);
- } catch (err) {
- console.error("Failed to save transcription:", err);
- }
-
- // Automatically paste the text
- await safePaste(text);
+
+ // Paste immediately - don't wait for database save
+ const pastePromise = safePaste(text);
+
+ // Save to database in parallel
+ const savePromise = window.electronAPI
+ .saveTranscription(text)
+ .catch((err) => {
+ console.error("Failed to save transcription:", err);
+ });
+
+ // Wait for paste to complete, but don't block on database save
+ await pastePromise;
} else {
- setError('No text transcribed. Try again.');
+ setError("No text transcribed. Try again.");
}
- } else if (result.success === false && result.message === 'No audio detected') {
- // Handle no audio detected case - don't show as error, just a brief message
- setError('No audio detected');
- return; // Don't attempt fallback for no audio
+ } else if (
+ result.success === false &&
+ result.message === "No audio detected"
+ ) {
+ setError("No audio detected");
+ return;
} else {
- throw new Error(result.error || 'Local Whisper transcription failed');
+ throw new Error(result.error || "Local Whisper transcription failed");
}
-
} catch (err) {
console.error("Local Whisper error:", err);
- // Check if we should attempt fallback to OpenAI API
- const allowFallback = localStorage.getItem('allowOpenAIFallback') === 'true';
-
+ const allowFallback =
+ localStorage.getItem("allowOpenAIFallback") === "true";
+
if (allowFallback) {
- setError('Local Whisper failed. Retrying with OpenAI API...');
+ setError("Local Whisper failed. Retrying with OpenAI API...");
await processWithOpenAIAPI(audioBlob);
} else {
throw new Error(`Local Whisper failed: ${err.message}`);
@@ -188,58 +200,74 @@ export default function App() {
const processWithOpenAIAPI = async (audioBlob) => {
try {
- const formData = new FormData();
- formData.append('file', audioBlob, 'audio.wav');
- formData.append('model', 'whisper-1');
-
- // Get API key from main process via IPC (will check .env and in-memory storage)
+ // Get API key early and cache it
let apiKey = await window.electronAPI.getOpenAIKey();
-
- // Fallback to localStorage if no key from main process
- if (!apiKey || apiKey.trim() === '' || apiKey === 'your_openai_api_key_here') {
- apiKey = localStorage.getItem('openaiApiKey');
+ if (
+ !apiKey ||
+ apiKey.trim() === "" ||
+ apiKey === "your_openai_api_key_here"
+ ) {
+ apiKey = localStorage.getItem("openaiApiKey");
}
-
- if (!apiKey || apiKey.trim() === '' || apiKey === 'your_openai_api_key_here') {
- setError('OpenAI API key not found. Please set your API key in the .env file or Control Panel.');
+
+ if (
+ !apiKey ||
+ apiKey.trim() === "" ||
+ apiKey === "your_openai_api_key_here"
+ ) {
+ setError(
+ "OpenAI API key not found. Please set your API key in the .env file or Control Panel."
+ );
return;
}
- const response = await fetch('https://api.openai.com/v1/audio/transcriptions', {
- method: 'POST',
- headers: {
- 'Authorization': `Bearer ${apiKey}`
- },
- body: formData
- });
+ const formData = new FormData();
+ formData.append("file", audioBlob, "audio.wav");
+ formData.append("model", "whisper-1");
+
+ const response = await fetch(
+ "https://api.openai.com/v1/audio/transcriptions",
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${apiKey}`,
+ },
+ body: formData,
+ }
+ );
if (!response.ok) {
- const errorText = await response.text()
- console.error("API Error:", errorText)
+ const errorText = await response.text();
+ console.error("API Error:", errorText);
setError(`Transcription failed: ${response.status} ${errorText}`);
- throw new Error(`Failed to transcribe audio: ${response.status} ${errorText}`)
+ throw new Error(
+ `Failed to transcribe audio: ${response.status} ${errorText}`
+ );
}
const result = await response.json();
const text = result.text.trim();
-
+
if (text) {
setTranscript(text);
-
- // Save transcription to database
- try {
- await window.electronAPI.saveTranscription(text);
- } catch (err) {
- console.error("Failed to save transcription:", err);
- }
-
- // Automatically paste the text
- await safePaste(text);
+
+ // Paste immediately - don't wait for database save
+ const pastePromise = safePaste(text);
+
+ // Save to database in parallel
+ const savePromise = window.electronAPI
+ .saveTranscription(text)
+ .catch((err) => {
+ console.error("Failed to save transcription:", err);
+ });
+
+ // Wait for paste to complete, but don't block on database save
+ await pastePromise;
} else {
- setError('No text transcribed. Try again.');
+ setError("No text transcribed. Try again.");
}
} catch (err) {
- console.error("OpenAI API error:", err)
+ console.error("OpenAI API error:", err);
throw err;
}
};
@@ -274,71 +302,69 @@ export default function App() {
};
const handleKeyPress = (e) => {
- if (e.key === 'Escape') {
+ if (e.key === "Escape") {
handleClose();
}
};
-
+
useEffect(() => {
- document.addEventListener('keydown', handleKeyPress);
- return () => document.removeEventListener('keydown', handleKeyPress);
+ document.addEventListener("keydown", handleKeyPress);
+ return () => document.removeEventListener("keydown", handleKeyPress);
}, []);
// Determine current mic state
const getMicState = () => {
- if (isRecording) return 'recording';
- if (isProcessing) return 'processing';
- if (isHovered && !isRecording && !isProcessing) return 'hover';
- return 'idle';
+ if (isRecording) return "recording";
+ if (isProcessing) return "processing";
+ if (isHovered && !isRecording && !isProcessing) return "hover";
+ return "idle";
};
-
+
const micState = getMicState();
const isListening = isRecording || isProcessing;
-
+
// Get microphone button properties based on state
const getMicButtonProps = () => {
- const baseClasses = 'rounded-full w-10 h-10 flex items-center justify-center relative overflow-hidden border-2 border-white/70 cursor-pointer';
-
+ const baseClasses =
+ "rounded-full w-10 h-10 flex items-center justify-center relative overflow-hidden border-2 border-white/70 cursor-pointer";
+
switch (micState) {
- case 'idle':
+ case "idle":
return {
className: `${baseClasses} bg-black/50 cursor-pointer`,
- tooltip: "Click to speak"
+ tooltip: "Click to speak",
};
- case 'hover':
+ case "hover":
return {
className: `${baseClasses} bg-black/50 cursor-pointer`,
- tooltip: "Click to speak"
+ tooltip: "Click to speak",
};
- case 'recording':
+ case "recording":
return {
className: `${baseClasses} bg-blue-600 cursor-pointer`,
- tooltip: "Recording..."
+ tooltip: "Recording...",
};
- case 'processing':
+ case "processing":
return {
className: `${baseClasses} bg-purple-600 cursor-not-allowed`,
- tooltip: "Processing..."
+ tooltip: "Processing...",
};
default:
return {
className: `${baseClasses} bg-black/50 cursor-pointer`,
- style: { transform: 'scale(0.8)' },
- tooltip: "Click to speak"
+ style: { transform: "scale(0.8)" },
+ tooltip: "Click to speak",
};
}
};
-
+
const micProps = getMicButtonProps();
-
+
return (
<>
-
{/* Fixed bottom-right voice button */}
<div className="fixed bottom-6 right-6 z-50">
- <Tooltip
- content={micProps.tooltip}
- >
+ <Tooltip content={micProps.tooltip}>
<button
onClick={toggleListening}
onMouseEnter={() => setIsHovered(true)}
@@ -346,40 +372,53 @@ export default function App() {
onFocus={() => setIsHovered(true)}
onBlur={() => setIsHovered(false)}
className={micProps.className}
- disabled={micState === 'processing'}
+ disabled={micState === "processing"}
style={{
...micProps.style,
- cursor: micState === 'processing' ? 'not-allowed !important' : 'pointer !important',
- transition: 'transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.25s ease-out'
+ cursor:
+ micState === "processing"
+ ? "not-allowed !important"
+ : "pointer !important",
+ transition:
+ "transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.25s ease-out",
}}
>
{/* Background effects */}
- <div className="absolute inset-0 bg-gradient-to-br from-white/10 to-transparent transition-opacity duration-150" style={{ opacity: micState === 'hover' ? 0.8 : 0 }}></div>
- <div className="absolute inset-0 transition-colors duration-150" style={{ backgroundColor: micState === 'hover' ? 'rgba(0,0,0,0.1)' : 'transparent' }}></div>
-
+ <div
+ className="absolute inset-0 bg-gradient-to-br from-white/10 to-transparent transition-opacity duration-150"
+ style={{ opacity: micState === "hover" ? 0.8 : 0 }}
+ ></div>
+ <div
+ className="absolute inset-0 transition-colors duration-150"
+ style={{
+ backgroundColor:
+ micState === "hover" ? "rgba(0,0,0,0.1)" : "transparent",
+ }}
+ ></div>
+
{/* Dynamic content based on state */}
- {micState === 'idle' || micState === 'hover' ? (
- <SoundWaveIcon size={micState === 'idle' ? 12 : 14} />
- ) : micState === 'recording' ? (
+ {micState === "idle" || micState === "hover" ? (
+ <SoundWaveIcon size={micState === "idle" ? 12 : 14} />
+ ) : micState === "recording" ? (
<LoadingDots />
- ) : micState === 'processing' ? (
+ ) : micState === "processing" ? (
<VoiceWaveIndicator isListening={true} />
) : null}
-
+
{/* State indicator ring for recording */}
- {micState === 'recording' && (
+ {micState === "recording" && (
<div className="absolute inset-0 rounded-full border-2 border-blue-300 animate-pulse"></div>
)}
-
+
{/* State indicator ring for processing */}
- {micState === 'processing' && (
+ {micState === "processing" && (
<div className="absolute inset-0 rounded-full border-2 border-purple-300 opacity-50"></div>
)}
</button>
</Tooltip>
</div>
-
- <Toast message={error} onClose={() => setError('')} />
+
+ <Toast message={error} onClose={() => setError("")} />
</>
);
}
diff --git a/src/helpers/whisper.js b/src/helpers/whisper.js
index 117fbbb20..ce6d0a1b9 100644
--- a/src/helpers/whisper.js
+++ b/src/helpers/whisper.js
@@ -10,149 +10,153 @@ class WhisperManager {
}
async transcribeLocalWhisper(audioBlob, options = {}) {
- try {
- console.log("🎀 Starting local Whisper transcription...");
-
- const tempDir = os.tmpdir();
- const filename = `whisper_audio_${crypto.randomUUID()}.wav`;
- const tempAudioPath = path.join(tempDir, filename);
-
- console.log("πŸ’Ύ Writing audio to temp file:", tempAudioPath);
-
- // Handle different audio data formats
- let buffer;
- if (audioBlob instanceof ArrayBuffer) {
- buffer = Buffer.from(audioBlob);
- } else if (audioBlob instanceof Uint8Array) {
- buffer = Buffer.from(audioBlob);
- } else if (typeof audioBlob === "string") {
- buffer = Buffer.from(audioBlob, "base64");
- } else if (audioBlob && audioBlob.buffer) {
- buffer = Buffer.from(audioBlob.buffer);
- } else {
- throw new Error(`Unsupported audio data type: ${typeof audioBlob}`);
- }
+ const tempAudioPath = await this.createTempAudioFile(audioBlob);
+ const model = options.model || "base";
+ const language = options.language || null;
- fs.writeFileSync(tempAudioPath, buffer);
+ try {
+ const result = await this.runWhisperProcess(
+ tempAudioPath,
+ model,
+ language
+ );
+ return this.parseWhisperResult(result);
+ } catch (error) {
+ console.error("❌ Local Whisper transcription error:", error);
+ throw error;
+ } finally {
+ this.cleanupTempFile(tempAudioPath);
+ }
+ }
- const model = options.model || "base";
- const language = options.language || null;
+ async createTempAudioFile(audioBlob) {
+ const tempDir = os.tmpdir();
+ const filename = `whisper_audio_${crypto.randomUUID()}.wav`;
+ const tempAudioPath = path.join(tempDir, filename);
+
+ let buffer;
+ if (audioBlob instanceof ArrayBuffer) {
+ buffer = Buffer.from(audioBlob);
+ } else if (audioBlob instanceof Uint8Array) {
+ buffer = Buffer.from(audioBlob);
+ } else if (typeof audioBlob === "string") {
+ buffer = Buffer.from(audioBlob, "base64");
+ } else if (audioBlob && audioBlob.buffer) {
+ buffer = Buffer.from(audioBlob.buffer);
+ } else {
+ throw new Error(`Unsupported audio data type: ${typeof audioBlob}`);
+ }
- const pythonCmd = await this.findPythonExecutable();
- const whisperScriptPath = path.join(
- __dirname,
- "..",
- "..",
- "whisper_bridge.py"
- );
+ fs.writeFileSync(tempAudioPath, buffer);
+ return tempAudioPath;
+ }
- const args = [whisperScriptPath, tempAudioPath, "--model", model];
- if (language) {
- args.push("--language", language);
- }
- args.push("--output-format", "json");
+ async runWhisperProcess(tempAudioPath, model, language) {
+ const pythonCmd = await this.findPythonExecutable();
+ const whisperScriptPath = path.join(
+ __dirname,
+ "..",
+ "..",
+ "whisper_bridge.py"
+ );
+ const args = [whisperScriptPath, tempAudioPath, "--model", model];
+ if (language) {
+ args.push("--language", language);
+ }
+ args.push("--output-format", "json");
- console.log("πŸ”§ Running Whisper command:", pythonCmd, args.join(" "));
+ return new Promise((resolve, reject) => {
+ const whisperProcess = spawn(pythonCmd, args, {
+ stdio: ["ignore", "pipe", "pipe"],
+ windowsHide: true,
+ });
- return new Promise((resolve, reject) => {
- const whisperProcess = spawn(pythonCmd, args);
+ let stdout = "";
+ let stderr = "";
+ let isResolved = false;
- let stdout = "";
- let stderr = "";
+ // Set timeout for faster failure detection
+ const timeout = setTimeout(() => {
+ if (!isResolved) {
+ whisperProcess.kill("SIGTERM");
+ reject(new Error("Whisper transcription timed out (15 seconds)"));
+ }
+ }, 15000);
- whisperProcess.stdout.on("data", (data) => {
- stdout += data.toString();
- });
+ whisperProcess.stdout.on("data", (data) => {
+ stdout += data.toString();
+ });
- whisperProcess.stderr.on("data", (data) => {
- stderr += data.toString();
+ whisperProcess.stderr.on("data", (data) => {
+ stderr += data.toString();
+ // Reduce logging verbosity
+ if (
+ data.toString().includes("Error") ||
+ data.toString().includes("failed")
+ ) {
console.log("Whisper log:", data.toString());
- });
+ }
+ });
- whisperProcess.on("close", (code) => {
- // Clean up temp file
- try {
- fs.unlinkSync(tempAudioPath);
- console.log("πŸ—‘οΈ Cleaned up temp audio file");
- } catch (cleanupError) {
- console.warn(
- "⚠️ Could not clean up temp file:",
- cleanupError.message
- );
- }
+ whisperProcess.on("close", (code) => {
+ if (isResolved) return;
+ isResolved = true;
+ clearTimeout(timeout);
- if (code === 0) {
- try {
- const result = JSON.parse(stdout);
+ if (code === 0) {
+ resolve(stdout);
+ } else {
+ reject(
+ new Error(`Whisper transcription failed (code ${code}): ${stderr}`)
+ );
+ }
+ });
- // Check if the transcription is empty or just whitespace
- if (!result.text || result.text.trim().length === 0) {
- console.log(
- "πŸ›‘ No meaningful audio content detected (empty transcription)"
- );
- resolve({ success: false, message: "No audio detected" });
- return;
- }
-
- console.log(
- "βœ… Whisper transcription successful:",
- result.text?.substring(0, 50) + "..."
- );
- resolve(result);
- } catch (parseError) {
- console.error("❌ Failed to parse Whisper output:", parseError);
- reject(
- new Error(
- `Failed to parse Whisper output: ${parseError.message}`
- )
- );
- }
- } else {
- if (stderr.includes("no audio") || stderr.includes("empty")) {
- console.log("πŸ›‘ No audio content detected");
- resolve({ success: false, message: "No audio detected" });
- } else {
- console.error("❌ Whisper process failed with code:", code);
- console.error("Stderr:", stderr);
- reject(
- new Error(
- `Whisper transcription failed (code ${code}): ${stderr}`
- )
- );
- }
- }
- });
+ whisperProcess.on("error", (error) => {
+ if (isResolved) return;
+ isResolved = true;
+ clearTimeout(timeout);
+ reject(new Error(`Whisper process error: ${error.message}`));
+ });
+ });
+ }
- whisperProcess.on("error", (error) => {
- try {
- fs.unlinkSync(tempAudioPath);
- } catch (cleanupError) {
- console.warn(
- "⚠️ Could not clean up temp file:",
- cleanupError.message
- );
- }
+ parseWhisperResult(stdout) {
+ try {
+ // Clean stdout by removing any non-JSON content
+ const lines = stdout.split("\n").filter((line) => line.trim());
+ let jsonLine = "";
+
+ // Find the line that looks like JSON (starts with { and ends with })
+ for (const line of lines) {
+ const trimmed = line.trim();
+ if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
+ jsonLine = trimmed;
+ break;
+ }
+ }
- console.error("❌ Whisper process error:", error);
- reject(new Error(`Whisper process error: ${error.message}`));
- });
+ if (!jsonLine) {
+ throw new Error("No JSON output found in Whisper response");
+ }
- setTimeout(() => {
- whisperProcess.kill("SIGTERM");
- try {
- fs.unlinkSync(tempAudioPath);
- } catch (cleanupError) {
- console.warn(
- "⚠️ Could not clean up temp file:",
- cleanupError.message
- );
- }
- reject(new Error("Whisper transcription timed out (30 seconds)"));
- }, 30000);
- });
- } catch (error) {
- console.error("❌ Local Whisper transcription error:", error);
- throw error;
+ const result = JSON.parse(jsonLine);
+ if (!result.text || result.text.trim().length === 0) {
+ return { success: false, message: "No audio detected" };
+ }
+ return { success: true, text: result.text.trim() };
+ } catch (parseError) {
+ console.error("Raw stdout:", stdout);
+ throw new Error(`Failed to parse Whisper output: ${parseError.message}`);
+ }
+ }
+
+ cleanupTempFile(tempAudioPath) {
+ try {
+ fs.unlinkSync(tempAudioPath);
+ console.log("πŸ—‘οΈ Cleaned up temp audio file");
+ } catch (cleanupError) {
+ console.warn("⚠️ Could not clean up temp file:", cleanupError.message);
}
}
diff --git a/whisper_bridge.py b/whisper_bridge.py
index f2b12b4bc..16fac68cc 100755
--- a/whisper_bridge.py
+++ b/whisper_bridge.py
@@ -289,27 +289,31 @@ def delete_model(model_name="base"):
def transcribe_audio(audio_path, model_name="base", language=None):
"""Transcribe audio file using Whisper"""
+ print(f"Starting transcription with model: {model_name}", file=sys.stderr)
+
try:
# Load model
model = load_model(model_name)
if model is None:
- return {"error": "Failed to load Whisper model"}
+ return {"error": "Failed to load Whisper model", "success": False}
- # Transcribe
+ # Transcribe with minimal logging to avoid stdout pollution
print(f"Transcribing audio file: {audio_path}", file=sys.stderr)
- # Set transcription options
- options = {}
+ # Set transcription options for performance
+ options = {
+ "fp16": False, # Use FP32 for better compatibility
+ "verbose": False, # Reduce logging overhead
+ }
if language:
options["language"] = language
result = model.transcribe(audio_path, **options)
- # Return results
+ # Return results - only JSON to stdout
return {
- "text": result["text"].strip(),
+ "text": result.get("text", "").strip(),
"language": result.get("language", "unknown"),
- "segments": result.get("segments", []),
"success": True
}