Spaces:
Sleeping
Sleeping
Update static/appS.js
Browse files- static/appS.js +75 -1
static/appS.js
CHANGED
@@ -173,7 +173,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
173 |
return bubble;
|
174 |
}
|
175 |
|
176 |
-
|
177 |
if (!selectedFile) {
|
178 |
alert("Please upload a file first");
|
179 |
return;
|
@@ -230,8 +230,82 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
230 |
} finally {
|
231 |
selectedFile = null;
|
232 |
}
|
|
|
|
|
|
|
|
|
|
|
233 |
}
|
234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
const style = document.createElement('style');
|
236 |
style.textContent = `
|
237 |
.loader {
|
|
|
173 |
return bubble;
|
174 |
}
|
175 |
|
176 |
+
/* async function handleSubmit() {
|
177 |
if (!selectedFile) {
|
178 |
alert("Please upload a file first");
|
179 |
return;
|
|
|
230 |
} finally {
|
231 |
selectedFile = null;
|
232 |
}
|
233 |
+
} */
|
234 |
+
async function handleSubmit() {
|
235 |
+
if (!selectedFile) {
|
236 |
+
alert("Please upload a file first");
|
237 |
+
return;
|
238 |
}
|
239 |
|
240 |
+
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
241 |
+
|
242 |
+
const endpoint = isSummarizeMode
|
243 |
+
? `/Summarization/summarize/`
|
244 |
+
: `/Summarization/imagecaption/`;
|
245 |
+
|
246 |
+
const thinkingBubble = createMessageBubble("Thinking...", "Aidan");
|
247 |
+
|
248 |
+
// ✅ Inject loader correctly
|
249 |
+
const messageDiv = thinkingBubble.querySelector('.text');
|
250 |
+
const loader = document.createElement('div');
|
251 |
+
loader.className = 'loader';
|
252 |
+
messageDiv.appendChild(loader);
|
253 |
+
|
254 |
+
const formData = new FormData();
|
255 |
+
formData.append('file', selectedFile);
|
256 |
+
if (isSummarizeMode) formData.append('length', 'medium');
|
257 |
+
|
258 |
+
try {
|
259 |
+
const response = await fetch(endpoint, {
|
260 |
+
method: 'POST',
|
261 |
+
body: formData
|
262 |
+
});
|
263 |
+
|
264 |
+
if (!response.ok) {
|
265 |
+
const error = await response.json().catch(() => null);
|
266 |
+
throw new Error(error?.detail || error?.error || "Request failed");
|
267 |
+
}
|
268 |
+
|
269 |
+
const result = await response.json();
|
270 |
+
thinkingBubble.remove();
|
271 |
+
|
272 |
+
if (isSummarizeMode) {
|
273 |
+
const answerBubble = createMessageBubble(
|
274 |
+
result.summary || "No summary generated.",
|
275 |
+
"Aidan",
|
276 |
+
result.audioUrl,
|
277 |
+
result.pdfUrl
|
278 |
+
);
|
279 |
+
if (result.audioUrl) injectAudio(answerBubble, result.audioUrl);
|
280 |
+
} else {
|
281 |
+
const answerBubble = createMessageBubble(
|
282 |
+
result.caption || result.answer || "No caption generated.",
|
283 |
+
"Aidan",
|
284 |
+
result.audio,
|
285 |
+
null
|
286 |
+
);
|
287 |
+
if (result.audio) injectAudio(answerBubble, result.audio);
|
288 |
+
}
|
289 |
+
|
290 |
+
} catch (error) {
|
291 |
+
thinkingBubble.remove();
|
292 |
+
createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
|
293 |
+
} finally {
|
294 |
+
selectedFile = null;
|
295 |
+
}
|
296 |
+
}
|
297 |
+
|
298 |
+
// ✅ Helper to inject audio player
|
299 |
+
function injectAudio(bubble, audioUrl) {
|
300 |
+
const messageDiv = bubble.querySelector('.text');
|
301 |
+
const audioPlayer = document.createElement('audio');
|
302 |
+
audioPlayer.src = audioUrl;
|
303 |
+
audioPlayer.controls = true;
|
304 |
+
audioPlayer.style.marginTop = "10px";
|
305 |
+
messageDiv.appendChild(audioPlayer);
|
306 |
+
}
|
307 |
+
|
308 |
+
|
309 |
const style = document.createElement('style');
|
310 |
style.textContent = `
|
311 |
.loader {
|