innocentpeter commited on
Commit
7c0c5fa
·
verified ·
1 Parent(s): 4197a81

Update tts_engine.py

Browse files
Files changed (1) hide show
  1. tts_engine.py +44 -31
tts_engine.py CHANGED
@@ -1,31 +1,44 @@
1
- import os
2
- import pyttsx3
3
- from TTS.api import TTS
4
-
5
- class TTSEngine:
6
- def __init__(self, use_coqui=False):
7
- self.use_coqui = use_coqui
8
- if self.use_coqui:
9
- # Multilingual model (supports Yoruba/Hausa/Igbo accents)
10
- self.tts = TTS("tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
11
-
12
- def speak(self, text, lang="english", voice_clone=False):
13
- """
14
- Returns path to generated audio file (for Gradio playback).
15
- """
16
- if not text:
17
- return None
18
-
19
- out_file = "output.wav"
20
-
21
- if self.use_coqui:
22
- if voice_clone and os.path.exists("my_voice.wav"):
23
- self.tts.tts_to_file(text=text, file_path=out_file, speaker_wav="my_voice.wav", language=lang)
24
- else:
25
- self.tts.tts_to_file(text=text, file_path=out_file, language=lang)
26
- else:
27
- engine = pyttsx3.init()
28
- engine.save_to_file(text, out_file)
29
- engine.runAndWait()
30
-
31
- return out_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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