chenguittiMaroua commited on
Commit
691c9e7
·
verified ·
1 Parent(s): 6f72d9f

Update static/test.js

Browse files
Files changed (1) hide show
  1. static/test.js +229 -97
static/test.js CHANGED
@@ -214,14 +214,13 @@ function showSection(sectionId) {
214
 
215
  // Function to show the selected section and hide the others
216
  // Main navigation functionality
 
217
  function showSection(sectionId) {
218
- // Hide all tool sections
219
  const sections = document.querySelectorAll('.tool-section');
220
  sections.forEach(section => {
221
  section.style.display = 'none';
222
  });
223
 
224
- // Show the selected section
225
  const activeSection = document.getElementById(sectionId);
226
  if (activeSection) {
227
  activeSection.style.display = 'block';
@@ -229,6 +228,54 @@ function showSection(sectionId) {
229
  }
230
  }
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  // Initialize the page
233
  document.addEventListener('DOMContentLoaded', () => {
234
  // Set up card click handlers
@@ -242,57 +289,65 @@ document.addEventListener('DOMContentLoaded', () => {
242
  // Show first section by default
243
  showSection('document-image-analysis');
244
 
245
- // Document Analysis Section Setup
246
- const summarizeDropArea = document.getElementById('upload-area');
247
- const summarizeFileInput = document.getElementById('file-input');
248
- const summarizePreview = document.getElementById('file-preview');
249
- const textInput = document.getElementById('text-input');
250
- const summarizeBtn = document.getElementById('summarize-btn');
251
- const summarizeResult = document.getElementById('result-content');
252
- const summarizeError = document.getElementById('error-message');
253
- const languageSelect = document.getElementById('language-select');
254
-
255
- // Set up drag and drop for file upload
 
 
 
 
 
 
 
 
 
 
 
 
256
  ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
257
- summarizeDropArea.addEventListener(eventName, preventDefaults, false);
258
  });
259
 
260
  ['dragenter', 'dragover'].forEach(eventName => {
261
- summarizeDropArea.addEventListener(eventName, highlight, false);
262
  });
263
 
264
  ['dragleave', 'drop'].forEach(eventName => {
265
- summarizeDropArea.addEventListener(eventName, unhighlight, false);
266
  });
267
 
268
- summarizeDropArea.addEventListener('drop', handleDrop, false);
269
- summarizeDropArea.addEventListener('click', () => summarizeFileInput.click());
270
- summarizeFileInput.addEventListener('change', handleFileSelect);
271
  textInput.addEventListener('input', validateInputs);
272
 
273
- // Summarize button click handler
274
- summarizeBtn.addEventListener('click', async () => {
275
- const file = summarizeFileInput.files[0];
276
  const text = textInput.value.trim();
277
  const language = languageSelect.value;
278
 
279
  if (!file && !text) {
280
- showError(summarizeError, "Please upload a file or enter text");
281
  return;
282
  }
283
 
284
  try {
285
- // Show loading state
286
- setLoading(true);
287
- hideError(summarizeError);
288
- summarizeResult.textContent = '';
289
 
290
  const formData = new FormData();
291
- if (file) {
292
- formData.append('file', file);
293
- } else {
294
- formData.append('text', text);
295
- }
296
  formData.append('language', language);
297
 
298
  const response = await fetch('/summarize', {
@@ -306,50 +361,36 @@ document.addEventListener('DOMContentLoaded', () => {
306
  }
307
 
308
  const result = await response.json();
309
- summarizeResult.textContent = result.summary;
310
  } catch (error) {
311
- showError(summarizeError, error.message);
312
  console.error("Summarization error:", error);
313
  } finally {
314
- setLoading(false);
315
  }
316
  });
317
 
318
- // Helper functions
319
- function preventDefaults(e) {
320
- e.preventDefault();
321
- e.stopPropagation();
322
- }
323
-
324
- function highlight() {
325
- summarizeDropArea.classList.add('highlight');
326
- }
327
-
328
- function unhighlight() {
329
- summarizeDropArea.classList.remove('highlight');
330
- }
331
-
332
  function handleDrop(e) {
333
  const dt = e.dataTransfer;
334
- const files = dt.files;
335
- handleFiles(files);
336
  }
337
 
338
  function handleFileSelect(e) {
339
- const files = e.target.files;
340
- if (files.length) {
341
- handleFiles(files);
342
- }
343
  }
344
 
345
  function handleFiles(files) {
 
 
346
  const file = files[0];
347
- if (!isValidFileType(file)) {
348
- showError(summarizeError, "Unsupported file type. Please upload PDF, DOCX, TXT, or image files.");
 
 
349
  return;
350
  }
351
 
352
- summarizePreview.innerHTML = `
353
  <div class="file-info">
354
  <span class="file-icon">${getFileIcon(file)}</span>
355
  <div>
@@ -362,54 +403,145 @@ document.addEventListener('DOMContentLoaded', () => {
362
  }
363
 
364
  function validateInputs() {
365
- const hasFile = summarizeFileInput.files.length > 0;
366
  const hasText = textInput.value.trim() !== '';
367
- summarizeBtn.disabled = !(hasFile || hasText);
368
  }
 
369
 
370
- function isValidFileType(file) {
371
- const validTypes = [
372
- 'application/pdf',
373
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
374
- 'text/plain',
375
- 'image/jpeg',
376
- 'image/png'
377
- ];
378
- return validTypes.includes(file.type) ||
379
- file.name.endsWith('.pdf') ||
380
- file.name.endsWith('.docx') ||
381
- file.name.endsWith('.txt');
382
- }
 
 
 
 
 
 
 
 
 
 
 
383
 
384
- function getFileIcon(file) {
385
- if (file.type.startsWith('image/')) return '🖼️';
386
- if (file.type === 'application/pdf') return '📄';
387
- if (file.type.includes('wordprocessingml')) return '📝';
388
- return '📁';
389
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
 
391
- function formatFileSize(bytes) {
392
- if (bytes === 0) return '0 Bytes';
393
- const k = 1024;
394
- const sizes = ['Bytes', 'KB', 'MB', 'GB'];
395
- const i = Math.floor(Math.log(bytes) / Math.log(k));
396
- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
  }
398
 
399
- function setLoading(isLoading) {
400
- summarizeBtn.disabled = isLoading;
401
- const btnText = summarizeBtn.querySelector('span');
402
- btnText.textContent = isLoading ? 'Processing...' : 'Summarize Document';
403
- const spinner = summarizeBtn.querySelector('.spinner');
404
- spinner.classList.toggle('hidden', !isLoading);
405
  }
406
 
407
- function showError(element, message) {
408
- element.textContent = message;
409
- element.classList.remove('hidden');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
  }
411
 
412
- function hideError() {
413
- summarizeError.classList.add('hidden');
 
 
414
  }
415
- });
 
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 => {
221
  section.style.display = 'none';
222
  });
223
 
 
224
  const activeSection = document.getElementById(sectionId);
225
  if (activeSection) {
226
  activeSection.style.display = 'block';
 
228
  }
229
  }
230
 
231
+ // Shared utility functions
232
+ function preventDefaults(e) {
233
+ e.preventDefault();
234
+ e.stopPropagation();
235
+ }
236
+
237
+ function isValidFileType(file, types) {
238
+ const validExtensions = types.map(t => `.${t}`);
239
+ return types.includes(file.type.split('/').pop()) ||
240
+ validExtensions.some(ext => file.name.toLowerCase().endsWith(ext));
241
+ }
242
+
243
+ function getFileIcon(file) {
244
+ if (file.type.startsWith('image/')) return '🖼️';
245
+ if (file.type === 'application/pdf') return '📄';
246
+ if (file.type.includes('wordprocessingml')) return '📝';
247
+ return '📁';
248
+ }
249
+
250
+ function formatFileSize(bytes) {
251
+ if (bytes === 0) return '0 Bytes';
252
+ const k = 1024;
253
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
254
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
255
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
256
+ }
257
+
258
+ function setLoading(button, isLoading) {
259
+ button.disabled = isLoading;
260
+ const btnText = button.querySelector('span:not(.spinner)');
261
+ if (btnText) {
262
+ btnText.textContent = isLoading ? 'Processing...' : button.dataset.defaultText;
263
+ }
264
+ const spinner = button.querySelector('.spinner');
265
+ if (spinner) {
266
+ spinner.classList.toggle('hidden', !isLoading);
267
+ }
268
+ }
269
+
270
+ function showError(element, message) {
271
+ element.textContent = message;
272
+ element.classList.remove('hidden');
273
+ }
274
+
275
+ function hideError(element) {
276
+ element.classList.add('hidden');
277
+ }
278
+
279
  // Initialize the page
280
  document.addEventListener('DOMContentLoaded', () => {
281
  // Set up card click handlers
 
289
  // Show first section by default
290
  showSection('document-image-analysis');
291
 
292
+ // Initialize all tool sections
293
+ initSummarization();
294
+ initQuestionAnswering();
295
+ });
296
+
297
+ // Document Summarization Section
298
+ function initSummarization() {
299
+ const section = document.getElementById('document-image-analysis');
300
+ if (!section) return;
301
+
302
+ const dropArea = section.querySelector('#upload-area');
303
+ const fileInput = section.querySelector('#file-input');
304
+ const filePreview = section.querySelector('#file-preview');
305
+ const textInput = section.querySelector('#text-input');
306
+ const submitBtn = section.querySelector('#summarize-btn');
307
+ const resultArea = section.querySelector('#result-content');
308
+ const errorElement = section.querySelector('#error-message');
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', {
 
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'];
387
+
388
+ if (!isValidFileType(file, validTypes)) {
389
+ showError(errorElement, "Unsupported file type. Please upload PDF, DOCX, TXT, or image files.");
390
  return;
391
  }
392
 
393
+ filePreview.innerHTML = `
394
  <div class="file-info">
395
  <span class="file-icon">${getFileIcon(file)}</span>
396
  <div>
 
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
+ }
411
 
412
+ // Question Answering Section
413
+ function initQuestionAnswering() {
414
+ const section = document.getElementById('question-answering');
415
+ if (!section) return;
416
+
417
+ const dropArea = section.querySelector('#qa-upload-area');
418
+ const fileInput = section.querySelector('#qa-file-input');
419
+ const filePreview = section.querySelector('#qa-file-preview');
420
+ const questionInput = section.querySelector('#qa-question');
421
+ const submitBtn = section.querySelector('#qa-submit-btn');
422
+ const answerArea = section.querySelector('#qa-answer');
423
+ const confidenceBar = section.querySelector('#qa-confidence-bar');
424
+ const confidenceValue = section.querySelector('#qa-confidence-value');
425
+ const resultContainer = section.querySelector('#qa-result-container');
426
+ const errorElement = section.querySelector('#qa-error-message');
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'];
524
+
525
+ if (!isValidFileType(file, validTypes)) {
526
+ showError(errorElement, "Unsupported file type. Please upload PDF, DOCX, or TXT files.");
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
+ }