I’m working on voice chatbot, everything works fine, but when the response is 2 or more paragraph it throughs out an error “There was an error with the text-to-speech request: Error: HTTP error! status: 504
at textToSpeech”. I used openAI TTS API, it’s online voicebot i’m developing so the code is in JS.
async function textToSpeech(text) {
stopRecognition();
const endpoint = 'https://server.com/api/openaiProxy';
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: 'tts', // Indicate that this is a TTS request
data: { // Data for the TTS request
model: "tts-1",
voice: "alloy",
input: text
}
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const audioData = await response.blob();
// Play the audio blob with an audio element
const audioUrl = URL.createObjectURL(audioData);
const audio = new Audio(audioUrl);
programmaticRestart = false;
setVoiceButtonState("LISTENING");
audio.play();
// Update the UI to reflect that the assistant has finished speaking
audio.onended = () => {
if (!manuallyStopped) {
onAudioEnd();
startRecognition();
console.log("voice message end.");
}
};
} catch (error) {
console.error('There was an error with the text-to-speech request:', error);
startRecognition();
}
}