Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

START OF PREVIOUS DISCUSSION END OF PREVIOUS DISCUSSION

  1. User

    START OF PREVIOUS DISCUSSION END OF PREVIOUS DISCUSSION user: function getStream(){ const apiUrl = 'https://pandora-s-playground-api.onrender.com/get_talk';

    let txt = document.querySelector('.overlay-section p').textContent.toLowerCase()
    
    const data = {
        history: [['user',txt]],
    };
    
    async function fetchStreamPost() {
    try {
        const response = await fetch(apiUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
        });
    
        if (!response.ok) {
        throw new Error(`Network response was not ok: ${response.statusText}`);
        }
    
        const reader = response.body.getReader();
    
        let fullData=""
        while (true) {
            const { done, value } = await reader.read();
    
            if (done) {
                break;
            }
    
            const postData = new TextDecoder().decode(value);
            fullData+=postData;
            console.log('Streamed post:', postData);
            if(fullData.endsWith('.') || fullData.endsWith(',') || fullData.endsWith('?') || fullData.endsWith('!')){
                talkVis(fullData);
                fullData="";
            }
        }
    } catch (error) {
        console.error('Error fetching stream post:', error.message);
    }
    }
    
    fetchStreamPost();
    

    }

    ive a problem with this code, the talkVis function that i made allows it to text2speech, however it uses webspeechapi, and i think it doesnt store the amount of calls? like i want it to say everything but its receiving the data faster than it says

  2. Assistant

    Your function getStream fetches data as a stream and attempts to process that data as it arrives. Your description indicates that you have an issue because your talkVis function, which uses the Web Speech API, isn't able to speak the incoming text fast enough to keep up with the stream. This is a common issue when dealing with streaming data and text-to-speech (TTS): the TTS service may not be able to speak as quickly as data arrives, leading to potentially missed information or buffer overflows.

    Here's how you can potentially handle this:

    1. Queue the incoming data: Rather than trying to immediately send all incoming text segments to the talkVis function, you can queue them and make sure that only one piece of text is spoken at a time.

    2. Handle talkVis scheduling: Use a mechanism to determine when the TTS has finished speaking the current text, and only then deque the next available text segment.

    Here's an example of how your code might be augmented to handle this with a basic queue system:

    function getStream() {
        const apiUrl = 'https://pandora-s-playground-api.onrender.com/get_talk';
        let txt = document.querySelector('.overlay-section p').textContent.toLowerCase();
    
        const data = {
            history: [['user', txt]],
        };
    
        const speechQueue = [];
        let speaking = false;
    
        async function fetchStreamPost() {
            // ... same as before ...
    
            const reader = response.body.getReader();
    
            let fullData = "";
            while (true) {
                const { done, value } = await reader.read();
    
                if (done) break;
                
                const fragment = new TextDecoder().decode(value);
                fullData += fragment;
                console.log('Streamed fragment:', fragment);
    
                if (fullData.endsWith('.') || fullData.endsWith(',') || fullData.endsWith('?') || fullData.endsWith('!')) {
                    speechQueue.push(fullData);
                    fullData = "";
                    if (!speaking) speakNextSegment();
                }
            }
        } catch (error) {
    
            // ... same as before ...
    
        }
    
        // Call this once to start the fetch operation
        fetchStreamPost();
    }
    
    function speakNextSegment() {
        if (speechQueue.length === 0) {
            speaking = false;
            return;
        }
    
        speaking = true;
        const textSegment = speechQueue.shift();
        talkVis(textSegment, function() {
            // This callback is called after the speech is finished
            speakNextSegment();
        });
    }
    
    // Modify your talkVis to accept a callback when finished speaking
    function talkVis(text, callback) {
        const utterance = new SpeechSynthesisUtterance(text);
        
        utterance.onend = function(event) {
            console.log('SpeechSynthesisUtterance.onend');
            if (typeof callback === "function") callback();
        };
    
        utterance.onerror = function(event) {
            console.error('SpeechSynthesisUtterance.onerror');
            if (typeof callback === "function") callback();
        };
    
        speechSynthesis.speak(utterance);
    }
    

    This code adds a speechQueue and ensures that talkVis is called one by one by using speakNextSegment. It also modifies the talkVis function to take a callback that is used to chain the speaking of the queued texts.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.