File size: 10,081 Bytes
89a3550
22c1e9b
c48e937
 
 
 
22c1e9b
 
 
a060b87
deb3326
89a3550
22c1e9b
89a3550
7d1820b
 
 
 
22c1e9b
727a479
89a3550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22c1e9b
89a3550
 
 
22c1e9b
 
 
 
 
 
 
 
 
 
89a3550
 
4b810e0
a060b87
 
 
89a3550
4b810e0
 
 
 
e6909d8
a172391
d1945c7
 
 
 
 
a060b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1945c7
 
 
a060b87
 
 
 
 
d1945c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a060b87
2a8891b
89a3550
4b810e0
7d1820b
 
 
 
 
 
 
 
 
 
 
 
4b810e0
 
 
 
 
 
7d1820b
4b810e0
7d1820b
 
 
e6909d8
4b810e0
7d1820b
 
 
 
 
 
 
 
 
 
 
 
4b810e0
 
 
7d1820b
 
4b810e0
 
 
7d1820b
 
 
 
 
8e4e74f
4b810e0
7d1820b
4b810e0
7d1820b
d1945c7
8e4e74f
7d1820b
 
 
 
8e4e74f
7d1820b
 
 
8e4e74f
7d1820b
22c1e9b
89a3550
7d1820b
 
 
 
 
2a8891b
22c1e9b
c3e40ec
89a3550
 
 
22c1e9b
89a3550
 
 
 
 
 
 
 
 
 
 
4b810e0
 
 
 
89a3550
 
a060b87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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");
    var gotItButton = document.querySelector('.explainChoix button');
    var explainChoixDiv = document.querySelector('.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];
        }
    });

    imageUpload.addEventListener('change', (e) => {
        if (e.target.files.length) {
            selectedFile = e.target.files[0];
        }
    });

    // "Got it" button to hide explanation div
    gotItButton.addEventListener('click', () => {
        explainChoixDiv.style.display = "none";
    });

    // Send button handlers
    sendButtons.forEach(button => {
        button.addEventListener('click', handleSubmit);
    });

    function displayFilePreview(file) {
        const newBubble = document.createElement("div");
        newBubble.className = "file-preview-bubble bubble right";
        newBubble.style.display = "flex";
        newBubble.style.flexDirection = "column";
        newBubble.style.maxWidth = "50%";

        // If it's an image
        if (file.type.startsWith('image/')) {
            const reader = new FileReader();
            reader.onload = (e) => {
                const img = document.createElement("img");
                img.src = e.target.result;
                img.style.width = "100%";
                img.style.height = "200px"; // You can adjust the height if you want
                img.style.objectFit = "cover"; // Makes it fill nicely
                img.style.borderRadius = "10px";
                img.style.marginBottom = "8px";

                const text = document.createElement("span");
                text.textContent = `πŸ“Ž Selected image: ${file.name}`;
                text.style.fontSize = "13px";

                newBubble.appendChild(img);
                newBubble.appendChild(text);
                convo.appendChild(newBubble);
                convo.scrollTop = convo.scrollHeight;
            };
            reader.readAsDataURL(file);
        } else {
            // Document preview stays as text
            const textPreview = `πŸ“Ž Selected document: ${file.name}`;
            const textNode = document.createElement('span');
            textNode.textContent = textPreview;
            newBubble.appendChild(textNode);
            convo.appendChild(newBubble);
            convo.scrollTop = convo.scrollHeight;
        }
    }

    async function handleSubmit() {
        if (!selectedFile) {
            alert("Please upload a file first");
            return;
        }

        const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
        const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
        const thinkingText = isSummarizeMode ? 'Processing document... <div class="loader"></div>' : "Generating caption... <div class='loader'></div>";
        const senderName = "Aidan";

        const thinkingBubble = createMessageBubble(thinkingText, senderName);

        const formData = new FormData();
        formData.append('file', selectedFile);
        if (isSummarizeMode) formData.append('length', 'medium');

        // Display file preview in new bubble
        displayFilePreview(selectedFile);

        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) {
                    // response not JSON
                }
                throw new Error(errorMessage);
            }

            const result = await response.json();
            thinkingBubble.remove();

            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");
        }
    }

    function createMessageBubble(text, sender = "You", audioSrc = null, fileName = 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.style.display = "flex";
        message.style.flexDirection = "column";

        const textSpan = document.createElement("span");
        textSpan.innerHTML = text;
        message.appendChild(textSpan);

        if (sender !== "You" && (audioSrc || fileName)) {
            const iconContainer = document.createElement('div');
            iconContainer.style.marginTop = "10px";
            iconContainer.style.display = "flex";
            iconContainer.style.justifyContent = "flex-end";
            iconContainer.style.gap = "15px";

            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.classList.remove("fa-volume-xmark");
                        audioIcon.classList.add("fa-volume-high");
                        audioIcon.title = "Mute Audio";
                    } else {
                        audio.pause();
                        audioIcon.classList.remove("fa-volume-high");
                        audioIcon.classList.add("fa-volume-xmark");
                        audioIcon.title = "Unmute Audio";
                    }
                });

                iconContainer.appendChild(audioIcon);
            }

            if (fileName) {
                const downloadLink = document.createElement('a');
                downloadLink.href = fileName;
                downloadLink.target = "_blank";
                //downloadLink.download = "summary.pdf"; // βœ… Suggest filename to browser

                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;
    }

    // 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); }
        }
        .audio-toggle {
            cursor: pointer;
            transition: all 0.2s;
        }
    `;
    document.head.appendChild(style);
});