File size: 18,090 Bytes
802512e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import json
import shutil
import subprocess
import requests
import tarfile
from pathlib import Path
import soundfile as sf
import sherpa_onnx
import numpy as np
import uuid

# List of available TTS models
MODELS = [
    ['mms fa', 'https://huggingface.co/willwade/mms-tts-multilingual-models-onnx/resolve/main/fas', "🌠 راد", 'https://huggingface.co/facebook/mms-tts-fas'],
    ['coqui-vits-female1-karim23657', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/persian-tts-female1-vits-coqui', "🌺 نگار", 'https://huggingface.co/Kamtera/persian-tts-female1-vits'],
    ['coqui-vits-male1-karim23657', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/persian-tts-male1-vits-coqui', "🌟 آرش", 'https://huggingface.co/Kamtera/persian-tts-male1-vits'],
    ['coqui-vits-male-karim23657', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/male-male-coqui-vits', "🦁 کیان", 'https://huggingface.co/Kamtera/persian-tts-male-vits'],
    ['coqui-vits-female-karim23657', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/female-female-coqui-vits', "🌷 مهتاب", 'https://huggingface.co/Kamtera/persian-tts-female-vits'],
    ['coqui-vits-female-GPTInformal-karim23657', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/female-GPTInformal-coqui-vits', "🌼 شیوا", 'https://huggingface.co/karim23657/persian-tts-female-GPTInformal-Persian-vits'],
    ['coqui-vits-male-SmartGitiCorp', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/male-SmartGitiCorp-coqui-vits', "🚀 بهمن", 'https://huggingface.co/SmartGitiCorp/persian_tts_vits'],
    ['vits-piper-fa-ganji', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/vits-piper-fa-ganji', "🚀 برنا", 'https://huggingface.co/SadeghK/persian-text-to-speech'],
    ['vits-piper-fa-ganji-adabi', 'https://huggingface.co/karim23657/persian-tts-vits/tree/main/vits-piper-fa-ganji-adabi', "🚀 برنا-1", 'https://huggingface.co/SadeghK/persian-text-to-speech'],
    ['vits-piper-fa-gyro-medium', 'https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_IR-gyro-medium.tar.bz2', "💧 نیما", 'https://huggingface.co/gyroing/Persian-Piper-Model-gyro'],
    ['piper-fa-amir-medium', 'https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_IR-amir-medium.tar.bz2', "⚡️ آریا", 'https://huggingface.co/SadeghK/persian-text-to-speech'],
    ['vits-mimic3-fa-haaniye_low', 'https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-mimic3-fa-haaniye_low.tar.bz2', "🌹 ریما", 'https://github.com/MycroftAI/mimic3'],
    ['vits-piper-fa_en-rezahedayatfar-ibrahimwalk-medium', 'https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_en-rezahedayatfar-ibrahimwalk-medium.tar.bz2', "🌠 پیام", 'https://huggingface.co/mah92/persian-english-piper-tts-model'],
]

def download_and_extract_model(url, destination):
    """Download and extract the model files."""
    print(f"Downloading from URL: {url}")
    print(f"Destination: {destination}")
    
    # Convert Hugging Face URL format if needed
    if "huggingface.co" in url:
        base_url = url.replace("/tree/main/", "/resolve/main/")
        model_id = base_url.split("/")[-1]
        
        # Check if this is an MMS model
        is_mms_model = True 
        
        if is_mms_model:
            # MMS models have both model.onnx and tokens.txt
            model_url = f"{base_url}/model.onnx"
            tokens_url = f"{base_url}/tokens.txt"
            
            # Download model.onnx
            print("Downloading model.onnx...")
            model_path = os.path.join(destination, "model.onnx")
            response = requests.get(model_url, stream=True)
            if response.status_code != 200:
                raise Exception(f"Failed to download model from {model_url}. Status code: {response.status_code}")
            
            total_size = int(response.headers.get('content-length', 0))
            block_size = 8192
            downloaded = 0
            
            print(f"Total size: {total_size / (1024*1024):.1f} MB")
            with open(model_path, "wb") as f:
                for chunk in response.iter_content(chunk_size=block_size):
                    if chunk:
                        f.write(chunk)
                        downloaded += len(chunk)
                        if total_size > 0:
                            percent = int((downloaded / total_size) * 100)
                            if percent % 10 == 0:
                                print(f" {percent}%", end="", flush=True)
            print("\nModel download complete")
            
            # Download tokens.txt
            print("Downloading tokens.txt...")
            tokens_path = os.path.join(destination, "tokens.txt")
            response = requests.get(tokens_url, stream=True)
            if response.status_code != 200:
                raise Exception(f"Failed to download tokens from {tokens_url}. Status code: {response.status_code}")
            
            with open(tokens_path, "wb") as f:
                f.write(response.content)
            print("Tokens download complete")
            
            return
        else:
            # Other models are stored as tar.bz2 files
            url = f"{base_url}.tar.bz2"
        
        # Try the URL
        response = requests.get(url, stream=True)
        if response.status_code != 200:
            raise Exception(f"Failed to download model from {url}. Status code: {response.status_code}")
        
        # Check if this is a Git LFS file pointer
        content_start = response.content[:100].decode('utf-8', errors='ignore')
        if content_start.startswith('version https://git-lfs.github.com/spec/v1'):
            raise Exception(f"Received Git LFS pointer instead of file content from {url}")
    
    # Create model directory if it doesn't exist
    os.makedirs(destination, exist_ok=True)
    
    # For non-MMS models, handle tar.bz2 files
    tar_path = os.path.join(destination, "model.tar.bz2")
    
    # Download the file
    print("Downloading model archive...")
    response = requests.get(url, stream=True)
    total_size = int(response.headers.get('content-length', 0))
    block_size = 8192
    downloaded = 0
    
    print(f"Total size: {total_size / (1024*1024):.1f} MB")
    with open(tar_path, "wb") as f:
        for chunk in response.iter_content(chunk_size=block_size):
            if chunk:
                f.write(chunk)
                downloaded += len(chunk)
                if total_size > 0:
                    percent = int((downloaded / total_size) * 100)
                    if percent % 10 == 0:
                        print(f" {percent}%", end="", flush=True)
    print("\nDownload complete")
    
    # Extract the tar.bz2 file
    print(f"Extracting {tar_path} to {destination}")
    try:
        with tarfile.open(tar_path, "r:bz2") as tar:
            tar.extractall(path=destination)
        os.remove(tar_path)
        print("Extraction complete")
    except Exception as e:
        print(f"Error during extraction: {str(e)}")
        raise
    
    print("Contents of destination directory:")
    for root, dirs, files in os.walk(destination):
        print(f"\nDirectory: {root}")
        if dirs:
            print("  Subdirectories:", dirs)
        if files:
            print("  Files:", files)

def find_model_files(model_dir):
    """Find model files in the given directory and its subdirectories."""
    model_files = {}
    
    # Check if this is an MMS model
    is_mms = True
    
    for root, _, files in os.walk(model_dir):
        for file in files:
            file_path = os.path.join(root, file)
            
            # Model file
            if file.endswith('.onnx'):
                model_files['model'] = file_path
            
            # Tokens file
            elif file == 'tokens.txt':
                model_files['tokens'] = file_path
            
            # Lexicon file (only for non-MMS models)
            elif file == 'lexicon.txt' and not is_mms:
                model_files['lexicon'] = file_path
    
    # Create empty lexicon file if needed (only for non-MMS models)
    if not is_mms and 'model' in model_files and 'lexicon' not in model_files:
        model_dir = os.path.dirname(model_files['model'])
        lexicon_path = os.path.join(model_dir, 'lexicon.txt')
        with open(lexicon_path, 'w', encoding='utf-8') as f:
            pass  # Create empty file
        model_files['lexicon'] = lexicon_path
    
    return model_files if 'model' in model_files else {}

def generate_audio(text, model_info):
    """Generate audio from text using the specified model."""
    try:
        model_dir = os.path.join("./models", model_info)
        
        print(f"\nLooking for model in: {model_dir}")
        
        # Download model if it doesn't exist
        if not os.path.exists(model_dir):
            print(f"Model directory doesn't exist, downloading {model_info}...")
            os.makedirs(model_dir, exist_ok=True)
            model_url = None
            for model in MODELS:
                if model_info == model[2]:
                    model_url = model[1]
                    break
            if not model_url:
                raise ValueError(f"Model {model_info} not found in the model list")
                
            download_and_extract_model(model_url, model_dir)
        
        print(f"Contents of {model_dir}:")
        for item in os.listdir(model_dir):
            item_path = os.path.join(model_dir, item)
            if os.path.isdir(item_path):
                print(f"  Directory: {item}")
                print(f"    Contents: {os.listdir(item_path)}")
            else:
                print(f"  File: {item}")
        
        # Find and validate model files
        model_files = find_model_files(model_dir)
        if not model_files or 'model' not in model_files:
            raise ValueError(f"Could not find required model files in {model_dir}")
        
        print("\nFound model files:")
        print(f"Model: {model_files['model']}")
        print(f"Tokens: {model_files.get('tokens', 'Not found')}")
        print(f"Lexicon: {model_files.get('lexicon', 'Not required for MMS')}\n")
        
        # Check if this is an MMS model
        is_mms = 'mms' in os.path.basename(model_dir).lower()
        
        # Create configuration based on model type
        if is_mms:
            if 'tokens' not in model_files or not os.path.exists(model_files['tokens']):
                raise ValueError("tokens.txt is required for MMS models")
                
            # MMS models use tokens.txt and no lexicon
            vits_config = sherpa_onnx.OfflineTtsVitsModelConfig(
                model_files['model'],  # model
                '',                    # lexicon
                model_files['tokens'], # tokens
                '',                    # data_dir
                '',                    # dict_dir
                0.667,                 # noise_scale
                0.8,                   # noise_scale_w
                1.0                    # length_scale
            )
        else:
            # Non-MMS models use lexicon.txt
            if 'tokens' not in model_files or not os.path.exists(model_files['tokens']):
                raise ValueError("tokens.txt is required for VITS models")
                
            # Set data dir if it exists
            espeak_data = os.path.join(os.path.dirname(model_files['model']), 'espeak-ng-data')
            data_dir = espeak_data if os.path.exists(espeak_data) else 'espeak-ng-data'
            
            # Get lexicon path if it exists
            lexicon = model_files.get('lexicon', '') if os.path.exists(model_files.get('lexicon', '')) else ''
            
            # Create VITS model config
            vits_config = sherpa_onnx.OfflineTtsVitsModelConfig(
                model_files['model'],  # model
                lexicon,               # lexicon
                model_files['tokens'], # tokens
                data_dir,             # data_dir
                '',                   # dict_dir
                0.667,                # noise_scale
                0.8,                  # noise_scale_w
                1.0                   # length_scale
            )
        
        # Create the model config with VITS
        model_config = sherpa_onnx.OfflineTtsModelConfig()
        model_config.vits = vits_config
        
        # Create TTS configuration
        config = sherpa_onnx.OfflineTtsConfig(
            model=model_config,
            max_num_sentences=2
        )
        
        # Initialize TTS engine
        tts = sherpa_onnx.OfflineTts(config)

        # Generate audio
        audio_data = tts.generate(text)

        # Ensure we have valid audio data
        if audio_data is None or len(audio_data.samples) == 0:
            raise ValueError("Failed to generate audio - no data generated")
            
        # Convert samples list to numpy array and normalize
        audio_array = np.array(audio_data.samples, dtype=np.float32)
        if np.any(audio_array):  # Check if array is not all zeros
            audio_array = audio_array / np.abs(audio_array).max()
        else:
            raise ValueError("Generated audio is empty")
            
        # Return audio array and sample rate
        return (audio_array, audio_data.sample_rate)
            
    except Exception as e:
        error_msg = str(e)
        # Check for OOV or token conversion errors
        if "out of vocabulary" in error_msg.lower() or "token" in error_msg.lower():
            error_msg = f"Text contains unsupported characters: {error_msg}"
        print(f"Error generating audio: {error_msg}")
        raise

def tts_interface(selected_model, text):
    """Gradio interface for Persian text-to-speech."""
    try:
        if not text.strip():
            return None, "لطفا متنی برای تبدیل به گفتار وارد کنید"
            
        # Store original text for status message
        original_text = text
        
        try:
            # Update status with language info
            voice_name = selected_model
            
            # Generate audio
            audio_data, sample_rate = generate_audio(text, voice_name)
            
            # Create audio file
            audio_filename = f"tts_output_{uuid.uuid4()}.wav"
            sf.write(audio_filename, audio_data, samplerate=sample_rate, subtype="PCM_16")
            
            # Get model URL for display
            model_url = ""
            for model in MODELS:
                if selected_model == model[2]:
                    model_url = model[3]
                    break
            
            status = f"مدل: {selected_model}\nمنبع مدل: {model_url}\nمتن: '{text}'"
            
            return audio_filename, status
            
        except ValueError as e:
            # Handle known errors with user-friendly messages
            error_msg = str(e)
            if "cannot process some words" in error_msg.lower():
                return None, error_msg
            return None, f"خطا: {error_msg}"
            
    except Exception as e:
        print(f"Error in TTS generation: {str(e)}")
        error_msg = str(e)
        return None, f"خطا: {error_msg}"

def create_gradio_interface():
    """Create the Gradio interface."""
    # Prepare voice options from models
    voice_options = [model[2] for model in MODELS]
    
    # Create Gradio interface
    with gr.Blocks(title="تبدیل متن به گفتار فارسی", theme=gr.themes.Soft()) as demo:
        gr.Markdown("""
        # تبدیل متن به گفتار فارسی
        با استفاده از مدل‌های مختلف متن را به گفتار تبدیل کنید
        """)
        
        with gr.Row():
            with gr.Column():
                text_input = gr.TextArea(
                    label="متن فارسی",
                    placeholder="متن خود را اینجا وارد کنید...",
                    lines=5
                )
                
                voice_dropdown = gr.Dropdown(
                    label="صدا",
                    choices=voice_options,
                    value=voice_options[0]
                )
                
                generate_button = gr.Button("تبدیل به گفتار")
                
            with gr.Column():
                audio_output = gr.Audio(
                    label="خروجی صوتی",
                    interactive=False
                )
                
                status_output = gr.Textbox(
                    label="وضعیت",
                    interactive=False
                )
        
        generate_button.click(
            fn=tts_interface,
            inputs=[voice_dropdown, text_input],
            outputs=[audio_output, status_output]
        )
        
        gr.Examples(
            examples=[
                ["سلام. این یک نمونه متن برای نمایش سیستم تبدیل متن به گفتار فارسی است.", voice_options[0]],
                ["تبدیل متن به گفتار یکی از کاربردهای مهم پردازش زبان طبیعی است.", voice_options[1]],
                ["این پروژه از مدل‌های متنوعی برای تولید صدای طبیعی استفاده می‌کند.", voice_options[5]]
            ],
            inputs=[text_input, voice_dropdown],
            outputs=[audio_output, status_output],
            fn=tts_interface,
            cache_examples=False
        )
    
    return demo

if __name__ == "__main__":
    # Create models directory if it doesn't exist
    os.makedirs("models", exist_ok=True)
    
    # Launch Gradio interface
    demo = create_gradio_interface()
    demo.launch(server_name="0.0.0.0", server_port=7860)