amine_dubs
commited on
Commit
·
b17d8bf
1
Parent(s):
3bb55a6
Fix document translation functionality
Browse files- backend/main.py +43 -8
- static/script.js +22 -5
backend/main.py
CHANGED
@@ -142,6 +142,7 @@ def translate_text_internal(text: str, source_lang: str, target_lang: str = "ar"
|
|
142 |
if translator is None:
|
143 |
success = initialize_model()
|
144 |
if not success:
|
|
|
145 |
return fallback_translate(text, source_lang, target_lang)
|
146 |
|
147 |
try:
|
@@ -154,15 +155,49 @@ Ensure the translation reads naturally to a native Arabic speaker.
|
|
154 |
Text to translate:
|
155 |
{text}"""
|
156 |
|
157 |
-
#
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
return fallback_translate(text, source_lang, target_lang)
|
167 |
|
168 |
except Exception as e:
|
|
|
142 |
if translator is None:
|
143 |
success = initialize_model()
|
144 |
if not success:
|
145 |
+
print("Model initialization failed, falling back to online translation")
|
146 |
return fallback_translate(text, source_lang, target_lang)
|
147 |
|
148 |
try:
|
|
|
155 |
Text to translate:
|
156 |
{text}"""
|
157 |
|
158 |
+
# Add timeout handling to prevent hanging
|
159 |
+
import threading
|
160 |
+
import queue
|
161 |
+
|
162 |
+
def model_inference():
|
163 |
+
try:
|
164 |
+
outputs = translator(prompt, max_length=512, do_sample=False)
|
165 |
+
result_queue.put(outputs)
|
166 |
+
except Exception as e:
|
167 |
+
result_queue.put(e)
|
168 |
+
|
169 |
+
# Create a queue to get the result or exception
|
170 |
+
result_queue = queue.Queue()
|
171 |
|
172 |
+
# Start the translation in a separate thread
|
173 |
+
thread = threading.Thread(target=model_inference)
|
174 |
+
thread.daemon = True
|
175 |
+
thread.start()
|
176 |
+
|
177 |
+
# Wait for the result with a timeout (10 seconds)
|
178 |
+
thread.join(timeout=10)
|
179 |
+
|
180 |
+
# Check if the thread completed within the timeout
|
181 |
+
if thread.is_alive():
|
182 |
+
print("Model inference timed out after 10 seconds, falling back to online translation")
|
183 |
+
return fallback_translate(text, source_lang, target_lang)
|
184 |
+
|
185 |
+
# Get the result from the queue
|
186 |
+
try:
|
187 |
+
result = result_queue.get(block=False)
|
188 |
+
if isinstance(result, Exception):
|
189 |
+
raise result
|
190 |
+
|
191 |
+
# Process the translation result
|
192 |
+
if result and len(result) > 0:
|
193 |
+
translated_text = result[0]['generated_text']
|
194 |
+
print(f"Translation successful using transformers model")
|
195 |
+
return culturally_adapt_arabic(translated_text)
|
196 |
+
else:
|
197 |
+
print("Model returned empty output")
|
198 |
+
return fallback_translate(text, source_lang, target_lang)
|
199 |
+
except queue.Empty:
|
200 |
+
print("No result in queue despite thread completing")
|
201 |
return fallback_translate(text, source_lang, target_lang)
|
202 |
|
203 |
except Exception as e:
|
static/script.js
CHANGED
@@ -9,7 +9,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
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) {
|
15 |
errorMessageDiv.textContent = `Error: ${message}`;
|
@@ -43,21 +43,38 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
43 |
button.textContent = 'Translating...';
|
44 |
|
45 |
try {
|
|
|
46 |
const response = await fetch('/translate/text', {
|
47 |
method: 'POST',
|
48 |
body: formData
|
49 |
});
|
50 |
|
|
|
|
|
51 |
if (!response.ok) {
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
|
|
|
56 |
const result = await response.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
textOutput.textContent = result.translated_text;
|
58 |
textResultBox.style.display = 'block';
|
59 |
-
//
|
60 |
-
|
61 |
|
62 |
} catch (error) {
|
63 |
console.error('Text translation error:', error);
|
|
|
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) {
|
15 |
errorMessageDiv.textContent = `Error: ${message}`;
|
|
|
43 |
button.textContent = 'Translating...';
|
44 |
|
45 |
try {
|
46 |
+
console.log('Sending translation request...');
|
47 |
const response = await fetch('/translate/text', {
|
48 |
method: 'POST',
|
49 |
body: formData
|
50 |
});
|
51 |
|
52 |
+
console.log('Response received:', response.status, response.statusText);
|
53 |
+
|
54 |
if (!response.ok) {
|
55 |
+
let errorMessage;
|
56 |
+
try {
|
57 |
+
const errorData = await response.json();
|
58 |
+
errorMessage = errorData.detail || `HTTP error! status: ${response.status}`;
|
59 |
+
} catch (jsonError) {
|
60 |
+
errorMessage = `HTTP error! status: ${response.status}. Failed to parse error response.`;
|
61 |
+
}
|
62 |
+
throw new Error(errorMessage);
|
63 |
}
|
64 |
|
65 |
+
console.log('Processing response...');
|
66 |
const result = await response.json();
|
67 |
+
console.log('Parsed JSON result:', result);
|
68 |
+
|
69 |
+
if (!result.translated_text) {
|
70 |
+
console.error('Missing translated_text in response:', result);
|
71 |
+
throw new Error('Server response missing translated text');
|
72 |
+
}
|
73 |
+
|
74 |
textOutput.textContent = result.translated_text;
|
75 |
textResultBox.style.display = 'block';
|
76 |
+
// Set text direction for Arabic
|
77 |
+
textOutput.dir = 'rtl';
|
78 |
|
79 |
} catch (error) {
|
80 |
console.error('Text translation error:', error);
|