28-nini commited on
Commit
d896082
·
verified ·
1 Parent(s): ed1dd14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -634,30 +634,38 @@ def apply_glossary_tooltips(text):
634
  html_output = f'<div style="padding: 10px; line-height: 1.5; font-size: 14px;">{text}</div>'
635
 
636
  return html_output
637
- def synthesize_audio(text, language):
638
- if not TTS_SUPPORT or not text:
639
  return None
640
-
641
  try:
642
- # Map language name to code (e.g., "English" → "en")
643
- lang_code = language.lower()[:2] # Simple fallback
644
- lang_map = {"english": "en", "spanish": "es", "french": "fr"} # Expand as needed
645
- lang_code = lang_map.get(language.lower(), lang_code)
646
 
647
- # Shorten text for gTTS limit
648
- audio_text = f"Security alert: {text[:100]}..." if len(text) > 100 else text
 
 
 
 
 
 
 
 
 
 
 
649
 
650
- tts = gTTS(text=audio_text, lang=lang_code, slow=False)
651
-
652
- with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
653
- tts.save(tmp_file.name)
654
- return tmp_file.name # Return full path
655
-
656
  except Exception as e:
657
- print(f"⚠️ Audio synthesis failed: {e}")
658
  return None
659
 
660
-
661
  def new_chat():
662
  """Reset all fields for a new chat session"""
663
  return (
 
634
  html_output = f'<div style="padding: 10px; line-height: 1.5; font-size: 14px;">{text}</div>'
635
 
636
  return html_output
637
+ def synthesize_audio(text, language, max_chars=200):
638
+ if not TTS_SUPPORT or not text.strip():
639
  return None
640
+
641
  try:
642
+ # Map language to code (e.g., "English" → "en")
643
+ lang_code = {"english": "en", "spanish": "es"}.get(language.lower(), "en")
 
 
644
 
645
+ # Split text into chunks
646
+ chunks = [text[i:i+max_chars] for i in range(0, len(text), max_chars)]
647
+ audio_files = []
648
+
649
+ for i, chunk in enumerate(chunks):
650
+ tts = gTTS(text=chunk, lang=lang_code, slow=False)
651
+ with tempfile.NamedTemporaryFile(suffix=f"_{i}.mp3", delete=False) as tmp_file:
652
+ tts.save(tmp_file.name)
653
+ audio_files.append(tmp_file.name)
654
+
655
+ # Combine all chunks into one file (requires ffmpeg)
656
+ combined_path = tempfile.NamedTemporaryFile(suffix="_combined.mp3", delete=False).name
657
+ os.system(f"ffmpeg -i 'concat:{'|'.join(audio_files)}' -acodec copy {combined_path}")
658
 
659
+ # Clean up individual files
660
+ for file in audio_files:
661
+ os.unlink(file)
662
+
663
+ return combined_path
664
+
665
  except Exception as e:
666
+ print(f" TTS Error: {e}")
667
  return None
668
 
 
669
  def new_chat():
670
  """Reset all fields for a new chat session"""
671
  return (