Spaces:
Running
Running
Rename tts_engine.py to audio.py
Browse files- audio.py +12 -0
- tts_engine.py +0 -59
audio.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# audio.py
|
2 |
+
import torchaudio
|
3 |
+
from bark import SAMPLE_RATE, generate_audio
|
4 |
+
from bark.generation import preload_models
|
5 |
+
import os
|
6 |
+
|
7 |
+
preload_models() # Download models at startup
|
8 |
+
|
9 |
+
def generate_speech(text, output_path="output.wav"):
|
10 |
+
audio_array = generate_audio(text)
|
11 |
+
torchaudio.save(output_path, audio_array, SAMPLE_RATE)
|
12 |
+
return output_path
|
tts_engine.py
DELETED
@@ -1,59 +0,0 @@
|
|
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)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|