tts
Browse files
tts.py
CHANGED
@@ -6,7 +6,7 @@ from huggingface_hub import hf_hub_download
|
|
6 |
from TTS.tts.configs.xtts_config import XttsConfig
|
7 |
from TTS.tts.models.xtts import Xtts
|
8 |
from vinorm import TTSnorm
|
9 |
-
from torch.
|
10 |
|
11 |
# Cấu hình đường dẫn và tải mô hình
|
12 |
checkpoint_dir = "model/"
|
@@ -34,11 +34,14 @@ required_files = ["model.pth", "config.json", "vocab.json", "speakers_xtts.pth"]
|
|
34 |
for file in required_files:
|
35 |
file_path = os.path.join(checkpoint_dir, file)
|
36 |
if not os.path.exists(file_path):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
43 |
# Tải cấu hình và mô hình
|
44 |
xtts_config = os.path.join(checkpoint_dir, "config.json")
|
@@ -52,30 +55,44 @@ MODEL.to(device)
|
|
52 |
supported_languages = ["vi", "en"]
|
53 |
|
54 |
def normalize_vietnamese_text(text):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
-
def generate_speech(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
if language not in supported_languages:
|
71 |
-
raise ValueError(f"Ngôn ngữ {language} không được hỗ trợ.")
|
72 |
if len(text) < 2:
|
73 |
raise ValueError("Văn bản quá ngắn.")
|
|
|
|
|
|
|
74 |
try:
|
75 |
if normalize_text and language == "vi":
|
76 |
text = normalize_vietnamese_text(text)
|
|
|
77 |
with torch.no_grad():
|
78 |
-
with autocast(enabled=use_fp16):
|
79 |
gpt_cond_latent, speaker_embedding = MODEL.get_conditioning_latents(
|
80 |
audio_path=speaker_wav,
|
81 |
gpt_cond_len=30 if device == "cuda" else 15,
|
@@ -87,12 +104,19 @@ def generate_speech(text, language="vi", speaker_wav=None, normalize_text=True):
|
|
87 |
language,
|
88 |
gpt_cond_latent,
|
89 |
speaker_embedding,
|
90 |
-
repetition_penalty=
|
91 |
-
temperature=
|
92 |
enable_text_splitting=True,
|
93 |
)
|
94 |
-
|
|
|
|
|
|
|
95 |
torchaudio.save(output_file, torch.tensor(out["wav"]).unsqueeze(0).to("cpu"), 24000)
|
|
|
|
|
|
|
|
|
96 |
return output_file
|
97 |
except Exception as e:
|
98 |
raise RuntimeError(f"Lỗi khi tạo giọng nói: {str(e)}")
|
|
|
6 |
from TTS.tts.configs.xtts_config import XttsConfig
|
7 |
from TTS.tts.models.xtts import Xtts
|
8 |
from vinorm import TTSnorm
|
9 |
+
from torch.amp import autocast
|
10 |
|
11 |
# Cấu hình đường dẫn và tải mô hình
|
12 |
checkpoint_dir = "model/"
|
|
|
34 |
for file in required_files:
|
35 |
file_path = os.path.join(checkpoint_dir, file)
|
36 |
if not os.path.exists(file_path):
|
37 |
+
try:
|
38 |
+
hf_hub_download(
|
39 |
+
repo_id=repo_id if file != "speakers_xtts.pth" else "coqui/XTTS-v2",
|
40 |
+
filename=file,
|
41 |
+
local_dir=checkpoint_dir,
|
42 |
+
)
|
43 |
+
except Exception as e:
|
44 |
+
raise RuntimeError(f"Không thể tải file {file} từ Hugging Face Hub: {str(e)}")
|
45 |
|
46 |
# Tải cấu hình và mô hình
|
47 |
xtts_config = os.path.join(checkpoint_dir, "config.json")
|
|
|
55 |
supported_languages = ["vi", "en"]
|
56 |
|
57 |
def normalize_vietnamese_text(text):
|
58 |
+
try:
|
59 |
+
text = (
|
60 |
+
TTSnorm(text, unknown=False, lower=False, rule=True)
|
61 |
+
.replace("..", ".")
|
62 |
+
.replace("!.", "!")
|
63 |
+
.replace("?.", "?")
|
64 |
+
.replace(" .", ".")
|
65 |
+
.replace(" ,", ",")
|
66 |
+
.replace('"', "")
|
67 |
+
.replace("'", "")
|
68 |
+
.replace("AI", "Ây Ai")
|
69 |
+
.replace("A.I", "Ây Ai")
|
70 |
+
)
|
71 |
+
return text
|
72 |
+
except Exception as e:
|
73 |
+
raise RuntimeError(f"Lỗi khi chuẩn hóa văn bản: {str(e)}")
|
74 |
|
75 |
+
def generate_speech(
|
76 |
+
text,
|
77 |
+
language="vi",
|
78 |
+
speaker_wav=None,
|
79 |
+
normalize_text=True,
|
80 |
+
repetition_penalty=5.0,
|
81 |
+
temperature=0.75,
|
82 |
+
):
|
83 |
if language not in supported_languages:
|
84 |
+
raise ValueError(f"Ngôn ngữ {language} không được hỗ trợ. Các ngôn ngữ được hỗ trợ: {', '.join(supported_languages)}")
|
85 |
if len(text) < 2:
|
86 |
raise ValueError("Văn bản quá ngắn.")
|
87 |
+
if speaker_wav and not os.path.isfile(speaker_wav):
|
88 |
+
raise ValueError(f"File speaker_wav không tồn tại: {speaker_wav}")
|
89 |
+
|
90 |
try:
|
91 |
if normalize_text and language == "vi":
|
92 |
text = normalize_vietnamese_text(text)
|
93 |
+
|
94 |
with torch.no_grad():
|
95 |
+
with autocast(device_type='cuda', enabled=use_fp16):
|
96 |
gpt_cond_latent, speaker_embedding = MODEL.get_conditioning_latents(
|
97 |
audio_path=speaker_wav,
|
98 |
gpt_cond_len=30 if device == "cuda" else 15,
|
|
|
104 |
language,
|
105 |
gpt_cond_latent,
|
106 |
speaker_embedding,
|
107 |
+
repetition_penalty=repetition_penalty,
|
108 |
+
temperature=temperature,
|
109 |
enable_text_splitting=True,
|
110 |
)
|
111 |
+
|
112 |
+
output_dir = "outputs/"
|
113 |
+
os.makedirs(output_dir, exist_ok=True)
|
114 |
+
output_file = os.path.join(output_dir, f"output_{os.urandom(4).hex()}.wav")
|
115 |
torchaudio.save(output_file, torch.tensor(out["wav"]).unsqueeze(0).to("cpu"), 24000)
|
116 |
+
|
117 |
+
if device == "cuda":
|
118 |
+
torch.cuda.empty_cache()
|
119 |
+
|
120 |
return output_file
|
121 |
except Exception as e:
|
122 |
raise RuntimeError(f"Lỗi khi tạo giọng nói: {str(e)}")
|