ikraamkb commited on
Commit
a22a174
Β·
verified Β·
1 Parent(s): c8235a4

Update static/appS.js

Browse files
Files changed (1) hide show
  1. 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 Switching
506
  document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
507
  radio.addEventListener('change', (e) => {
508
- if (e.target.checked) {
509
- const selectedValue = e.target.value;
510
- if (selectedValue === "Summarize") {
511
- SummarizeInput.classList.add("active");
512
- SummarizeInput.classList.remove("innactive");
513
- CaptionInput.classList.remove("active");
514
- CaptionInput.classList.add("innactive");
515
- } else {
516
- SummarizeInput.classList.add("innactive");
517
- SummarizeInput.classList.remove("active");
518
- CaptionInput.classList.remove("innactive");
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
- // βœ… File preview in convo
 
 
 
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 text = document.createElement("span");
568
- text.textContent = `πŸ“Ž Selected image: ${file.name}`;
569
- text.style.fontSize = "13px";
570
 
571
  previewBubble.appendChild(img);
572
- previewBubble.appendChild(text);
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 textSpan = document.createElement("span");
606
- textSpan.innerHTML = text;
607
- message.appendChild(textSpan);
608
 
609
  if (sender !== "You" && (audioSrc || fileName)) {
610
- const iconContainer = document.createElement('div');
611
- iconContainer.style.marginTop = "10px";
612
- iconContainer.style.display = "flex";
613
- iconContainer.style.justifyContent = "flex-end";
614
- iconContainer.style.gap = "15px";
615
 
616
  if (audioSrc) {
617
  const audio = new Audio(audioSrc);
618
- const audioIcon = document.createElement("i");
619
- audioIcon.className = "fa-solid fa-volume-high audio-toggle";
620
- audioIcon.title = "Play Audio";
621
- audioIcon.style.cursor = "pointer";
622
- audioIcon.style.fontSize = "18px";
623
-
624
- audioIcon.addEventListener("click", () => {
625
  if (audio.paused) {
626
  audio.play();
627
- audioIcon.classList.remove("fa-volume-xmark");
628
- audioIcon.classList.add("fa-volume-high");
629
- audioIcon.title = "Mute Audio";
630
  } else {
631
  audio.pause();
632
- audioIcon.classList.remove("fa-volume-high");
633
- audioIcon.classList.add("fa-volume-xmark");
634
- audioIcon.title = "Unmute Audio";
635
  }
636
  });
637
-
638
- iconContainer.appendChild(audioIcon);
639
  }
640
 
641
  if (fileName) {
642
- const downloadLink = document.createElement('a');
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
- iconContainer.appendChild(downloadLink);
654
  }
655
 
656
- message.appendChild(iconContainer);
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 isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
674
- const endpoint = isSummarizeMode ? '/Summarization/summarize/' : '/Summarization/imagecaption/';
675
- const thinkingText = isSummarizeMode
676
- ? 'Processing document πŸ“„... <div class="loader"></div>'
677
- : "Generating caption πŸ–ΌοΈ ... <div class='loader'></div>";
678
 
679
- const thinkingBubble = createMessageBubble(thinkingText, "Aidan");
 
 
680
 
681
- const formData = new FormData();
682
- formData.append('file', selectedFile);
 
683
 
684
- // βœ… Add summary length from selected radio
685
- if (isSummarizeMode) {
686
- const lengthChoice = document.querySelector('input[name="lenght"]:checked');
687
- const summaryLength = lengthChoice ? lengthChoice.value.toLowerCase() : 'medium';
688
- formData.append('length', summaryLength);
689
- }
690
 
691
  try {
692
  const response = await fetch(endpoint, {
693
- method: 'POST',
694
  body: formData
695
  });
696
 
@@ -702,21 +690,12 @@ document.addEventListener('DOMContentLoaded', () => {
702
  const result = await response.json();
703
  thinkingBubble.remove();
704
 
705
- if (isSummarizeMode) {
706
- createMessageBubble(
707
- result.summary || "No summary generated.",
708
- "Aidan",
709
- result.audioUrl,
710
- result.pdfUrl
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
- // βœ… Spinner CSS
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
- cursor: pointer;
746
- transition: all 0.2s;
747
  }
748
  `;
749
  document.head.appendChild(style);
750
 
751
- // Back to home
752
- const backarrow = document.querySelector(".fa-arrow-left");
753
- backarrow.addEventListener('click', () => {
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
  });