Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +18 -57
static/appS.js
CHANGED
@@ -276,7 +276,7 @@ function injectAudioPlayer(bubble, audioUrl) {
|
|
276 |
audioContainer.appendChild(audioIcon);
|
277 |
messageDiv.appendChild(audioContainer);
|
278 |
} */
|
279 |
-
|
280 |
if (!selectedFile) {
|
281 |
alert("Please upload a file first");
|
282 |
return;
|
@@ -284,16 +284,17 @@ function injectAudioPlayer(bubble, audioUrl) {
|
|
284 |
|
285 |
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
286 |
|
287 |
-
//
|
288 |
const endpoint = isSummarizeMode
|
289 |
? '/summarization/summarize/'
|
290 |
: '/summarization/imagecaption/';
|
291 |
|
292 |
-
const thinkingText = isSummarizeMode
|
293 |
-
? 'Processing document ๐... <div class="loader"></div>'
|
294 |
-
: "Generating caption
|
|
|
295 |
|
296 |
-
const thinkingBubble = createMessageBubble(thinkingText,
|
297 |
|
298 |
const formData = new FormData();
|
299 |
formData.append('file', selectedFile);
|
@@ -306,80 +307,40 @@ function injectAudioPlayer(bubble, audioUrl) {
|
|
306 |
});
|
307 |
|
308 |
if (!response.ok) {
|
309 |
-
|
310 |
-
|
|
|
|
|
|
|
|
|
311 |
}
|
312 |
|
313 |
const result = await response.json();
|
314 |
thinkingBubble.remove();
|
315 |
|
316 |
if (isSummarizeMode) {
|
317 |
-
|
318 |
-
const bubble = createMessageBubble(
|
319 |
result.summary || "No summary generated.",
|
320 |
"Aidan",
|
321 |
result.audioUrl, // From summarization endpoint
|
322 |
result.pdfUrl
|
323 |
);
|
324 |
-
|
325 |
-
if (result.audioUrl) {
|
326 |
-
injectAudioPlayer(bubble, result.audioUrl);
|
327 |
-
}
|
328 |
} else {
|
329 |
-
|
330 |
-
|
331 |
-
result.caption || result.answer, // From image captioning
|
332 |
"Aidan",
|
333 |
-
result.audio, // From image captioning
|
334 |
null
|
335 |
);
|
336 |
-
|
337 |
-
if (result.audio) {
|
338 |
-
injectAudioPlayer(bubble, result.audio);
|
339 |
-
}
|
340 |
}
|
341 |
} catch (error) {
|
342 |
thinkingBubble.remove();
|
343 |
createMessageBubble(`โ ๏ธ Error: ${error.message}`, "Aidan");
|
344 |
} finally {
|
345 |
-
selectedFile = null;
|
346 |
}
|
347 |
}
|
348 |
|
349 |
-
// Audio player helper function
|
350 |
-
function injectAudioPlayer(bubble, audioUrl) {
|
351 |
-
const messageDiv = bubble.querySelector('.text');
|
352 |
-
const audioContainer = document.createElement('div');
|
353 |
-
audioContainer.style.marginTop = "10px";
|
354 |
-
audioContainer.style.display = "flex";
|
355 |
-
audioContainer.style.justifyContent = "flex-end";
|
356 |
-
audioContainer.style.gap = "15px";
|
357 |
-
|
358 |
-
const audio = new Audio(audioUrl);
|
359 |
-
const audioIcon = document.createElement("i");
|
360 |
-
audioIcon.className = "fa-solid fa-volume-high audio-toggle";
|
361 |
-
audioIcon.title = "Play Audio";
|
362 |
-
audioIcon.style.cursor = "pointer";
|
363 |
-
audioIcon.style.fontSize = "18px";
|
364 |
-
|
365 |
-
audioIcon.addEventListener("click", () => {
|
366 |
-
if (audio.paused) {
|
367 |
-
audio.play();
|
368 |
-
audioIcon.classList.remove("fa-volume-xmark");
|
369 |
-
audioIcon.classList.add("fa-volume-high");
|
370 |
-
audioIcon.title = "Mute Audio";
|
371 |
-
} else {
|
372 |
-
audio.pause();
|
373 |
-
audioIcon.classList.remove("fa-volume-high");
|
374 |
-
audioIcon.classList.add("fa-volume-xmark");
|
375 |
-
audioIcon.title = "Unmute Audio";
|
376 |
-
}
|
377 |
-
});
|
378 |
-
|
379 |
-
audioContainer.appendChild(audioIcon);
|
380 |
-
messageDiv.appendChild(audioContainer);
|
381 |
-
}
|
382 |
-
|
383 |
const style = document.createElement('style');
|
384 |
style.textContent = `
|
385 |
.loader {
|
|
|
276 |
audioContainer.appendChild(audioIcon);
|
277 |
messageDiv.appendChild(audioContainer);
|
278 |
} */
|
279 |
+
async function handleSubmit() {
|
280 |
if (!selectedFile) {
|
281 |
alert("Please upload a file first");
|
282 |
return;
|
|
|
284 |
|
285 |
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
286 |
|
287 |
+
// Use the correct endpoint paths with /summarization/ prefix
|
288 |
const endpoint = isSummarizeMode
|
289 |
? '/summarization/summarize/'
|
290 |
: '/summarization/imagecaption/';
|
291 |
|
292 |
+
const thinkingText = isSummarizeMode
|
293 |
+
? 'Processing document ๐... <div class="loader"></div>'
|
294 |
+
: "Generating caption ๐ผ๏ธ ... <div class='loader'></div>";
|
295 |
+
const senderName = "Aidan";
|
296 |
|
297 |
+
const thinkingBubble = createMessageBubble(thinkingText, senderName);
|
298 |
|
299 |
const formData = new FormData();
|
300 |
formData.append('file', selectedFile);
|
|
|
307 |
});
|
308 |
|
309 |
if (!response.ok) {
|
310 |
+
let errorMessage = 'Request failed';
|
311 |
+
try {
|
312 |
+
const error = await response.json();
|
313 |
+
errorMessage = error.detail || error.error || errorMessage;
|
314 |
+
} catch (e) {}
|
315 |
+
throw new Error(errorMessage);
|
316 |
}
|
317 |
|
318 |
const result = await response.json();
|
319 |
thinkingBubble.remove();
|
320 |
|
321 |
if (isSummarizeMode) {
|
322 |
+
createMessageBubble(
|
|
|
323 |
result.summary || "No summary generated.",
|
324 |
"Aidan",
|
325 |
result.audioUrl, // From summarization endpoint
|
326 |
result.pdfUrl
|
327 |
);
|
|
|
|
|
|
|
|
|
328 |
} else {
|
329 |
+
createMessageBubble(
|
330 |
+
result.caption || result.answer || "No caption generated.",
|
|
|
331 |
"Aidan",
|
332 |
+
result.audio, // From image captioning endpoint
|
333 |
null
|
334 |
);
|
|
|
|
|
|
|
|
|
335 |
}
|
336 |
} catch (error) {
|
337 |
thinkingBubble.remove();
|
338 |
createMessageBubble(`โ ๏ธ Error: ${error.message}`, "Aidan");
|
339 |
} finally {
|
340 |
+
selectedFile = null; // โ
Reset selected file AFTER send
|
341 |
}
|
342 |
}
|
343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
344 |
const style = document.createElement('style');
|
345 |
style.textContent = `
|
346 |
.loader {
|