Spaces:
Sleeping
Sleeping
Update static/appS.js
Browse files- static/appS.js +68 -91
static/appS.js
CHANGED
|
@@ -502,27 +502,24 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 502 |
const summarizeRadio = document.getElementById('summarize-radio');
|
| 503 |
if (summarizeRadio) summarizeRadio.checked = true;
|
| 504 |
|
| 505 |
-
// β
Mode
|
| 506 |
document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
|
| 507 |
radio.addEventListener('change', (e) => {
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
CaptionInput.classList.add("active");
|
| 520 |
-
}
|
| 521 |
}
|
| 522 |
});
|
| 523 |
});
|
| 524 |
|
| 525 |
-
// β
File triggers
|
| 526 |
fileBtn.addEventListener('click', () => fileUpload.click());
|
| 527 |
imageBtn.addEventListener('click', () => imageUpload.click());
|
| 528 |
|
|
@@ -540,18 +537,20 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 540 |
}
|
| 541 |
});
|
| 542 |
|
| 543 |
-
// β
Hide intro
|
| 544 |
gotItButton.addEventListener('click', () => {
|
| 545 |
explainChoixDiv.style.display = "none";
|
| 546 |
});
|
| 547 |
|
| 548 |
-
|
|
|
|
|
|
|
|
|
|
| 549 |
function displayFilePreview(file) {
|
| 550 |
const previewBubble = document.createElement("div");
|
| 551 |
previewBubble.className = "file-preview-bubble bubble right";
|
|
|
|
| 552 |
previewBubble.style.display = "flex";
|
| 553 |
previewBubble.style.flexDirection = "column";
|
| 554 |
-
previewBubble.style.maxWidth = "50%";
|
| 555 |
|
| 556 |
if (file.type.startsWith('image/')) {
|
| 557 |
const reader = new FileReader();
|
|
@@ -564,12 +563,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 564 |
img.style.borderRadius = "10px";
|
| 565 |
img.style.marginBottom = "8px";
|
| 566 |
|
| 567 |
-
const
|
| 568 |
-
|
| 569 |
-
|
| 570 |
|
| 571 |
previewBubble.appendChild(img);
|
| 572 |
-
previewBubble.appendChild(
|
| 573 |
convo.appendChild(previewBubble);
|
| 574 |
convo.scrollTop = convo.scrollHeight;
|
| 575 |
};
|
|
@@ -578,19 +577,16 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 578 |
const text = document.createElement("span");
|
| 579 |
text.textContent = `π Selected document: ${file.name}`;
|
| 580 |
text.style.fontSize = "13px";
|
| 581 |
-
|
| 582 |
previewBubble.appendChild(text);
|
| 583 |
convo.appendChild(previewBubble);
|
| 584 |
convo.scrollTop = convo.scrollHeight;
|
| 585 |
}
|
| 586 |
}
|
| 587 |
|
| 588 |
-
// β
Create message bubble
|
| 589 |
function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
|
| 590 |
const bubble = document.createElement('div');
|
| 591 |
bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
|
| 592 |
bubble.style.maxWidth = "50%";
|
| 593 |
-
bubble.style.wordWrap = "break-word";
|
| 594 |
|
| 595 |
const label = document.createElement('div');
|
| 596 |
label.className = "label";
|
|
@@ -602,58 +598,52 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 602 |
message.style.display = "flex";
|
| 603 |
message.style.flexDirection = "column";
|
| 604 |
|
| 605 |
-
const
|
| 606 |
-
|
| 607 |
-
message.appendChild(
|
| 608 |
|
| 609 |
if (sender !== "You" && (audioSrc || fileName)) {
|
| 610 |
-
const
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
|
| 616 |
if (audioSrc) {
|
| 617 |
const audio = new Audio(audioSrc);
|
| 618 |
-
const
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
audioIcon.addEventListener("click", () => {
|
| 625 |
if (audio.paused) {
|
| 626 |
audio.play();
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
audioIcon.title = "Mute Audio";
|
| 630 |
} else {
|
| 631 |
audio.pause();
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
audioIcon.title = "Unmute Audio";
|
| 635 |
}
|
| 636 |
});
|
| 637 |
-
|
| 638 |
-
iconContainer.appendChild(audioIcon);
|
| 639 |
}
|
| 640 |
|
| 641 |
if (fileName) {
|
| 642 |
-
const downloadLink = document.createElement(
|
| 643 |
downloadLink.href = fileName;
|
| 644 |
-
downloadLink.target = "_blank";
|
| 645 |
downloadLink.download = "summary.pdf";
|
|
|
|
| 646 |
|
| 647 |
const downloadIcon = document.createElement("i");
|
| 648 |
downloadIcon.className = "fa-solid fa-file-arrow-down";
|
| 649 |
-
downloadIcon.style.fontSize = "18px";
|
| 650 |
downloadIcon.style.cursor = "pointer";
|
| 651 |
|
| 652 |
downloadLink.appendChild(downloadIcon);
|
| 653 |
-
|
| 654 |
}
|
| 655 |
|
| 656 |
-
message.appendChild(
|
| 657 |
}
|
| 658 |
|
| 659 |
bubble.appendChild(label);
|
|
@@ -663,34 +653,32 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 663 |
return bubble;
|
| 664 |
}
|
| 665 |
|
| 666 |
-
// β
Main send logic
|
| 667 |
async function handleSubmit() {
|
| 668 |
if (!selectedFile) {
|
| 669 |
alert("Please upload a file first");
|
| 670 |
return;
|
| 671 |
}
|
| 672 |
|
| 673 |
-
const
|
| 674 |
-
const
|
| 675 |
-
const thinkingText = isSummarizeMode
|
| 676 |
-
? 'Processing document π... <div class="loader"></div>'
|
| 677 |
-
: "Generating caption πΌοΈ ... <div class='loader'></div>";
|
| 678 |
|
| 679 |
-
const
|
|
|
|
|
|
|
| 680 |
|
| 681 |
-
const
|
| 682 |
-
|
|
|
|
| 683 |
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
}
|
| 690 |
|
| 691 |
try {
|
| 692 |
const response = await fetch(endpoint, {
|
| 693 |
-
method:
|
| 694 |
body: formData
|
| 695 |
});
|
| 696 |
|
|
@@ -702,21 +690,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 702 |
const result = await response.json();
|
| 703 |
thinkingBubble.remove();
|
| 704 |
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
);
|
| 712 |
-
} else {
|
| 713 |
-
createMessageBubble(
|
| 714 |
-
result.caption || result.answer || "No caption generated.",
|
| 715 |
-
"Aidan",
|
| 716 |
-
result.audio,
|
| 717 |
-
null
|
| 718 |
-
);
|
| 719 |
-
}
|
| 720 |
} catch (error) {
|
| 721 |
thinkingBubble.remove();
|
| 722 |
createMessageBubble(`β οΈ Error: ${error.message}`, "Aidan");
|
|
@@ -725,7 +704,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 725 |
}
|
| 726 |
}
|
| 727 |
|
| 728 |
-
//
|
| 729 |
const style = document.createElement('style');
|
| 730 |
style.textContent = `
|
| 731 |
.loader {
|
|
@@ -742,15 +721,13 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 742 |
100% { transform: rotate(360deg); }
|
| 743 |
}
|
| 744 |
.audio-toggle {
|
| 745 |
-
|
| 746 |
-
transition: all 0.2s;
|
| 747 |
}
|
| 748 |
`;
|
| 749 |
document.head.appendChild(style);
|
| 750 |
|
| 751 |
-
// Back
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
window.location.href = '/';
|
| 755 |
});
|
| 756 |
});
|
|
|
|
| 502 |
const summarizeRadio = document.getElementById('summarize-radio');
|
| 503 |
if (summarizeRadio) summarizeRadio.checked = true;
|
| 504 |
|
| 505 |
+
// β
Mode switching
|
| 506 |
document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
|
| 507 |
radio.addEventListener('change', (e) => {
|
| 508 |
+
const selected = e.target.value;
|
| 509 |
+
if (selected === "Summarize") {
|
| 510 |
+
SummarizeInput.classList.add("active");
|
| 511 |
+
SummarizeInput.classList.remove("innactive");
|
| 512 |
+
CaptionInput.classList.remove("active");
|
| 513 |
+
CaptionInput.classList.add("innactive");
|
| 514 |
+
} else {
|
| 515 |
+
SummarizeInput.classList.add("innactive");
|
| 516 |
+
SummarizeInput.classList.remove("active");
|
| 517 |
+
CaptionInput.classList.remove("innactive");
|
| 518 |
+
CaptionInput.classList.add("active");
|
|
|
|
|
|
|
| 519 |
}
|
| 520 |
});
|
| 521 |
});
|
| 522 |
|
|
|
|
| 523 |
fileBtn.addEventListener('click', () => fileUpload.click());
|
| 524 |
imageBtn.addEventListener('click', () => imageUpload.click());
|
| 525 |
|
|
|
|
| 537 |
}
|
| 538 |
});
|
| 539 |
|
|
|
|
| 540 |
gotItButton.addEventListener('click', () => {
|
| 541 |
explainChoixDiv.style.display = "none";
|
| 542 |
});
|
| 543 |
|
| 544 |
+
sendButtons.forEach(button => {
|
| 545 |
+
button.addEventListener('click', handleSubmit);
|
| 546 |
+
});
|
| 547 |
+
|
| 548 |
function displayFilePreview(file) {
|
| 549 |
const previewBubble = document.createElement("div");
|
| 550 |
previewBubble.className = "file-preview-bubble bubble right";
|
| 551 |
+
previewBubble.style.maxWidth = "50%";
|
| 552 |
previewBubble.style.display = "flex";
|
| 553 |
previewBubble.style.flexDirection = "column";
|
|
|
|
| 554 |
|
| 555 |
if (file.type.startsWith('image/')) {
|
| 556 |
const reader = new FileReader();
|
|
|
|
| 563 |
img.style.borderRadius = "10px";
|
| 564 |
img.style.marginBottom = "8px";
|
| 565 |
|
| 566 |
+
const label = document.createElement("span");
|
| 567 |
+
label.textContent = `π Selected image: ${file.name}`;
|
| 568 |
+
label.style.fontSize = "13px";
|
| 569 |
|
| 570 |
previewBubble.appendChild(img);
|
| 571 |
+
previewBubble.appendChild(label);
|
| 572 |
convo.appendChild(previewBubble);
|
| 573 |
convo.scrollTop = convo.scrollHeight;
|
| 574 |
};
|
|
|
|
| 577 |
const text = document.createElement("span");
|
| 578 |
text.textContent = `π Selected document: ${file.name}`;
|
| 579 |
text.style.fontSize = "13px";
|
|
|
|
| 580 |
previewBubble.appendChild(text);
|
| 581 |
convo.appendChild(previewBubble);
|
| 582 |
convo.scrollTop = convo.scrollHeight;
|
| 583 |
}
|
| 584 |
}
|
| 585 |
|
|
|
|
| 586 |
function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
|
| 587 |
const bubble = document.createElement('div');
|
| 588 |
bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
|
| 589 |
bubble.style.maxWidth = "50%";
|
|
|
|
| 590 |
|
| 591 |
const label = document.createElement('div');
|
| 592 |
label.className = "label";
|
|
|
|
| 598 |
message.style.display = "flex";
|
| 599 |
message.style.flexDirection = "column";
|
| 600 |
|
| 601 |
+
const span = document.createElement("span");
|
| 602 |
+
span.innerHTML = text;
|
| 603 |
+
message.appendChild(span);
|
| 604 |
|
| 605 |
if (sender !== "You" && (audioSrc || fileName)) {
|
| 606 |
+
const iconRow = document.createElement('div');
|
| 607 |
+
iconRow.style.marginTop = "10px";
|
| 608 |
+
iconRow.style.display = "flex";
|
| 609 |
+
iconRow.style.justifyContent = "flex-end";
|
| 610 |
+
iconRow.style.gap = "15px";
|
| 611 |
|
| 612 |
if (audioSrc) {
|
| 613 |
const audio = new Audio(audioSrc);
|
| 614 |
+
const icon = document.createElement("i");
|
| 615 |
+
icon.className = "fa-solid fa-volume-high audio-toggle";
|
| 616 |
+
icon.style.cursor = "pointer";
|
| 617 |
+
icon.title = "Play Audio";
|
| 618 |
+
icon.addEventListener("click", () => {
|
|
|
|
|
|
|
| 619 |
if (audio.paused) {
|
| 620 |
audio.play();
|
| 621 |
+
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
| 622 |
+
icon.title = "Mute Audio";
|
|
|
|
| 623 |
} else {
|
| 624 |
audio.pause();
|
| 625 |
+
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
| 626 |
+
icon.title = "Unmute Audio";
|
|
|
|
| 627 |
}
|
| 628 |
});
|
| 629 |
+
iconRow.appendChild(icon);
|
|
|
|
| 630 |
}
|
| 631 |
|
| 632 |
if (fileName) {
|
| 633 |
+
const downloadLink = document.createElement("a");
|
| 634 |
downloadLink.href = fileName;
|
|
|
|
| 635 |
downloadLink.download = "summary.pdf";
|
| 636 |
+
downloadLink.target = "_blank";
|
| 637 |
|
| 638 |
const downloadIcon = document.createElement("i");
|
| 639 |
downloadIcon.className = "fa-solid fa-file-arrow-down";
|
|
|
|
| 640 |
downloadIcon.style.cursor = "pointer";
|
| 641 |
|
| 642 |
downloadLink.appendChild(downloadIcon);
|
| 643 |
+
iconRow.appendChild(downloadLink);
|
| 644 |
}
|
| 645 |
|
| 646 |
+
message.appendChild(iconRow);
|
| 647 |
}
|
| 648 |
|
| 649 |
bubble.appendChild(label);
|
|
|
|
| 653 |
return bubble;
|
| 654 |
}
|
| 655 |
|
|
|
|
| 656 |
async function handleSubmit() {
|
| 657 |
if (!selectedFile) {
|
| 658 |
alert("Please upload a file first");
|
| 659 |
return;
|
| 660 |
}
|
| 661 |
|
| 662 |
+
const isSummarize = document.querySelector('input[name="mode"]:checked').value === "Summarize";
|
| 663 |
+
const length = document.querySelector('input[name="length"]:checked')?.value || "Medium";
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
+
const endpoint = isSummarize
|
| 666 |
+
? "/Summarization/summarize/"
|
| 667 |
+
: "/Summarization/imagecaption/";
|
| 668 |
|
| 669 |
+
const loaderText = isSummarize
|
| 670 |
+
? "Processing document π... <div class='loader'></div>"
|
| 671 |
+
: "Generating caption πΌοΈ... <div class='loader'></div>";
|
| 672 |
|
| 673 |
+
const thinkingBubble = createMessageBubble(loaderText, "Aidan");
|
| 674 |
+
|
| 675 |
+
const formData = new FormData();
|
| 676 |
+
formData.append("file", selectedFile);
|
| 677 |
+
if (isSummarize) formData.append("length", length);
|
|
|
|
| 678 |
|
| 679 |
try {
|
| 680 |
const response = await fetch(endpoint, {
|
| 681 |
+
method: "POST",
|
| 682 |
body: formData
|
| 683 |
});
|
| 684 |
|
|
|
|
| 690 |
const result = await response.json();
|
| 691 |
thinkingBubble.remove();
|
| 692 |
|
| 693 |
+
createMessageBubble(
|
| 694 |
+
isSummarize ? (result.summary || "No summary generated.") : (result.caption || "No caption generated."),
|
| 695 |
+
"Aidan",
|
| 696 |
+
result.audio || result.audioUrl || null,
|
| 697 |
+
result.pdfUrl || null
|
| 698 |
+
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
} catch (error) {
|
| 700 |
thinkingBubble.remove();
|
| 701 |
createMessageBubble(`β οΈ Error: ${error.message}`, "Aidan");
|
|
|
|
| 704 |
}
|
| 705 |
}
|
| 706 |
|
| 707 |
+
// Add loader CSS dynamically
|
| 708 |
const style = document.createElement('style');
|
| 709 |
style.textContent = `
|
| 710 |
.loader {
|
|
|
|
| 721 |
100% { transform: rotate(360deg); }
|
| 722 |
}
|
| 723 |
.audio-toggle {
|
| 724 |
+
transition: 0.2s;
|
|
|
|
| 725 |
}
|
| 726 |
`;
|
| 727 |
document.head.appendChild(style);
|
| 728 |
|
| 729 |
+
// Back button
|
| 730 |
+
document.querySelector(".fa-arrow-left").addEventListener("click", () => {
|
| 731 |
+
window.location.href = "/";
|
|
|
|
| 732 |
});
|
| 733 |
});
|