ikraamkb commited on
Commit
1d6f5ca
·
verified ·
1 Parent(s): 84ee43e

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +104 -2
static/appS.js CHANGED
@@ -173,7 +173,7 @@ document.addEventListener('DOMContentLoaded', () => {
173
  return bubble;
174
  }
175
 
176
- async function handleSubmit() {
177
  if (!selectedFile) {
178
  alert("Please upload a file first");
179
  return;
@@ -275,8 +275,110 @@ function injectAudioPlayer(bubble, audioUrl) {
275
 
276
  audioContainer.appendChild(audioIcon);
277
  messageDiv.appendChild(audioContainer);
278
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
  const style = document.createElement('style');
282
  style.textContent = `
 
173
  return bubble;
174
  }
175
 
176
+ /*async function handleSubmit() {
177
  if (!selectedFile) {
178
  alert("Please upload a file first");
179
  return;
 
275
 
276
  audioContainer.appendChild(audioIcon);
277
  messageDiv.appendChild(audioContainer);
278
+ } */
279
+ async function handleSubmit() {
280
+ if (!selectedFile) {
281
+ alert("Please upload a file first");
282
+ return;
283
+ }
284
+
285
+ const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
286
+
287
+ // ✅ Use the correct endpoint paths
288
+ const endpoint = isSummarizeMode
289
+ ? '/summarization/summarize/'
290
+ : '/summarization/imagecaption/';
291
+
292
+ const thinkingText = isSummarizeMode
293
+ ? 'Processing document 📄... <div class="loader"></div>'
294
+ : "Generating caption 🖼️... <div class='loader'></div>";
295
+
296
+ const thinkingBubble = createMessageBubble(thinkingText, "Aidan");
297
 
298
+ const formData = new FormData();
299
+ formData.append('file', selectedFile);
300
+ if (isSummarizeMode) formData.append('length', 'medium');
301
+
302
+ try {
303
+ const response = await fetch(endpoint, {
304
+ method: 'POST',
305
+ body: formData
306
+ });
307
+
308
+ if (!response.ok) {
309
+ const error = await response.json().catch(() => null);
310
+ throw new Error(error?.detail || error?.error || "Request failed");
311
+ }
312
+
313
+ const result = await response.json();
314
+ thinkingBubble.remove();
315
+
316
+ if (isSummarizeMode) {
317
+ // Handle summarization response
318
+ const bubble = createMessageBubble(
319
+ result.summary || "No summary generated.",
320
+ "Aidan",
321
+ result.audioUrl, // From summarization endpoint
322
+ result.pdfUrl
323
+ );
324
+
325
+ if (result.audioUrl) {
326
+ injectAudioPlayer(bubble, result.audioUrl);
327
+ }
328
+ } else {
329
+ // Handle image captioning response
330
+ const bubble = createMessageBubble(
331
+ result.caption || result.answer, // From image captioning
332
+ "Aidan",
333
+ result.audio, // From image captioning
334
+ null
335
+ );
336
+
337
+ if (result.audio) {
338
+ injectAudioPlayer(bubble, result.audio);
339
+ }
340
+ }
341
+ } catch (error) {
342
+ thinkingBubble.remove();
343
+ createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
344
+ } finally {
345
+ selectedFile = null;
346
+ }
347
+ }
348
+
349
+ // Audio player helper function
350
+ function injectAudioPlayer(bubble, audioUrl) {
351
+ const messageDiv = bubble.querySelector('.text');
352
+ const audioContainer = document.createElement('div');
353
+ audioContainer.style.marginTop = "10px";
354
+ audioContainer.style.display = "flex";
355
+ audioContainer.style.justifyContent = "flex-end";
356
+ audioContainer.style.gap = "15px";
357
+
358
+ const audio = new Audio(audioUrl);
359
+ const audioIcon = document.createElement("i");
360
+ audioIcon.className = "fa-solid fa-volume-high audio-toggle";
361
+ audioIcon.title = "Play Audio";
362
+ audioIcon.style.cursor = "pointer";
363
+ audioIcon.style.fontSize = "18px";
364
+
365
+ audioIcon.addEventListener("click", () => {
366
+ if (audio.paused) {
367
+ audio.play();
368
+ audioIcon.classList.remove("fa-volume-xmark");
369
+ audioIcon.classList.add("fa-volume-high");
370
+ audioIcon.title = "Mute Audio";
371
+ } else {
372
+ audio.pause();
373
+ audioIcon.classList.remove("fa-volume-high");
374
+ audioIcon.classList.add("fa-volume-xmark");
375
+ audioIcon.title = "Unmute Audio";
376
+ }
377
+ });
378
+
379
+ audioContainer.appendChild(audioIcon);
380
+ messageDiv.appendChild(audioContainer);
381
+ }
382
 
383
  const style = document.createElement('style');
384
  style.textContent = `