Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +228 -1
static/appS.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
document.addEventListener('DOMContentLoaded', () => {
|
2 |
const convo = document.querySelector(".convo");
|
3 |
const fileUpload = document.getElementById('file-upload');
|
4 |
const imageUpload = document.getElementById('image-upload');
|
@@ -352,6 +352,233 @@ function injectAudioPlayer(bubble, audioUrl) {
|
|
352 |
`;
|
353 |
document.head.appendChild(style);
|
354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
var backarrow = document.querySelector(".fa-arrow-left");
|
356 |
backarrow.addEventListener('click', function () {
|
357 |
window.location.href = '/';
|
|
|
1 |
+
/*document.addEventListener('DOMContentLoaded', () => {
|
2 |
const convo = document.querySelector(".convo");
|
3 |
const fileUpload = document.getElementById('file-upload');
|
4 |
const imageUpload = document.getElementById('image-upload');
|
|
|
352 |
`;
|
353 |
document.head.appendChild(style);
|
354 |
|
355 |
+
var backarrow = document.querySelector(".fa-arrow-left");
|
356 |
+
backarrow.addEventListener('click', function () {
|
357 |
+
window.location.href = '/';
|
358 |
+
});
|
359 |
+
}); */
|
360 |
+
document.addEventListener('DOMContentLoaded', () => {
|
361 |
+
const convo = document.querySelector(".convo");
|
362 |
+
const fileUpload = document.getElementById('file-upload');
|
363 |
+
const imageUpload = document.getElementById('image-upload');
|
364 |
+
const fileBtn = document.getElementById('file-btn');
|
365 |
+
const imageBtn = document.getElementById('image-btn');
|
366 |
+
const sendButtons = document.querySelectorAll('.sendingQA');
|
367 |
+
const SummarizeInput = document.querySelector(".SummarizeInput");
|
368 |
+
const CaptionInput = document.querySelector(".CaptionInput");
|
369 |
+
const gotItButton = document.querySelector('.explainChoix button');
|
370 |
+
const explainChoixDiv = document.querySelector('.explainChoix');
|
371 |
+
|
372 |
+
let selectedFile = null;
|
373 |
+
|
374 |
+
const summarizeRadio = document.getElementById('summarize-radio');
|
375 |
+
if (summarizeRadio) summarizeRadio.checked = true;
|
376 |
+
|
377 |
+
document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
|
378 |
+
radio.addEventListener('change', (e) => {
|
379 |
+
if (e.target.checked) {
|
380 |
+
const selectedValue = e.target.value;
|
381 |
+
if (selectedValue === "Summarize") {
|
382 |
+
SummarizeInput.classList.add("active");
|
383 |
+
SummarizeInput.classList.remove("innactive");
|
384 |
+
CaptionInput.classList.remove("active");
|
385 |
+
CaptionInput.classList.add("innactive");
|
386 |
+
} else {
|
387 |
+
SummarizeInput.classList.add("innactive");
|
388 |
+
SummarizeInput.classList.remove("active");
|
389 |
+
CaptionInput.classList.remove("innactive");
|
390 |
+
CaptionInput.classList.add("active");
|
391 |
+
}
|
392 |
+
}
|
393 |
+
});
|
394 |
+
});
|
395 |
+
|
396 |
+
fileBtn.addEventListener('click', () => fileUpload.click());
|
397 |
+
imageBtn.addEventListener('click', () => imageUpload.click());
|
398 |
+
|
399 |
+
fileUpload.addEventListener('change', (e) => {
|
400 |
+
if (e.target.files.length) {
|
401 |
+
selectedFile = e.target.files[0];
|
402 |
+
displayFilePreview(selectedFile);
|
403 |
+
}
|
404 |
+
});
|
405 |
+
|
406 |
+
imageUpload.addEventListener('change', (e) => {
|
407 |
+
if (e.target.files.length) {
|
408 |
+
selectedFile = e.target.files[0];
|
409 |
+
displayFilePreview(selectedFile);
|
410 |
+
}
|
411 |
+
});
|
412 |
+
|
413 |
+
gotItButton.addEventListener('click', () => {
|
414 |
+
explainChoixDiv.style.display = "none";
|
415 |
+
});
|
416 |
+
|
417 |
+
sendButtons.forEach(button => {
|
418 |
+
button.addEventListener('click', handleSubmit);
|
419 |
+
});
|
420 |
+
|
421 |
+
function displayFilePreview(file) {
|
422 |
+
const previewBubble = document.createElement("div");
|
423 |
+
previewBubble.className = "file-preview-bubble bubble right";
|
424 |
+
previewBubble.style.display = "flex";
|
425 |
+
previewBubble.style.flexDirection = "column";
|
426 |
+
previewBubble.style.maxWidth = "50%";
|
427 |
+
|
428 |
+
if (file.type.startsWith('image/')) {
|
429 |
+
const reader = new FileReader();
|
430 |
+
reader.onload = (e) => {
|
431 |
+
const img = document.createElement("img");
|
432 |
+
img.src = e.target.result;
|
433 |
+
img.style.width = "100%";
|
434 |
+
img.style.height = "200px";
|
435 |
+
img.style.objectFit = "cover";
|
436 |
+
img.style.borderRadius = "10px";
|
437 |
+
img.style.marginBottom = "8px";
|
438 |
+
|
439 |
+
const text = document.createElement("span");
|
440 |
+
text.textContent = `π Selected image: ${file.name}`;
|
441 |
+
text.style.fontSize = "13px";
|
442 |
+
|
443 |
+
previewBubble.appendChild(img);
|
444 |
+
previewBubble.appendChild(text);
|
445 |
+
convo.appendChild(previewBubble);
|
446 |
+
convo.scrollTop = convo.scrollHeight;
|
447 |
+
};
|
448 |
+
reader.readAsDataURL(file);
|
449 |
+
} else {
|
450 |
+
const text = document.createElement("span");
|
451 |
+
text.textContent = `π Selected document: ${file.name}`;
|
452 |
+
text.style.fontSize = "13px";
|
453 |
+
|
454 |
+
previewBubble.appendChild(text);
|
455 |
+
convo.appendChild(previewBubble);
|
456 |
+
convo.scrollTop = convo.scrollHeight;
|
457 |
+
}
|
458 |
+
}
|
459 |
+
|
460 |
+
function createMessageBubble(text, sender = "You", fileName = null) {
|
461 |
+
const bubble = document.createElement('div');
|
462 |
+
bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
|
463 |
+
bubble.style.maxWidth = "50%";
|
464 |
+
bubble.style.wordWrap = "break-word";
|
465 |
+
|
466 |
+
const label = document.createElement('div');
|
467 |
+
label.className = "label";
|
468 |
+
label.textContent = sender;
|
469 |
+
|
470 |
+
const message = document.createElement('div');
|
471 |
+
message.className = "text";
|
472 |
+
message.style.whiteSpace = "pre-wrap";
|
473 |
+
message.style.display = "flex";
|
474 |
+
message.style.flexDirection = "column";
|
475 |
+
|
476 |
+
const textSpan = document.createElement("span");
|
477 |
+
textSpan.innerHTML = text;
|
478 |
+
message.appendChild(textSpan);
|
479 |
+
|
480 |
+
if (sender !== "You" && fileName) {
|
481 |
+
const iconContainer = document.createElement('div');
|
482 |
+
iconContainer.style.marginTop = "10px";
|
483 |
+
iconContainer.style.display = "flex";
|
484 |
+
iconContainer.style.justifyContent = "flex-end";
|
485 |
+
iconContainer.style.gap = "15px";
|
486 |
+
|
487 |
+
const downloadLink = document.createElement('a');
|
488 |
+
downloadLink.href = fileName;
|
489 |
+
downloadLink.target = "_blank";
|
490 |
+
downloadLink.download = "summary.pdf";
|
491 |
+
const downloadIcon = document.createElement("i");
|
492 |
+
downloadIcon.className = "fa-solid fa-file-arrow-down";
|
493 |
+
downloadIcon.style.fontSize = "18px";
|
494 |
+
downloadIcon.style.cursor = "pointer";
|
495 |
+
downloadLink.appendChild(downloadIcon);
|
496 |
+
iconContainer.appendChild(downloadLink);
|
497 |
+
|
498 |
+
message.appendChild(iconContainer);
|
499 |
+
}
|
500 |
+
|
501 |
+
bubble.appendChild(label);
|
502 |
+
bubble.appendChild(message);
|
503 |
+
convo.appendChild(bubble);
|
504 |
+
convo.scrollTop = convo.scrollHeight;
|
505 |
+
return bubble;
|
506 |
+
}
|
507 |
+
|
508 |
+
async function handleSubmit() {
|
509 |
+
if (!selectedFile) {
|
510 |
+
alert("Please upload a file first");
|
511 |
+
return;
|
512 |
+
}
|
513 |
+
|
514 |
+
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
515 |
+
const endpoint = isSummarizeMode ? '/Summarization/summarize/' : '/Summarization/imagecaption/';
|
516 |
+
const thinkingText = isSummarizeMode ? 'Processing document π... <div class="loader"></div>' : "Generating caption πΌοΈ ... <div class='loader'></div>";
|
517 |
+
const senderName = "Aidan";
|
518 |
+
|
519 |
+
const thinkingBubble = createMessageBubble(thinkingText, senderName);
|
520 |
+
|
521 |
+
const formData = new FormData();
|
522 |
+
formData.append('file', selectedFile);
|
523 |
+
if (isSummarizeMode) formData.append('length', 'medium');
|
524 |
+
|
525 |
+
try {
|
526 |
+
const response = await fetch(endpoint, {
|
527 |
+
method: 'POST',
|
528 |
+
body: formData
|
529 |
+
});
|
530 |
+
|
531 |
+
if (!response.ok) {
|
532 |
+
let errorMessage = 'Request failed';
|
533 |
+
try {
|
534 |
+
const error = await response.json();
|
535 |
+
errorMessage = error.detail || error.error || errorMessage;
|
536 |
+
} catch (e) {}
|
537 |
+
throw new Error(errorMessage);
|
538 |
+
}
|
539 |
+
|
540 |
+
const result = await response.json();
|
541 |
+
thinkingBubble.remove();
|
542 |
+
|
543 |
+
if (isSummarizeMode) {
|
544 |
+
createMessageBubble(
|
545 |
+
result.summary || "No summary generated.",
|
546 |
+
"Aidan",
|
547 |
+
result.pdfUrl
|
548 |
+
);
|
549 |
+
} else {
|
550 |
+
createMessageBubble(
|
551 |
+
result.caption || result.answer || "No caption generated.",
|
552 |
+
"Aidan",
|
553 |
+
null
|
554 |
+
);
|
555 |
+
}
|
556 |
+
} catch (error) {
|
557 |
+
thinkingBubble.remove();
|
558 |
+
createMessageBubble(`β οΈ Error: ${error.message}`, "Aidan");
|
559 |
+
} finally {
|
560 |
+
selectedFile = null;
|
561 |
+
}
|
562 |
+
}
|
563 |
+
|
564 |
+
const style = document.createElement('style');
|
565 |
+
style.textContent = `
|
566 |
+
.loader {
|
567 |
+
display: inline-block;
|
568 |
+
border: 2px solid #f3f3f3;
|
569 |
+
border-top: 2px solid #3b82f6;
|
570 |
+
border-radius: 50%;
|
571 |
+
width: 16px;
|
572 |
+
height: 16px;
|
573 |
+
animation: spin 1s linear infinite;
|
574 |
+
}
|
575 |
+
@keyframes spin {
|
576 |
+
0% { transform: rotate(0deg); }
|
577 |
+
100% { transform: rotate(360deg); }
|
578 |
+
}
|
579 |
+
`;
|
580 |
+
document.head.appendChild(style);
|
581 |
+
|
582 |
var backarrow = document.querySelector(".fa-arrow-left");
|
583 |
backarrow.addEventListener('click', function () {
|
584 |
window.location.href = '/';
|