ikraamkb commited on
Commit
2a8891b
·
verified ·
1 Parent(s): a377214

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +64 -41
static/appS.js CHANGED
@@ -73,27 +73,31 @@ document.addEventListener('DOMContentLoaded', () => {
73
  }
74
  });
75
 
76
- async function handleSummarize() {
77
- const file = fileUpload.files[0];
78
- if (!file) {
79
- displayError('Please upload a document first');
80
- return;
81
- }
82
- const length = document.querySelector('input[name="optionS"]:checked')?.value || "medium";
83
 
84
- try {
85
- convo.innerHTML = '';
86
- displayFileInfo(file.name, 'document');
87
- displayThinkingMessage();
88
 
89
- const result = await summarizeDocument(file, length);
 
 
 
90
 
91
- displaySummaryResult(file.name, result.summary, result.audioUrl, result.pdfUrl);
92
- } catch (error) {
93
- displayError(error.message || 'Failed to summarize document');
94
- }
95
- }
96
 
 
 
 
 
 
 
97
  async function handleCaption() {
98
  const file = imageUpload.files[0];
99
  if (!file) {
@@ -113,24 +117,34 @@ document.addEventListener('DOMContentLoaded', () => {
113
  }
114
  }
115
 
116
- async function summarizeDocument(file, length) {
117
- const formData = new FormData();
118
- formData.append('file', file);
119
- formData.append('length', length);
120
 
 
121
  const response = await fetch('/summarize/', {
122
  method: 'POST',
123
  body: formData
124
  });
125
 
 
 
126
  if (!response.ok) {
127
- const error = await response.json();
128
- throw new Error(error.detail || 'Summarization failed');
129
  }
130
 
131
- return await response.json();
 
 
 
 
 
 
 
 
132
  }
133
-
134
  async function captionImage(file) {
135
  const formData = new FormData();
136
  formData.append('file', file);
@@ -175,24 +189,33 @@ document.addEventListener('DOMContentLoaded', () => {
175
  convo.scrollTop = convo.scrollHeight;
176
  }
177
 
178
- function displaySummaryResult(filename, summary, audioUrl, pdfUrl) {
179
- convo.removeChild(convo.lastChild);
180
-
181
- const bubble = document.createElement('div');
182
- bubble.className = 'bubble left';
183
- bubble.innerHTML = `
184
- <div class="label">Aidan</div>
185
- <div class="text">
186
- <strong>Summary:</strong><br><br>
187
- ${summary.replace(/\n/g, '<br>')}
188
- ${audioUrl ? `<br><br><audio controls src="${audioUrl}" style="width: 100%;"></audio>` : ''}
189
- ${pdfUrl ? `<br><a href="${pdfUrl}" download class="download-link">📥 Download PDF Summary</a>` : ''}
190
- </div>
191
- `;
192
- convo.appendChild(bubble);
193
- convo.scrollTop = convo.scrollHeight;
194
  }
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  function displayCaptionResult(filename, caption) {
197
  convo.removeChild(convo.lastChild);
198
 
 
73
  }
74
  });
75
 
76
+ async function handleSummarize() {
77
+ const file = fileUpload.files[0];
78
+ if (!file) {
79
+ displayError('Please upload a document first');
80
+ return;
81
+ }
 
82
 
83
+ console.log("Selected file:", file.name, "Type:", file.type);
84
+ const length = document.querySelector('input[name="optionS"]:checked')?.value || "medium";
 
 
85
 
86
+ try {
87
+ convo.innerHTML = '';
88
+ displayFileInfo(file.name, 'document');
89
+ displayThinkingMessage();
90
 
91
+ console.log("Starting summarization...");
92
+ const result = await summarizeDocument(file, length);
93
+ console.log("Summarization result:", result);
 
 
94
 
95
+ displaySummaryResult(file.name, result.summary, result.audioUrl, result.pdfUrl);
96
+ } catch (error) {
97
+ console.error("Summarization failed:", error);
98
+ displayError(error.message || 'Failed to summarize document');
99
+ }
100
+ }
101
  async function handleCaption() {
102
  const file = imageUpload.files[0];
103
  if (!file) {
 
117
  }
118
  }
119
 
120
+ async function summarizeDocument(file, length) {
121
+ const formData = new FormData();
122
+ formData.append('file', file);
123
+ formData.append('length', length);
124
 
125
+ try {
126
  const response = await fetch('/summarize/', {
127
  method: 'POST',
128
  body: formData
129
  });
130
 
131
+ const result = await response.json();
132
+
133
  if (!response.ok) {
134
+ throw new Error(result.detail || result.message || "Summarization failed");
 
135
  }
136
 
137
+ // Map backend fields to frontend expectations
138
+ return {
139
+ summary: result.summary,
140
+ audioUrl: result.audio_url || result.audioUrl,
141
+ pdfUrl: result.pdf_url || result.pdfUrl
142
+ };
143
+ } catch (error) {
144
+ console.error("Summarization error:", error);
145
+ throw error;
146
  }
147
+ }
148
  async function captionImage(file) {
149
  const formData = new FormData();
150
  formData.append('file', file);
 
189
  convo.scrollTop = convo.scrollHeight;
190
  }
191
 
192
+ function displaySummaryResult(filename, summary, audioUrl, pdfUrl) {
193
+ // Remove thinking message if it exists
194
+ const lastBubble = convo.lastChild;
195
+ if (lastBubble && lastBubble.querySelector('.loader')) {
196
+ convo.removeChild(lastBubble);
 
 
 
 
 
 
 
 
 
 
 
197
  }
198
 
199
+ const bubble = document.createElement('div');
200
+ bubble.className = 'bubble left';
201
+
202
+ // Safely handle missing summary
203
+ const summaryContent = summary ? summary.replace(/\n/g, '<br>') : "No summary generated";
204
+
205
+ bubble.innerHTML = `
206
+ <div class="label">Aidan</div>
207
+ <div class="text">
208
+ <strong>Summary:</strong><br><br>
209
+ ${summaryContent}
210
+ ${audioUrl ? `<br><br><audio controls src="${audioUrl}" style="width: 100%;"></audio>` : ''}
211
+ ${pdfUrl ? `<br><a href="${pdfUrl}" download="${filename.replace(/\.[^/.]+$/, '')}_summary.pdf" class="download-link">📥 Download PDF Summary</a>` : ''}
212
+ </div>
213
+ `;
214
+
215
+ convo.appendChild(bubble);
216
+ convo.scrollTop = convo.scrollHeight;
217
+ }
218
+
219
  function displayCaptionResult(filename, caption) {
220
  convo.removeChild(convo.lastChild);
221