document.addEventListener('DOMContentLoaded', () => { const convo = document.querySelector(".convo"); const fileUpload = document.getElementById('file-upload'); const imageUpload = document.getElementById('image-upload'); const fileBtn = document.getElementById('file-btn'); const imageBtn = document.getElementById('image-btn'); const sendButtons = document.querySelectorAll('.sendingQA'); const SummarizeInput = document.querySelector(".SummarizeInput"); const CaptionInput = document.querySelector(".CaptionInput"); const gotItButton = document.getElementById('got-it-btn'); const explainChoixDiv = document.getElementById('explainChoix'); let selectedFile = null; // ✅ Default mode: Summarize selected const summarizeRadio = document.getElementById('summarize-radio'); if (summarizeRadio) summarizeRadio.checked = true; // Mode switching document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => { radio.addEventListener('change', (e) => { if (e.target.checked) { const selectedValue = e.target.value; if (selectedValue === "Summarize") { SummarizeInput.classList.add("active"); SummarizeInput.classList.remove("innactive"); CaptionInput.classList.remove("active"); CaptionInput.classList.add("innactive"); } else { SummarizeInput.classList.add("innactive"); SummarizeInput.classList.remove("active"); CaptionInput.classList.remove("innactive"); CaptionInput.classList.add("active"); } } }); }); // File upload handlers fileBtn.addEventListener('click', () => fileUpload.click()); imageBtn.addEventListener('click', () => imageUpload.click()); fileUpload.addEventListener('change', (e) => { if (e.target.files.length) { selectedFile = e.target.files[0]; handleFileUpload(selectedFile); } }); imageUpload.addEventListener('change', (e) => { if (e.target.files.length) { selectedFile = e.target.files[0]; handleFileUpload(selectedFile); } }); // "Got it" button gotItButton.addEventListener('click', () => { explainChoixDiv.style.display = "none"; }); async function handleFileUpload(file) { if (!file) return; const isImage = file.type.startsWith('image/'); const icon = isImage ? '🖼️' : '📎'; // User bubble: Uploaded file createMessageBubble(`${icon} Uploaded: ${file.name}`, "You"); const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize'; const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/'; const thinkingText = isSummarizeMode ? 'Analyzing document...
' : 'Analyzing image... '; // AI thinking bubble const thinkingBubble = createMessageBubble(thinkingText, "Aidan"); const formData = new FormData(); formData.append('file', file); if (isSummarizeMode) { const selectedLength = document.querySelector('input[name="length"]:checked')?.value || 'medium'; formData.append('length', selectedLength); } try { const response = await fetch(endpoint, { method: 'POST', body: formData }); if (!response.ok) { let errorMessage = 'Request failed'; try { const error = await response.json(); errorMessage = error.detail || error.error || errorMessage; } catch (e) {} throw new Error(errorMessage); } const result = await response.json(); thinkingBubble.remove(); // Aidan's response bubble if (isSummarizeMode) { createMessageBubble( result.summary || "No summary generated.", "Aidan", result.audioUrl, result.pdfUrl ); } else { createMessageBubble( result.caption || result.answer || "No caption generated.", "Aidan", result.audio, null ); } } catch (error) { thinkingBubble.remove(); createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan"); } finally { resetFileInputs(); } } function createMessageBubble(text, sender = "You", audioSrc = null, fileUrl = null) { const bubble = document.createElement('div'); bubble.className = `bubble ${sender === "You" ? "right" : "left"}`; bubble.style.maxWidth = "50%"; bubble.style.wordWrap = "break-word"; const label = document.createElement('div'); label.className = "label"; label.textContent = sender; const message = document.createElement('div'); message.className = "text"; message.style.whiteSpace = "pre-wrap"; message.innerHTML = text; // Extra controls (audio/PDF download) if (sender !== "You" && (audioSrc || fileUrl)) { const iconContainer = document.createElement('div'); iconContainer.style.marginTop = "10px"; iconContainer.style.display = "flex"; iconContainer.style.gap = "15px"; iconContainer.style.justifyContent = "flex-end"; if (audioSrc) { const audio = new Audio(audioSrc); const audioIcon = document.createElement("i"); audioIcon.className = "fa-solid fa-volume-high audio-toggle"; audioIcon.title = "Play Audio"; audioIcon.style.cursor = "pointer"; audioIcon.style.fontSize = "18px"; audioIcon.addEventListener("click", () => { if (audio.paused) { audio.play(); audioIcon.title = "Pause Audio"; } else { audio.pause(); audioIcon.title = "Play Audio"; } }); iconContainer.appendChild(audioIcon); } if (fileUrl) { const downloadLink = document.createElement('a'); downloadLink.href = fileUrl; downloadLink.setAttribute('download', 'summary.pdf'); downloadLink.target = "_blank"; const downloadIcon = document.createElement("i"); downloadIcon.className = "fa-solid fa-file-arrow-down"; downloadIcon.style.fontSize = "18px"; downloadIcon.style.cursor = "pointer"; downloadLink.appendChild(downloadIcon); iconContainer.appendChild(downloadLink); } message.appendChild(iconContainer); } bubble.appendChild(label); bubble.appendChild(message); convo.appendChild(bubble); convo.scrollTop = convo.scrollHeight; return bubble; } function resetFileInputs() { selectedFile = null; fileUpload.value = ''; imageUpload.value = ''; } // Loader CSS const style = document.createElement('style'); style.textContent = ` .loader { display: inline-block; border: 2px solid #f3f3f3; border-top: 2px solid #3b82f6; border-radius: 50%; width: 16px; height: 16px; animation: spin 1s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } `; document.head.appendChild(style); });