Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +80 -57
static/appS.js
CHANGED
@@ -173,64 +173,48 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
173 |
return bubble;
|
174 |
}
|
175 |
|
176 |
-
|
177 |
if (!selectedFile) {
|
178 |
alert("Please upload a file first");
|
179 |
return;
|
180 |
}
|
181 |
-
|
182 |
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
const thinkingText = isSummarizeMode
|
189 |
? 'Processing document ๐... <div class="loader"></div>'
|
190 |
: "Generating caption ๐ผ๏ธ... <div class='loader'></div>";
|
191 |
-
|
192 |
const thinkingBubble = createMessageBubble(thinkingText, "Aidan");
|
193 |
-
|
194 |
const formData = new FormData();
|
195 |
formData.append('file', selectedFile);
|
196 |
if (isSummarizeMode) formData.append('length', 'medium');
|
197 |
-
|
198 |
try {
|
199 |
const response = await fetch(endpoint, {
|
200 |
method: 'POST',
|
201 |
body: formData
|
202 |
});
|
203 |
-
|
204 |
if (!response.ok) {
|
205 |
const error = await response.json().catch(() => null);
|
206 |
throw new Error(error?.detail || error?.error || "Request failed");
|
207 |
}
|
208 |
-
|
209 |
const result = await response.json();
|
210 |
thinkingBubble.remove();
|
211 |
-
|
212 |
if (isSummarizeMode) {
|
213 |
-
|
214 |
result.summary || "No summary generated.",
|
215 |
"Aidan",
|
216 |
-
result.
|
217 |
result.pdfUrl
|
218 |
);
|
219 |
-
|
220 |
-
if (result.audio) {
|
221 |
-
injectAudioPlayer(bubble, result.audio);
|
222 |
-
}
|
223 |
} else {
|
224 |
-
|
225 |
-
result.caption || result.answer,
|
226 |
"Aidan",
|
227 |
-
result.audio,
|
228 |
null
|
229 |
);
|
230 |
-
|
231 |
-
if (result.audio) {
|
232 |
-
injectAudioPlayer(bubble, result.audio);
|
233 |
-
}
|
234 |
}
|
235 |
} catch (error) {
|
236 |
thinkingBubble.remove();
|
@@ -238,42 +222,82 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
238 |
} finally {
|
239 |
selectedFile = null;
|
240 |
}
|
|
|
|
|
|
|
|
|
|
|
241 |
}
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
audio.pause();
|
266 |
-
audioIcon.classList.remove("fa-volume-high");
|
267 |
-
audioIcon.classList.add("fa-volume-xmark");
|
268 |
-
audioIcon.title = "Unmute Audio";
|
269 |
-
}
|
270 |
});
|
271 |
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
-
// Add loader styles
|
277 |
const style = document.createElement('style');
|
278 |
style.textContent = `
|
279 |
.loader {
|
@@ -292,9 +316,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
292 |
`;
|
293 |
document.head.appendChild(style);
|
294 |
|
295 |
-
// Back button
|
296 |
var backarrow = document.querySelector(".fa-arrow-left");
|
297 |
backarrow.addEventListener('click', function () {
|
298 |
window.location.href = '/';
|
299 |
});
|
300 |
-
});
|
|
|
173 |
return bubble;
|
174 |
}
|
175 |
|
176 |
+
/* async function handleSubmit() {
|
177 |
if (!selectedFile) {
|
178 |
alert("Please upload a file first");
|
179 |
return;
|
180 |
}
|
|
|
181 |
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
182 |
|
183 |
+
const endpoint = isSummarizeMode
|
184 |
+
? `/Summarization/summarize/`
|
185 |
+
: `/Summarization/imagecaption/`;
|
|
|
186 |
const thinkingText = isSummarizeMode
|
187 |
? 'Processing document ๐... <div class="loader"></div>'
|
188 |
: "Generating caption ๐ผ๏ธ... <div class='loader'></div>";
|
|
|
189 |
const thinkingBubble = createMessageBubble(thinkingText, "Aidan");
|
|
|
190 |
const formData = new FormData();
|
191 |
formData.append('file', selectedFile);
|
192 |
if (isSummarizeMode) formData.append('length', 'medium');
|
|
|
193 |
try {
|
194 |
const response = await fetch(endpoint, {
|
195 |
method: 'POST',
|
196 |
body: formData
|
197 |
});
|
|
|
198 |
if (!response.ok) {
|
199 |
const error = await response.json().catch(() => null);
|
200 |
throw new Error(error?.detail || error?.error || "Request failed");
|
201 |
}
|
|
|
202 |
const result = await response.json();
|
203 |
thinkingBubble.remove();
|
|
|
204 |
if (isSummarizeMode) {
|
205 |
+
createMessageBubble(
|
206 |
result.summary || "No summary generated.",
|
207 |
"Aidan",
|
208 |
+
result.audioUrl,
|
209 |
result.pdfUrl
|
210 |
);
|
|
|
|
|
|
|
|
|
211 |
} else {
|
212 |
+
createMessageBubble(
|
213 |
+
result.caption || result.answer || "No caption generated.",
|
214 |
"Aidan",
|
215 |
+
result.audio,
|
216 |
null
|
217 |
);
|
|
|
|
|
|
|
|
|
218 |
}
|
219 |
} catch (error) {
|
220 |
thinkingBubble.remove();
|
|
|
222 |
} finally {
|
223 |
selectedFile = null;
|
224 |
}
|
225 |
+
} */
|
226 |
+
async function handleSubmit() {
|
227 |
+
if (!selectedFile) {
|
228 |
+
alert("Please upload a file first");
|
229 |
+
return;
|
230 |
}
|
231 |
|
232 |
+
const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
|
233 |
+
|
234 |
+
const endpoint = isSummarizeMode
|
235 |
+
? `/Summarization/summarize/`
|
236 |
+
: `/Summarization/imagecaption/`;
|
237 |
+
|
238 |
+
const thinkingBubble = createMessageBubble("Thinking...", "Aidan");
|
239 |
+
|
240 |
+
// โ
Inject loader correctly
|
241 |
+
const messageDiv = thinkingBubble.querySelector('.text');
|
242 |
+
const loader = document.createElement('div');
|
243 |
+
loader.className = 'loader';
|
244 |
+
messageDiv.appendChild(loader);
|
245 |
+
|
246 |
+
const formData = new FormData();
|
247 |
+
formData.append('file', selectedFile);
|
248 |
+
if (isSummarizeMode) formData.append('length', 'medium');
|
249 |
+
|
250 |
+
try {
|
251 |
+
const response = await fetch(endpoint, {
|
252 |
+
method: 'POST',
|
253 |
+
body: formData
|
|
|
|
|
|
|
|
|
|
|
254 |
});
|
255 |
|
256 |
+
if (!response.ok) {
|
257 |
+
const error = await response.json().catch(() => null);
|
258 |
+
throw new Error(error?.detail || error?.error || "Request failed");
|
259 |
+
}
|
260 |
+
|
261 |
+
const result = await response.json();
|
262 |
+
thinkingBubble.remove();
|
263 |
+
|
264 |
+
if (isSummarizeMode) {
|
265 |
+
const answerBubble = createMessageBubble(
|
266 |
+
result.summary || "No summary generated.",
|
267 |
+
"Aidan",
|
268 |
+
result.audioUrl,
|
269 |
+
result.pdfUrl
|
270 |
+
);
|
271 |
+
if (result.audioUrl) injectAudio(answerBubble, result.audioUrl);
|
272 |
+
} else {
|
273 |
+
const answerBubble = createMessageBubble(
|
274 |
+
result.caption || result.answer || "No caption generated.",
|
275 |
+
"Aidan",
|
276 |
+
result.audio,
|
277 |
+
null
|
278 |
+
);
|
279 |
+
if (result.audio) injectAudio(answerBubble, result.audio);
|
280 |
+
}
|
281 |
+
|
282 |
+
} catch (error) {
|
283 |
+
thinkingBubble.remove();
|
284 |
+
createMessageBubble(`โ ๏ธ Error: ${error.message}`, "Aidan");
|
285 |
+
} finally {
|
286 |
+
selectedFile = null;
|
287 |
}
|
288 |
+
}
|
289 |
+
|
290 |
+
// โ
Helper to inject audio player
|
291 |
+
function injectAudio(bubble, audioUrl) {
|
292 |
+
const messageDiv = bubble.querySelector('.text');
|
293 |
+
const audioPlayer = document.createElement('audio');
|
294 |
+
audioPlayer.src = audioUrl;
|
295 |
+
audioPlayer.controls = true;
|
296 |
+
audioPlayer.style.marginTop = "10px";
|
297 |
+
messageDiv.appendChild(audioPlayer);
|
298 |
+
}
|
299 |
+
|
300 |
|
|
|
301 |
const style = document.createElement('style');
|
302 |
style.textContent = `
|
303 |
.loader {
|
|
|
316 |
`;
|
317 |
document.head.appendChild(style);
|
318 |
|
|
|
319 |
var backarrow = document.querySelector(".fa-arrow-left");
|
320 |
backarrow.addEventListener('click', function () {
|
321 |
window.location.href = '/';
|
322 |
});
|
323 |
+
});
|