Update error handling for TTS API
This commit is contained in:
parent
a5ea48ed3d
commit
b110cafbff
2 changed files with 37 additions and 15 deletions
|
|
@ -20,8 +20,8 @@ export function Footer() {
|
||||||
Privacy info
|
Privacy info
|
||||||
</PopoverButton>
|
</PopoverButton>
|
||||||
<PopoverPanel anchor="top" className="bg-base p-4 rounded-lg shadow-lg w-64">
|
<PopoverPanel anchor="top" className="bg-base p-4 rounded-lg shadow-lg w-64">
|
||||||
<p>No data collection. Documents are uploaded to your local browser cache.</p>
|
<p>Documents are uploaded to your local browser cache.</p>
|
||||||
<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>
|
<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>
|
||||||
</PopoverPanel>
|
</PopoverPanel>
|
||||||
</Popover>
|
</Popover>
|
||||||
<span className='w-full sm:w-fit'>•</span>
|
<span className='w-full sm:w-fit'>•</span>
|
||||||
|
|
|
||||||
|
|
@ -386,22 +386,35 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
|
||||||
|
|
||||||
// If not cached, fetch the audio from OpenAI API
|
// If not cached, fetch the audio from OpenAI API
|
||||||
if (openaiRef.current) {
|
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({
|
const response = await openaiRef.current.audio.speech.create({
|
||||||
model: 'tts-1',
|
model: 'tts-1',
|
||||||
voice: voice as "alloy",
|
voice: voice as "alloy",
|
||||||
input: sentence,
|
input: sentence,
|
||||||
speed: speed,
|
speed: speed,
|
||||||
});
|
});
|
||||||
|
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
const audioBuffer = await audioContext!.decodeAudioData(arrayBuffer);
|
const audioBuffer = await audioContext!.decodeAudioData(arrayBuffer);
|
||||||
|
|
||||||
// Cache the audio buffer
|
// Cache the audio buffer
|
||||||
audioCache.set(sentence, audioBuffer);
|
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]);
|
}, [audioContext, voice, speed, audioCache]);
|
||||||
|
|
||||||
|
|
@ -474,7 +487,16 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
|
||||||
console.error('Error playing TTS:', error);
|
console.error('Error playing TTS:', error);
|
||||||
setActiveHowl(null);
|
setActiveHowl(null);
|
||||||
setIsProcessing(false);
|
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
|
advance(); // Skip problematic sentence
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue