amine_dubs commited on
Commit
3bb55a6
·
1 Parent(s): 2ec6790

Fix document translation functionality

Browse files
Files changed (2) hide show
  1. static/script.js +13 -2
  2. templates/index.html +9 -1
static/script.js CHANGED
@@ -8,6 +8,7 @@ document.addEventListener('DOMContentLoaded', () => {
8
  const docFilename = document.getElementById('doc-filename');
9
  const docSourceLang = document.getElementById('doc-source-lang');
10
  const errorMessageDiv = document.getElementById('error-message');
 
11
 
12
  // Helper function to display errors
13
  function displayError(message) {
@@ -28,6 +29,7 @@ document.addEventListener('DOMContentLoaded', () => {
28
  docOutput.textContent = '';
29
  docFilename.textContent = '';
30
  docSourceLang.textContent = '';
 
31
  }
32
 
33
  // Handle Text Translation Form Submission
@@ -82,6 +84,8 @@ document.addEventListener('DOMContentLoaded', () => {
82
 
83
  button.disabled = true;
84
  button.textContent = 'Translating...';
 
 
85
 
86
  try {
87
  const response = await fetch('/translate/document', {
@@ -95,12 +99,17 @@ document.addEventListener('DOMContentLoaded', () => {
95
  }
96
 
97
  const result = await response.json();
 
 
 
 
 
 
 
98
  docFilename.textContent = result.original_filename || 'N/A';
99
  docSourceLang.textContent = result.detected_source_lang || 'N/A';
100
  docOutput.textContent = result.translated_text;
101
  docResultBox.style.display = 'block';
102
- // Optionally update language direction based on result if needed
103
- // docResultBox.dir = result.target_lang === 'ar' ? 'rtl' : 'ltr';
104
 
105
  } catch (error) {
106
  console.error('Document translation error:', error);
@@ -108,6 +117,8 @@ document.addEventListener('DOMContentLoaded', () => {
108
  } finally {
109
  button.disabled = false;
110
  button.textContent = 'Translate Document';
 
 
111
  }
112
  });
113
  });
 
8
  const docFilename = document.getElementById('doc-filename');
9
  const docSourceLang = document.getElementById('doc-source-lang');
10
  const errorMessageDiv = document.getElementById('error-message');
11
+ const docLoadingIndicator = document.getElementById('doc-loading');
12
 
13
  // Helper function to display errors
14
  function displayError(message) {
 
29
  docOutput.textContent = '';
30
  docFilename.textContent = '';
31
  docSourceLang.textContent = '';
32
+ docLoadingIndicator.style.display = 'none';
33
  }
34
 
35
  // Handle Text Translation Form Submission
 
84
 
85
  button.disabled = true;
86
  button.textContent = 'Translating...';
87
+ // Show loading indicator
88
+ docLoadingIndicator.style.display = 'block';
89
 
90
  try {
91
  const response = await fetch('/translate/document', {
 
99
  }
100
 
101
  const result = await response.json();
102
+ console.log('Document translation response:', result); // Added debug logging
103
+
104
+ // Check if result contains the expected fields
105
+ if (!result.translated_text) {
106
+ throw new Error('Translation response is missing translated text');
107
+ }
108
+
109
  docFilename.textContent = result.original_filename || 'N/A';
110
  docSourceLang.textContent = result.detected_source_lang || 'N/A';
111
  docOutput.textContent = result.translated_text;
112
  docResultBox.style.display = 'block';
 
 
113
 
114
  } catch (error) {
115
  console.error('Document translation error:', error);
 
117
  } finally {
118
  button.disabled = false;
119
  button.textContent = 'Translate Document';
120
+ // Hide loading indicator
121
+ docLoadingIndicator.style.display = 'none';
122
  }
123
  });
124
  });
templates/index.html CHANGED
@@ -68,7 +68,15 @@
68
  <label for="doc-input">Upload Document (.pdf, .docx, .xlsx, .pptx, .txt):</label>
69
  <input type="file" id="doc-input" name="file" accept=".pdf,.docx,.xlsx,.pptx,.txt">
70
  </div>
71
- <button type="submit">Translate Document</button>
 
 
 
 
 
 
 
 
72
  </form>
73
  <div id="doc-result" class="result-box" dir="rtl"> <!- Set default to RTL for Arabic output ->
74
  <h3>Translation:</h3>
 
68
  <label for="doc-input">Upload Document (.pdf, .docx, .xlsx, .pptx, .txt):</label>
69
  <input type="file" id="doc-input" name="file" accept=".pdf,.docx,.xlsx,.pptx,.txt">
70
  </div>
71
+ <div class="form-group">
72
+ <button type="submit" class="btn btn-primary">Translate Document</button>
73
+ </div>
74
+ <div id="doc-loading" class="loading-spinner" style="display: none;">
75
+ <div class="spinner-border text-primary" role="status">
76
+ <span class="sr-only">Loading...</span>
77
+ </div>
78
+ <p>Translating document... This may take a few minutes for large files or first-time use.</p>
79
+ </div>
80
  </form>
81
  <div id="doc-result" class="result-box" dir="rtl"> <!- Set default to RTL for Arabic output ->
82
  <h3>Translation:</h3>