From b110cafbff741cfb7d6ac4b0ac146314d273c48f Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Sun, 9 Feb 2025 18:00:19 -0700 Subject: [PATCH] Update error handling for TTS API --- src/components/Footer.tsx | 4 ++-- src/contexts/TTSContext.tsx | 48 +++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx index 72fae59..8a45f84 100644 --- a/src/components/Footer.tsx +++ b/src/components/Footer.tsx @@ -20,8 +20,8 @@ export function Footer() { Privacy info -

No data collection. Documents are uploaded to your local browser cache.

-

Each sentence of the document you are viewing is sent to my FastAPI server for audio generation, no requests or data is collected.

+

Documents are uploaded to your local browser cache.

+

Each sentence of the document you are viewing is sent to my Kokoro-FastAPI server for audio generation, no requests or data is collected.

diff --git a/src/contexts/TTSContext.tsx b/src/contexts/TTSContext.tsx index 6eadc35..295dda1 100644 --- a/src/contexts/TTSContext.tsx +++ b/src/contexts/TTSContext.tsx @@ -386,22 +386,35 @@ export function TTSProvider({ children }: { children: React.ReactNode }) { // If not cached, fetch the audio from OpenAI API if (openaiRef.current) { - console.log('Requesting audio for sentence:', sentence); + try { + console.log('Requesting audio for sentence:', sentence); - const response = await openaiRef.current.audio.speech.create({ - model: 'tts-1', - voice: voice as "alloy", - input: sentence, - speed: speed, - }); + const response = await openaiRef.current.audio.speech.create({ + model: 'tts-1', + voice: voice as "alloy", + input: sentence, + speed: speed, + }); - const arrayBuffer = await response.arrayBuffer(); - const audioBuffer = await audioContext!.decodeAudioData(arrayBuffer); + const arrayBuffer = await response.arrayBuffer(); + const audioBuffer = await audioContext!.decodeAudioData(arrayBuffer); - // Cache the audio buffer - audioCache.set(sentence, audioBuffer); + // Cache the audio buffer + audioCache.set(sentence, audioBuffer); - return audioBuffer; + return audioBuffer; + } catch (error) { + setIsPlaying(false); + toast.error('Failed to generate audio. API not responding.', { + id: 'tts-api-error', + style: { + background: 'var(--background)', + color: 'var(--accent)', + }, + duration: 7000, + }); + throw error; + } } }, [audioContext, voice, speed, audioCache]); @@ -474,7 +487,16 @@ export function TTSProvider({ children }: { children: React.ReactNode }) { console.error('Error playing TTS:', error); setActiveHowl(null); setIsProcessing(false); - //setIsPlaying(false); // Stop playback on error + //setIsPlaying(false); + + toast.error('Failed to process audio. Skipping problematic sentence.', { + id: 'tts-processing-error', + style: { + background: 'var(--background)', + color: 'var(--accent)', + }, + duration: 3000, + }); advance(); // Skip problematic sentence }