Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +89 -92
static/appS.js
CHANGED
@@ -173,111 +173,107 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
173 |
return bubble;
|
174 |
}
|
175 |
|
176 |
-
async function handleSubmit() {
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
|
192 |
-
|
193 |
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
throw new Error(
|
207 |
}
|
208 |
-
throw new Error("Request failed");
|
209 |
-
}
|
210 |
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
}
|
|
|
|
|
|
|
|
|
|
|
237 |
}
|
238 |
-
} catch (error) {
|
239 |
-
thinkingBubble.remove();
|
240 |
-
createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
|
241 |
-
} finally {
|
242 |
-
selectedFile = null;
|
243 |
}
|
244 |
-
}
|
245 |
-
|
246 |
-
// Audio player helper function remains the same
|
247 |
-
function injectAudioPlayer(bubble, audioUrl) {
|
248 |
-
const messageDiv = bubble.querySelector('.text');
|
249 |
-
const audioContainer = document.createElement('div');
|
250 |
-
audioContainer.style.marginTop = "10px";
|
251 |
-
audioContainer.style.display = "flex";
|
252 |
-
audioContainer.style.justifyContent = "flex-end";
|
253 |
-
audioContainer.style.gap = "15px";
|
254 |
-
|
255 |
-
const audio = new Audio(audioUrl);
|
256 |
-
const audioIcon = document.createElement("i");
|
257 |
-
audioIcon.className = "fa-solid fa-volume-high audio-toggle";
|
258 |
-
audioIcon.title = "Play Audio";
|
259 |
-
audioIcon.style.cursor = "pointer";
|
260 |
-
audioIcon.style.fontSize = "18px";
|
261 |
-
|
262 |
-
audioIcon.addEventListener("click", () => {
|
263 |
-
if (audio.paused) {
|
264 |
-
audio.play();
|
265 |
-
audioIcon.classList.remove("fa-volume-xmark");
|
266 |
-
audioIcon.classList.add("fa-volume-high");
|
267 |
-
audioIcon.title = "Mute Audio";
|
268 |
-
} else {
|
269 |
-
audio.pause();
|
270 |
-
audioIcon.classList.remove("fa-volume-high");
|
271 |
-
audioIcon.classList.add("fa-volume-xmark");
|
272 |
-
audioIcon.title = "Unmute Audio";
|
273 |
-
}
|
274 |
-
});
|
275 |
|
276 |
-
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
|
|
|
|
|
|
280 |
|
|
|
281 |
const style = document.createElement('style');
|
282 |
style.textContent = `
|
283 |
.loader {
|
@@ -296,8 +292,9 @@ function injectAudioPlayer(bubble, audioUrl) {
|
|
296 |
`;
|
297 |
document.head.appendChild(style);
|
298 |
|
|
|
299 |
var backarrow = document.querySelector(".fa-arrow-left");
|
300 |
backarrow.addEventListener('click', function () {
|
301 |
window.location.href = '/';
|
302 |
});
|
303 |
-
});
|
|
|
173 |
return bubble;
|
174 |
}
|
175 |
|
176 |
+
async function handleSubmit() {
|
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 |
+
const endpoint = isSummarizeMode
|
185 |
+
? '/summarization/summarize/'
|
186 |
+
: '/summarization/imagecaption/';
|
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 |
+
const bubble = createMessageBubble(
|
214 |
+
result.summary || "No summary generated.",
|
215 |
+
"Aidan",
|
216 |
+
result.audio, // Using audio from response
|
217 |
+
result.pdfUrl
|
218 |
+
);
|
219 |
+
|
220 |
+
if (result.audio) {
|
221 |
+
injectAudioPlayer(bubble, result.audio);
|
222 |
+
}
|
223 |
+
} else {
|
224 |
+
const bubble = createMessageBubble(
|
225 |
+
result.caption || result.answer,
|
226 |
+
"Aidan",
|
227 |
+
result.audio, // Using audio from response
|
228 |
+
null
|
229 |
+
);
|
230 |
+
|
231 |
+
if (result.audio) {
|
232 |
+
injectAudioPlayer(bubble, result.audio);
|
233 |
+
}
|
234 |
}
|
235 |
+
} catch (error) {
|
236 |
+
thinkingBubble.remove();
|
237 |
+
createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
|
238 |
+
} finally {
|
239 |
+
selectedFile = null;
|
240 |
}
|
|
|
|
|
|
|
|
|
|
|
241 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
|
243 |
+
function injectAudioPlayer(bubble, audioUrl) {
|
244 |
+
const messageDiv = bubble.querySelector('.text');
|
245 |
+
const audioContainer = document.createElement('div');
|
246 |
+
audioContainer.style.marginTop = "10px";
|
247 |
+
audioContainer.style.display = "flex";
|
248 |
+
audioContainer.style.justifyContent = "flex-end";
|
249 |
+
audioContainer.style.gap = "15px";
|
250 |
+
|
251 |
+
const audio = new Audio(audioUrl);
|
252 |
+
const audioIcon = document.createElement("i");
|
253 |
+
audioIcon.className = "fa-solid fa-volume-high audio-toggle";
|
254 |
+
audioIcon.title = "Play Audio";
|
255 |
+
audioIcon.style.cursor = "pointer";
|
256 |
+
audioIcon.style.fontSize = "18px";
|
257 |
+
|
258 |
+
audioIcon.addEventListener("click", () => {
|
259 |
+
if (audio.paused) {
|
260 |
+
audio.play();
|
261 |
+
audioIcon.classList.remove("fa-volume-xmark");
|
262 |
+
audioIcon.classList.add("fa-volume-high");
|
263 |
+
audioIcon.title = "Mute Audio";
|
264 |
+
} else {
|
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 |
+
audioContainer.appendChild(audioIcon);
|
273 |
+
messageDiv.appendChild(audioContainer);
|
274 |
+
}
|
275 |
|
276 |
+
// Add loader styles
|
277 |
const style = document.createElement('style');
|
278 |
style.textContent = `
|
279 |
.loader {
|
|
|
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 |
+
});
|