sreepathi-ravikumar commited on
Commit
bbec22f
·
verified ·
1 Parent(s): ef53bee

Rename AudioGeneration.py to tts_engine.py

Browse files
Files changed (2) hide show
  1. AudioGeneration.py +0 -10
  2. tts_engine.py +59 -0
AudioGeneration.py DELETED
@@ -1,10 +0,0 @@
1
- from TTS.api import TTS
2
- import os
3
-
4
- # Initialize the TTS model (use a lightweight but high-quality model)
5
- tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
6
-
7
- def generate_audio(text):
8
- output_path = "output.wav"
9
- tts.tts_to_file(text=text, file_path=output_path)
10
- return output_path
 
 
 
 
 
 
 
 
 
 
 
tts_engine.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import edge_tts
2
+ import asyncio
3
+ import os
4
+ from pydub import AudioSegment
5
+ from langdetect import detect, LangDetectException
6
+ import logging
7
+
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(_name_)
10
+
11
+ class BilingualTTS:
12
+ def _init_(self):
13
+ self.voices = {
14
+ 'ta': 'ta-IN-PallaviNeural', # Tamil voice
15
+ 'en': 'en-US-AriaNeural' # English voice
16
+ }
17
+ self.temp_dir = "/app/temp"
18
+ os.makedirs(self.temp_dir, exist_ok=True)
19
+
20
+ async def _generate_segment(self, text, voice, idx):
21
+ output_file = os.path.join(self.temp_dir, f"segment_{idx}.mp3")
22
+ communicate = edge_tts.Communicate(text, voice)
23
+ await communicate.save(output_file)
24
+ return output_file
25
+
26
+ async def generate_audio(self, text):
27
+ try:
28
+ # Split text into sentences
29
+ sentences = [s.strip() for s in text.split('.') if s.strip()]
30
+
31
+ # Generate audio segments
32
+ tasks = []
33
+ for idx, sentence in enumerate(sentences):
34
+ try:
35
+ lang = detect(sentence)
36
+ voice = self.voices['en'] if lang not in ['ta', 'en'] else self.voices[lang]
37
+ except LangDetectException:
38
+ voice = self.voices['en']
39
+
40
+ tasks.append(self._generate_segment(sentence, voice, idx))
41
+
42
+ # Process all segments concurrently
43
+ segment_files = await asyncio.gather(*tasks)
44
+
45
+ # Combine audio segments
46
+ combined = AudioSegment.empty()
47
+ for sf in segment_files:
48
+ combined += AudioSegment.from_mp3(sf)
49
+ os.remove(sf) # Cleanup
50
+
51
+ # Save final output
52
+ output_path = os.path.join(self.temp_dir, "final_output.mp3")
53
+ combined.export(output_path, format="mp3")
54
+
55
+ return output_path
56
+
57
+ except Exception as e:
58
+ logger.error(f"TTS Generation Error: {str(e)}")
59
+ raise RuntimeError(f"TTS Failed: {str(e)}")