File size: 16,072 Bytes
3447474
 
043ab85
3447474
 
 
 
 
043ab85
 
 
 
3447474
3f89022
 
 
3447474
3f89022
e849c49
3f89022
3447474
 
 
 
 
 
3f89022
 
 
 
043ab85
 
 
 
 
 
 
 
 
 
b254d98
 
043ab85
b254d98
043ab85
b254d98
043ab85
 
 
 
 
 
 
 
 
 
b254d98
 
043ab85
 
 
 
 
 
 
 
 
 
 
 
 
 
e849c49
043ab85
 
b254d98
043ab85
b254d98
 
043ab85
 
b254d98
043ab85
b254d98
 
 
043ab85
b254d98
043ab85
b254d98
 
043ab85
 
 
 
 
 
 
 
 
 
 
3f89022
 
043ab85
3f89022
 
 
3447474
 
3f89022
 
 
 
 
 
 
043ab85
 
 
3f89022
 
 
 
043ab85
 
 
3f89022
 
 
 
043ab85
 
 
 
 
 
b254d98
043ab85
 
b254d98
043ab85
 
b254d98
3447474
043ab85
 
3f89022
043ab85
3f89022
 
 
 
 
043ab85
3f89022
 
 
 
 
 
 
 
043ab85
 
3f89022
 
 
 
b254d98
043ab85
 
 
 
 
 
 
 
 
3f89022
 
b254d98
3f89022
 
043ab85
3f89022
 
043ab85
 
 
3447474
3f89022
043ab85
 
 
 
 
 
 
 
 
 
3f89022
 
3447474
 
254ef61
043ab85
 
 
 
 
 
 
 
3447474
043ab85
 
 
 
 
 
 
 
 
 
 
 
3447474
 
 
 
 
043ab85
 
 
3447474
3f89022
 
3447474
 
043ab85
 
 
254ef61
fa84412
043ab85
 
 
 
 
3f89022
3447474
 
043ab85
3447474
043ab85
3f89022
3447474
 
043ab85
3447474
 
 
 
 
 
 
043ab85
3447474
 
 
 
 
 
 
043ab85
 
3447474
 
 
 
 
 
 
043ab85
 
3447474
 
 
 
 
 
 
043ab85
 
3447474
3f89022
3447474
3f89022
3447474
 
 
043ab85
 
3447474
 
 
 
 
 
043ab85
 
3447474
3f89022
043ab85
3447474
 
 
043ab85
3447474
043ab85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254ef61
043ab85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e849c49
043ab85
 
 
 
 
 
 
3447474
3f89022
043ab85
3447474
 
fa84412
3447474
 
 
 
 
 
043ab85
3447474
 
 
 
 
 
 
043ab85
3447474
 
 
 
 
 
3f89022
3447474
 
 
 
043ab85
254ef61
043ab85
3447474
043ab85
 
3447474
 
 
043ab85
 
3f89022
 
043ab85
3f89022
 
3447474
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os

# Set environment variables BEFORE any imports
os.environ["TORCHDYNAMO_DISABLE"] = "1"
os.environ["TORCH_COMPILE_DISABLE"] = "1"
os.environ["PYTORCH_DISABLE_CUDNN_BENCHMARK"] = "1"
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# Set CUDA environment to help with unsloth GPU detection
os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # Force GPU visibility
os.environ["FORCE_CUDA"] = "1"            # Force CUDA usage

import torch
import gradio as gr
import numpy as np
import spaces
import logging
from huggingface_hub import login
import time

torch._dynamo.config.disable = True
torch._dynamo.config.suppress_errors = True

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

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

# Check GPU availability
if torch.cuda.is_available():
    device = "cuda"
    logger.info("Using CUDA for inference.")
elif torch.backends.mps.is_available():
    device = "mps"
    logger.info("Using MPS for inference.")
else:
    device = "cpu"
    logger.info("Using CPU for inference.")

def get_speakers_dict():
    """Get speakers dictionary using the new package structure"""
    try:
        from maliba_ai.config.settings import Speakers
        return {
            "Adama": Speakers.Adama,
            "Moussa": Speakers.Moussa, 
            "Bourama": Speakers.Bourama,
            "Modibo": Speakers.Modibo,
            "Seydou": Speakers.Seydou,
            "Amadou": Speakers.Amadou,
            "Bakary": Speakers.Bakary,
            "Ngolo": Speakers.Ngolo,
            "Ibrahima": Speakers.Ibrahima,
            "Amara": Speakers.Amara
        }
    except Exception as e:
        logger.error(f"Failed to import all speakers: {e}")
        # Fallback to core speakers only
        try:
            from maliba_ai.config.settings import Speakers
            return {
                "Adama": Speakers.Adama,
                "Moussa": Speakers.Moussa, 
                "Bourama": Speakers.Bourama,
                "Modibo": Speakers.Modibo,
                "Seydou": Speakers.Seydou
            }
        except:
            logger.error("Failed to import even core speakers")
            return {}

def initialize_tts_model():
    """Initialize TTS model globally - similar to ASR space pattern"""
    try:
        logger.info("Initializing Bambara TTS model globally...")
        start_time = time.time()
        
        # Import and initialize the TTS model
        from maliba_ai.tts import BambaraTTSInference
        
        # Initialize model 
        model = BambaraTTSInference()
        
        elapsed = time.time() - start_time
        logger.info(f"TTS Model initialized successfully in {elapsed:.2f} seconds!")
        
        return model
        
    except Exception as e:
        logger.error(f"Failed to initialize TTS model: {e}")
        logger.info("Model will be initialized on first request instead")
        return None

# Initialize speakers dictionary
speakers_dict = get_speakers_dict()
logger.info(f"Available speakers: {list(speakers_dict.keys())}")

# Try to initialize model globally (like ASR space)
# If it fails due to GPU detection, it will be None and we'll init on first request
tts_model = initialize_tts_model()

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 not (0.001 <= temperature <= 2.0):
        return False, "Temperature must be between 0.001 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 len(text.strip()) > 1000:
        return False, "Text is too long. Please use shorter text (max 1000 characters)."
    
    return True, ""

@spaces.GPU()
def generate_speech(text, speaker_name, use_advanced, temperature, top_k, top_p, max_tokens):
    """Generate speech - with fallback initialization if global init failed"""
    global tts_model
    
    if not text.strip():
        return None, "Please enter some Bambara text."
    
    try:
        # If global initialization failed, try to initialize here with GPU decorator
        if tts_model is None:
            logger.info("Global model initialization failed, initializing with GPU decorator...")
            from maliba_ai.tts import BambaraTTSInference
            tts_model = BambaraTTSInference()
            logger.info("Model initialized successfully with GPU decorator!")
        
        if not speakers_dict:
            return None, "❌ Speakers not properly loaded"
        
        if speaker_name not in speakers_dict:
            available_speakers = list(speakers_dict.keys())
            return None, f"❌ Speaker '{speaker_name}' not found. Available: {available_speakers}"
        
        speaker = speakers_dict[speaker_name]
        logger.info(f"Generating speech with speaker: {speaker_name}")
        
        # Validate inputs if using advanced settings
        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_model.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:
            # Use default settings
            waveform = tts_model.generate_speech(
                text=text.strip(),
                speaker_id=speaker
            )
        
        if waveform is None or waveform.size == 0:
            return None, "❌ Failed to generate audio. Please try again with different text."
        
        # Ensure waveform is in correct format
        if isinstance(waveform, torch.Tensor):
            waveform = waveform.cpu().numpy()
        
        # Normalize audio to prevent clipping
        if np.max(np.abs(waveform)) > 0:
            waveform = waveform / np.max(np.abs(waveform)) * 0.9
        
        sample_rate = 16000
        return (sample_rate, waveform), f"✅ Audio generated successfully for speaker {speaker_name}"
        
    except Exception as e:
        logger.error(f"Speech generation failed: {e}", exc_info=True)
        return None, f"❌ Error: {str(e)}"

# Get available speakers for dropdown
SPEAKER_NAMES = list(speakers_dict.keys()) if speakers_dict else ["Adama", "Moussa", "Bourama", "Modibo", "Seydou"]


examples = [
    ["Aw ni ce", "Adama"],  
    ["Mali bɛna diya kɔsɛbɛ, ka a da a kan baara bɛ ka kɛ.", "Bakary"],  
    ["Ne bɛ se ka sɛbɛnni yɛlɛma ka kɛ kuma ye", "Moussa"],  
    ["I ka kɛnɛ wa?", "Ngolo"],  
    ["Lakɔli karamɔgɔw tun tɛ ka se ka sɛbɛnni kɛ ka ɲɛ walanda 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.", "Bourama"],  
    ["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ɔ.", "Ibrahima"],  
    ["Aw ni ce. Ne tɔgɔ ye Adama. Awɔ, ne ye maliden de ye. Aw Sanbɛ Sanbɛ. 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.", "Amara"],  
    ["An dɔlakelen bɛ masike bilenman don ka tɔw gɛn.", "Modibo"],  
    ["Aw ni ce. Seidu bɛ aw fo wa aw ka yafa a ma, ka da a kan tuma dɔw la kow ka can.", "Amadou"],  
    ["Bamanankan ye kan ɲuman ye", "Seydou"],  
]

def build_interface():
    """Build the Gradio interface for Bambara TTS"""
    
    with gr.Blocks(
        title="Bambara TTS - MALIBA-AI", 
        theme=gr.themes.Soft(),
        css="""
        .main-header { text-align: center; margin-bottom: 2rem; }
        .status-box { margin-top: 1rem; }
        """
    ) as demo:
        
        with gr.Row():
            gr.Markdown(f"""
            # 🎤 Bambara Text-to-Speech
            
            **Powered by MALIBA-AI** | *First Open-Source Bambara TTS*
            
            Convert Bambara text to natural-sounding speech using our state-of-the-art neural TTS system.
            
            **Bambara** is spoken by millions of people in Mali and West Africa 🌍
            
            **Status**: {'✅ Model loaded' if tts_model is not None else '⏳ Model will load on first request'}
            """, elem_classes=["main-header"])

        with gr.Row():
            with gr.Column(scale=2):
                text_input = gr.Textbox(
                    label="📝 Bambara Text",
                    placeholder="I ni ce... (Type your Bambara text here)",
                    lines=4,
                    max_lines=8,
                    value="I ni ce"
                )
                
                speaker_dropdown = gr.Dropdown(
                    choices=SPEAKER_NAMES,
                    value=SPEAKER_NAMES[0] if SPEAKER_NAMES else "Bourama",  # Default to most stable speaker
                    label="🗣️ Speaker Voice",
                    info=f"Choose from {len(SPEAKER_NAMES)} authentic voices (Bourama recommended for best quality)"
                )
                
                generate_btn = gr.Button(
                    "🎵 Generate Speech", 
                    variant="primary", 
                    size="lg"
                )
                
            with gr.Column(scale=1):
                use_advanced = gr.Checkbox(
                    label="⚙️ Advanced Settings", 
                    value=False,
                    info="Customize generation parameters"
                )
                
                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 speech"
                    )
                    
                    top_k = gr.Slider(
                        minimum=1, 
                        maximum=100, 
                        value=50, 
                        step=5,
                        label="Top-K",
                        info="Vocabulary selection size"
                    )
                    
                    top_p = gr.Slider(
                        minimum=0.1, 
                        maximum=1.0, 
                        value=0.9, 
                        step=0.05,
                        label="Top-P",
                        info="Nucleus sampling threshold"
                    )
                    
                    max_tokens = gr.Slider(
                        minimum=256, 
                        maximum=4096, 
                        value=2048, 
                        step=256,
                        label="Max Audio Length",
                        info="Maximum audio duration"
                    )
        
        gr.Markdown("### 🔊 Generated Audio")
        
        audio_output = gr.Audio(
            label="Generated Speech",
            type="numpy",
            interactive=False,
            show_download_button=True
        )
            
        status_output = gr.Textbox(
            label="Status",
            interactive=False,
            show_label=False,
            container=False,
            elem_classes=["status-box"]
        )
        
        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 to try it:**")
            
            with gr.Row():
                for i, (text, speaker) in enumerate(examples[:5]):
                    btn = gr.Button(
                        f"🔹 {text[:25]}{'...' if len(text) > 25 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]
                    )
            
            with gr.Row():
                for i, (text, speaker) in enumerate(examples[5:]):
                    btn = gr.Button(
                        f"🔹 {text[:25]}{'...' if len(text) > 25 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]
                    )
        
        with gr.Accordion("ℹ️ About", open=False):
            gr.Markdown(f"""
            ## About MALIBA-AI Bambara TTS
            
            - **🎯 Purpose**: First open-source Text-to-Speech system for Bambara language
            - **🗣️ Speakers**: {len(SPEAKER_NAMES)} different authentic voices  
            - **🔊 Quality**: 16kHz neural speech synthesis
            - **⚡ Performance**: Optimized for real-time generation
            - **📱 Usage**: Educational, accessibility, and cultural preservation
            
            ### 🎭 Speaker Characteristics:
            
            - **Bourama**: Most stable and accurate (recommended)
            - **Adama**: Natural conversational tone
            - **Moussa**: Clear pronunciation for educational content
            - **Modibo**: Expressive delivery for storytelling
            - **Seydou**: Balanced characteristics for general use
            - **Amadou**: Warm and friendly voice
            - **Bakary**: Deep, authoritative tone
            - **Ngolo**: Youthful and energetic
            - **Ibrahima**: Calm and measured delivery
            - **Amara**: Melodic and smooth
            
            **Model Architecture**: Built on state-of-the-art neural TTS with Bambara-specific optimizations
            
            **License**: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 (CC BY-NC-SA 4.0)
            
            ---
            
            **MALIBA-AI Mission**: Ensuring no Malian is left behind by technological advances 🇲🇱
            """)
        
        # Event handlers
        def toggle_advanced(use_adv):
            return gr.Group(visible=use_adv)
        
        use_advanced.change(
            fn=toggle_advanced,
            inputs=[use_advanced],
            outputs=[advanced_group]
        )
        
        # Generate speech on button click
        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],
            show_progress=True
        )
        
        # Generate speech on Enter key
        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],
            show_progress=True
        )
    
    return demo

def main():
    """Main function to launch the Gradio interface"""
    logger.info("Starting MALIBA-AI Bambara TTS Gradio interface...")
    
    # Build interface
    interface = build_interface()
    
    # Launch interface
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True
    )
    
    logger.info("Gradio interface launched successfully!")

if __name__ == "__main__":
    main()