innocentpeter commited on
Commit
6aff7fa
·
verified ·
1 Parent(s): 7c0c5fa

Update tts_engine.py

Browse files
Files changed (1) hide show
  1. tts_engine.py +27 -6
tts_engine.py CHANGED
@@ -1,15 +1,33 @@
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
  """
@@ -21,21 +39,24 @@ class TTSEngine:
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)
 
1
  import os
2
  from TTS.api import TTS
3
 
4
+ # Map our app languages → Coqui TTS codes
5
+ LANG_MAP = {
6
+ "english": "en",
7
+ "yoruba": "en", # no native Yoruba model, fallback to English phonemes
8
+ "hausa": "en", # same here, fallback
9
+ "igbo": "en", # same here
10
+ "pidgin": "en", # close enough to English
11
+ "esan": "en",
12
+ "tiv": "en",
13
+ "calabar": "en",
14
+ "benin": "en",
15
+ "french": "fr-fr",
16
+ "portuguese": "pt-br"
17
+ }
18
+
19
  class TTSEngine:
20
  def __init__(self, use_coqui=False):
21
  self.use_coqui = use_coqui
22
  self.tts = None
23
 
24
  if self.use_coqui:
25
+ # Multilingual model (limited codes available)
26
+ self.tts = TTS(
27
+ "tts_models/multilingual/multi-dataset/your_tts",
28
+ progress_bar=False,
29
+ gpu=False
30
+ )
31
 
32
  def speak(self, text, lang="english", voice_clone=False):
33
  """
 
39
  out_file = "output.wav"
40
 
41
  if self.use_coqui:
42
+ # normalize language
43
+ lang_code = LANG_MAP.get(lang.lower(), "en")
44
+
45
  if voice_clone and os.path.exists("my_voice.wav"):
46
  self.tts.tts_to_file(
47
  text=text,
48
  file_path=out_file,
49
  speaker_wav="my_voice.wav",
50
+ language=lang_code
51
  )
52
  else:
53
  self.tts.tts_to_file(
54
  text=text,
55
  file_path=out_file,
56
+ language=lang_code
57
  )
58
  else:
59
+ # Lazy import to avoid HF crash
60
  import pyttsx3
61
  engine = pyttsx3.init()
62
  engine.save_to_file(text, out_file)