Spaces:
Running
Running
Update static/test.js
Browse files- static/test.js +131 -300
static/test.js
CHANGED
@@ -216,12 +216,14 @@ function showSection(sectionId) {
|
|
216 |
// Main navigation functionality
|
217 |
|
218 |
// Main navigation and shared utility functions
|
219 |
-
function showSection(sectionId) {
|
|
|
220 |
const sections = document.querySelectorAll('.tool-section');
|
221 |
sections.forEach(section => {
|
222 |
section.style.display = 'none';
|
223 |
});
|
224 |
|
|
|
225 |
const activeSection = document.getElementById(sectionId);
|
226 |
if (activeSection) {
|
227 |
activeSection.style.display = 'block';
|
@@ -229,54 +231,6 @@ function showSection(sectionId) {
|
|
229 |
}
|
230 |
}
|
231 |
|
232 |
-
// Shared utility functions
|
233 |
-
function preventDefaults(e) {
|
234 |
-
e.preventDefault();
|
235 |
-
e.stopPropagation();
|
236 |
-
}
|
237 |
-
|
238 |
-
function isValidFileType(file, types) {
|
239 |
-
const validExtensions = types.map(t => `.${t}`);
|
240 |
-
return types.includes(file.type.split('/').pop()) ||
|
241 |
-
validExtensions.some(ext => file.name.toLowerCase().endsWith(ext));
|
242 |
-
}
|
243 |
-
|
244 |
-
function getFileIcon(file) {
|
245 |
-
if (file.type.startsWith('image/')) return 'πΌοΈ';
|
246 |
-
if (file.type === 'application/pdf') return 'π';
|
247 |
-
if (file.type.includes('wordprocessingml')) return 'π';
|
248 |
-
return 'π';
|
249 |
-
}
|
250 |
-
|
251 |
-
function formatFileSize(bytes) {
|
252 |
-
if (bytes === 0) return '0 Bytes';
|
253 |
-
const k = 1024;
|
254 |
-
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
255 |
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
256 |
-
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
257 |
-
}
|
258 |
-
|
259 |
-
function setLoading(button, isLoading) {
|
260 |
-
button.disabled = isLoading;
|
261 |
-
const btnText = button.querySelector('span:not(.spinner)');
|
262 |
-
if (btnText) {
|
263 |
-
btnText.textContent = isLoading ? 'Processing...' : button.dataset.defaultText;
|
264 |
-
}
|
265 |
-
const spinner = button.querySelector('.spinner');
|
266 |
-
if (spinner) {
|
267 |
-
spinner.classList.toggle('hidden', !isLoading);
|
268 |
-
}
|
269 |
-
}
|
270 |
-
|
271 |
-
function showError(element, message) {
|
272 |
-
element.textContent = message;
|
273 |
-
element.classList.remove('hidden');
|
274 |
-
}
|
275 |
-
|
276 |
-
function hideError(element) {
|
277 |
-
element.classList.add('hidden');
|
278 |
-
}
|
279 |
-
|
280 |
// Initialize the page
|
281 |
document.addEventListener('DOMContentLoaded', () => {
|
282 |
// Set up card click handlers
|
@@ -290,298 +244,175 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
290 |
// Show first section by default
|
291 |
showSection('document-image-analysis');
|
292 |
|
293 |
-
//
|
294 |
-
|
295 |
-
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
|
298 |
-
//
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
const submitBtn = section.querySelector('#summarize-btn');
|
308 |
-
const resultArea = section.querySelector('#result-content');
|
309 |
-
const errorElement = section.querySelector('#error-message');
|
310 |
-
const languageSelect = section.querySelector('#language-select');
|
311 |
-
|
312 |
-
// Store default button text
|
313 |
-
if (submitBtn) {
|
314 |
-
const btnText = submitBtn.querySelector('span');
|
315 |
-
if (btnText) {
|
316 |
-
submitBtn.dataset.defaultText = btnText.textContent;
|
317 |
}
|
318 |
-
}
|
319 |
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
|
326 |
-
|
327 |
-
|
328 |
-
|
|
|
329 |
|
330 |
-
|
331 |
-
|
332 |
-
|
|
|
333 |
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
|
|
|
|
|
|
|
|
|
|
338 |
|
339 |
-
|
340 |
-
|
|
|
|
|
341 |
}
|
342 |
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
const text = textInput.value.trim();
|
347 |
-
const language = languageSelect.value;
|
348 |
-
|
349 |
-
if (!file && !text) {
|
350 |
-
showError(errorElement, "Please upload a file or enter text");
|
351 |
-
return;
|
352 |
-
}
|
353 |
|
354 |
-
|
355 |
-
|
356 |
-
hideError(errorElement);
|
357 |
-
if (resultArea) resultArea.textContent = '';
|
358 |
-
|
359 |
-
const formData = new FormData();
|
360 |
-
if (file) formData.append('file', file);
|
361 |
-
if (text) formData.append('text', text);
|
362 |
-
formData.append('language', language);
|
363 |
-
|
364 |
-
const response = await fetch('/summarize', {
|
365 |
-
method: 'POST',
|
366 |
-
body: formData
|
367 |
-
});
|
368 |
-
|
369 |
-
if (!response.ok) {
|
370 |
-
const error = await response.json();
|
371 |
-
throw new Error(error.detail || "Failed to summarize content");
|
372 |
-
}
|
373 |
-
|
374 |
-
const result = await response.json();
|
375 |
-
if (resultArea) resultArea.textContent = result.summary;
|
376 |
-
} catch (error) {
|
377 |
-
showError(errorElement, error.message);
|
378 |
-
console.error("Summarization error:", error);
|
379 |
-
} finally {
|
380 |
-
setLoading(submitBtn, false);
|
381 |
-
}
|
382 |
-
});
|
383 |
}
|
384 |
|
385 |
function handleDrop(e) {
|
386 |
const dt = e.dataTransfer;
|
387 |
-
|
388 |
-
|
389 |
-
}
|
390 |
}
|
391 |
|
392 |
function handleFileSelect(e) {
|
393 |
-
|
394 |
-
|
|
|
395 |
}
|
396 |
}
|
397 |
|
398 |
function handleFiles(files) {
|
399 |
-
if (!files || files.length === 0) return;
|
400 |
-
|
401 |
const file = files[0];
|
402 |
-
|
403 |
-
|
404 |
-
if (!isValidFileType(file, validTypes)) {
|
405 |
-
showError(errorElement, "Unsupported file type. Please upload PDF, DOCX, TXT, or image files.");
|
406 |
return;
|
407 |
}
|
408 |
|
409 |
-
|
410 |
-
|
411 |
-
<
|
412 |
-
|
413 |
-
<
|
414 |
-
|
415 |
-
<span>${formatFileSize(file.size)}</span>
|
416 |
-
</div>
|
417 |
</div>
|
418 |
-
|
419 |
-
|
420 |
validateInputs();
|
421 |
}
|
422 |
|
423 |
function validateInputs() {
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
const hasText = textInput ? textInput.value.trim() !== '' : false;
|
428 |
-
submitBtn.disabled = !(hasFile || hasText);
|
429 |
}
|
430 |
-
}
|
431 |
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
const confidenceValue = section.querySelector('#qa-confidence-value');
|
445 |
-
const resultContainer = section.querySelector('#qa-result-container');
|
446 |
-
const errorElement = section.querySelector('#qa-error-message');
|
447 |
-
const languageSelect = section.querySelector('#qa-language');
|
448 |
-
|
449 |
-
// Store default button text
|
450 |
-
if (submitBtn) {
|
451 |
-
const btnText = submitBtn.querySelector('span');
|
452 |
-
if (btnText) {
|
453 |
-
submitBtn.dataset.defaultText = btnText.textContent;
|
454 |
-
}
|
455 |
}
|
456 |
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
['dragenter', 'dragover'].forEach(eventName => {
|
464 |
-
dropArea.addEventListener(eventName, () => dropArea.classList.add('highlight'));
|
465 |
-
});
|
466 |
-
|
467 |
-
['dragleave', 'drop'].forEach(eventName => {
|
468 |
-
dropArea.addEventListener(eventName, () => dropArea.classList.remove('highlight'));
|
469 |
-
});
|
470 |
-
|
471 |
-
dropArea.addEventListener('drop', handleDrop, false);
|
472 |
-
dropArea.addEventListener('click', () => fileInput.click());
|
473 |
-
fileInput.addEventListener('change', handleFileSelect);
|
474 |
}
|
475 |
|
476 |
-
|
477 |
-
|
|
|
|
|
|
|
|
|
478 |
}
|
479 |
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
if (!file) {
|
487 |
-
showError(errorElement, "Please upload a document first");
|
488 |
-
return;
|
489 |
-
}
|
490 |
-
|
491 |
-
if (!question) {
|
492 |
-
showError(errorElement, "Please enter your question");
|
493 |
-
return;
|
494 |
-
}
|
495 |
-
|
496 |
-
try {
|
497 |
-
setLoading(submitBtn, true);
|
498 |
-
hideError(errorElement);
|
499 |
-
if (resultContainer) resultContainer.classList.add('hidden');
|
500 |
-
|
501 |
-
const formData = new FormData();
|
502 |
-
formData.append('file', file);
|
503 |
-
formData.append('question', question);
|
504 |
-
formData.append('language', language);
|
505 |
-
|
506 |
-
const response = await fetch('/qa', {
|
507 |
-
method: 'POST',
|
508 |
-
body: formData
|
509 |
-
});
|
510 |
-
|
511 |
-
if (!response.ok) {
|
512 |
-
const error = await response.json();
|
513 |
-
throw new Error(error.detail || "Failed to get answer");
|
514 |
-
}
|
515 |
-
|
516 |
-
const result = await response.json();
|
517 |
-
|
518 |
-
// Display results
|
519 |
-
if (answerArea) answerArea.textContent = result.answer;
|
520 |
-
|
521 |
-
// Update confidence display if elements exist
|
522 |
-
if (confidenceBar && confidenceValue) {
|
523 |
-
const confidencePercent = Math.round((result.confidence || 0) * 100);
|
524 |
-
confidenceBar.style.width = `${confidencePercent}%`;
|
525 |
-
confidenceValue.textContent = `${confidencePercent}%`;
|
526 |
-
confidenceBar.style.backgroundColor =
|
527 |
-
confidencePercent < 30 ? '#f44336' :
|
528 |
-
confidencePercent < 70 ? '#ff9800' : '#4CAF50';
|
529 |
-
}
|
530 |
-
|
531 |
-
if (resultContainer) resultContainer.classList.remove('hidden');
|
532 |
-
} catch (error) {
|
533 |
-
showError(errorElement, error.message);
|
534 |
-
console.error("QA error:", error);
|
535 |
-
} finally {
|
536 |
-
setLoading(submitBtn, false);
|
537 |
-
}
|
538 |
-
});
|
539 |
-
}
|
540 |
-
|
541 |
-
function handleDrop(e) {
|
542 |
-
const dt = e.dataTransfer;
|
543 |
-
if (dt && dt.files) {
|
544 |
-
handleFiles(dt.files);
|
545 |
-
}
|
546 |
-
}
|
547 |
-
|
548 |
-
function handleFileSelect(e) {
|
549 |
-
if (e.target.files) {
|
550 |
-
handleFiles(e.target.files);
|
551 |
-
}
|
552 |
}
|
553 |
|
554 |
-
function
|
555 |
-
|
556 |
-
|
557 |
-
const file = files[0];
|
558 |
-
const validTypes = ['pdf', 'docx', 'txt', 'jpeg', 'png'];
|
559 |
-
|
560 |
-
if (!isValidFileType(file, validTypes)) {
|
561 |
-
showError(errorElement, "Unsupported file type. Please upload PDF, DOCX, or TXT files.");
|
562 |
-
return;
|
563 |
-
}
|
564 |
-
|
565 |
-
if (filePreview) {
|
566 |
-
filePreview.innerHTML = `
|
567 |
-
<div class="file-info">
|
568 |
-
<span class="file-icon">${getFileIcon(file)}</span>
|
569 |
-
<div>
|
570 |
-
<strong>${file.name}</strong>
|
571 |
-
<span>${formatFileSize(file.size)}</span>
|
572 |
-
</div>
|
573 |
-
</div>
|
574 |
-
`;
|
575 |
-
}
|
576 |
-
validateInputs();
|
577 |
}
|
578 |
|
579 |
-
function
|
580 |
-
|
581 |
-
|
582 |
-
const hasFile = fileInput.files.length > 0;
|
583 |
-
const hasQuestion = questionInput.value.trim() !== '';
|
584 |
-
submitBtn.disabled = !(hasFile && hasQuestion);
|
585 |
}
|
586 |
-
}
|
587 |
|
|
|
216 |
// Main navigation functionality
|
217 |
|
218 |
// Main navigation and shared utility functions
|
219 |
+
i alerdy have this for summerise function showSection(sectionId) {
|
220 |
+
// Hide all tool sections
|
221 |
const sections = document.querySelectorAll('.tool-section');
|
222 |
sections.forEach(section => {
|
223 |
section.style.display = 'none';
|
224 |
});
|
225 |
|
226 |
+
// Show the selected section
|
227 |
const activeSection = document.getElementById(sectionId);
|
228 |
if (activeSection) {
|
229 |
activeSection.style.display = 'block';
|
|
|
231 |
}
|
232 |
}
|
233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
// Initialize the page
|
235 |
document.addEventListener('DOMContentLoaded', () => {
|
236 |
// Set up card click handlers
|
|
|
244 |
// Show first section by default
|
245 |
showSection('document-image-analysis');
|
246 |
|
247 |
+
// Document Analysis Section Setup
|
248 |
+
const summarizeDropArea = document.getElementById('upload-area');
|
249 |
+
const summarizeFileInput = document.getElementById('file-input');
|
250 |
+
const summarizePreview = document.getElementById('file-preview');
|
251 |
+
const textInput = document.getElementById('text-input');
|
252 |
+
const summarizeBtn = document.getElementById('summarize-btn');
|
253 |
+
const summarizeResult = document.getElementById('result-content');
|
254 |
+
const summarizeError = document.getElementById('error-message');
|
255 |
+
const languageSelect = document.getElementById('language-select');
|
256 |
+
|
257 |
+
// Set up drag and drop for file upload
|
258 |
+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
259 |
+
summarizeDropArea.addEventListener(eventName, preventDefaults, false);
|
260 |
+
});
|
261 |
+
|
262 |
+
['dragenter', 'dragover'].forEach(eventName => {
|
263 |
+
summarizeDropArea.addEventListener(eventName, highlight, false);
|
264 |
+
});
|
265 |
+
|
266 |
+
['dragleave', 'drop'].forEach(eventName => {
|
267 |
+
summarizeDropArea.addEventListener(eventName, unhighlight, false);
|
268 |
+
});
|
269 |
+
|
270 |
+
summarizeDropArea.addEventListener('drop', handleDrop, false);
|
271 |
+
summarizeDropArea.addEventListener('click', () => summarizeFileInput.click());
|
272 |
+
summarizeFileInput.addEventListener('change', handleFileSelect);
|
273 |
+
textInput.addEventListener('input', validateInputs);
|
274 |
|
275 |
+
// Summarize button click handler
|
276 |
+
summarizeBtn.addEventListener('click', async () => {
|
277 |
+
const file = summarizeFileInput.files[0];
|
278 |
+
const text = textInput.value.trim();
|
279 |
+
const language = languageSelect.value;
|
280 |
+
|
281 |
+
if (!file && !text) {
|
282 |
+
showError(summarizeError, "Please upload a file or enter text");
|
283 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
}
|
|
|
285 |
|
286 |
+
try {
|
287 |
+
// Show loading state
|
288 |
+
setLoading(true);
|
289 |
+
hideError(summarizeError);
|
290 |
+
summarizeResult.textContent = '';
|
291 |
+
|
292 |
+
const formData = new FormData();
|
293 |
+
if (file) {
|
294 |
+
formData.append('file', file);
|
295 |
+
} else {
|
296 |
+
formData.append('text', text);
|
297 |
+
}
|
298 |
+
formData.append('language', language);
|
299 |
|
300 |
+
const response = await fetch('/summarize', {
|
301 |
+
method: 'POST',
|
302 |
+
body: formData
|
303 |
+
});
|
304 |
|
305 |
+
if (!response.ok) {
|
306 |
+
const error = await response.json();
|
307 |
+
throw new Error(error.detail || "Failed to summarize content");
|
308 |
+
}
|
309 |
|
310 |
+
const result = await response.json();
|
311 |
+
summarizeResult.textContent = result.summary;
|
312 |
+
} catch (error) {
|
313 |
+
showError(summarizeError, error.message);
|
314 |
+
console.error("Summarization error:", error);
|
315 |
+
} finally {
|
316 |
+
setLoading(false);
|
317 |
+
}
|
318 |
+
});
|
319 |
|
320 |
+
// Helper functions
|
321 |
+
function preventDefaults(e) {
|
322 |
+
e.preventDefault();
|
323 |
+
e.stopPropagation();
|
324 |
}
|
325 |
|
326 |
+
function highlight() {
|
327 |
+
summarizeDropArea.classList.add('highlight');
|
328 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
|
330 |
+
function unhighlight() {
|
331 |
+
summarizeDropArea.classList.remove('highlight');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
}
|
333 |
|
334 |
function handleDrop(e) {
|
335 |
const dt = e.dataTransfer;
|
336 |
+
const files = dt.files;
|
337 |
+
handleFiles(files);
|
|
|
338 |
}
|
339 |
|
340 |
function handleFileSelect(e) {
|
341 |
+
const files = e.target.files;
|
342 |
+
if (files.length) {
|
343 |
+
handleFiles(files);
|
344 |
}
|
345 |
}
|
346 |
|
347 |
function handleFiles(files) {
|
|
|
|
|
348 |
const file = files[0];
|
349 |
+
if (!isValidFileType(file)) {
|
350 |
+
showError(summarizeError, "Unsupported file type. Please upload PDF, DOCX, TXT, or image files.");
|
|
|
|
|
351 |
return;
|
352 |
}
|
353 |
|
354 |
+
summarizePreview.innerHTML = `
|
355 |
+
<div class="file-info">
|
356 |
+
<span class="file-icon">${getFileIcon(file)}</span>
|
357 |
+
<div>
|
358 |
+
<strong>${file.name}</strong>
|
359 |
+
<span>${formatFileSize(file.size)}</span>
|
|
|
|
|
360 |
</div>
|
361 |
+
</div>
|
362 |
+
`;
|
363 |
validateInputs();
|
364 |
}
|
365 |
|
366 |
function validateInputs() {
|
367 |
+
const hasFile = summarizeFileInput.files.length > 0;
|
368 |
+
const hasText = textInput.value.trim() !== '';
|
369 |
+
summarizeBtn.disabled = !(hasFile || hasText);
|
|
|
|
|
370 |
}
|
|
|
371 |
|
372 |
+
function isValidFileType(file) {
|
373 |
+
const validTypes = [
|
374 |
+
'application/pdf',
|
375 |
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
376 |
+
'text/plain',
|
377 |
+
'image/jpeg',
|
378 |
+
'image/png'
|
379 |
+
];
|
380 |
+
return validTypes.includes(file.type) ||
|
381 |
+
file.name.endsWith('.pdf') ||
|
382 |
+
file.name.endsWith('.docx') ||
|
383 |
+
file.name.endsWith('.txt');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
}
|
385 |
|
386 |
+
function getFileIcon(file) {
|
387 |
+
if (file.type.startsWith('image/')) return 'πΌοΈ';
|
388 |
+
if (file.type === 'application/pdf') return 'π';
|
389 |
+
if (file.type.includes('wordprocessingml')) return 'π';
|
390 |
+
return 'π';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
391 |
}
|
392 |
|
393 |
+
function formatFileSize(bytes) {
|
394 |
+
if (bytes === 0) return '0 Bytes';
|
395 |
+
const k = 1024;
|
396 |
+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
397 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
398 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
399 |
}
|
400 |
|
401 |
+
function setLoading(isLoading) {
|
402 |
+
summarizeBtn.disabled = isLoading;
|
403 |
+
const btnText = summarizeBtn.querySelector('span');
|
404 |
+
btnText.textContent = isLoading ? 'Processing...' : 'Summarize Document';
|
405 |
+
const spinner = summarizeBtn.querySelector('.spinner');
|
406 |
+
spinner.classList.toggle('hidden', !isLoading);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
407 |
}
|
408 |
|
409 |
+
function showError(element, message) {
|
410 |
+
element.textContent = message;
|
411 |
+
element.classList.remove('hidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
}
|
413 |
|
414 |
+
function hideError() {
|
415 |
+
summarizeError.classList.add('hidden');
|
|
|
|
|
|
|
|
|
416 |
}
|
417 |
+
});
|
418 |
|