ikraamkb commited on
Commit
7d1820b
Β·
verified Β·
1 Parent(s): 6cdb9f4

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +83 -86
static/appS.js CHANGED
@@ -7,12 +7,16 @@ document.addEventListener('DOMContentLoaded', () => {
7
  const sendButtons = document.querySelectorAll('.sendingQA');
8
  const SummarizeInput = document.querySelector(".SummarizeInput");
9
  const CaptionInput = document.querySelector(".CaptionInput");
10
- const gotItButton = document.querySelector('.explainChoix Botton'); // βœ… Button to hide explain div
11
- const explainChoixDiv = document.querySelector('.explainChoix'); // βœ… Div to hide
12
 
13
  let selectedFile = null;
14
  let filePreviewBubble = null;
15
 
 
 
 
 
16
  // Mode switching
17
  document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
18
  radio.addEventListener('change', (e) => {
@@ -51,9 +55,7 @@ document.addEventListener('DOMContentLoaded', () => {
51
  }
52
  });
53
 
54
- // βœ… REMOVE Drag & Drop handlers (completely deleted!)
55
-
56
- // βœ… "Got it" button to hide explanation div
57
  if (gotItButton) {
58
  gotItButton.addEventListener('click', () => {
59
  if (explainChoixDiv) {
@@ -76,86 +78,83 @@ document.addEventListener('DOMContentLoaded', () => {
76
  );
77
  }
78
 
79
- function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
80
- const bubble = document.createElement('div');
81
- bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
82
- bubble.style.maxWidth = "50%";
83
- bubble.style.wordWrap = "break-word";
84
-
85
- const label = document.createElement('div');
86
- label.className = "label";
87
- label.textContent = sender;
88
-
89
- const message = document.createElement('div');
90
- message.className = "text";
91
- message.style.whiteSpace = "pre-wrap";
92
- message.style.display = "flex";
93
- message.style.flexDirection = "column";
94
-
95
- const textSpan = document.createElement("span");
96
- textSpan.innerHTML = text;
97
- message.appendChild(textSpan);
98
-
99
- if (sender !== "You" && (audioSrc || fileName)) {
100
- const iconContainer = document.createElement('div');
101
- iconContainer.style.marginTop = "10px";
102
- iconContainer.style.display = "flex";
103
- iconContainer.style.justifyContent = "flex-end";
104
- iconContainer.style.gap = "15px";
105
-
106
- // 🎡 Audio Icon
107
- if (audioSrc) {
108
- const audio = new Audio(audioSrc);
109
- const audioIcon = document.createElement("i");
110
- audioIcon.className = "fa-solid fa-volume-high audio-toggle";
111
- audioIcon.title = "Play Audio";
112
- audioIcon.style.cursor = "pointer";
113
- audioIcon.style.fontSize = "18px";
114
-
115
- audioIcon.addEventListener("click", () => {
116
- if (audio.paused) {
117
- audio.play();
118
- audioIcon.classList.remove("fa-volume-xmark");
119
- audioIcon.classList.add("fa-volume-high");
120
- audioIcon.title = "Mute Audio";
121
- } else {
122
- audio.pause();
123
- audioIcon.classList.remove("fa-volume-high");
124
- audioIcon.classList.add("fa-volume-xmark");
125
- audioIcon.title = "Unmute Audio";
126
- }
127
- });
 
 
128
 
129
- iconContainer.appendChild(audioIcon);
130
- }
 
 
 
131
 
132
- // πŸ“₯ Download Icon
133
- if (fileName) {
134
- const downloadLink = document.createElement('a');
135
- downloadLink.href = fileName;
136
- downloadLink.target = "_blank";
137
- downloadLink.title = "Download PDF";
138
 
139
- const downloadIcon = document.createElement("i");
140
- downloadIcon.className = "fa-solid fa-file-arrow-down";
141
- downloadIcon.style.fontSize = "18px";
142
- downloadIcon.style.cursor = "pointer";
143
 
144
- downloadLink.appendChild(downloadIcon);
145
- iconContainer.appendChild(downloadLink);
146
  }
147
 
148
- message.appendChild(iconContainer);
 
 
 
 
149
  }
150
 
151
- bubble.appendChild(label);
152
- bubble.appendChild(message);
153
- convo.appendChild(bubble);
154
- convo.scrollTop = convo.scrollHeight;
155
- return bubble;
156
- }
157
-
158
-
159
  async function handleSubmit() {
160
  if (!selectedFile) {
161
  alert("Please upload a file first");
@@ -165,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => {
165
  const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
166
  const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
167
  const thinkingText = isSummarizeMode ? 'Processing document...' : "Generating caption... <div class='loader'></div>";
168
- const senderName = 'Aidan';
169
 
170
  const thinkingBubble = createMessageBubble(thinkingText, senderName);
171
 
@@ -202,16 +201,17 @@ document.addEventListener('DOMContentLoaded', () => {
202
  );
203
  } else {
204
  createMessageBubble(
205
- `<strong>Caption:</strong><br><br>${result.answer || result.caption}`,
206
  "Aidan",
207
- result.audio
 
208
  );
209
  }
210
  } catch (error) {
211
  thinkingBubble.remove();
212
- createMessageBubble(`⚠️ Error: ${error.message}`, senderName);
213
  } finally {
214
- resetFileInputs();
215
  }
216
  }
217
 
@@ -219,10 +219,7 @@ document.addEventListener('DOMContentLoaded', () => {
219
  selectedFile = null;
220
  fileUpload.value = '';
221
  imageUpload.value = '';
222
- if (filePreviewBubble) {
223
- filePreviewBubble.remove();
224
- filePreviewBubble = null;
225
- }
226
  }
227
 
228
  // Loader CSS
 
7
  const sendButtons = document.querySelectorAll('.sendingQA');
8
  const SummarizeInput = document.querySelector(".SummarizeInput");
9
  const CaptionInput = document.querySelector(".CaptionInput");
10
+ const gotItButton = document.getElementById('got-it-btn');
11
+ const explainChoixDiv = document.getElementById('explainChoix');
12
 
13
  let selectedFile = null;
14
  let filePreviewBubble = null;
15
 
16
+ // βœ… Default mode: Summarize selected
17
+ const summarizeRadio = document.getElementById('summarize-radio');
18
+ if (summarizeRadio) summarizeRadio.checked = true;
19
+
20
  // Mode switching
21
  document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
22
  radio.addEventListener('change', (e) => {
 
55
  }
56
  });
57
 
58
+ // "Got it" button to hide explanation div
 
 
59
  if (gotItButton) {
60
  gotItButton.addEventListener('click', () => {
61
  if (explainChoixDiv) {
 
78
  );
79
  }
80
 
81
+ function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
82
+ const bubble = document.createElement('div');
83
+ bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
84
+ bubble.style.maxWidth = "50%";
85
+ bubble.style.wordWrap = "break-word";
86
+
87
+ const label = document.createElement('div');
88
+ label.className = "label";
89
+ label.textContent = sender;
90
+
91
+ const message = document.createElement('div');
92
+ message.className = "text";
93
+ message.style.whiteSpace = "pre-wrap";
94
+ message.style.display = "flex";
95
+ message.style.flexDirection = "column";
96
+
97
+ const textSpan = document.createElement("span");
98
+ textSpan.innerHTML = text;
99
+ message.appendChild(textSpan);
100
+
101
+ if (sender !== "You" && (audioSrc || fileName)) {
102
+ const iconContainer = document.createElement('div');
103
+ iconContainer.style.marginTop = "10px";
104
+ iconContainer.style.display = "flex";
105
+ iconContainer.style.justifyContent = "flex-end";
106
+ iconContainer.style.gap = "15px";
107
+
108
+ if (audioSrc) {
109
+ const audio = new Audio(audioSrc);
110
+ const audioIcon = document.createElement("i");
111
+ audioIcon.className = "fa-solid fa-volume-high audio-toggle";
112
+ audioIcon.title = "Play Audio";
113
+ audioIcon.style.cursor = "pointer";
114
+ audioIcon.style.fontSize = "18px";
115
+
116
+ audioIcon.addEventListener("click", () => {
117
+ if (audio.paused) {
118
+ audio.play();
119
+ audioIcon.classList.remove("fa-volume-xmark");
120
+ audioIcon.classList.add("fa-volume-high");
121
+ audioIcon.title = "Mute Audio";
122
+ } else {
123
+ audio.pause();
124
+ audioIcon.classList.remove("fa-volume-high");
125
+ audioIcon.classList.add("fa-volume-xmark");
126
+ audioIcon.title = "Unmute Audio";
127
+ }
128
+ });
129
+
130
+ iconContainer.appendChild(audioIcon);
131
+ }
132
 
133
+ if (fileName) {
134
+ const downloadLink = document.createElement('a');
135
+ downloadLink.href = fileName;
136
+ downloadLink.target = "_blank";
137
+ downloadLink.download = "summary.pdf"; // βœ… Suggest filename to browser
138
 
139
+ const downloadIcon = document.createElement("i");
140
+ downloadIcon.className = "fa-solid fa-file-arrow-down";
141
+ downloadIcon.style.fontSize = "18px";
142
+ downloadIcon.style.cursor = "pointer";
 
 
143
 
144
+ downloadLink.appendChild(downloadIcon);
145
+ iconContainer.appendChild(downloadLink);
146
+ }
 
147
 
148
+ message.appendChild(iconContainer);
 
149
  }
150
 
151
+ bubble.appendChild(label);
152
+ bubble.appendChild(message);
153
+ convo.appendChild(bubble);
154
+ convo.scrollTop = convo.scrollHeight;
155
+ return bubble;
156
  }
157
 
 
 
 
 
 
 
 
 
158
  async function handleSubmit() {
159
  if (!selectedFile) {
160
  alert("Please upload a file first");
 
164
  const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
165
  const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
166
  const thinkingText = isSummarizeMode ? 'Processing document...' : "Generating caption... <div class='loader'></div>";
167
+ const senderName = "Aidan";
168
 
169
  const thinkingBubble = createMessageBubble(thinkingText, senderName);
170
 
 
201
  );
202
  } else {
203
  createMessageBubble(
204
+ result.caption || result.answer || "No caption generated.",
205
  "Aidan",
206
+ result.audio,
207
+ null
208
  );
209
  }
210
  } catch (error) {
211
  thinkingBubble.remove();
212
+ createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
213
  } finally {
214
+ resetFileInputs(); // βœ… Only reset upload inputs, KEEP preview bubble
215
  }
216
  }
217
 
 
219
  selectedFile = null;
220
  fileUpload.value = '';
221
  imageUpload.value = '';
222
+ // βœ… DO NOT remove file preview bubble anymore
 
 
 
223
  }
224
 
225
  // Loader CSS