File size: 21,128 Bytes
9434032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import gradio as gr
import soundfile as sf
import tempfile
import torch
from vieneu_tts import VieNeuTTS
import os
import time
import threading
import pickle
import hashlib
import numpy as np
from pydub import AudioSegment

print("🎚️ Speed Control: Sử dụng Pydub (đơn giản, sạch, không vang)")

print("⏳ Đang khởi động VieNeu-TTS...")

# --- 1. SETUP MODEL ---
print("📦 Đang tải model...")
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"🖥️ Sử dụng thiết bị: {device.upper()}")

# Cache cho reference codes
CACHE_DIR = "./reference_cache"
os.makedirs(CACHE_DIR, exist_ok=True)

reference_cache = {}
reference_cache_lock = threading.Lock()

def get_cache_path(cache_key):
    """Tạo đường dẫn file cache từ key"""
    # Hash key để tạo tên file an toàn
    key_hash = hashlib.md5(cache_key.encode()).hexdigest()
    return os.path.join(CACHE_DIR, f"{key_hash}.pkl")

def load_cache_from_disk(cache_key):
    """Load cache từ disk nếu có"""
    cache_path = get_cache_path(cache_key)
    if os.path.exists(cache_path):
        try:
            with open(cache_path, 'rb') as f:
                return pickle.load(f)
        except:
            return None
    return None

def save_cache_to_disk(cache_key, ref_codes):
    """Lưu cache xuống disk"""
    cache_path = get_cache_path(cache_key)
    try:
        with open(cache_path, 'wb') as f:
            pickle.dump(ref_codes, f)
        # Lưu metadata để dễ debug
        meta_path = cache_path.replace('.pkl', '.txt')
        with open(meta_path, 'w', encoding='utf-8') as f:
            f.write(f"Cache key: {cache_key}\n")
            f.write(f"Created: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
    except Exception as e:
        print(f"   ⚠️ Không thể lưu cache: {e}")

try:
    tts = VieNeuTTS(
        backbone_repo="pnnbao-ump/VieNeu-TTS",
        backbone_device=device,
        codec_repo="neuphonic/neucodec",
        codec_device=device
    )
    print("✅ Model đã tải xong!")
    
    # Kiểm tra device thực tế
    if hasattr(tts, 'backbone'):
        if hasattr(tts.backbone, 'device'):
            print(f"   📍 Backbone device: {tts.backbone.device}")
        else:
            # For transformers model
            print(f"   📍 Backbone device: {next(tts.backbone.parameters()).device}")
    
    if hasattr(tts, 'codec'):
        if hasattr(tts.codec, 'device'):
            print(f"   📍 Codec device: {tts.codec.device}")
        else:
            print(f"   📍 Codec device: {next(tts.codec.parameters()).device}")
    
    print(f"   💾 GPU Memory allocated: {torch.cuda.memory_allocated(0) / 1024**3:.2f} GB" if torch.cuda.is_available() else "")
    
except Exception as e:
    print(f"⚠️ Không thể tải model (Chế độ UI Demo): {e}")
    class MockTTS:
        def encode_reference(self, path): return None
        def infer(self, text, ref, ref_text): 
            import numpy as np
            # Giả lập độ trễ để test tính năng đo thời gian
            time.sleep(1.5) 
            return np.random.uniform(-0.5, 0.5, 24000*3)
    tts = MockTTS()

# --- 2. DATA ---
VOICE_SAMPLES = {
    "Tuyên (nam miền Bắc)": {"audio": "./sample/Tuyên (nam miền Bắc).wav", "text": "./sample/Tuyên (nam miền Bắc).txt"},
    "Vĩnh (nam miền Nam)": {"audio": "./sample/Vĩnh (nam miền Nam).wav", "text": "./sample/Vĩnh (nam miền Nam).txt"},
    "Bình (nam miền Bắc)": {"audio": "./sample/Bình (nam miền Bắc).wav", "text": "./sample/Bình (nam miền Bắc).txt"},
    "Nguyên (nam miền Nam)": {"audio": "./sample/Nguyên (nam miền Nam).wav", "text": "./sample/Nguyên (nam miền Nam).txt"},
    "Sơn (nam miền Nam)": {"audio": "./sample/Sơn (nam miền Nam).wav", "text": "./sample/Sơn (nam miền Nam).txt"},
    "Đoan (nữ miền Nam)": {"audio": "./sample/Đoan (nữ miền Nam).wav", "text": "./sample/Đoan (nữ miền Nam).txt"},
    "Ngọc (nữ miền Bắc)": {"audio": "./sample/Ngọc (nữ miền Bắc).wav", "text": "./sample/Ngọc (nữ miền Bắc).txt"},
    "Ly (nữ miền Bắc)": {"audio": "./sample/Ly (nữ miền Bắc).wav", "text": "./sample/Ly (nữ miền Bắc).txt"},
    "Dung (nữ miền Nam)": {"audio": "./sample/Dung (nữ miền Nam).wav", "text": "./sample/Dung (nữ miền Nam).txt"},
    "Nhỏ Ngọt Ngào": {"audio": "./sample/Nhỏ Ngọt Ngào.wav", "text": "./sample/Nhỏ Ngọt Ngào.txt"},
    
    # Thêm giọng mới ở đây:
    # "Tên Giọng": {"audio": "./sample/Tên_Giọng.wav", "text": "./sample/Tên_Giọng.txt"},
}

# --- 3. HELPER FUNCTIONS ---
def load_reference_info(voice_choice):
    if voice_choice in VOICE_SAMPLES:
        audio_path = VOICE_SAMPLES[voice_choice]["audio"]
        text_path = VOICE_SAMPLES[voice_choice]["text"]
        try:
            if os.path.exists(text_path):
                with open(text_path, "r", encoding="utf-8") as f:
                    ref_text = f.read()
                return audio_path, ref_text
            else:
                return audio_path, "⚠️ Không tìm thấy file text mẫu."
        except Exception as e:
            return None, f"❌ Lỗi: {str(e)}"
    return None, ""

def synthesize_speech(text, voice_choice, custom_audio, custom_text, mode_tab, speed_factor):
    try:
        if not text or text.strip() == "":
            return None, "⚠️ Vui lòng nhập văn bản cần tổng hợp!"
        
        # --- LOGIC CHECK LIMIT 250 ---
        if len(text) > 250:
            return None, f"❌ Văn bản quá dài ({len(text)}/250 ký tự)! Vui lòng cắt ngắn lại để đảm bảo chất lượng."

        # Logic chọn Reference
        if mode_tab == "custom_mode": 
            if custom_audio is None or not custom_text:
                return None, "⚠️ Vui lòng tải lên Audio và nhập nội dung Audio đó."
            ref_audio_path = custom_audio
            ref_text_raw = custom_text
            print("🎨 Mode: Custom Voice")
        else: # Preset
            if voice_choice not in VOICE_SAMPLES:
                 return None, "⚠️ Vui lòng chọn một giọng mẫu."
            ref_audio_path = VOICE_SAMPLES[voice_choice]["audio"]
            ref_text_path = VOICE_SAMPLES[voice_choice]["text"]
            
            if not os.path.exists(ref_audio_path):
                 return None, f"❌ Không tìm thấy file audio: {ref_audio_path}"
                 
            with open(ref_text_path, "r", encoding="utf-8") as f:
                ref_text_raw = f.read()
            print(f"🎤 Mode: Preset Voice ({voice_choice})")

        # Inference & Đo thời gian
        print(f"📝 Text: {text[:50]}...")
        
        start_time = time.time()
        
        # Encode reference (with thread-safe cache + disk cache)
        t1 = time.time()
        cache_key = f"{mode_tab}:{voice_choice}" if mode_tab == "preset_mode" else ref_audio_path
        
        with reference_cache_lock:
            # Check memory cache first
            if cache_key in reference_cache:
                print(f"   ✨ Using memory cache for {cache_key}")
                ref_codes = reference_cache[cache_key]
            else:
                # Check disk cache
                ref_codes = load_cache_from_disk(cache_key)
                if ref_codes is not None:
                    print(f"   💿 Loaded from disk cache for {cache_key}")
                    reference_cache[cache_key] = ref_codes
                else:
                    # Encode mới
                    print(f"   🔄 Encoding reference (first time for {cache_key})...")
                    ref_codes = tts.encode_reference(ref_audio_path)
                    reference_cache[cache_key] = ref_codes
                    save_cache_to_disk(cache_key, ref_codes)
                    print(f"   💾 Saved to disk cache for {cache_key}")
        
        t2 = time.time()
        print(f"   ⏱️ Encode reference: {t2-t1:.2f}s")
        
        # Inference
        t3 = time.time()
        wav = tts.infer(text, ref_codes, ref_text_raw)
        t4 = time.time()
        print(f"   ⏱️ Inference: {t4-t3:.2f}s")
        
        # Apply speed adjustment if needed (using Pydub - simple & clean)
        if speed_factor != 1.0:
            t5 = time.time()
            print(f"   🎚️ Applying speed adjustment: {speed_factor}x")
            print(f"   🔧 Using Pydub (simple & clean)")
            
            # Pydub method - thay đổi frame rate
            with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
                sf.write(tmp.name, wav, 24000)
                tmp_path = tmp.name
            
            sound = AudioSegment.from_wav(tmp_path)
            new_frame_rate = int(sound.frame_rate * speed_factor)
            sound_stretched = sound._spawn(sound.raw_data, overrides={'frame_rate': new_frame_rate})
            sound_stretched = sound_stretched.set_frame_rate(24000)
            
            wav = np.array(sound_stretched.get_array_of_samples()).astype(np.float32) / 32768.0
            if sound_stretched.channels == 2:
                wav = wav.reshape((-1, 2)).mean(axis=1)
            
            os.unlink(tmp_path)
            
            t6 = time.time()
            print(f"   ⏱️ Speed adjustment: {t6-t5:.2f}s")
        
        end_time = time.time()   # <--- Kết thúc bấm giờ
        process_time = end_time - start_time # <--- Tính thời gian xử lý
        
        # Save
        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
            sf.write(tmp_file.name, wav, 24000)
            output_path = tmp_file.name
        
        # <--- Cập nhật thông báo kết quả
        speed_info = f" (Speed: {speed_factor}x)" if speed_factor != 1.0 else ""
        return output_path, f"✅ Thành công! (Mất {process_time:.2f} giây để tạo){speed_info}"

    except Exception as e:
        import traceback
        traceback.print_exc()
        return None, f"❌ Lỗi hệ thống: {str(e)}"

# --- 4. UI SETUP ---
theme = gr.themes.Ocean(
    primary_hue="indigo",
    secondary_hue="cyan",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont('Inter'), 'ui-sans-serif', 'system-ui'],
).set(
    button_primary_background_fill="linear-gradient(90deg, #6366f1 0%, #0ea5e9 100%)",
    button_primary_background_fill_hover="linear-gradient(90deg, #4f46e5 0%, #0284c7 100%)",
    block_shadow="0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)",
)

# <--- CSS ĐÃ SỬA (Background xanh đen + Chữ sáng)
css = """

.container { max-width: 1200px; margin: auto; }

.header-box { 

    text-align: center; 

    margin-bottom: 25px; 

    padding: 25px; 

    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); /* Xanh đen (Slate 900 -> 800) */

    border-radius: 12px; 

    border: 1px solid #334155; 

    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);

}

.header-title { 

    font-size: 2.5rem; 

    font-weight: 800; 

    color: white; /* Chữ trắng */

    background: -webkit-linear-gradient(45deg, #60A5FA, #22D3EE); /* Gradient xanh sáng cho chữ nổi bật */

    -webkit-background-clip: text; 

    -webkit-text-fill-color: transparent; 

    margin-bottom: 10px; 

}

.header-desc {

    font-size: 1.1rem; 

    color: #cbd5e1; /* Màu xám sáng (Slate-300) */

    margin-bottom: 15px;

}

.link-group a { 

    text-decoration: none; 

    margin: 0 10px; 

    font-weight: 600; 

    color: #94a3b8; /* Màu link sáng hơn chút */

    transition: color 0.2s; 

}

.link-group a:hover { color: #38bdf8; text-shadow: 0 0 5px rgba(56, 189, 248, 0.5); }



.status-box { font-weight: bold; text-align: center; border: none; background: transparent; }

.warning-note { 

    background-color: #fff7ed; 

    border-left: 4px solid #f97316; 

    padding: 12px; 

    color: #9a3412; 

    font-size: 0.9rem; 

    border-radius: 4px;

    margin-top: 10px;

    margin-bottom: 10px;

}

"""

EXAMPLES_LIST = [
    # Nam Miền Nam
    ["Về miền Tây không chỉ để ngắm nhìn sông nước hữu tình, mà còn để cảm nhận tấm chân tình của người dân nơi đây. Cùng ngồi xuồng ba lá len lỏi qua rặng dừa nước, nghe câu vọng cổ ngọt ngào thì còn gì bằng.", "Vĩnh (nam miền Nam)"],
    
    # Nam Miền Bắc
    ["Hà Nội những ngày vào thu mang một vẻ đẹp trầm mặc và cổ kính đến lạ thường. Đi dạo quanh Hồ Gươm vào sáng sớm, hít hà mùi hoa sữa nồng nàn và thưởng thức chút cốm làng Vòng là trải nghiệm khó quên.", "Bình (nam miền Bắc)"],
    
    # Nam Miền Bắc
    ["Sự bùng nổ của trí tuệ nhân tạo đang định hình lại cách chúng ta làm việc và sinh sống. Từ xe tự lái đến trợ lý ảo thông minh, công nghệ đang dần xóa nhòa ranh giới giữa thực tại và những bộ phim viễn tưởng.", "Tuyên (nam miền Bắc)"],
    
    # Nam Miền Nam
    ["Sài Gòn hối hả là thế, nhưng chỉ cần tấp vào một quán cà phê ven đường, gọi ly bạc xỉu đá và ngắm nhìn dòng người qua lại, bạn sẽ thấy thành phố này cũng có những khoảng lặng thật bình yên và đáng yêu.", "Nguyên (nam miền Nam)"],
    
    # Nam Miền Nam
    ["Để đảm bảo tiến độ dự án quan trọng này, chúng ta cần tập trung tối đa nguồn lực và phối hợp chặt chẽ giữa các phòng ban. Mọi khó khăn phát sinh cần được báo cáo ngay lập tức để ban lãnh đạo xử lý kịp thời.", "Sơn (nam miền Nam)"],

    # Nữ Miền Nam
    ["Ngày xửa ngày xưa, ở một ngôi làng nọ có cô Tấm xinh đẹp, nết na nhưng sớm mồ côi mẹ. Dù bị mẹ kế và Cám hãm hại đủ đường, Tấm vẫn giữ được tấm lòng lương thiện và cuối cùng tìm được hạnh phúc xứng đáng.", "Đoan (nữ miền Nam)"],
    
    # Nữ Miền Bắc
    ["Dạ em chào anh chị, hiện tại bên em đang có chương trình ưu đãi đặc biệt cho căn hộ hướng sông này. Với thiết kế hiện đại và không gian xanh mát, đây chắc chắn là tổ ấm lý tưởng mà gia đình mình đang tìm kiếm.", "Ly (nữ miền Bắc)"],
    
    # Nữ Miền Bắc
    ["Dưới cơn mưa phùn lất phất của những ngày cuối đông, em khẽ nép vào vai anh, cảm nhận hơi ấm lan tỏa. Những khoảnh khắc bình dị như thế này khiến em nhận ra rằng, hạnh phúc đôi khi chỉ đơn giản là được ở bên nhau.", "Ngọc (nữ miền Bắc)"],
]

with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS Studio") as demo:
    
    with gr.Column(elem_classes="container"):
        # Header - Cập nhật class cho HTML
        gr.HTML("""

            <div class="header-box">

                <div class="header-title">🎙️ VieNeu-TTS Studio</div>

                <div class="header-desc">

                    Phiên bản: VieNeu-TTS-1000h (model mới nhất, train trên 1000 giờ dữ liệu)

                </div>

                <div class="link-group">

                    <a href="https://huggingface.co/pnnbao-ump/VieNeu-TTS" target="_blank">🤗 Model Card</a> • 

                    <a href="https://huggingface.co/datasets/pnnbao-ump/VieNeu-TTS-1000h" target="_blank">📖 Dataset 1000h</a> • 

                    <a href="https://github.com/pnnbao97/VieNeu-TTS" target="_blank">🦜 GitHub</a>

                </div>

            </div>

        """)
    
    with gr.Row(elem_classes="container", equal_height=False):
        
        # --- LEFT: INPUT ---
        with gr.Column(scale=3, variant="panel"):
            gr.Markdown("### 📝 Văn bản đầu vào")
            text_input = gr.Textbox(
                label="Nhập văn bản",
                placeholder="Nhập nội dung tiếng Việt cần chuyển thành giọng nói...",
                lines=4,
                value="Sự bùng nổ của trí tuệ nhân tạo đang định hình lại cách chúng ta làm việc và sinh sống. Từ xe tự lái đến trợ lý ảo thông minh, công nghệ đang dần xóa nhòa ranh giới giữa thực tại và những bộ phim viễn tưởng.",
                show_label=False
            )
            
            # Counter + Warning
            with gr.Row():
                char_count = gr.HTML("<div style='text-align: right; color: #64748B; font-size: 0.8rem;'>0 / 250 ký tự</div>")
            
            gr.Markdown("### 🗣️ Chọn giọng đọc")
            with gr.Tabs() as tabs:
                with gr.TabItem("👤 Giọng có sẵn (Preset)", id="preset_mode"):
                    voice_select = gr.Dropdown(
                        choices=list(VOICE_SAMPLES.keys()),
                        value="Tuyên (nam miền Bắc)",
                        label="Danh sách giọng",
                        interactive=True
                    )
                    with gr.Accordion("Thông tin giọng mẫu", open=False):
                        ref_audio_preview = gr.Audio(label="Audio mẫu", interactive=False, type="filepath")
                        ref_text_preview = gr.Markdown("...")

                with gr.TabItem("🎙️ Giọng tùy chỉnh (Custom)", id="custom_mode"):
                    gr.Markdown("Tải lên giọng của bạn (Zero-shot Cloning)")
                    custom_audio = gr.Audio(label="File ghi âm (.wav)", type="filepath")
                    custom_text = gr.Textbox(label="Nội dung ghi âm", placeholder="Nhập chính xác lời thoại...")

            current_mode = gr.Textbox(visible=False, value="preset_mode")
            
            gr.Markdown("### ⚙️ Cài đặt nâng cao")
            speed_slider = gr.Slider(
                minimum=0.5,
                maximum=2.0,
                value=1.0,
                step=0.1,
                label="Tốc độ giọng nói (Speed)",
                info="0.5x = chậm, 1.0x = bình thường, 2.0x = nhanh"
            )
            
            btn_generate = gr.Button("Tổng hợp giọng nói", variant="primary", size="lg")

        # --- RIGHT: OUTPUT ---
        with gr.Column(scale=2):
            gr.Markdown("### 🎧 Kết quả")
            with gr.Group():
                audio_output = gr.Audio(label="Audio đầu ra", type="filepath", show_download_button=True, autoplay=True)
                status_output = gr.Textbox(label="Trạng thái", show_label=False, elem_classes="status-box", placeholder="Sẵn sàng...")

    # --- EXAMPLES ---
    with gr.Row(elem_classes="container"):
        with gr.Column():
            gr.Markdown("### 📚 Ví dụ mẫu")
            gr.Examples(examples=EXAMPLES_LIST, inputs=[text_input, voice_select], label="Thử nghiệm nhanh")

    # --- LOGIC ---
    def update_count(text):
        l = len(text)
        if l > 250:
            color = "#dc2626" # Red
            msg = f"⚠️ <b>{l} / 250</b> - Quá giới hạn!"
        elif l > 200:
            color = "#ea580c" # Orange
            msg = f"{l} / 250"
        else:
            color = "#64748B" # Gray
            msg = f"{l} / 250 ký tự"
        return f"<div style='text-align: right; color: {color}; font-size: 0.8rem; font-weight: bold'>{msg}</div>"

    text_input.change(update_count, text_input, char_count)

    def update_ref_preview(voice):
        audio, text = load_reference_info(voice)
        return audio, f"> *\"{text}\"*"
    
    voice_select.change(update_ref_preview, voice_select, [ref_audio_preview, ref_text_preview])
    demo.load(update_ref_preview, voice_select, [ref_audio_preview, ref_text_preview])

    # Tab handling - FIXED WITH *ARGS
    tab_preset = tabs.children[0]
    tab_custom = tabs.children[1]
    
    # Dùng *args để nhận bất kỳ số lượng tham số nào (0 hoặc 1), tránh lỗi Warning
    tab_preset.select(fn=lambda *args: "preset_mode", inputs=None, outputs=current_mode)
    tab_custom.select(fn=lambda *args: "custom_mode", inputs=None, outputs=current_mode)

    btn_generate.click(
        fn=synthesize_speech,
        inputs=[text_input, voice_select, custom_audio, custom_text, current_mode, speed_slider],
        outputs=[audio_output, status_output]
    )

if __name__ == "__main__":
    demo.queue(default_concurrency_limit=20).launch(
        server_name="127.0.0.1", 
        server_port=7860, 
        share=False
    )