Skip to content

Commit b110caf

Browse files
committed
Update error handling for TTS API
1 parent a5ea48e commit b110caf

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/components/Footer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function Footer() {
2020
Privacy info
2121
</PopoverButton>
2222
<PopoverPanel anchor="top" className="bg-base p-4 rounded-lg shadow-lg w-64">
23-
<p>No data collection. Documents are uploaded to your local browser cache.</p>
24-
<p className='mt-3'>Each sentence of the document you are viewing is sent to my FastAPI server for audio generation, no requests or data is collected.</p>
23+
<p>Documents are uploaded to your local browser cache.</p>
24+
<p className='mt-3'>Each sentence of the document you are viewing is sent to my Kokoro-FastAPI server for audio generation, no requests or data is collected.</p>
2525
</PopoverPanel>
2626
</Popover>
2727
<span className='w-full sm:w-fit'></span>

src/contexts/TTSContext.tsx

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -386,22 +386,35 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
386386

387387
// If not cached, fetch the audio from OpenAI API
388388
if (openaiRef.current) {
389-
console.log('Requesting audio for sentence:', sentence);
390-
391-
const response = await openaiRef.current.audio.speech.create({
392-
model: 'tts-1',
393-
voice: voice as "alloy",
394-
input: sentence,
395-
speed: speed,
396-
});
389+
try {
390+
console.log('Requesting audio for sentence:', sentence);
391+
392+
const response = await openaiRef.current.audio.speech.create({
393+
model: 'tts-1',
394+
voice: voice as "alloy",
395+
input: sentence,
396+
speed: speed,
397+
});
397398

398-
const arrayBuffer = await response.arrayBuffer();
399-
const audioBuffer = await audioContext!.decodeAudioData(arrayBuffer);
399+
const arrayBuffer = await response.arrayBuffer();
400+
const audioBuffer = await audioContext!.decodeAudioData(arrayBuffer);
400401

401-
// Cache the audio buffer
402-
audioCache.set(sentence, audioBuffer);
402+
// Cache the audio buffer
403+
audioCache.set(sentence, audioBuffer);
403404

404-
return audioBuffer;
405+
return audioBuffer;
406+
} catch (error) {
407+
setIsPlaying(false);
408+
toast.error('Failed to generate audio. API not responding.', {
409+
id: 'tts-api-error',
410+
style: {
411+
background: 'var(--background)',
412+
color: 'var(--accent)',
413+
},
414+
duration: 7000,
415+
});
416+
throw error;
417+
}
405418
}
406419
}, [audioContext, voice, speed, audioCache]);
407420

@@ -474,7 +487,16 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
474487
console.error('Error playing TTS:', error);
475488
setActiveHowl(null);
476489
setIsProcessing(false);
477-
//setIsPlaying(false); // Stop playback on error
490+
//setIsPlaying(false);
491+
492+
toast.error('Failed to process audio. Skipping problematic sentence.', {
493+
id: 'tts-processing-error',
494+
style: {
495+
background: 'var(--background)',
496+
color: 'var(--accent)',
497+
},
498+
duration: 3000,
499+
});
478500

479501
advance(); // Skip problematic sentence
480502
}

0 commit comments

Comments
 (0)