File size: 14,812 Bytes
6890329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Models module for Kokoro TTS Local"""
from typing import Optional, Tuple, List
import torch
from kokoro import KPipeline
import os
import json
import codecs
from pathlib import Path
import numpy as np
import shutil

# Set environment variables for proper encoding
os.environ["PYTHONIOENCODING"] = "utf-8"
# Disable symlinks warning
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"

# List of available voice files
VOICE_FILES = [
    # American Female voices
    "af_alloy.pt", "af_aoede.pt", "af_bella.pt", "af_jessica.pt",
    "af_kore.pt", "af_nicole.pt", "af_nova.pt", "af_river.pt",
    "af_sarah.pt", "af_sky.pt",
    # American Male voices
    "am_adam.pt", "am_echo.pt", "am_eric.pt", "am_fenrir.pt",
    "am_liam.pt", "am_michael.pt", "am_onyx.pt", "am_puck.pt",
    "am_santa.pt",
    # British Female voices
    "bf_alice.pt", "bf_emma.pt", "bf_isabella.pt", "bf_lily.pt",
    # British Male voices
    "bm_daniel.pt", "bm_fable.pt", "bm_george.pt", "bm_lewis.pt",
    # Special voices
    "el_dora.pt", "em_alex.pt", "em_santa.pt",
    "ff_siwis.pt",
    "hf_alpha.pt", "hf_beta.pt",
    "hm_omega.pt", "hm_psi.pt",
    "jf_sara.pt", "jm_nicola.pt",
    "jf_alpha.pt", "jf_gongtsuene.pt", "jf_nezumi.pt", "jf_tebukuro.pt",
    "jm_kumo.pt",
    "pf_dora.pt", "pm_alex.pt", "pm_santa.pt",
    "zf_xiaobei.pt", "zf_xiaoni.pt", "zf_xiaoqiao.pt", "zf_xiaoyi.pt"
]

# Patch KPipeline's load_voice method to use weights_only=False
original_load_voice = KPipeline.load_voice

def patched_load_voice(self, voice_path):
    """Load voice model with weights_only=False for compatibility"""
    if not os.path.exists(voice_path):
        raise FileNotFoundError(f"Voice file not found: {voice_path}")
    voice_name = Path(voice_path).stem
    voice_model = torch.load(voice_path, weights_only=False)
    if voice_model is None:
        raise ValueError(f"Failed to load voice model from {voice_path}")
    # Ensure device is set
    if not hasattr(self, 'device'):
        self.device = 'cpu'
    # Move model to device and store in voices dictionary
    self.voices[voice_name] = voice_model.to(self.device)
    return self.voices[voice_name]

KPipeline.load_voice = patched_load_voice

def patch_json_load():
    """Patch json.load to handle UTF-8 encoded files with special characters"""
    original_load = json.load
    
    def custom_load(fp, *args, **kwargs):
        try:
            # Try reading with UTF-8 encoding
            if hasattr(fp, 'buffer'):
                content = fp.buffer.read().decode('utf-8')
            else:
                content = fp.read()
            return json.loads(content)
        except UnicodeDecodeError:
            # If UTF-8 fails, try with utf-8-sig for files with BOM
            fp.seek(0)
            content = fp.read()
            if isinstance(content, bytes):
                content = content.decode('utf-8-sig', errors='replace')
            return json.loads(content)
    
    json.load = custom_load

def load_config(config_path: str) -> dict:
    """Load configuration file with proper encoding handling"""
    try:
        with codecs.open(config_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except UnicodeDecodeError:
        # Fallback to utf-8-sig if regular utf-8 fails
        with codecs.open(config_path, 'r', encoding='utf-8-sig') as f:
            return json.load(f)

# Initialize espeak-ng
phonemizer_available = False  # Global flag to track if phonemizer is working
try:
    from phonemizer.backend.espeak.wrapper import EspeakWrapper
    from phonemizer import phonemize
    import espeakng_loader
    
    # Make library available first
    library_path = espeakng_loader.get_library_path()
    data_path = espeakng_loader.get_data_path()
    espeakng_loader.make_library_available()
    
    # Set up espeak-ng paths
    EspeakWrapper.library_path = library_path
    EspeakWrapper.data_path = data_path
    
    # Verify espeak-ng is working
    try:
        test_phonemes = phonemize('test', language='en-us')
        if test_phonemes:
            phonemizer_available = True
            print("Phonemizer successfully initialized")
        else:
            print("Note: Phonemization returned empty result")
            print("TTS will work, but phoneme visualization will be disabled")
    except Exception as e:
        # Continue without espeak functionality
        print(f"Note: Phonemizer not available: {e}")
        print("TTS will work, but phoneme visualization will be disabled")

except ImportError as e:
    print(f"Installing required phonemizer packages...")
    import subprocess
    try:
        subprocess.check_call(["pip", "install", "espeakng-loader", "phonemizer-fork"])
        
        # Try again after installation
        from phonemizer.backend.espeak.wrapper import EspeakWrapper
        from phonemizer import phonemize
        import espeakng_loader
        
        library_path = espeakng_loader.get_library_path()
        data_path = espeakng_loader.get_data_path()
        espeakng_loader.make_library_available()
        EspeakWrapper.library_path = library_path
        EspeakWrapper.data_path = data_path
        
        # Test if it works now
        try:
            test_phonemes = phonemize('test', language='en-us')
            if test_phonemes:
                phonemizer_available = True
                print("Phonemizer successfully initialized")
            else:
                print("Note: Phonemization returned empty result")
                print("TTS will work, but phoneme visualization will be disabled")
        except Exception as e:
            print(f"Note: Phonemizer still not functional: {e}")
            print("TTS will work, but phoneme visualization will be disabled")
    except Exception as e:
        print(f"Note: Could not install or initialize phonemizer: {e}")
        print("TTS will work, but phoneme visualization will be disabled")

# Initialize pipeline globally
_pipeline = None

def download_voice_files():
    """Download voice files from Hugging Face."""
    voices_dir = Path("voices")
    voices_dir.mkdir(exist_ok=True)
    
    from huggingface_hub import hf_hub_download
    downloaded_voices = []
    
    print("\nDownloading voice files...")
    for voice_file in VOICE_FILES:
        try:
            # Full path where the voice file should be
            voice_path = voices_dir / voice_file
            
            if not voice_path.exists():
                print(f"Downloading {voice_file}...")
                # Download to a temporary location first
                temp_path = hf_hub_download(
                    repo_id="hexgrad/Kokoro-82M",
                    filename=f"voices/{voice_file}",
                    local_dir="temp_voices",
                    force_download=True
                )
                
                # Move the file to the correct location
                os.makedirs(os.path.dirname(voice_path), exist_ok=True)
                shutil.move(temp_path, voice_path)
                downloaded_voices.append(voice_file)
                print(f"Successfully downloaded {voice_file}")
            else:
                print(f"Voice file {voice_file} already exists")
                downloaded_voices.append(voice_file)
        except Exception as e:
            print(f"Warning: Failed to download {voice_file}: {e}")
            continue
    
    # Clean up temporary directory
    if os.path.exists("temp_voices"):
        shutil.rmtree("temp_voices")
    
    if not downloaded_voices:
        print("Warning: No voice files could be downloaded. Please check your internet connection.")
    else:
        print(f"Successfully processed {len(downloaded_voices)} voice files")
    
    return downloaded_voices

def build_model(model_path: str, device: str) -> KPipeline:
    """Build and return the Kokoro pipeline with proper encoding configuration"""
    global _pipeline
    if _pipeline is None:
        try:
            # Patch json loading before initializing pipeline
            patch_json_load()
            
            # Download model if it doesn't exist
            if model_path is None:
                model_path = 'kokoro-v1_0.pth'
            
            if not os.path.exists(model_path):
                print(f"Downloading model file {model_path}...")
                from huggingface_hub import hf_hub_download
                model_path = hf_hub_download(
                    repo_id="hexgrad/Kokoro-82M",
                    filename="kokoro-v1_0.pth",
                    local_dir=".",
                    force_download=True
                )
                print(f"Model downloaded to {model_path}")
            
            # Download config if it doesn't exist
            config_path = "config.json"
            if not os.path.exists(config_path):
                print("Downloading config file...")
                config_path = hf_hub_download(
                    repo_id="hexgrad/Kokoro-82M",
                    filename="config.json",
                    local_dir=".",
                    force_download=True
                )
                print(f"Config downloaded to {config_path}")
            
            # Download voice files
            downloaded_voices = download_voice_files()
            
            if not downloaded_voices:
                print("Error: No voice files available. Cannot proceed.")
                raise ValueError("No voice files available")
            
            # Initialize pipeline with American English by default
            _pipeline = KPipeline(lang_code='a')
            if _pipeline is None:
                raise ValueError("Failed to initialize KPipeline - pipeline is None")
                
            # Store device parameter for reference in other operations
            _pipeline.device = device
            
            # Initialize voices dictionary if it doesn't exist
            if not hasattr(_pipeline, 'voices'):
                _pipeline.voices = {}
            
            # Try to load the first available voice
            for voice_file in downloaded_voices:
                voice_path = f"voices/{voice_file}"
                if os.path.exists(voice_path):
                    try:
                        _pipeline.load_voice(voice_path)
                        print(f"Successfully loaded voice: {voice_file}")
                        break  # Successfully loaded a voice
                    except Exception as e:
                        print(f"Warning: Failed to load voice {voice_file}: {e}")
                        continue
            
        except Exception as e:
            print(f"Error initializing pipeline: {e}")
            raise
    return _pipeline

def list_available_voices() -> List[str]:
    """List all available voice models"""
    voices_dir = Path("voices")
    
    # Create voices directory if it doesn't exist
    if not voices_dir.exists():
        print(f"Creating voices directory at {voices_dir.absolute()}")
        voices_dir.mkdir(exist_ok=True)
        return []
    
    # Get all .pt files in the voices directory
    voice_files = list(voices_dir.glob("*.pt"))
    
    # If no voice files found in voices directory
    if not voice_files:
        print(f"No voice files found in {voices_dir.absolute()}")
        # Try to find voice files in the root directory's voices folder
        root_voices = list(Path(".").glob("voices/*.pt"))
        if root_voices:
            print("Found voice files in root voices directory, moving them...")
            for voice_file in root_voices:
                target_path = voices_dir / voice_file.name
                if not target_path.exists():
                    shutil.move(str(voice_file), str(target_path))
            # Recheck voices directory
            voice_files = list(voices_dir.glob("*.pt"))
    
    if not voice_files:
        print("No voice files found. Please run the application again to download voices.")
        return []
    
    return [f.stem for f in voice_files]

def load_voice(voice_name: str, device: str) -> torch.Tensor:
    """Load a voice model"""
    pipeline = build_model(None, device)
    # Format voice path correctly - strip .pt if it was included
    voice_name = voice_name.replace('.pt', '')
    voice_path = f"voices/{voice_name}.pt"
    if not os.path.exists(voice_path):
        raise ValueError(f"Voice file not found: {voice_path}")
    return pipeline.load_voice(voice_path)

def generate_speech(
    model: KPipeline,
    text: str,
    voice: str,
    lang: str = 'a',
    device: str = 'cpu',
    speed: float = 1.0
) -> Tuple[Optional[torch.Tensor], Optional[str]]:
    """Generate speech using the Kokoro pipeline
    
    Args:
        model: KPipeline instance
        text: Text to synthesize
        voice: Voice name (e.g. 'af_bella')
        lang: Language code ('a' for American English, 'b' for British English)
        device: Device to use ('cuda' or 'cpu')
        speed: Speech speed multiplier (default: 1.0)
        
    Returns:
        Tuple of (audio tensor, phonemes string) or (None, None) on error
    """
    try:
        if model is None:
            raise ValueError("Model is None - pipeline not properly initialized")
            
        # Initialize voices dictionary if it doesn't exist
        if not hasattr(model, 'voices'):
            model.voices = {}
            
        # Ensure device is set
        if not hasattr(model, 'device'):
            model.device = device
            
        # Format voice path and ensure voice is loaded
        voice_name = voice.replace('.pt', '')
        voice_path = f"voices/{voice_name}.pt"
        if not os.path.exists(voice_path):
            raise ValueError(f"Voice file not found: {voice_path}")
            
        # Ensure voice is loaded before generating
        if voice_name not in model.voices:
            print(f"Loading voice {voice_name}...")
            model.load_voice(voice_path)
            
        if voice_name not in model.voices:
            raise ValueError(f"Failed to load voice {voice_name}")
            
        # Generate speech with the new API
        print(f"Generating speech with device: {model.device}")
        generator = model(
            text, 
            voice=voice_path,
            speed=speed,
            split_pattern=r'\n+'
        )
        
        # Get first generated segment and convert numpy array to tensor if needed
        for gs, ps, audio in generator:
            if audio is not None:
                if isinstance(audio, np.ndarray):
                    audio = torch.from_numpy(audio).float()
                return audio, ps
            
        return None, None
    except Exception as e:
        print(f"Error generating speech: {e}")
        return None, None