Update tts.py
Browse files
tts.py
CHANGED
|
@@ -12,7 +12,8 @@ checkpoint_dir = "model/"
|
|
| 12 |
repo_id = "capleaf/viXTTS"
|
| 13 |
use_deepspeed = False
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
# Tạo thư mục nếu chưa tồn tại
|
| 18 |
os.makedirs(checkpoint_dir, exist_ok=True)
|
|
@@ -69,10 +70,8 @@ def generate_speech(text, language="vi", speaker_wav=None, normalize_text=True):
|
|
| 69 |
"""
|
| 70 |
if language not in supported_languages:
|
| 71 |
raise ValueError(f"Ngôn ngữ {language} không được hỗ trợ. Chỉ hỗ trợ tiếng Việt (vi) và tiếng Anh (en).")
|
| 72 |
-
|
| 73 |
if len(text) < 2:
|
| 74 |
raise ValueError("Văn bản quá ngắn. Vui lòng nhập văn bản dài hơn.")
|
| 75 |
-
|
| 76 |
try:
|
| 77 |
# Chuẩn hóa văn bản nếu cần
|
| 78 |
if normalize_text and language == "vi":
|
|
@@ -86,7 +85,6 @@ def generate_speech(text, language="vi", speaker_wav=None, normalize_text=True):
|
|
| 86 |
gpt_cond_chunk_len=8 if device == "cuda" else 4,
|
| 87 |
max_ref_length=60 if device == "cuda" else 30,
|
| 88 |
)
|
| 89 |
-
|
| 90 |
# Tạo giọng nói
|
| 91 |
out = MODEL.inference(
|
| 92 |
text,
|
|
@@ -101,8 +99,6 @@ def generate_speech(text, language="vi", speaker_wav=None, normalize_text=True):
|
|
| 101 |
# Lưu file âm thanh
|
| 102 |
output_file = "output.wav"
|
| 103 |
torchaudio.save(output_file, torch.tensor(out["wav"]).unsqueeze(0).to("cpu"), 24000)
|
| 104 |
-
|
| 105 |
return output_file
|
| 106 |
-
|
| 107 |
except Exception as e:
|
| 108 |
-
raise RuntimeError(f"Lỗi khi tạo giọng nói: {str(e)}")
|
|
|
|
| 12 |
repo_id = "capleaf/viXTTS"
|
| 13 |
use_deepspeed = False
|
| 14 |
|
| 15 |
+
# Kiểm tra xem GPU có sẵn không và sử dụng A100 nếu có
|
| 16 |
+
device = "cuda" if torch.cuda.is_available() and "A100" in torch.cuda.get_device_name(0) else "cpu"
|
| 17 |
|
| 18 |
# Tạo thư mục nếu chưa tồn tại
|
| 19 |
os.makedirs(checkpoint_dir, exist_ok=True)
|
|
|
|
| 70 |
"""
|
| 71 |
if language not in supported_languages:
|
| 72 |
raise ValueError(f"Ngôn ngữ {language} không được hỗ trợ. Chỉ hỗ trợ tiếng Việt (vi) và tiếng Anh (en).")
|
|
|
|
| 73 |
if len(text) < 2:
|
| 74 |
raise ValueError("Văn bản quá ngắn. Vui lòng nhập văn bản dài hơn.")
|
|
|
|
| 75 |
try:
|
| 76 |
# Chuẩn hóa văn bản nếu cần
|
| 77 |
if normalize_text and language == "vi":
|
|
|
|
| 85 |
gpt_cond_chunk_len=8 if device == "cuda" else 4,
|
| 86 |
max_ref_length=60 if device == "cuda" else 30,
|
| 87 |
)
|
|
|
|
| 88 |
# Tạo giọng nói
|
| 89 |
out = MODEL.inference(
|
| 90 |
text,
|
|
|
|
| 99 |
# Lưu file âm thanh
|
| 100 |
output_file = "output.wav"
|
| 101 |
torchaudio.save(output_file, torch.tensor(out["wav"]).unsqueeze(0).to("cpu"), 24000)
|
|
|
|
| 102 |
return output_file
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
+
raise RuntimeError(f"Lỗi khi tạo giọng nói: {str(e)}")
|