Spaces:
Sleeping
Sleeping
Update static/test.js
Browse files- static/test.js +182 -142
static/test.js
CHANGED
|
@@ -214,7 +214,8 @@ function showSection(sectionId) {
|
|
| 214 |
|
| 215 |
// Function to show the selected section and hide the others
|
| 216 |
// Main navigation functionality
|
| 217 |
-
|
|
|
|
| 218 |
function showSection(sectionId) {
|
| 219 |
const sections = document.querySelectorAll('.tool-section');
|
| 220 |
sections.forEach(section => {
|
|
@@ -309,78 +310,93 @@ function initSummarization() {
|
|
| 309 |
const languageSelect = section.querySelector('#language-select');
|
| 310 |
|
| 311 |
// Store default button text
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
// Set up drag and drop
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
const text = textInput.value.trim();
|
| 336 |
-
const language = languageSelect.value;
|
| 337 |
-
|
| 338 |
-
if (!file && !text) {
|
| 339 |
-
showError(errorElement, "Please upload a file or enter text");
|
| 340 |
-
return;
|
| 341 |
-
}
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
| 347 |
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
formData.append('language', language);
|
| 352 |
-
|
| 353 |
-
const response = await fetch('/summarize', {
|
| 354 |
-
method: 'POST',
|
| 355 |
-
body: formData
|
| 356 |
-
});
|
| 357 |
-
|
| 358 |
-
if (!response.ok) {
|
| 359 |
-
const error = await response.json();
|
| 360 |
-
throw new Error(error.detail || "Failed to summarize content");
|
| 361 |
}
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
function handleDrop(e) {
|
| 374 |
const dt = e.dataTransfer;
|
| 375 |
-
|
|
|
|
|
|
|
| 376 |
}
|
| 377 |
|
| 378 |
function handleFileSelect(e) {
|
| 379 |
-
|
|
|
|
|
|
|
| 380 |
}
|
| 381 |
|
| 382 |
function handleFiles(files) {
|
| 383 |
-
if (!files.length) return;
|
| 384 |
|
| 385 |
const file = files[0];
|
| 386 |
const validTypes = ['pdf', 'docx', 'txt', 'jpeg', 'png'];
|
|
@@ -390,21 +406,25 @@ function initSummarization() {
|
|
| 390 |
return;
|
| 391 |
}
|
| 392 |
|
| 393 |
-
filePreview
|
| 394 |
-
|
| 395 |
-
<
|
| 396 |
-
|
| 397 |
-
<
|
| 398 |
-
|
|
|
|
|
|
|
| 399 |
</div>
|
| 400 |
-
|
| 401 |
-
|
| 402 |
validateInputs();
|
| 403 |
}
|
| 404 |
|
| 405 |
function validateInputs() {
|
|
|
|
|
|
|
| 406 |
const hasFile = fileInput.files.length > 0;
|
| 407 |
-
const hasText = textInput.value.trim() !== '';
|
| 408 |
submitBtn.disabled = !(hasFile || hasText);
|
| 409 |
}
|
| 410 |
}
|
|
@@ -427,97 +447,112 @@ function initQuestionAnswering() {
|
|
| 427 |
const languageSelect = section.querySelector('#qa-language');
|
| 428 |
|
| 429 |
// Store default button text
|
| 430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
// Set up drag and drop
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
['dragenter', 'dragover'].forEach(eventName => {
|
| 438 |
-
dropArea.addEventListener(eventName, () => dropArea.classList.add('highlight'));
|
| 439 |
-
});
|
| 440 |
-
|
| 441 |
-
['dragleave', 'drop'].forEach(eventName => {
|
| 442 |
-
dropArea.addEventListener(eventName, () => dropArea.classList.remove('highlight'));
|
| 443 |
-
});
|
| 444 |
-
|
| 445 |
-
dropArea.addEventListener('drop', handleDrop, false);
|
| 446 |
-
dropArea.addEventListener('click', () => fileInput.click());
|
| 447 |
-
fileInput.addEventListener('change', handleFileSelect);
|
| 448 |
-
questionInput.addEventListener('input', validateInputs);
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
const question = questionInput.value.trim();
|
| 454 |
-
const language = languageSelect.value;
|
| 455 |
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
}
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
resultContainer.classList.add('hidden');
|
| 470 |
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
|
|
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
}
|
| 480 |
|
| 481 |
-
if (!
|
| 482 |
-
|
| 483 |
-
|
| 484 |
}
|
| 485 |
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
|
| 510 |
function handleDrop(e) {
|
| 511 |
const dt = e.dataTransfer;
|
| 512 |
-
|
|
|
|
|
|
|
| 513 |
}
|
| 514 |
|
| 515 |
function handleFileSelect(e) {
|
| 516 |
-
|
|
|
|
|
|
|
| 517 |
}
|
| 518 |
|
| 519 |
function handleFiles(files) {
|
| 520 |
-
if (!files.length) return;
|
| 521 |
|
| 522 |
const file = files[0];
|
| 523 |
const validTypes = ['pdf', 'docx', 'txt', 'jpeg', 'png'];
|
|
@@ -527,21 +562,26 @@ function initQuestionAnswering() {
|
|
| 527 |
return;
|
| 528 |
}
|
| 529 |
|
| 530 |
-
filePreview
|
| 531 |
-
|
| 532 |
-
<
|
| 533 |
-
|
| 534 |
-
<
|
| 535 |
-
|
|
|
|
|
|
|
| 536 |
</div>
|
| 537 |
-
|
| 538 |
-
|
| 539 |
validateInputs();
|
| 540 |
}
|
| 541 |
|
| 542 |
function validateInputs() {
|
|
|
|
|
|
|
| 543 |
const hasFile = fileInput.files.length > 0;
|
| 544 |
const hasQuestion = questionInput.value.trim() !== '';
|
| 545 |
submitBtn.disabled = !(hasFile && hasQuestion);
|
| 546 |
}
|
| 547 |
}
|
|
|
|
|
|
| 214 |
|
| 215 |
// Function to show the selected section and hide the others
|
| 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 => {
|
|
|
|
| 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 |
// Set up drag and drop
|
| 321 |
+
if (dropArea && fileInput) {
|
| 322 |
+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
| 323 |
+
dropArea.addEventListener(eventName, preventDefaults, false);
|
| 324 |
+
});
|
| 325 |
|
| 326 |
+
['dragenter', 'dragover'].forEach(eventName => {
|
| 327 |
+
dropArea.addEventListener(eventName, () => dropArea.classList.add('highlight'));
|
| 328 |
+
});
|
| 329 |
|
| 330 |
+
['dragleave', 'drop'].forEach(eventName => {
|
| 331 |
+
dropArea.addEventListener(eventName, () => dropArea.classList.remove('highlight'));
|
| 332 |
+
});
|
| 333 |
|
| 334 |
+
dropArea.addEventListener('drop', handleDrop, false);
|
| 335 |
+
dropArea.addEventListener('click', () => fileInput.click());
|
| 336 |
+
fileInput.addEventListener('change', handleFileSelect);
|
| 337 |
+
}
|
| 338 |
|
| 339 |
+
if (textInput) {
|
| 340 |
+
textInput.addEventListener('input', validateInputs);
|
| 341 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
+
if (submitBtn) {
|
| 344 |
+
submitBtn.addEventListener('click', async () => {
|
| 345 |
+
const file = fileInput.files[0];
|
| 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 |
+
try {
|
| 355 |
+
setLoading(submitBtn, true);
|
| 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 |
+
if (dt && dt.files) {
|
| 388 |
+
handleFiles(dt.files);
|
| 389 |
+
}
|
| 390 |
}
|
| 391 |
|
| 392 |
function handleFileSelect(e) {
|
| 393 |
+
if (e.target.files) {
|
| 394 |
+
handleFiles(e.target.files);
|
| 395 |
+
}
|
| 396 |
}
|
| 397 |
|
| 398 |
function handleFiles(files) {
|
| 399 |
+
if (!files || files.length === 0) return;
|
| 400 |
|
| 401 |
const file = files[0];
|
| 402 |
const validTypes = ['pdf', 'docx', 'txt', 'jpeg', 'png'];
|
|
|
|
| 406 |
return;
|
| 407 |
}
|
| 408 |
|
| 409 |
+
if (filePreview) {
|
| 410 |
+
filePreview.innerHTML = `
|
| 411 |
+
<div class="file-info">
|
| 412 |
+
<span class="file-icon">${getFileIcon(file)}</span>
|
| 413 |
+
<div>
|
| 414 |
+
<strong>${file.name}</strong>
|
| 415 |
+
<span>${formatFileSize(file.size)}</span>
|
| 416 |
+
</div>
|
| 417 |
</div>
|
| 418 |
+
`;
|
| 419 |
+
}
|
| 420 |
validateInputs();
|
| 421 |
}
|
| 422 |
|
| 423 |
function validateInputs() {
|
| 424 |
+
if (!fileInput || !submitBtn) return;
|
| 425 |
+
|
| 426 |
const hasFile = fileInput.files.length > 0;
|
| 427 |
+
const hasText = textInput ? textInput.value.trim() !== '' : false;
|
| 428 |
submitBtn.disabled = !(hasFile || hasText);
|
| 429 |
}
|
| 430 |
}
|
|
|
|
| 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 |
// Set up drag and drop
|
| 458 |
+
if (dropArea && fileInput) {
|
| 459 |
+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
| 460 |
+
dropArea.addEventListener(eventName, preventDefaults, false);
|
| 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 |
+
if (questionInput) {
|
| 477 |
+
questionInput.addEventListener('input', validateInputs);
|
| 478 |
+
}
|
|
|
|
| 479 |
|
| 480 |
+
if (submitBtn) {
|
| 481 |
+
submitBtn.addEventListener('click', async () => {
|
| 482 |
+
const file = fileInput.files[0];
|
| 483 |
+
const question = questionInput.value.trim();
|
| 484 |
+
const language = languageSelect.value;
|
| 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 handleFiles(files) {
|
| 555 |
+
if (!files || files.length === 0) return;
|
| 556 |
|
| 557 |
const file = files[0];
|
| 558 |
const validTypes = ['pdf', 'docx', 'txt', 'jpeg', 'png'];
|
|
|
|
| 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 validateInputs() {
|
| 580 |
+
if (!fileInput || !questionInput || !submitBtn) return;
|
| 581 |
+
|
| 582 |
const hasFile = fileInput.files.length > 0;
|
| 583 |
const hasQuestion = questionInput.value.trim() !== '';
|
| 584 |
submitBtn.disabled = !(hasFile && hasQuestion);
|
| 585 |
}
|
| 586 |
}
|
| 587 |
+
|