willwade commited on
Commit
fe88c98
·
verified ·
1 Parent(s): 7dbd6ec

fix translated str output

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -122,27 +122,34 @@ def get_translate_code(iso_code):
122
 
123
  def translate_text(input_text, source_lang="en", target_lang="en"):
124
  """Translate text using Google Translator."""
125
- if source_lang == target_lang:
126
- return input_text
127
  try:
128
- # Convert ISO code to Google Translate code
129
- target_lang = get_translate_code(target_lang)
130
-
 
 
 
131
  try:
132
  translated = GoogleTranslator(source=source_lang, target=target_lang).translate(input_text)
133
- return f"{translated} (translated from: {input_text})"
134
- except Exception as first_error:
135
- # If the first attempt fails with the mapped code, try with the original
136
- try:
137
- translated = GoogleTranslator(source=source_lang, target=target_lang).translate(input_text)
138
- return f"{translated} (translated from: {input_text})"
139
- except:
 
 
 
 
 
140
  raise first_error
 
141
 
142
  except Exception as e:
143
  print(f"Translation error: {str(e)} for target language: {target_lang}")
144
  print(f"Attempted to use language code: {target_lang}")
145
- return f"Translation Error: Could not translate to {target_lang}. Original text: {input_text}"
146
 
147
  def download_and_extract_model(url, destination):
148
  """Download and extract the model files."""
@@ -425,6 +432,7 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
425
  # Store original text for status message
426
  original_text = text
427
  translated_text = None
 
428
 
429
  # For MMS models, we always need to translate
430
  if is_mms:
@@ -432,6 +440,8 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
432
  return None, f"Cannot determine translation target language from code: {lang_code}"
433
  print(f"MMS model detected, translating to {translate_code}")
434
  translated_text = translate_text(text, "en", translate_code)
 
 
435
  text = translated_text
436
  # For other models, check if translation is enabled and needed
437
  elif translate_enabled and translate_code and translate_code != "en":
@@ -439,6 +449,8 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
439
  return None, f"Cannot determine translation target language from code: {lang_code}"
440
  print(f"Will translate to {translate_code} (from ISO code {lang_code})")
441
  translated_text = translate_text(text, "en", translate_code)
 
 
442
  text = translated_text
443
 
444
  try:
@@ -451,10 +463,12 @@ def tts_interface(selected_model, text, translate_enabled, status_output):
451
  # Generate audio
452
  audio_data, sample_rate = generate_audio(text, model_info)
453
 
454
- # Include translation info in final status if text was translated
455
  final_status = f"Generated speech using {voice_name} ({lang_name})"
456
- if translated_text:
457
  final_status += f"\nTranslated: '{original_text}' → '{translated_text}'"
 
 
458
 
459
  return (sample_rate, audio_data), final_status
460
 
 
122
 
123
  def translate_text(input_text, source_lang="en", target_lang="en"):
124
  """Translate text using Google Translator."""
 
 
125
  try:
126
+ # If source and target are the same, or if target is English, return as is
127
+ if source_lang == target_lang or target_lang == "en":
128
+ return input_text
129
+
130
+ first_error = None
131
+ # Try with original language code
132
  try:
133
  translated = GoogleTranslator(source=source_lang, target=target_lang).translate(input_text)
134
+ return translated
135
+ except Exception as e:
136
+ first_error = e
137
+ print(f"First translation attempt failed: {str(e)}")
138
+
139
+ # Try with 'auto' as source language
140
+ try:
141
+ translated = GoogleTranslator(source='auto', target=target_lang).translate(input_text)
142
+ return translated
143
+ except Exception as e:
144
+ print(f"Second translation attempt failed: {str(e)}")
145
+ if first_error:
146
  raise first_error
147
+ raise e
148
 
149
  except Exception as e:
150
  print(f"Translation error: {str(e)} for target language: {target_lang}")
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."""
 
432
  # Store original text for status message
433
  original_text = text
434
  translated_text = None
435
+ was_translated = False
436
 
437
  # For MMS models, we always need to translate
438
  if is_mms:
 
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":
 
449
  return None, f"Cannot determine translation target language from code: {lang_code}"
450
  print(f"Will translate to {translate_code} (from ISO code {lang_code})")
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
  text = translated_text
455
 
456
  try:
 
463
  # Generate audio
464
  audio_data, sample_rate = generate_audio(text, model_info)
465
 
466
+ # Include translation info in final status if text was actually translated
467
  final_status = f"Generated speech using {voice_name} ({lang_name})"
468
+ if was_translated:
469
  final_status += f"\nTranslated: '{original_text}' → '{translated_text}'"
470
+ else:
471
+ final_status += f"\nText: '{text}'"
472
 
473
  return (sample_rate, audio_data), final_status
474