document.addEventListener('DOMContentLoaded', () => { // UI Elements const GotitB = document.querySelector(".explainChoix button"); const explain = document.querySelector(".explainChoix"); const SummarizeInput = document.querySelector(".SummarizeInput"); const CaptionInput = document.querySelector(".CaptionInput"); 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 convo = document.querySelector('.convo'); // Hide explanation after click GotitB.addEventListener("click", () => { explain.style.opacity = "0"; }); // Handle mode switching document.querySelectorAll('.select-options input[type="radio"]').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()); // Send button handlers document.querySelectorAll('.sendingQA').forEach(button => { button.addEventListener('click', async () => { const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize'; if (isSummarizeMode) { await handleSummarize(); } else { await handleCaption(); } }); }); // Handle document summarization async function handleSummarize() { const file = fileUpload.files[0]; if (!file) { displayError('Please upload a document first'); return; } const length = document.querySelector('input[name="optionS"]:checked')?.value || 'medium'; try { // Show loading state convo.innerHTML = ''; displayFileInfo(file.name, 'document'); displayThinkingMessage(); // Call API const result = await summarizeDocument(file, length); // Display results displaySummaryResult(file.name, result.summary, result.audio_url, result.pdf_url); } catch (error) { displayError(error.message || 'Failed to summarize document'); } } // Handle image captioning async function handleCaption() { const file = imageUpload.files[0]; if (!file) { displayError('Please upload an image first'); return; } try { // Show loading state convo.innerHTML = ''; displayFileInfo(file.name, 'image'); displayThinkingMessage(); // Call API const result = await captionImage(file); // Display results displayCaptionResult(file.name, result.answer, result.audio); } catch (error) { displayError(error.message || 'Failed to generate caption'); } } // API Functions async function summarizeDocument(file, length) { const formData = new FormData(); formData.append('file', file); formData.append('length', length); const response = await fetch('/summarize/', { method: 'POST', body: formData }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Summarization failed'); } return await response.json(); } async function captionImage(file) { const formData = new FormData(); formData.append('file', file); const response = await fetch('/imagecaption/', { method: 'POST', body: formData }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Captioning failed'); } return await response.json(); } // UI Helper Functions function displayFileInfo(filename, type) { const bubble = document.createElement('div'); bubble.className = 'bubble right'; bubble.innerHTML = `