Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update static/appS.js
Browse files- static/appS.js +83 -86
 
    	
        static/appS.js
    CHANGED
    
    | 
         @@ -7,12 +7,16 @@ document.addEventListener('DOMContentLoaded', () => { 
     | 
|
| 7 | 
         
             
                const sendButtons = document.querySelectorAll('.sendingQA');
         
     | 
| 8 | 
         
             
                const SummarizeInput = document.querySelector(".SummarizeInput");
         
     | 
| 9 | 
         
             
                const CaptionInput = document.querySelector(".CaptionInput");
         
     | 
| 10 | 
         
            -
                const gotItButton = document. 
     | 
| 11 | 
         
            -
                const explainChoixDiv = document. 
     | 
| 12 | 
         | 
| 13 | 
         
             
                let selectedFile = null;
         
     | 
| 14 | 
         
             
                let filePreviewBubble = null;
         
     | 
| 15 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 16 | 
         
             
                // Mode switching
         
     | 
| 17 | 
         
             
                document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
         
     | 
| 18 | 
         
             
                    radio.addEventListener('change', (e) => {
         
     | 
| 
         @@ -51,9 +55,7 @@ document.addEventListener('DOMContentLoaded', () => { 
     | 
|
| 51 | 
         
             
                    }
         
     | 
| 52 | 
         
             
                });
         
     | 
| 53 | 
         | 
| 54 | 
         
            -
                //  
     | 
| 55 | 
         
            -
             
     | 
| 56 | 
         
            -
                // β
 "Got it" button to hide explanation div
         
     | 
| 57 | 
         
             
                if (gotItButton) {
         
     | 
| 58 | 
         
             
                    gotItButton.addEventListener('click', () => {
         
     | 
| 59 | 
         
             
                        if (explainChoixDiv) {
         
     | 
| 
         @@ -76,86 +78,83 @@ document.addEventListener('DOMContentLoaded', () => { 
     | 
|
| 76 | 
         
             
                    );
         
     | 
| 77 | 
         
             
                }
         
     | 
| 78 | 
         | 
| 79 | 
         
            -
             
     | 
| 80 | 
         
            -
             
     | 
| 81 | 
         
            -
             
     | 
| 82 | 
         
            -
             
     | 
| 83 | 
         
            -
             
     | 
| 84 | 
         
            -
             
     | 
| 85 | 
         
            -
             
     | 
| 86 | 
         
            -
             
     | 
| 87 | 
         
            -
             
     | 
| 88 | 
         
            -
             
     | 
| 89 | 
         
            -
             
     | 
| 90 | 
         
            -
             
     | 
| 91 | 
         
            -
             
     | 
| 92 | 
         
            -
             
     | 
| 93 | 
         
            -
             
     | 
| 94 | 
         
            -
             
     | 
| 95 | 
         
            -
             
     | 
| 96 | 
         
            -
             
     | 
| 97 | 
         
            -
             
     | 
| 98 | 
         
            -
             
     | 
| 99 | 
         
            -
             
     | 
| 100 | 
         
            -
             
     | 
| 101 | 
         
            -
             
     | 
| 102 | 
         
            -
             
     | 
| 103 | 
         
            -
             
     | 
| 104 | 
         
            -
             
     | 
| 105 | 
         
            -
             
     | 
| 106 | 
         
            -
             
     | 
| 107 | 
         
            -
             
     | 
| 108 | 
         
            -
             
     | 
| 109 | 
         
            -
             
     | 
| 110 | 
         
            -
             
     | 
| 111 | 
         
            -
             
     | 
| 112 | 
         
            -
             
     | 
| 113 | 
         
            -
             
     | 
| 114 | 
         
            -
             
     | 
| 115 | 
         
            -
             
     | 
| 116 | 
         
            -
             
     | 
| 117 | 
         
            -
             
     | 
| 118 | 
         
            -
             
     | 
| 119 | 
         
            -
             
     | 
| 120 | 
         
            -
                                 
     | 
| 121 | 
         
            -
             
     | 
| 122 | 
         
            -
             
     | 
| 123 | 
         
            -
             
     | 
| 124 | 
         
            -
             
     | 
| 125 | 
         
            -
                                 
     | 
| 126 | 
         
            -
                            }
         
     | 
| 127 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 128 | 
         | 
| 129 | 
         
            -
                         
     | 
| 130 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 131 | 
         | 
| 132 | 
         
            -
             
     | 
| 133 | 
         
            -
             
     | 
| 134 | 
         
            -
             
     | 
| 135 | 
         
            -
             
     | 
| 136 | 
         
            -
                        downloadLink.target = "_blank";
         
     | 
| 137 | 
         
            -
                        downloadLink.title = "Download PDF";
         
     | 
| 138 | 
         | 
| 139 | 
         
            -
             
     | 
| 140 | 
         
            -
             
     | 
| 141 | 
         
            -
                         
     | 
| 142 | 
         
            -
                        downloadIcon.style.cursor = "pointer";
         
     | 
| 143 | 
         | 
| 144 | 
         
            -
                         
     | 
| 145 | 
         
            -
                        iconContainer.appendChild(downloadLink);
         
     | 
| 146 | 
         
             
                    }
         
     | 
| 147 | 
         | 
| 148 | 
         
            -
                     
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 149 | 
         
             
                }
         
     | 
| 150 | 
         | 
| 151 | 
         
            -
                bubble.appendChild(label);
         
     | 
| 152 | 
         
            -
                bubble.appendChild(message);
         
     | 
| 153 | 
         
            -
                convo.appendChild(bubble);
         
     | 
| 154 | 
         
            -
                convo.scrollTop = convo.scrollHeight;
         
     | 
| 155 | 
         
            -
                return bubble;
         
     | 
| 156 | 
         
            -
            }
         
     | 
| 157 | 
         
            -
             
     | 
| 158 | 
         
            -
             
     | 
| 159 | 
         
             
                async function handleSubmit() {
         
     | 
| 160 | 
         
             
                    if (!selectedFile) {
         
     | 
| 161 | 
         
             
                        alert("Please upload a file first");
         
     | 
| 
         @@ -165,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => { 
     | 
|
| 165 | 
         
             
                    const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
         
     | 
| 166 | 
         
             
                    const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
         
     | 
| 167 | 
         
             
                    const thinkingText = isSummarizeMode ? 'Processing document...' : "Generating caption... <div class='loader'></div>";
         
     | 
| 168 | 
         
            -
                    const senderName =  
     | 
| 169 | 
         | 
| 170 | 
         
             
                    const thinkingBubble = createMessageBubble(thinkingText, senderName);
         
     | 
| 171 | 
         | 
| 
         @@ -202,16 +201,17 @@ document.addEventListener('DOMContentLoaded', () => { 
     | 
|
| 202 | 
         
             
                            );
         
     | 
| 203 | 
         
             
                        } else {
         
     | 
| 204 | 
         
             
                            createMessageBubble(
         
     | 
| 205 | 
         
            -
                                 
     | 
| 206 | 
         
             
                                "Aidan",
         
     | 
| 207 | 
         
            -
                                result.audio
         
     | 
| 
         | 
|
| 208 | 
         
             
                            );
         
     | 
| 209 | 
         
             
                        }
         
     | 
| 210 | 
         
             
                    } catch (error) {
         
     | 
| 211 | 
         
             
                        thinkingBubble.remove();
         
     | 
| 212 | 
         
            -
                        createMessageBubble(`β οΈ Error: ${error.message}`,  
     | 
| 213 | 
         
             
                    } finally {
         
     | 
| 214 | 
         
            -
                        resetFileInputs();
         
     | 
| 215 | 
         
             
                    }
         
     | 
| 216 | 
         
             
                }
         
     | 
| 217 | 
         | 
| 
         @@ -219,10 +219,7 @@ document.addEventListener('DOMContentLoaded', () => { 
     | 
|
| 219 | 
         
             
                    selectedFile = null;
         
     | 
| 220 | 
         
             
                    fileUpload.value = '';
         
     | 
| 221 | 
         
             
                    imageUpload.value = '';
         
     | 
| 222 | 
         
            -
                     
     | 
| 223 | 
         
            -
                        filePreviewBubble.remove();
         
     | 
| 224 | 
         
            -
                        filePreviewBubble = null;
         
     | 
| 225 | 
         
            -
                    }
         
     | 
| 226 | 
         
             
                }
         
     | 
| 227 | 
         | 
| 228 | 
         
             
                // Loader CSS
         
     | 
| 
         | 
|
| 7 | 
         
             
                const sendButtons = document.querySelectorAll('.sendingQA');
         
     | 
| 8 | 
         
             
                const SummarizeInput = document.querySelector(".SummarizeInput");
         
     | 
| 9 | 
         
             
                const CaptionInput = document.querySelector(".CaptionInput");
         
     | 
| 10 | 
         
            +
                const gotItButton = document.getElementById('got-it-btn');
         
     | 
| 11 | 
         
            +
                const explainChoixDiv = document.getElementById('explainChoix');
         
     | 
| 12 | 
         | 
| 13 | 
         
             
                let selectedFile = null;
         
     | 
| 14 | 
         
             
                let filePreviewBubble = null;
         
     | 
| 15 | 
         | 
| 16 | 
         
            +
                // β
 Default mode: Summarize selected
         
     | 
| 17 | 
         
            +
                const summarizeRadio = document.getElementById('summarize-radio');
         
     | 
| 18 | 
         
            +
                if (summarizeRadio) summarizeRadio.checked = true;
         
     | 
| 19 | 
         
            +
             
     | 
| 20 | 
         
             
                // Mode switching
         
     | 
| 21 | 
         
             
                document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
         
     | 
| 22 | 
         
             
                    radio.addEventListener('change', (e) => {
         
     | 
| 
         | 
|
| 55 | 
         
             
                    }
         
     | 
| 56 | 
         
             
                });
         
     | 
| 57 | 
         | 
| 58 | 
         
            +
                // "Got it" button to hide explanation div
         
     | 
| 
         | 
|
| 
         | 
|
| 59 | 
         
             
                if (gotItButton) {
         
     | 
| 60 | 
         
             
                    gotItButton.addEventListener('click', () => {
         
     | 
| 61 | 
         
             
                        if (explainChoixDiv) {
         
     | 
| 
         | 
|
| 78 | 
         
             
                    );
         
     | 
| 79 | 
         
             
                }
         
     | 
| 80 | 
         | 
| 81 | 
         
            +
                function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
         
     | 
| 82 | 
         
            +
                    const bubble = document.createElement('div');
         
     | 
| 83 | 
         
            +
                    bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
         
     | 
| 84 | 
         
            +
                    bubble.style.maxWidth = "50%";
         
     | 
| 85 | 
         
            +
                    bubble.style.wordWrap = "break-word";
         
     | 
| 86 | 
         
            +
             
     | 
| 87 | 
         
            +
                    const label = document.createElement('div');
         
     | 
| 88 | 
         
            +
                    label.className = "label";
         
     | 
| 89 | 
         
            +
                    label.textContent = sender;
         
     | 
| 90 | 
         
            +
             
     | 
| 91 | 
         
            +
                    const message = document.createElement('div');
         
     | 
| 92 | 
         
            +
                    message.className = "text";
         
     | 
| 93 | 
         
            +
                    message.style.whiteSpace = "pre-wrap";
         
     | 
| 94 | 
         
            +
                    message.style.display = "flex";
         
     | 
| 95 | 
         
            +
                    message.style.flexDirection = "column";
         
     | 
| 96 | 
         
            +
             
     | 
| 97 | 
         
            +
                    const textSpan = document.createElement("span");
         
     | 
| 98 | 
         
            +
                    textSpan.innerHTML = text;
         
     | 
| 99 | 
         
            +
                    message.appendChild(textSpan);
         
     | 
| 100 | 
         
            +
             
     | 
| 101 | 
         
            +
                    if (sender !== "You" && (audioSrc || fileName)) {
         
     | 
| 102 | 
         
            +
                        const iconContainer = document.createElement('div');
         
     | 
| 103 | 
         
            +
                        iconContainer.style.marginTop = "10px";
         
     | 
| 104 | 
         
            +
                        iconContainer.style.display = "flex";
         
     | 
| 105 | 
         
            +
                        iconContainer.style.justifyContent = "flex-end";
         
     | 
| 106 | 
         
            +
                        iconContainer.style.gap = "15px";
         
     | 
| 107 | 
         
            +
             
     | 
| 108 | 
         
            +
                        if (audioSrc) {
         
     | 
| 109 | 
         
            +
                            const audio = new Audio(audioSrc);
         
     | 
| 110 | 
         
            +
                            const audioIcon = document.createElement("i");
         
     | 
| 111 | 
         
            +
                            audioIcon.className = "fa-solid fa-volume-high audio-toggle";
         
     | 
| 112 | 
         
            +
                            audioIcon.title = "Play Audio";
         
     | 
| 113 | 
         
            +
                            audioIcon.style.cursor = "pointer";
         
     | 
| 114 | 
         
            +
                            audioIcon.style.fontSize = "18px";
         
     | 
| 115 | 
         
            +
             
     | 
| 116 | 
         
            +
                            audioIcon.addEventListener("click", () => {
         
     | 
| 117 | 
         
            +
                                if (audio.paused) {
         
     | 
| 118 | 
         
            +
                                    audio.play();
         
     | 
| 119 | 
         
            +
                                    audioIcon.classList.remove("fa-volume-xmark");
         
     | 
| 120 | 
         
            +
                                    audioIcon.classList.add("fa-volume-high");
         
     | 
| 121 | 
         
            +
                                    audioIcon.title = "Mute Audio";
         
     | 
| 122 | 
         
            +
                                } else {
         
     | 
| 123 | 
         
            +
                                    audio.pause();
         
     | 
| 124 | 
         
            +
                                    audioIcon.classList.remove("fa-volume-high");
         
     | 
| 125 | 
         
            +
                                    audioIcon.classList.add("fa-volume-xmark");
         
     | 
| 126 | 
         
            +
                                    audioIcon.title = "Unmute Audio";
         
     | 
| 127 | 
         
            +
                                }
         
     | 
| 128 | 
         
            +
                            });
         
     | 
| 129 | 
         
            +
             
     | 
| 130 | 
         
            +
                            iconContainer.appendChild(audioIcon);
         
     | 
| 131 | 
         
            +
                        }
         
     | 
| 132 | 
         | 
| 133 | 
         
            +
                        if (fileName) {
         
     | 
| 134 | 
         
            +
                            const downloadLink = document.createElement('a');
         
     | 
| 135 | 
         
            +
                            downloadLink.href = fileName;
         
     | 
| 136 | 
         
            +
                            downloadLink.target = "_blank";
         
     | 
| 137 | 
         
            +
                            downloadLink.download = "summary.pdf"; // β
 Suggest filename to browser
         
     | 
| 138 | 
         | 
| 139 | 
         
            +
                            const downloadIcon = document.createElement("i");
         
     | 
| 140 | 
         
            +
                            downloadIcon.className = "fa-solid fa-file-arrow-down";
         
     | 
| 141 | 
         
            +
                            downloadIcon.style.fontSize = "18px";
         
     | 
| 142 | 
         
            +
                            downloadIcon.style.cursor = "pointer";
         
     | 
| 
         | 
|
| 
         | 
|
| 143 | 
         | 
| 144 | 
         
            +
                            downloadLink.appendChild(downloadIcon);
         
     | 
| 145 | 
         
            +
                            iconContainer.appendChild(downloadLink);
         
     | 
| 146 | 
         
            +
                        }
         
     | 
| 
         | 
|
| 147 | 
         | 
| 148 | 
         
            +
                        message.appendChild(iconContainer);
         
     | 
| 
         | 
|
| 149 | 
         
             
                    }
         
     | 
| 150 | 
         | 
| 151 | 
         
            +
                    bubble.appendChild(label);
         
     | 
| 152 | 
         
            +
                    bubble.appendChild(message);
         
     | 
| 153 | 
         
            +
                    convo.appendChild(bubble);
         
     | 
| 154 | 
         
            +
                    convo.scrollTop = convo.scrollHeight;
         
     | 
| 155 | 
         
            +
                    return bubble;
         
     | 
| 156 | 
         
             
                }
         
     | 
| 157 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 158 | 
         
             
                async function handleSubmit() {
         
     | 
| 159 | 
         
             
                    if (!selectedFile) {
         
     | 
| 160 | 
         
             
                        alert("Please upload a file first");
         
     | 
| 
         | 
|
| 164 | 
         
             
                    const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
         
     | 
| 165 | 
         
             
                    const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
         
     | 
| 166 | 
         
             
                    const thinkingText = isSummarizeMode ? 'Processing document...' : "Generating caption... <div class='loader'></div>";
         
     | 
| 167 | 
         
            +
                    const senderName = "Aidan";
         
     | 
| 168 | 
         | 
| 169 | 
         
             
                    const thinkingBubble = createMessageBubble(thinkingText, senderName);
         
     | 
| 170 | 
         | 
| 
         | 
|
| 201 | 
         
             
                            );
         
     | 
| 202 | 
         
             
                        } else {
         
     | 
| 203 | 
         
             
                            createMessageBubble(
         
     | 
| 204 | 
         
            +
                                result.caption || result.answer || "No caption generated.",
         
     | 
| 205 | 
         
             
                                "Aidan",
         
     | 
| 206 | 
         
            +
                                result.audio,
         
     | 
| 207 | 
         
            +
                                null
         
     | 
| 208 | 
         
             
                            );
         
     | 
| 209 | 
         
             
                        }
         
     | 
| 210 | 
         
             
                    } catch (error) {
         
     | 
| 211 | 
         
             
                        thinkingBubble.remove();
         
     | 
| 212 | 
         
            +
                        createMessageBubble(`β οΈ Error: ${error.message}`, "Aidan");
         
     | 
| 213 | 
         
             
                    } finally {
         
     | 
| 214 | 
         
            +
                        resetFileInputs(); // β
 Only reset upload inputs, KEEP preview bubble
         
     | 
| 215 | 
         
             
                    }
         
     | 
| 216 | 
         
             
                }
         
     | 
| 217 | 
         | 
| 
         | 
|
| 219 | 
         
             
                    selectedFile = null;
         
     | 
| 220 | 
         
             
                    fileUpload.value = '';
         
     | 
| 221 | 
         
             
                    imageUpload.value = '';
         
     | 
| 222 | 
         
            +
                    // β
 DO NOT remove file preview bubble anymore
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 223 | 
         
             
                }
         
     | 
| 224 | 
         | 
| 225 | 
         
             
                // Loader CSS
         
     |