Text to speech in chat GPT streaming response with google cloud api in next js

83 views Asked by At

I want to implement Google Cloud text-to-speech API in the streaming response of chat GPT in such a way that if first sentence i got call the API and audio will start play a second time 2 sentence call so on . Or you can suggest a Google Cloudfront-based library. I am using next js to achieve this.

 async function gpt(prompt) {
    try {
        setGptloading(true);
        const abortController = new AbortController();
        const message = JSON.parse(localStorage.getItem('AlImPTerIstRoISHAilighoRibLeCirearINaTEs'));
        const response = await fetch("/api/chat", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                message,
            }),
            signal: abortController.signal
        });
        if (!response.ok) {
            throw new Error(response.statusText);

        }
        const reader = response.body
            .pipeThrough(new TextDecoderStream())
            .getReader()
        setIsListening(false);
        setJustdata(true);
        setGptloading(false);
        let accumulatedText = '';
        let done = false;
        let firstSentenceProcessed = false;
        try {
            while (true) {
                const { value, done } = await reader.read();
                if (done) break;
                if (streamingStoppedRef.current) {
                    abortController.abort();
                }
                setText((prev) => prev + value);
                handleScroll();
                accumulatedText += value;
                const sentences = accumulatedText.split(/([.!?ред])/);
                console.log(sentences.length)
                while (sentences.length > 3) {
                    const sentenceToSpeak = sentences.shift() + sentences.shift();
                    if (!firstSentenceProcessed) {
                        googleaudio(sentenceToSpeak);
                        firstSentenceProcessed = true; // Set the flag to true to indicate that pqr() has been called
                    }
                }
            }
                            if (done == false) {
                setaddStopBtn(false);
            }
        } finally {
            reader.releaseLock();
        }
    } catch (error) {
        if (error.name === 'AbortError' || error.code === 'ECONNRESET') {
            toast.error("Stopped generating:", error);
        } else {
            toast.error("Stopped generating:", error);
            setGptloading(false);
        }
    }

}

  
0

There are 0 answers