ikraamkb commited on
Commit
9790e5d
·
verified ·
1 Parent(s): 08ee300

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +62 -59
static/appS.js CHANGED
@@ -276,70 +276,73 @@ function injectAudioPlayer(bubble, audioUrl) {
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 with /summarization/ prefix
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
- const senderName = "Aidan";
296
-
297
- const thinkingBubble = createMessageBubble(thinkingText, senderName);
298
-
299
- const formData = new FormData();
300
- formData.append('file', selectedFile);
301
- if (isSummarizeMode) formData.append('length', 'medium');
302
-
303
- try {
304
- const response = await fetch(endpoint, {
305
- method: 'POST',
306
- body: formData
307
- });
308
-
309
- if (!response.ok) {
310
- let errorMessage = 'Request failed';
311
- try {
312
- const error = await response.json();
313
- errorMessage = error.detail || error.error || errorMessage;
314
- } catch (e) {}
315
- throw new Error(errorMessage);
316
- }
317
 
318
- const result = await response.json();
319
- thinkingBubble.remove();
320
-
321
- if (isSummarizeMode) {
322
- createMessageBubble(
323
- result.summary || "No summary generated.",
324
- "Aidan",
325
- result.audioUrl, // From summarization endpoint
326
- result.pdfUrl
327
- );
328
- } else {
329
- createMessageBubble(
330
- result.caption || result.answer || "No caption generated.",
331
- "Aidan",
332
- result.audio, // From image captioning endpoint
333
- null
334
- );
335
- }
336
- } catch (error) {
337
- thinkingBubble.remove();
338
- createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
339
- } finally {
340
- selectedFile = null; // ✅ Reset selected file AFTER send
341
  }
 
 
 
 
 
 
342
  }
 
343
 
344
  const style = document.createElement('style');
345
  style.textContent = `
 
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 with /Summarization/ prefix
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
+ const senderName = "Aidan";
296
+
297
+ const thinkingBubble = createMessageBubble(thinkingText, senderName);
298
+
299
+ const formData = new FormData();
300
+ formData.append('file', selectedFile);
301
+ if (isSummarizeMode) formData.append('length', 'medium');
302
+
303
+ try {
304
+ const response = await fetch(endpoint, {
305
+ method: 'POST',
306
+ body: formData
307
+ });
308
+
309
+ if (!response.ok) {
310
+ let errorMessage = 'Request failed';
311
+ try {
312
+ const error = await response.json();
313
+ errorMessage = error.detail || error.error || errorMessage;
314
+ } catch (e) {}
315
+ throw new Error(errorMessage);
316
  }
317
 
318
+ const result = await response.json();
319
+ thinkingBubble.remove();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
 
321
+ if (isSummarizeMode) {
322
+ createMessageBubble(
323
+ result.summary || "No summary generated.",
324
+ "Aidan",
325
+ result.audioUrl,
326
+ result.pdfUrl
327
+ );
328
+ } else {
329
+
330
+ const audioUrl = result.audio ? `${result.audio}?t=${Date.now()}` : null;
331
+ createMessageBubble(
332
+ result.caption || result.answer || "No caption generated.",
333
+ "Aidan",
334
+ audioUrl,
335
+ null
336
+ );
 
 
 
 
 
 
 
337
  }
338
+ } catch (error) {
339
+ thinkingBubble.remove();
340
+ createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
341
+ console.error("API Error:", error); // Log error for debugging
342
+ } finally {
343
+ selectedFile = null;
344
  }
345
+ }
346
 
347
  const style = document.createElement('style');
348
  style.textContent = `