Update app.py
Browse files
app.py
CHANGED
@@ -2,102 +2,183 @@ import gradio as gr
|
|
2 |
from transformers import VitsModel, AutoTokenizer
|
3 |
import torch
|
4 |
import logging
|
|
|
|
|
|
|
5 |
|
6 |
logging.basicConfig(level=logging.INFO)
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
tokenizers = {}
|
13 |
|
14 |
examples = {
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
models
|
27 |
-
tokenizers
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
if not text.strip():
|
35 |
return None, "Please enter some text to synthesize."
|
36 |
|
37 |
try:
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
|
|
|
42 |
|
43 |
-
with torch.no_grad():
|
44 |
-
output = model(**inputs).waveform
|
45 |
-
|
46 |
-
waveform = output.squeeze().cpu().numpy()
|
47 |
-
sample_rate = model.config.sampling_rate
|
48 |
-
|
49 |
-
return (sample_rate, waveform), None
|
50 |
except Exception as e:
|
51 |
-
logger.error(f"
|
52 |
-
return None, f"Error
|
53 |
|
54 |
-
def load_example(language):
|
|
|
55 |
return examples.get(language, "No example available")
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
with gr.Column():
|
77 |
-
text = gr.Textbox(
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
|
92 |
-
generate_btn.click(
|
93 |
-
fn=generate_audio,
|
94 |
-
inputs=[language, text],
|
95 |
-
outputs=[audio_output, error_msg]
|
96 |
-
)
|
97 |
-
example_btn.click(
|
98 |
-
fn=load_example,
|
99 |
-
inputs=language,
|
100 |
-
outputs=text
|
101 |
-
)
|
102 |
|
103 |
-
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import VitsModel, AutoTokenizer
|
3 |
import torch
|
4 |
import logging
|
5 |
+
import spaces
|
6 |
+
from typing import Tuple, Optional
|
7 |
+
import numpy as np
|
8 |
|
9 |
logging.basicConfig(level=logging.INFO)
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
+
if torch.cuda.is_available():
|
13 |
+
device = "cuda"
|
14 |
+
logger.info("Using CUDA for inference.")
|
15 |
+
elif torch.backends.mps.is_available():
|
16 |
+
device = "mps"
|
17 |
+
logger.info("Using MPS for inference.")
|
18 |
+
else:
|
19 |
+
device = "cpu"
|
20 |
+
logger.info("Using CPU for inference.")
|
21 |
|
22 |
+
languages = ["bambara", "boomu", "dogon", "pular", "songhoy", "tamasheq"]
|
|
|
23 |
|
24 |
examples = {
|
25 |
+
|
26 |
+
"bambara": "An filɛ ni ye yɔrɔ minna ni an ye an sigi ka a layɛ yala an bɛ ka baara min kɛ ɛsike a kɛlen don ka Ɲɛ wa ?",
|
27 |
+
|
28 |
+
"boomu": "Vunurobe wozomɛ pɛɛ, Poli we zo woro han Deeɓenu wara li Deeɓenu faralo zuun. Lo we baba a lo wara yi see ɓa Zuwifera ma ɓa Gɛrɛkela wa.",
|
29 |
+
|
30 |
+
"dogon": "Pɔɔlɔ, kubɔ lugo joo le, bana dɛin dɛin le, inɛw Ama titiyaanw le digɛu, Ama, emɛ babe bɛrɛ sɔɔ sɔi.",
|
31 |
+
|
32 |
+
"pular": "Miɗo ndaarde saabe Laamɗo e saabe Iisaa Almasiihu caroyoowo wuurɓe e maayɓe oo, miɗo ndaardire saabe gartol makko ka num e Laamu makko",
|
33 |
+
|
34 |
+
"songhoy": "Haya ka se beenediyo kokoyteraydi go hima nda huukoy foo ka fatta ja subaahi ka taasi goykoyyo ngu rezẽ faridi se",
|
35 |
+
|
36 |
+
"tamasheq": "Toḍă tăfukt ɣas, issăɣră-dd măssi-s n-ašĕkrĕš ănaẓraf-net, inn'-as: 'Ǝɣĕr-dd inaxdimăn, tĕẓlĕd-asăn, sănt s-wi dd-ĕšrăynen har tĕkkĕd wi dd-ăzzarnen."
|
37 |
+
|
38 |
}
|
39 |
|
40 |
+
class MalianTTS:
|
41 |
+
def __init__(self, model_name: str = "MALIBA-AI/malian-tts"):
|
42 |
+
self.model_name = model_name
|
43 |
+
self.models = {}
|
44 |
+
self.tokenizers = {}
|
45 |
+
self._load_models()
|
46 |
+
|
47 |
+
def _load_models(self):
|
48 |
+
"""Load all language models and tokenizers"""
|
49 |
+
try:
|
50 |
+
for lang in languages:
|
51 |
+
logger.info(f"Loading model and tokenizer for {lang}...")
|
52 |
+
self.models[lang] = VitsModel.from_pretrained(
|
53 |
+
self.model_name,
|
54 |
+
subfolder=f"models/{lang}"
|
55 |
+
).to(device)
|
56 |
+
self.tokenizers[lang] = AutoTokenizer.from_pretrained(
|
57 |
+
self.model_name,
|
58 |
+
subfolder=f"models/{lang}"
|
59 |
+
)
|
60 |
+
logger.info(f"Successfully loaded {lang}")
|
61 |
+
except Exception as e:
|
62 |
+
logger.error(f"Failed to load models: {str(e)}")
|
63 |
+
raise Exception(f"Model loading failed: {str(e)}")
|
64 |
+
|
65 |
+
def synthesize(self, language: str, text: str) -> Tuple[Optional[Tuple[int, np.ndarray]], Optional[str]]:
|
66 |
+
"""Generate audio from text for the specified language"""
|
67 |
+
if not text.strip():
|
68 |
+
return None, "Please enter some text to synthesize."
|
69 |
+
|
70 |
+
try:
|
71 |
+
model = self.models[language]
|
72 |
+
tokenizer = self.tokenizers[language]
|
73 |
+
|
74 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
75 |
+
|
76 |
+
with torch.no_grad():
|
77 |
+
output = model(**inputs).waveform
|
78 |
+
|
79 |
+
waveform = output.squeeze().cpu().numpy()
|
80 |
+
sample_rate = model.config.sampling_rate
|
81 |
+
|
82 |
+
return (sample_rate, waveform), None
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
logger.error(f"Error during inference for {language}: {str(e)}")
|
86 |
+
return None, f"Error generating audio: {str(e)}"
|
87 |
+
|
88 |
+
# Initialize the TTS system
|
89 |
+
tts_system = MalianTTS()
|
90 |
|
91 |
+
@spaces.GPU()
|
92 |
+
def generate_audio(language: str, text: str) -> Tuple[Optional[Tuple[int, np.ndarray]], str]:
|
93 |
+
"""
|
94 |
+
Generate audio from text using the specified language model.
|
95 |
+
"""
|
96 |
if not text.strip():
|
97 |
return None, "Please enter some text to synthesize."
|
98 |
|
99 |
try:
|
100 |
+
audio_output, error_msg = tts_system.synthesize(language, text)
|
101 |
+
if error_msg:
|
102 |
+
logger.error(f"TTS generation failed: {error_msg}")
|
103 |
+
return None, error_msg
|
104 |
|
105 |
+
logger.info(f"Successfully generated audio for {language}")
|
106 |
+
return audio_output, "Audio generated successfully!"
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
except Exception as e:
|
109 |
+
logger.error(f"Audio generation failed: {e}")
|
110 |
+
return None, f"Error: {str(e)}"
|
111 |
|
112 |
+
def load_example(language: str) -> str:
|
113 |
+
"""Load example text for the selected language"""
|
114 |
return examples.get(language, "No example available")
|
115 |
|
116 |
+
def build_interface():
|
117 |
+
"""
|
118 |
+
Builds the Gradio interface for Malian TTS.
|
119 |
+
"""
|
120 |
+
with gr.Blocks(title="MalianVoices") as demo:
|
121 |
+
gr.Markdown(
|
122 |
+
"""
|
123 |
+
# MalianVoices: 🇲🇱 Text-to-Speech in Six Malian Languages
|
124 |
+
|
125 |
+
Lightweight TTS for six Malian languages: **Bambara, Boomu, Dogon, Pular, Songhoy, Tamasheq**.
|
126 |
+
|
127 |
+
- ✅ Real-time TTS with fast response
|
128 |
+
|
129 |
+
## How to Use
|
130 |
+
1. Pick a language from the dropdown
|
131 |
+
2. Enter your text or load an example
|
132 |
+
3. Click **"Generate Audio"** to listen
|
133 |
+
"""
|
134 |
+
)
|
135 |
+
|
136 |
+
with gr.Row():
|
137 |
+
language = gr.Dropdown(
|
138 |
+
choices=languages,
|
139 |
+
label="Language",
|
140 |
+
value="bambara"
|
141 |
+
)
|
142 |
+
|
143 |
with gr.Column():
|
144 |
+
text = gr.Textbox(
|
145 |
+
label="Input Text",
|
146 |
+
lines=5,
|
147 |
+
placeholder="Type your text here..."
|
148 |
+
)
|
149 |
+
|
150 |
+
with gr.Row():
|
151 |
+
example_btn = gr.Button("Load Example")
|
152 |
+
generate_btn = gr.Button("Generate Audio", variant="primary")
|
153 |
+
|
154 |
+
audio_output = gr.Audio(label="Generated Audio", type="numpy")
|
155 |
+
status_msg = gr.Textbox(label="Status", interactive=False)
|
156 |
+
|
157 |
+
# Footer
|
158 |
+
gr.Markdown(
|
159 |
+
"""
|
160 |
+
By [sudoping01](https://huggingface.co/sudoping01), from [sudoping01/malian-tts](https://huggingface.co/sudoping01/malian-tts).
|
161 |
+
Fine-tuned on Meta's MMS, CC BY-NC 4.0, non-commercial.
|
162 |
+
"""
|
163 |
+
)
|
164 |
+
|
165 |
+
# Connect buttons to functions
|
166 |
+
generate_btn.click(
|
167 |
+
fn=generate_audio,
|
168 |
+
inputs=[language, text],
|
169 |
+
outputs=[audio_output, status_msg]
|
170 |
+
)
|
171 |
+
|
172 |
+
example_btn.click(
|
173 |
+
fn=load_example,
|
174 |
+
inputs=language,
|
175 |
+
outputs=text
|
176 |
+
)
|
177 |
|
178 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
+
if __name__ == "__main__":
|
181 |
+
logger.info("Starting the Gradio interface for MalianVoices TTS.")
|
182 |
+
interface = build_interface()
|
183 |
+
interface.launch()
|
184 |
+
logger.info("Gradio interface running.")
|