amine_dubs
commited on
Commit
·
050f2a9
1
Parent(s):
f742657
- backend/main.py +5 -4
- static/script.js +6 -2
backend/main.py
CHANGED
@@ -367,13 +367,14 @@ async def read_root(request: Request):
|
|
367 |
async def translate_text_endpoint(request: TranslationRequest):
|
368 |
global translator, model, tokenizer
|
369 |
|
|
|
370 |
try:
|
371 |
# Explicitly extract fields from request to ensure they exist
|
372 |
source_lang = request.source_lang
|
373 |
target_lang = request.target_lang
|
374 |
text = request.text
|
375 |
|
376 |
-
print(f"
|
377 |
|
378 |
translation_result = ""
|
379 |
error_message = None
|
@@ -381,7 +382,7 @@ async def translate_text_endpoint(request: TranslationRequest):
|
|
381 |
try:
|
382 |
# Check if translator is initialized, if not, initialize it
|
383 |
if translator is None:
|
384 |
-
print("Translator not initialized. Attempting to initialize model...")
|
385 |
success = initialize_model()
|
386 |
if not success:
|
387 |
raise Exception("Failed to initialize translation model")
|
@@ -402,11 +403,11 @@ async def translate_text_endpoint(request: TranslationRequest):
|
|
402 |
|
403 |
# Check that translator is callable before proceeding
|
404 |
if not callable(translator):
|
405 |
-
print("Translator is not callable, attempting to reinitialize")
|
406 |
success = initialize_model()
|
407 |
if not success or not callable(translator):
|
408 |
raise Exception("Translator is not callable after reinitialization")
|
409 |
-
|
410 |
# Use a thread pool to execute the translation with a timeout
|
411 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
412 |
future = executor.submit(
|
|
|
367 |
async def translate_text_endpoint(request: TranslationRequest):
|
368 |
global translator, model, tokenizer
|
369 |
|
370 |
+
print("[DEBUG] /translate/text endpoint called")
|
371 |
try:
|
372 |
# Explicitly extract fields from request to ensure they exist
|
373 |
source_lang = request.source_lang
|
374 |
target_lang = request.target_lang
|
375 |
text = request.text
|
376 |
|
377 |
+
print(f"[DEBUG] Received request: source_lang={source_lang}, target_lang={target_lang}, text={text[:50]}")
|
378 |
|
379 |
translation_result = ""
|
380 |
error_message = None
|
|
|
382 |
try:
|
383 |
# Check if translator is initialized, if not, initialize it
|
384 |
if translator is None:
|
385 |
+
print("[DEBUG] Translator not initialized. Attempting to initialize model...")
|
386 |
success = initialize_model()
|
387 |
if not success:
|
388 |
raise Exception("Failed to initialize translation model")
|
|
|
403 |
|
404 |
# Check that translator is callable before proceeding
|
405 |
if not callable(translator):
|
406 |
+
print("[DEBUG] Translator is not callable, attempting to reinitialize")
|
407 |
success = initialize_model()
|
408 |
if not success or not callable(translator):
|
409 |
raise Exception("Translator is not callable after reinitialization")
|
410 |
+
print("[DEBUG] Calling translator model...")
|
411 |
# Use a thread pool to execute the translation with a timeout
|
412 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
413 |
future = executor.submit(
|
static/script.js
CHANGED
@@ -75,6 +75,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
75 |
// Show loading state
|
76 |
document.getElementById('text-loading').style.display = 'block';
|
77 |
|
|
|
|
|
|
|
78 |
const response = await fetch('/translate/text', {
|
79 |
method: 'POST',
|
80 |
headers: {
|
@@ -90,10 +93,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
90 |
// Hide loading state
|
91 |
document.getElementById('text-loading').style.display = 'none';
|
92 |
|
|
|
|
|
|
|
93 |
const data = await response.json();
|
94 |
|
95 |
if (!response.ok) {
|
96 |
-
// Properly extract error message from the response
|
97 |
if (data && data.error) {
|
98 |
displayError(data.error);
|
99 |
} else {
|
@@ -107,7 +112,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
107 |
return;
|
108 |
}
|
109 |
|
110 |
-
// Display the successful translation
|
111 |
textOutput.textContent = data.translated_text;
|
112 |
textResultBox.style.display = 'block';
|
113 |
|
|
|
75 |
// Show loading state
|
76 |
document.getElementById('text-loading').style.display = 'block';
|
77 |
|
78 |
+
// Log payload for debugging
|
79 |
+
console.log('Sending payload:', { text: sourceText, source_lang: sourceLang, target_lang: targetLang });
|
80 |
+
|
81 |
const response = await fetch('/translate/text', {
|
82 |
method: 'POST',
|
83 |
headers: {
|
|
|
93 |
// Hide loading state
|
94 |
document.getElementById('text-loading').style.display = 'none';
|
95 |
|
96 |
+
// Log response status
|
97 |
+
console.log('Response status:', response.status);
|
98 |
+
|
99 |
const data = await response.json();
|
100 |
|
101 |
if (!response.ok) {
|
|
|
102 |
if (data && data.error) {
|
103 |
displayError(data.error);
|
104 |
} else {
|
|
|
112 |
return;
|
113 |
}
|
114 |
|
|
|
115 |
textOutput.textContent = data.translated_text;
|
116 |
textResultBox.style.display = 'block';
|
117 |
|