ikraamkb commited on
Commit
b81dd45
Β·
verified Β·
1 Parent(s): 2c83986

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +275 -3
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');
@@ -254,7 +254,7 @@ document.addEventListener('DOMContentLoaded', () => {
254
  backarrow.addEventListener('click', function () {
255
  window.location.href = '/';
256
  });
257
- });
258
  /* document.addEventListener('DOMContentLoaded', () => {
259
  const convo = document.querySelector(".convo");
260
  const fileUpload = document.getElementById('file-upload');
@@ -481,4 +481,276 @@ document.addEventListener('DOMContentLoaded', () => {
481
  backarrow.addEventListener('click', function () {
482
  window.location.href = '/';
483
  });
484
- }); */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*document.addEventListener('DOMContentLoaded', () => {
2
  const convo = document.querySelector(".convo");
3
  const fileUpload = document.getElementById('file-upload');
4
  const imageUpload = document.getElementById('image-upload');
 
254
  backarrow.addEventListener('click', function () {
255
  window.location.href = '/';
256
  });
257
+ }); */
258
  /* document.addEventListener('DOMContentLoaded', () => {
259
  const convo = document.querySelector(".convo");
260
  const fileUpload = document.getElementById('file-upload');
 
481
  backarrow.addEventListener('click', function () {
482
  window.location.href = '/';
483
  });
484
+ }); */
485
+
486
+
487
+ document.addEventListener('DOMContentLoaded', () => {
488
+ const convo = document.querySelector(".convo");
489
+ const fileUpload = document.getElementById('file-upload');
490
+ const imageUpload = document.getElementById('image-upload');
491
+ const fileBtn = document.getElementById('file-btn');
492
+ const imageBtn = document.getElementById('image-btn');
493
+ const sendButtons = document.querySelectorAll('.sendingQA');
494
+ const SummarizeInput = document.querySelector(".SummarizeInput");
495
+ const CaptionInput = document.querySelector(".CaptionInput");
496
+ const gotItButton = document.querySelector('.explainChoix button');
497
+ const explainChoixDiv = document.querySelector('.explainChoix');
498
+
499
+ let selectedFile = null;
500
+
501
+ // βœ… Default mode
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
+
529
+ fileUpload.addEventListener('change', (e) => {
530
+ if (e.target.files.length) {
531
+ selectedFile = e.target.files[0];
532
+ displayFilePreview(selectedFile);
533
+ }
534
+ });
535
+
536
+ imageUpload.addEventListener('change', (e) => {
537
+ if (e.target.files.length) {
538
+ selectedFile = e.target.files[0];
539
+ displayFilePreview(selectedFile);
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();
558
+ reader.onload = (e) => {
559
+ const img = document.createElement("img");
560
+ img.src = e.target.result;
561
+ img.style.width = "100%";
562
+ img.style.height = "200px";
563
+ img.style.objectFit = "cover";
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
+ };
576
+ reader.readAsDataURL(file);
577
+ } else {
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";
597
+ label.textContent = sender;
598
+
599
+ const message = document.createElement('div');
600
+ message.className = "text";
601
+ message.style.whiteSpace = "pre-wrap";
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);
660
+ bubble.appendChild(message);
661
+ convo.appendChild(bubble);
662
+ convo.scrollTop = convo.scrollHeight;
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
+
697
+ if (!response.ok) {
698
+ const error = await response.json().catch(() => null);
699
+ throw new Error(error?.detail || error?.error || "Request failed");
700
+ }
701
+
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");
723
+ } finally {
724
+ selectedFile = null;
725
+ }
726
+ }
727
+
728
+ // βœ… Spinner CSS
729
+ const style = document.createElement('style');
730
+ style.textContent = `
731
+ .loader {
732
+ display: inline-block;
733
+ border: 2px solid #f3f3f3;
734
+ border-top: 2px solid #3b82f6;
735
+ border-radius: 50%;
736
+ width: 16px;
737
+ height: 16px;
738
+ animation: spin 1s linear infinite;
739
+ }
740
+ @keyframes spin {
741
+ 0% { transform: rotate(0deg); }
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
+ });