Spaces:
Sleeping
Sleeping
Update static/test.js
Browse files- static/test.js +74 -71
static/test.js
CHANGED
@@ -417,19 +417,24 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
417 |
|
418 |
// Add this after your existing summarization code, inside the DOMContentLoaded event listener
|
419 |
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
|
|
|
|
|
|
|
|
|
|
433 |
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
434 |
qaDropArea.addEventListener(eventName, preventDefaults, false);
|
435 |
});
|
@@ -445,71 +450,69 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
445 |
qaDropArea.addEventListener('drop', handleQADrop, false);
|
446 |
qaDropArea.addEventListener('click', () => qaFileInput.click());
|
447 |
qaFileInput.addEventListener('change', handleQAFileSelect);
|
448 |
-
|
449 |
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
const language = qaLanguageSelect.value;
|
455 |
-
|
456 |
-
if (!file) {
|
457 |
-
showError(qaError, "Please upload a document first");
|
458 |
-
return;
|
459 |
-
}
|
460 |
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
465 |
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
qaResult.textContent = '';
|
471 |
-
|
472 |
-
const formData = new FormData();
|
473 |
-
formData.append('file', file);
|
474 |
-
formData.append('question', question);
|
475 |
-
formData.append('language', language);
|
476 |
|
477 |
-
|
478 |
-
|
479 |
-
body: formData
|
480 |
-
});
|
481 |
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
486 |
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
// Update confidence display
|
493 |
-
const confidencePercent = Math.round(result.confidence * 100);
|
494 |
-
qaConfidenceBar.style.width = `${confidencePercent}%`;
|
495 |
-
qaConfidenceValue.textContent = `${confidencePercent}%`;
|
496 |
-
|
497 |
-
// Color code confidence level
|
498 |
-
if (confidencePercent < 30) {
|
499 |
-
qaConfidenceBar.style.backgroundColor = '#f44336';
|
500 |
-
} else if (confidencePercent < 70) {
|
501 |
-
qaConfidenceBar.style.backgroundColor = '#ff9800';
|
502 |
-
} else {
|
503 |
-
qaConfidenceBar.style.backgroundColor = '#4CAF50';
|
504 |
-
}
|
505 |
-
|
506 |
-
} catch (error) {
|
507 |
-
showError(qaError, error.message);
|
508 |
-
console.error("Question answering error:", error);
|
509 |
-
} finally {
|
510 |
-
setQALoading(false);
|
511 |
}
|
512 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
513 |
|
514 |
// QA-specific helper functions
|
515 |
function handleQADrop(e) {
|
|
|
417 |
|
418 |
// Add this after your existing summarization code, inside the DOMContentLoaded event listener
|
419 |
|
420 |
+
// Question Answering Section Setup
|
421 |
+
const qaDropArea = document.getElementById('qa-upload-area');
|
422 |
+
const qaFileInput = document.getElementById('qa-file-input');
|
423 |
+
const qaPreview = document.getElementById('qa-file-preview');
|
424 |
+
const qaQuestionInput = document.getElementById('qa-question');
|
425 |
+
const qaSubmitBtn = document.getElementById('qa-submit-btn');
|
426 |
+
const qaResult = document.getElementById('qa-answer');
|
427 |
+
const qaError = document.getElementById('qa-error-message');
|
428 |
+
const qaLanguageSelect = document.getElementById('qa-language');
|
429 |
+
|
430 |
+
// Debug: Verify elements exist
|
431 |
+
console.log('QA Elements:', {
|
432 |
+
qaDropArea, qaFileInput, qaPreview, qaQuestionInput,
|
433 |
+
qaSubmitBtn, qaResult, qaError, qaLanguageSelect
|
434 |
+
});
|
435 |
+
|
436 |
+
// Setup event listeners
|
437 |
+
if (qaDropArea && qaFileInput) {
|
438 |
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
439 |
qaDropArea.addEventListener(eventName, preventDefaults, false);
|
440 |
});
|
|
|
450 |
qaDropArea.addEventListener('drop', handleQADrop, false);
|
451 |
qaDropArea.addEventListener('click', () => qaFileInput.click());
|
452 |
qaFileInput.addEventListener('change', handleQAFileSelect);
|
453 |
+
}
|
454 |
|
455 |
+
if (qaQuestionInput && qaSubmitBtn) {
|
456 |
+
qaQuestionInput.addEventListener('input', validateQAInputs);
|
457 |
+
qaSubmitBtn.addEventListener('click', handleQASubmit);
|
458 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
459 |
|
460 |
+
function handleQASubmit() {
|
461 |
+
console.log('Submit button clicked!');
|
462 |
+
|
463 |
+
const file = qaFileInput.files[0];
|
464 |
+
const question = qaQuestionInput.value.trim();
|
465 |
+
const language = qaLanguageSelect.value;
|
466 |
+
|
467 |
+
console.log('Submission data:', { file, question, language });
|
468 |
+
|
469 |
+
if (!file) {
|
470 |
+
showError(qaError, "Please upload a document first");
|
471 |
+
return;
|
472 |
+
}
|
473 |
|
474 |
+
if (!question) {
|
475 |
+
showError(qaError, "Please enter your question");
|
476 |
+
return;
|
477 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
|
479 |
+
submitQARequest(file, question, language);
|
480 |
+
}
|
|
|
|
|
481 |
|
482 |
+
async function submitQARequest(file, question, language) {
|
483 |
+
try {
|
484 |
+
setQALoading(true);
|
485 |
+
hideError(qaError);
|
486 |
+
|
487 |
+
const formData = new FormData();
|
488 |
+
formData.append('file', file);
|
489 |
+
formData.append('question', question);
|
490 |
+
formData.append('language', language);
|
491 |
+
|
492 |
+
console.log('Sending request to /qa...');
|
493 |
+
const response = await fetch('/qa', {
|
494 |
+
method: 'POST',
|
495 |
+
body: formData
|
496 |
+
});
|
497 |
|
498 |
+
console.log('Response status:', response.status);
|
499 |
+
|
500 |
+
if (!response.ok) {
|
501 |
+
const error = await response.json();
|
502 |
+
throw new Error(error.detail || "Failed to get answer");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
}
|
504 |
+
|
505 |
+
const result = await response.json();
|
506 |
+
console.log('API Response:', result);
|
507 |
+
|
508 |
+
displayQAResult(result);
|
509 |
+
} catch (error) {
|
510 |
+
console.error("QA Error:", error);
|
511 |
+
showError(qaError, error.message);
|
512 |
+
} finally {
|
513 |
+
setQALoading(false);
|
514 |
+
}
|
515 |
+
}
|
516 |
|
517 |
// QA-specific helper functions
|
518 |
function handleQADrop(e) {
|