28-nini commited on
Commit
9b77562
·
verified ·
1 Parent(s): 684add8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -57
app.py CHANGED
@@ -635,6 +635,8 @@ def apply_glossary_tooltips(text):
635
 
636
  return html_output
637
 
 
 
638
  def synthesize_audio(text, language="English"):
639
  if not text.strip():
640
  return None
@@ -648,69 +650,19 @@ def synthesize_audio(text, language="English"):
648
  "german": "de",
649
  "hindi": "hi",
650
  "arabic": "ar",
651
- "urdu": "ur",
652
- "chinese": "zh",
653
- "japanese": "ja"
654
  }
655
 
656
  # Get language code (default to English if not found)
657
  lang_code = lang_map.get(language.lower(), "en")
658
 
659
- # Split text into chunks of 200 characters (gTTS limit)
660
- chunk_size = 200
661
- text_chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
662
-
663
- audio_files = []
664
-
665
- # Generate audio for each chunk
666
- for i, chunk in enumerate(text_chunks):
667
- tts = gTTS(text=chunk, lang=lang_code, slow=False)
668
- with tempfile.NamedTemporaryFile(suffix=f"_{i}.mp3", delete=False) as tmp_file:
669
- chunk_path = tmp_file.name
670
- tts.save(chunk_path)
671
- audio_files.append(chunk_path)
672
 
673
- # Combine all chunks into one audio file
674
- with tempfile.NamedTemporaryFile(suffix="_combined.mp3", delete=False) as combined_file:
675
- combined_path = combined_file.name
676
-
677
- # Use ffmpeg to concatenate audio files
678
- if len(audio_files) > 1:
679
- try:
680
- import subprocess
681
- # Create a file list for ffmpeg
682
- with open("file_list.txt", "w") as f:
683
- for file in audio_files:
684
- f.write(f"file '{file}'\n")
685
-
686
- # Concatenate using ffmpeg
687
- subprocess.run([
688
- "ffmpeg",
689
- "-f", "concat",
690
- "-safe", "0",
691
- "-i", "file_list.txt",
692
- "-c", "copy",
693
- combined_path
694
- ], check=True)
695
- except Exception as e:
696
- print(f"Audio concatenation error: {e}")
697
- # Fallback to first chunk if concatenation fails
698
- combined_path = audio_files[0]
699
- else:
700
- combined_path = audio_files[0]
701
-
702
- # Clean up temporary files
703
- for file in audio_files:
704
- try:
705
- os.unlink(file)
706
- except:
707
- pass
708
- try:
709
- os.unlink("file_list.txt")
710
- except:
711
- pass
712
-
713
- return combined_path
714
 
715
  except Exception as e:
716
  print(f"Audio synthesis error: {e}")
 
635
 
636
  return html_output
637
 
638
+
639
+
640
  def synthesize_audio(text, language="English"):
641
  if not text.strip():
642
  return None
 
650
  "german": "de",
651
  "hindi": "hi",
652
  "arabic": "ar",
653
+ "urdu": "ur" # gTTS supports Urdu as 'ur'
 
 
654
  }
655
 
656
  # Get language code (default to English if not found)
657
  lang_code = lang_map.get(language.lower(), "en")
658
 
659
+ # Generate speech with proper language handling
660
+ tts = gTTS(text=text[:200], lang=lang_code, slow=False)
 
 
 
 
 
 
 
 
 
 
 
661
 
662
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
663
+ tmp_path = tmp_file.name
664
+ tts.save(tmp_path)
665
+ return tmp_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666
 
667
  except Exception as e:
668
  print(f"Audio synthesis error: {e}")