File size: 9,041 Bytes
3f89022
 
254ef61
3f89022
254ef61
3f89022
2506e7b
3f89022
 
 
 
 
2506e7b
254ef61
 
 
379fa64
254ef61
 
 
 
92275ac
254ef61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f89022
 
254ef61
3f89022
 
 
254ef61
 
 
 
 
3f89022
 
 
 
 
 
 
254ef61
 
 
3f89022
 
 
 
254ef61
 
 
 
 
3f89022
 
 
 
254ef61
 
3f89022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254ef61
3f89022
 
 
 
 
 
 
 
 
 
 
 
 
 
2506e7b
3f89022
 
6469f27
 
 
 
3f89022
6469f27
3f89022
 
6469f27
3f89022
 
6469f27
254ef61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f89022
254ef61
 
 
 
 
 
 
3f89022
 
254ef61
 
 
 
 
 
 
fa84412
254ef61
 
 
 
 
 
3f89022
 
254ef61
 
 
 
 
 
3f89022
254ef61
 
 
 
 
 
 
 
 
3f89022
254ef61
 
 
 
 
 
 
 
 
 
 
3f89022
254ef61
 
3f89022
254ef61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f89022
254ef61
fa84412
254ef61
 
 
 
 
3f89022
254ef61
 
 
3f89022
254ef61
 
 
 
 
 
 
 
 
 
 
3f89022
 
254ef61
 
 
 
 
 
3f89022
 
254ef61
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import gradio as gr
import numpy as np
import os
import spaces
import sys
from huggingface_hub import login


hf_token = os.getenv("HF_TOKEN")
if hf_token:
    login(token=hf_token)


try:
    from maliba_ai.tts.inference import BambaraTTSInference
    from maliba_ai.config.speakers import Adame, Moussa, Bourama, Modibo, Seydou
    

    print("Loading Bambara TTS model...")
    tts = BambaraTTSInference()
    print("Model loaded successfully!")
    
    MODEL_LOADED = True
except Exception as e:
    print(f"Error loading model: {e}")
    MODEL_LOADED = False
    tts = None


if MODEL_LOADED:
    SPEAKERS = {
        "Adame": Adame,
        "Moussa": Moussa, 
        "Bourama": Bourama,
        "Modibo": Modibo,
        "Seydou": Seydou
    }
else:
    SPEAKERS = {
        "Adame": "Adame",
        "Moussa": "Moussa", 
        "Bourama": "Bourama",
        "Modibo": "Modibo",
        "Seydou": "Seydou"
    }

def validate_inputs(text, temperature, top_k, top_p, max_tokens):
    """Validate user inputs"""
    if not text or not text.strip():
        return False, "Please enter some Bambara text."
    
    if len(text.strip()) > 1000:
        return False, "Text is too long. Please use shorter text (max 1000 characters)."
    
    if not (0.1 <= temperature <= 2.0):
        return False, "Temperature must be between 0.1 and 2.0"
    
    if not (1 <= top_k <= 100):
        return False, "Top-K must be between 1 and 100"
    
    if not (0.1 <= top_p <= 1.0):
        return False, "Top-P must be between 0.1 and 1.0"
    
    if not (256 <= max_tokens <= 4096):
        return False, "Max tokens must be between 256 and 4096"
    
    return True, ""

@spaces.GPU()
def generate_speech(text, speaker_name, use_advanced, temperature, top_k, top_p, max_tokens):
    """Generate speech from Bambara text"""
    
    if not MODEL_LOADED:
        return None, "❌ Model not loaded. Please check the logs for errors."
    
    if not text.strip():
        return None, "Please enter some Bambara text."
    
    try:

        speaker = SPEAKERS[speaker_name]
        
        if use_advanced:
            is_valid, error_msg = validate_inputs(text, temperature, top_k, top_p, max_tokens)
            if not is_valid:
                return None, f"❌ {error_msg}"
            
            waveform = tts.generate_speech(
                text=text.strip(),
                speaker_id=speaker,
                temperature=temperature,
                top_k=int(top_k),
                top_p=top_p,
                max_new_audio_tokens=int(max_tokens)
            )
        else:

            waveform = tts.generate_speech(
                text=text.strip(),
                speaker_id=speaker
            )
        
        if waveform.size == 0:
            return None, "Failed to generate audio. Please try again."
        
        sample_rate = 16000
        return (sample_rate, waveform), f"✅ Audio generated successfully"
        
    except Exception as e:
        return None, f"❌ Error: {str(e)}"


examples = [
    ["Aw ni ce", "Adame"],
    ["Mali bɛna diya kɔsɛbɛ,  ka a da a kan baara bɛ ka kɛ.", "Moussa"],
    ["Ne bɛ se ka sɛbɛnni yɛlɛma ka kɛ kuma ye", "Bourama"],
    ["I ka kɛnɛ wa?", "Modibo"],
    ["Lakɔli karamɔgɔw tun tɛ ka se ka sɛbɛnni kɛ ka ɲɛ walanba kan wa denmisɛnw tun tɛ ka se ka o sɛbɛnni ninnu ye,  kuma tɛ ka u kalan. Denmisɛnw kɛra kunfinw ye", "Adame"],
    ["sigikafɔ kɔnɔ jamanaw ni ɲɔgɔn cɛ, olu ye a haminankow ye, wa o ko ninnu ka kan ka kɛ sariya ani tilennenya kɔnɔ", "Seydou"],
    ["Aw ni ce. Ne tɔgɔ ye Adama. Awɔ ne ye Maliden de ye. Aw Sanbe Sanbe. San min tɛ ɲinan ye, an bɛɛ ka jɛ ka o seli ɲɔgɔn fɛ,  hɛɛrɛ  ni lafiya la. Ala ka Mali suma. Ala ka Mali yiriwa. Ala ka Mali taa ɲɛ. Ala ka an ka seliw caya. Ala ka yafa an bɛɛ ma", "Moussa"],
    ["An dɔlakelen bɛ masike bilenman don ka tɔw gɛn.", "Bourama"],
    ["Aw ni ce. Seidu bɛ aw fo wa aw ka yafa a ma, ka da a kan tuma dɔw la kow ka can.", "Modibo"],
    
]


with gr.Blocks(title="Bambara TTS - EXPERIMENTAL", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🎤 Bambara Text-to-Speech ⚠️ EXPERIMENTAL
    
    Convert Bambara text to speech using AI. This model is currently experimental.
    
    **Bambara** is spoken by millions of people in Mali and West Africa.
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            # Input section
            text_input = gr.Textbox(
                label="📝 Bambara Text",
                placeholder="Type your Bambara text here...",
                lines=3,
                max_lines=6,
                value="Aw ni ce"
            )
            
            # Speaker selection
            speaker_dropdown = gr.Dropdown(
                choices=list(SPEAKERS.keys()),
                value="Adame",
                label="🗣️ Speaker Voice"
            )
            
            # Generation button
            generate_btn = gr.Button("🎵 Generate Speech", variant="primary", size="lg")
            
        with gr.Column(scale=1):
            # Advanced settings toggle
            use_advanced = gr.Checkbox(
                label="⚙️ Use Advanced Settings", 
                value=False,
                info="Enable to customize generation parameters"
            )
            
            # Advanced settings (hidden by default)
            with gr.Group(visible=False) as advanced_group:
                gr.Markdown("**Advanced Parameters:**")
                
                temperature = gr.Slider(
                    minimum=0.1, 
                    maximum=2.0, 
                    value=0.8, 
                    step=0.1,
                    label="Temperature",
                    info="Higher = more varied"
                )
                
                top_k = gr.Slider(
                    minimum=1, 
                    maximum=100, 
                    value=50, 
                    step=5,
                    label="Top-K"
                )
                
                top_p = gr.Slider(
                    minimum=0.1, 
                    maximum=1.0, 
                    value=0.9, 
                    step=0.05,
                    label="Top-P"
                )
                
                max_tokens = gr.Slider(
                    minimum=256, 
                    maximum=4096, 
                    value=2048, 
                    step=256,
                    label="Max Length"
                )
    
    # Output section
    gr.Markdown("### 🔊 Generated Audio")
    
    audio_output = gr.Audio(
        label="Generated Speech",
        type="numpy",
        interactive=False
    )
        
    status_output = gr.Textbox(
        label="Status",
        interactive=False,
        show_label=False,
        container=False
    )
    
    # Examples section
    with gr.Accordion("📚 Try These Examples", open=True):
        def load_example(text, speaker):
            return text, speaker, False, 0.8, 50, 0.9, 2048
        
        gr.Markdown("**Click any example below:**")
        example_buttons = []
        
        for i, (text, speaker) in enumerate(examples):
            btn = gr.Button(f"🎯 {text[:30]}{'...' if len(text) > 30 else ''}", size="sm")
            btn.click(
                fn=lambda t=text, s=speaker: load_example(t, s),
                outputs=[text_input, speaker_dropdown, use_advanced, temperature, top_k, top_p, max_tokens]
            )
    
    # Information section
    with gr.Accordion("ℹ️ About", open=False):
        gr.Markdown("""
        **⚠️ This is an experimental Bambara TTS model.**
        
        **Common Bambara Phrases:**
        - **Aw ni ce** - Hello (formal)
        - **I ni ce** - Hello (informal)  
        - **I ka kene wa?** - How are you?
        - **Aw ni tile** - Good afternoon
        
        **Available Speakers:** Adame, Moussa, Bourama, Modibo, Seydou
        
        **Tips:**
        - Start with default settings
        - Use shorter texts for better results
        - Try different speakers for variety
        """)
    
    # Toggle advanced settings visibility
    def toggle_advanced(use_adv):
        return gr.Group(visible=use_adv)
    
    use_advanced.change(
        fn=toggle_advanced,
        inputs=[use_advanced],
        outputs=[advanced_group]
    )
    
    # Wire up the interface
    generate_btn.click(
        fn=generate_speech,
        inputs=[text_input, speaker_dropdown, use_advanced, temperature, top_k, top_p, max_tokens],
        outputs=[audio_output, status_output]
    )
    
    # Auto-generate on Enter key in text input
    text_input.submit(
        fn=generate_speech,
        inputs=[text_input, speaker_dropdown, use_advanced, temperature, top_k, top_p, max_tokens],
        outputs=[audio_output, status_output]
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )