Spaces:
Runtime error
Runtime error
Update tts_engine.py
Browse files- tts_engine.py +44 -31
tts_engine.py
CHANGED
|
@@ -1,31 +1,44 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
self.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
self.tts.tts_to_file(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
|
| 4 |
+
class TTSEngine:
|
| 5 |
+
def __init__(self, use_coqui=False):
|
| 6 |
+
self.use_coqui = use_coqui
|
| 7 |
+
self.tts = None
|
| 8 |
+
|
| 9 |
+
if self.use_coqui:
|
| 10 |
+
# Multilingual model (handles Yoruba/Hausa/Igbo tones)
|
| 11 |
+
self.tts = TTS("tts_models/multilingual/multi-dataset/your_tts",
|
| 12 |
+
progress_bar=False, gpu=False)
|
| 13 |
+
|
| 14 |
+
def speak(self, text, lang="english", voice_clone=False):
|
| 15 |
+
"""
|
| 16 |
+
Returns path to generated audio file (for Gradio playback).
|
| 17 |
+
"""
|
| 18 |
+
if not text:
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
out_file = "output.wav"
|
| 22 |
+
|
| 23 |
+
if self.use_coqui:
|
| 24 |
+
if voice_clone and os.path.exists("my_voice.wav"):
|
| 25 |
+
self.tts.tts_to_file(
|
| 26 |
+
text=text,
|
| 27 |
+
file_path=out_file,
|
| 28 |
+
speaker_wav="my_voice.wav",
|
| 29 |
+
language=lang
|
| 30 |
+
)
|
| 31 |
+
else:
|
| 32 |
+
self.tts.tts_to_file(
|
| 33 |
+
text=text,
|
| 34 |
+
file_path=out_file,
|
| 35 |
+
language=lang
|
| 36 |
+
)
|
| 37 |
+
else:
|
| 38 |
+
# Lazy import to avoid crash on Hugging Face
|
| 39 |
+
import pyttsx3
|
| 40 |
+
engine = pyttsx3.init()
|
| 41 |
+
engine.save_to_file(text, out_file)
|
| 42 |
+
engine.runAndWait()
|
| 43 |
+
|
| 44 |
+
return out_file
|