Model Text-to-Speech (TTS) tiếng Việt
3 vùng miền (Bắc – Trung – Nam), được fine-tune từ kiến trúc
VITS sử dụng framework
Coqui TTS.
1import os
2import json
3import torch
4import IPython.display as ipd
5from TTS.utils.synthesizer import Synthesizer
6
7MODEL_FOLDER = "./model"
8
9model_path = os.path.join(MODEL_FOLDER, "best_model.pth")
10config_path = os.path.join(MODEL_FOLDER, "config.json")
11speakers_file_path = os.path.join(MODEL_FOLDER, "speakers.pth")
12
13if not os.path.exists(model_path):
14 print("LỖI: Không tìm thấy file model!")
15else:
16 try:
17 with open(config_path, 'r', encoding='utf-8') as f:
18 cfg = json.load(f)
19 cfg['speakers_file'] = speakers_file_path
20 if 'model_args' in cfg:
21 cfg['model_args']['speakers_file'] = speakers_file_path
22 with open(config_path, 'w', encoding='utf-8') as f:
23 json.dump(cfg, f, indent=4)
24 print("Đã tự động dọn dẹp các đường dẫn dư thừa từ Kaggle trong file Thiết kế (config.json).")
25 except Exception as e:
26 print(f"Bỏ qua bước dọn dẹp file config do lỗi: {e}")
27
28 use_cuda = torch.cuda.is_available()
29 print(f"\nTrạng thái Card Đồ họa (GPU): {'Hoạt động' if use_cuda else 'KHÔNG CÓ (Sẽ chạy bằng CPU)'}")
30 print("Đang tải...")
31
32 synthesizer = Synthesizer(
33 tts_checkpoint=model_path,
34 tts_config_path=config_path,
35 tts_speakers_file=speakers_file_path,
36 use_cuda=use_cuda
37 )
38
39 available_speakers = synthesizer.tts_model.speaker_manager.speaker_names
40 print(f"\nCác giọng vùng miền của bạn: {available_speakers}")
41
42TEXT_TO_SPEAK = "Xin chào tất cả mọi người đang nghe đoạn ghi âm này."
43
44# Chọn giọng ['@HUE', '@QuangDien', '@speaker']
45SPEAKER_NAME = "@HUE"
46
47if 'synthesizer' not in locals():
48 print("LỖI: Kích hoạt lõi")
49else:
50 target_spk = SPEAKER_NAME if SPEAKER_NAME in available_speakers else available_speakers[0]
51 print(f"Đang ép xung nặn âm thanh với nét giọng: '{target_spk}'...")
52
53 wav = synthesizer.tts(
54 text=TEXT_TO_SPEAK,
55 speaker_name=target_spk,
56 )
57
58 print("Thành công! Ấn nút Play bên dưới để thưởng thức:")
59 ipd.display(ipd.Audio(wav, rate=synthesizer.output_sample_rate))
Model này sử dụng bản Coqui TTS đã được chỉnh sửa so với upstream với các thay đổi sau:
Các file đã patch từ upstream
coqui-ai/TTS và thêm mới:
TTS/tts/configs/vits_config.py
TTS/tts/datasets/dataset.py
TTS/tts/layers/losses.py
TTS/tts/models/vits.py
TTS/tts/utils/text/phonemizers/__init__.py
TTS/utils/audio/numpy_transforms.py
TTS/utils/samplers.py
recipes\vietnamese\vits\train_vits_vi.py
recipes\vietnamese\vits\benchmark_inference.py
recipes\vietnamese\vits\evaluate.py