Spaces:
Running
Running
better translation and dropdown settings
Browse files
app.py
CHANGED
@@ -151,6 +151,13 @@ def translate_text(input_text, source_lang="en", target_lang="en"):
|
|
151 |
print(f"Attempted to use language code: {target_lang}")
|
152 |
return input_text
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
def download_and_extract_model(url, destination):
|
155 |
"""Download and extract the model files."""
|
156 |
print(f"Downloading from URL: {url}")
|
@@ -422,9 +429,6 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
|
|
422 |
|
423 |
model_info = models[model_id]
|
424 |
|
425 |
-
# Check if this is an MMS model
|
426 |
-
is_mms = 'mms' in model_id.lower()
|
427 |
-
|
428 |
# Get the language code and check if translation is needed
|
429 |
lang_code = get_language_code(model_info)
|
430 |
translate_code = get_translate_code(lang_code)
|
@@ -434,24 +438,16 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
|
|
434 |
translated_text = None
|
435 |
was_translated = False
|
436 |
|
437 |
-
#
|
438 |
-
if
|
439 |
-
if not translate_code:
|
440 |
-
return None, f"Cannot determine translation target language from code: {lang_code}"
|
441 |
-
print(f"MMS model detected, translating to {translate_code}")
|
442 |
-
translated_text = translate_text(text, "en", translate_code)
|
443 |
-
if translated_text != text: # Only mark as translated if text actually changed
|
444 |
-
was_translated = True
|
445 |
-
text = translated_text
|
446 |
-
# For other models, check if translation is enabled and needed
|
447 |
-
elif translate_enabled and translate_code and translate_code != "en":
|
448 |
if not translate_code:
|
449 |
return None, f"Cannot determine translation target language from code: {lang_code}"
|
450 |
-
|
|
|
451 |
translated_text = translate_text(text, "en", translate_code)
|
452 |
if translated_text != text: # Only mark as translated if text actually changed
|
453 |
was_translated = True
|
454 |
-
|
455 |
|
456 |
try:
|
457 |
# Update status with language info
|
@@ -471,7 +467,6 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
|
|
471 |
final_status += f"\nText: '{text}'"
|
472 |
|
473 |
return (sample_rate, audio_data), final_status
|
474 |
-
|
475 |
except ValueError as e:
|
476 |
# Handle known errors with user-friendly messages
|
477 |
error_msg = str(e)
|
@@ -517,29 +512,26 @@ with gr.Blocks() as app:
|
|
517 |
interactive=False
|
518 |
)
|
519 |
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
return {"visible": False}
|
533 |
-
except Exception as e:
|
534 |
-
print(f"Error updating translate checkbox: {str(e)}")
|
535 |
-
return {"visible": False}
|
536 |
|
|
|
537 |
model_dropdown.change(
|
538 |
-
fn=
|
539 |
inputs=[model_dropdown],
|
540 |
outputs=[translate_checkbox]
|
541 |
)
|
542 |
-
|
543 |
# Set up event handlers
|
544 |
gen_event = generate_btn.click(
|
545 |
fn=tts_interface,
|
|
|
151 |
print(f"Attempted to use language code: {target_lang}")
|
152 |
return input_text
|
153 |
|
154 |
+
def detect_language(text):
|
155 |
+
"""Detect the language of the input text using Google Translator."""
|
156 |
+
try:
|
157 |
+
return GoogleTranslator().detect(text).lower()
|
158 |
+
except:
|
159 |
+
return "en" # Default to English if detection fails
|
160 |
+
|
161 |
def download_and_extract_model(url, destination):
|
162 |
"""Download and extract the model files."""
|
163 |
print(f"Downloading from URL: {url}")
|
|
|
429 |
|
430 |
model_info = models[model_id]
|
431 |
|
|
|
|
|
|
|
432 |
# Get the language code and check if translation is needed
|
433 |
lang_code = get_language_code(model_info)
|
434 |
translate_code = get_translate_code(lang_code)
|
|
|
438 |
translated_text = None
|
439 |
was_translated = False
|
440 |
|
441 |
+
# Only translate if translation is enabled and needed
|
442 |
+
if translate_enabled and translate_code and translate_code != "en":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
if not translate_code:
|
444 |
return None, f"Cannot determine translation target language from code: {lang_code}"
|
445 |
+
|
446 |
+
print(f"Translating to {translate_code}")
|
447 |
translated_text = translate_text(text, "en", translate_code)
|
448 |
if translated_text != text: # Only mark as translated if text actually changed
|
449 |
was_translated = True
|
450 |
+
text = translated_text
|
451 |
|
452 |
try:
|
453 |
# Update status with language info
|
|
|
467 |
final_status += f"\nText: '{text}'"
|
468 |
|
469 |
return (sample_rate, audio_data), final_status
|
|
|
470 |
except ValueError as e:
|
471 |
# Handle known errors with user-friendly messages
|
472 |
error_msg = str(e)
|
|
|
512 |
interactive=False
|
513 |
)
|
514 |
|
515 |
+
def on_model_change(model_name):
|
516 |
+
# Get model info
|
517 |
+
model_id = models_by_display.get(model_name)
|
518 |
+
if not model_id or model_id not in models:
|
519 |
+
return False
|
520 |
+
|
521 |
+
model_info = models[model_id]
|
522 |
+
lang_code = get_language_code(model_info)
|
523 |
+
|
524 |
+
# Auto-check translation for non-English models
|
525 |
+
should_translate = lang_code and lang_code.lower() != "en"
|
526 |
+
return should_translate
|
|
|
|
|
|
|
|
|
527 |
|
528 |
+
# Update translation checkbox when model changes
|
529 |
model_dropdown.change(
|
530 |
+
fn=on_model_change,
|
531 |
inputs=[model_dropdown],
|
532 |
outputs=[translate_checkbox]
|
533 |
)
|
534 |
+
|
535 |
# Set up event handlers
|
536 |
gen_event = generate_btn.click(
|
537 |
fn=tts_interface,
|