File size: 9,200 Bytes
d2cdbdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import numpy as np
import soundfile as sf
import os
import tempfile
import logging
from pathlib import Path

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Global variable to store TTS model
tts_model = None
model_loaded = False

def load_tts_model():
    """Load the TTS model with multiple fallback methods"""
    global tts_model, model_loaded
    
    if model_loaded:
        return True
    
    try:
        # Method 1: Try loading from Hugging Face Hub
        try:
            from TTS.api import TTS
            from huggingface_hub import hf_hub_download
            
            model_repo = "SYSPIN/vits_Chhattisgarhi_Female"
            logger.info(f"Attempting to load model from {model_repo}...")
            
            # Download model files from HF
            model_path = hf_hub_download(
                repo_id=model_repo, 
                filename="best_model.pth",
                cache_dir="./model_cache"
            )
            config_path = hf_hub_download(
                repo_id=model_repo, 
                filename="config.json",
                cache_dir="./model_cache"
            )
            
            # Initialize TTS with downloaded files
            tts_model = TTS(model_path=model_path, config_path=config_path)
            model_loaded = True
            logger.info("✅ Model loaded successfully from Hugging Face Hub!")
            return True
            
        except ImportError:
            logger.warning("huggingface_hub not available, trying local files...")
        except Exception as e:
            logger.warning(f"Failed to load from HF Hub: {e}")
        
        # Method 2: Try loading from local files (if uploaded to space or cloned)
        local_paths = [
            ("./best_model.pth", "./config.json"),  # Current directory
            ("./model/best_model.pth", "./model/config.json"),  # Model subdirectory
            ("../best_model.pth", "../config.json"),  # Parent directory
        ]
        
        for model_path, config_path in local_paths:
            if os.path.exists(model_path) and os.path.exists(config_path):
                logger.info(f"Found local model files at {model_path}")
                from TTS.api import TTS
                tts_model = TTS(model_path=model_path, config_path=config_path)
                model_loaded = True
                logger.info("✅ Model loaded successfully from local files!")
                return True
        
        # Method 3: Try to use a generic VITS model as fallback
        logger.warning("Custom model not found, trying generic VITS model...")
        try:
            from TTS.api import TTS
            # Use a generic multilingual model as fallback
            tts_model = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
            model_loaded = True
            logger.info("✅ Loaded fallback multilingual model")
            return True
        except Exception as e:
            logger.error(f"Failed to load fallback model: {e}")
        
        return False
        
    except Exception as e:
        logger.error(f"Critical error loading model: {str(e)}")
        return False

def generate_speech(text, speed=1.0):
    """Generate speech from text"""
    global tts_model, model_loaded
    
    if not text.strip():
        return None, "⚠️ Please enter some text to synthesize."
    
    # Try to load model if not already loaded
    if not model_loaded:
        success = load_tts_model()
        if not success:
            return None, "❌ Error: Could not load any TTS model. Please check the setup."
    
    try:
        logger.info(f"Synthesizing: {text[:50]}...")
        
        # Create temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
            output_path = tmp_file.name
        
        # Generate speech - handle different TTS API versions
        try:
            # Method for custom models
            tts_model.tts_to_file(
                text=text,
                file_path=output_path,
                speed=speed
            )
        except TypeError:
            # Fallback for models that don't support speed parameter
            try:
                tts_model.tts_to_file(text=text, file_path=output_path)
            except Exception:
                # For XTTS and other models that need different parameters
                tts_model.tts_to_file(
                    text=text,
                    file_path=output_path,
                    speaker_wav=None,  # Use default speaker
                    language="hi"  # Hindi as closest language
                )
        
        # Check if file was created and has content
        if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
            return None, "❌ Error: Audio file was not generated properly."
        
        # Read audio data
        audio_data, sample_rate = sf.read(output_path)
        
        # Clean up
        os.unlink(output_path)
        
        if len(audio_data) == 0:
            return None, "❌ Error: Generated audio is empty."
        
        logger.info("✅ Speech generated successfully!")
        return (sample_rate, audio_data), "✅ Speech generated successfully!"
        
    except Exception as e:
        error_msg = f"❌ Error during synthesis: {str(e)}"
        logger.error(error_msg)
        return None, error_msg

# Sample texts
examples = [
    ["नमस्कार, का हाल बा?", 1.0],
    ["आज मोसम बहुत बढ़िया हे।", 1.0],
    ["तुमन कइसे हव?", 0.9],
    ["धन्यवाद।", 1.1],
    ["Hello, how are you?", 1.0]  # English fallback for testing
]

# Create Gradio interface
with gr.Blocks(
    title="Chhattisgarhi TTS",
    theme=gr.themes.Default(primary_hue="blue")
) as demo:
    
    gr.HTML("""
    <div style="text-align: center; margin: 20px 0;">
        <h1>🗣️ Chhattisgarhi Text-to-Speech</h1>
        <p style="color: #666;">Generate natural Chhattisgarhi speech with AI</p>
        <p style="color: #888; font-size: 0.9em;">Powered by SySpin & Coqui TTS</p>
    </div>
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            text_input = gr.Textbox(
                label="📝 Enter Text",
                placeholder="छत्तीसगढ़ी में अपना टेक्स्ट लिखें... (Enter Chhattisgarhi text here)",
                lines=3,
                max_lines=6
            )
            
            speed_slider = gr.Slider(
                minimum=0.5,
                maximum=2.0,
                value=1.0,
                step=0.1,
                label="🎚️ Speech Speed",
                info="Adjust speaking rate (may not work with all models)"
            )
            
            generate_btn = gr.Button(
                "🎵 Generate Speech",
                variant="primary",
                size="lg"
            )
        
        with gr.Column(scale=1):
            gr.Markdown("### Quick Examples")
            for text, _ in examples:
                btn = gr.Button(text, size="sm")
                btn.click(lambda x=text: x, outputs=text_input)
    
    with gr.Row():
        audio_output = gr.Audio(
            label="🔊 Generated Speech",
            type="numpy"
        )
        
        status_output = gr.Textbox(
            label="📊 Status",
            interactive=False,
            max_lines=3
        )
    
    gr.Examples(
        examples=examples,
        inputs=[text_input, speed_slider],
        outputs=[audio_output, status_output],
        fn=generate_speech,
        cache_examples=False
    )
    
    with gr.Accordion("ℹ️ Model Information", open=False):
        gr.Markdown("""
        ### About This Model
        - **Language**: Chhattisgarhi (छत्तीसगढ़ी)
        - **Voice Type**: Female
        - **Training**: SySpin dataset
        - **Engine**: Coqui TTS
        
        ### Model Loading Strategy
        1. First tries to load the custom Chhattisgarhi model from Hugging Face
        2. Falls back to local files if available
        3. Uses a multilingual model as last resort
        
        ### How to Use
        1. Enter your text in Chhattisgarhi (Devanagari script preferred)
        2. Adjust speech speed if needed (may not work with all models)
        3. Click "Generate Speech"
        4. Listen to the generated audio
        
        ### Tips
        - Use proper punctuation for natural pauses
        - Shorter sentences often work better
        - If the custom model fails, a fallback model will be used
        """)
    
    # Event binding
    generate_btn.click(
        fn=generate_speech,
        inputs=[text_input, speed_slider],
        outputs=[audio_output, status_output]
    )
    
    # Load model on startup
    demo.load(
        fn=lambda: "🔄 Loading TTS model..." if not load_tts_model() else "✅ Model ready!",
        outputs=status_output
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(share=True)