chenguittiMaroua commited on
Commit
b575674
·
verified ·
1 Parent(s): c76a8dc

Update static/test.js

Browse files
Files changed (1) hide show
  1. 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
- f// Main navigation and shared utility functions
 
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
- submitBtn.dataset.defaultText = submitBtn.querySelector('span').textContent;
 
 
 
 
 
313
 
314
  // Set up drag and drop
315
- ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
316
- dropArea.addEventListener(eventName, preventDefaults, false);
317
- });
 
318
 
319
- ['dragenter', 'dragover'].forEach(eventName => {
320
- dropArea.addEventListener(eventName, () => dropArea.classList.add('highlight'));
321
- });
322
 
323
- ['dragleave', 'drop'].forEach(eventName => {
324
- dropArea.addEventListener(eventName, () => dropArea.classList.remove('highlight'));
325
- });
326
 
327
- dropArea.addEventListener('drop', handleDrop, false);
328
- dropArea.addEventListener('click', () => fileInput.click());
329
- fileInput.addEventListener('change', handleFileSelect);
330
- textInput.addEventListener('input', validateInputs);
331
 
332
- // Submit handler
333
- submitBtn.addEventListener('click', async () => {
334
- const file = fileInput.files[0];
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
- try {
344
- setLoading(submitBtn, true);
345
- hideError(errorElement);
346
- resultArea.textContent = '';
 
347
 
348
- const formData = new FormData();
349
- if (file) formData.append('file', file);
350
- if (text) formData.append('text', text);
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
- const result = await response.json();
364
- resultArea.textContent = result.summary;
365
- } catch (error) {
366
- showError(errorElement, error.message);
367
- console.error("Summarization error:", error);
368
- } finally {
369
- setLoading(submitBtn, false);
370
- }
371
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
 
373
  function handleDrop(e) {
374
  const dt = e.dataTransfer;
375
- handleFiles(dt.files);
 
 
376
  }
377
 
378
  function handleFileSelect(e) {
379
- handleFiles(e.target.files);
 
 
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.innerHTML = `
394
- <div class="file-info">
395
- <span class="file-icon">${getFileIcon(file)}</span>
396
- <div>
397
- <strong>${file.name}</strong>
398
- <span>${formatFileSize(file.size)}</span>
 
 
399
  </div>
400
- </div>
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
- submitBtn.dataset.defaultText = submitBtn.querySelector('span').textContent;
 
 
 
 
 
431
 
432
  // Set up drag and drop
433
- ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
434
- dropArea.addEventListener(eventName, preventDefaults, false);
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
- // Submit handler
451
- submitBtn.addEventListener('click', async () => {
452
- const file = fileInput.files[0];
453
- const question = questionInput.value.trim();
454
- const language = languageSelect.value;
455
 
456
- if (!file) {
457
- showError(errorElement, "Please upload a document first");
458
- return;
459
- }
460
 
461
- if (!question) {
462
- showError(errorElement, "Please enter your question");
463
- return;
464
- }
465
 
466
- try {
467
- setLoading(submitBtn, true);
468
- hideError(errorElement);
469
- resultContainer.classList.add('hidden');
470
 
471
- const formData = new FormData();
472
- formData.append('file', file);
473
- formData.append('question', question);
474
- formData.append('language', language);
 
475
 
476
- const response = await fetch('/qa', {
477
- method: 'POST',
478
- body: formData
479
- });
480
 
481
- if (!response.ok) {
482
- const error = await response.json();
483
- throw new Error(error.detail || "Failed to get answer");
484
  }
485
 
486
- const result = await response.json();
487
-
488
- // Display results
489
- answerArea.textContent = result.answer;
490
-
491
- // Update confidence display
492
- const confidencePercent = Math.round((result.confidence || 0) * 100);
493
- confidenceBar.style.width = `${confidencePercent}%`;
494
- confidenceValue.textContent = `${confidencePercent}%`;
495
-
496
- // Color code confidence
497
- confidenceBar.style.backgroundColor =
498
- confidencePercent < 30 ? '#f44336' :
499
- confidencePercent < 70 ? '#ff9800' : '#4CAF50';
500
-
501
- resultContainer.classList.remove('hidden');
502
- } catch (error) {
503
- showError(errorElement, error.message);
504
- console.error("QA error:", error);
505
- } finally {
506
- setLoading(submitBtn, false);
507
- }
508
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509
 
510
  function handleDrop(e) {
511
  const dt = e.dataTransfer;
512
- handleFiles(dt.files);
 
 
513
  }
514
 
515
  function handleFileSelect(e) {
516
- handleFiles(e.target.files);
 
 
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.innerHTML = `
531
- <div class="file-info">
532
- <span class="file-icon">${getFileIcon(file)}</span>
533
- <div>
534
- <strong>${file.name}</strong>
535
- <span>${formatFileSize(file.size)}</span>
 
 
536
  </div>
537
- </div>
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
+