Upload text_to_speech.py
Browse files- VietTTS/text_to_speech.py +47 -0
VietTTS/text_to_speech.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import base64
|
| 4 |
+
from tts import TTS
|
| 5 |
+
from utils.file_utils import load_prompt_speech_from_file, load_voices
|
| 6 |
+
from app.config import settings
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def load_model(
|
| 11 |
+
speed,
|
| 12 |
+
voice,
|
| 13 |
+
text,
|
| 14 |
+
output_path,
|
| 15 |
+
output_format="wav",
|
| 16 |
+
):
|
| 17 |
+
print("Loading TTS model...", settings.DIR_ROOT)
|
| 18 |
+
tts_obj = TTS(model_dir=os.path.join(settings.DIR_ROOT, "VietTTS", "models"))
|
| 19 |
+
VOICE_MAP = load_voices(os.path.join(settings.DIR_ROOT, "VietTTS", "samples"))
|
| 20 |
+
|
| 21 |
+
speed = float(speed)
|
| 22 |
+
|
| 23 |
+
if voice.isdigit():
|
| 24 |
+
voice_file = list(VOICE_MAP.values())[int(voice)]
|
| 25 |
+
else:
|
| 26 |
+
voice_file = VOICE_MAP.get(voice)
|
| 27 |
+
|
| 28 |
+
if not voice_file or not os.path.exists(voice_file):
|
| 29 |
+
raise ValueError("Voice file not found")
|
| 30 |
+
|
| 31 |
+
print(f"Output path: {output_path}")
|
| 32 |
+
|
| 33 |
+
prompt_speech_16k = load_prompt_speech_from_file(filepath=voice_file, min_duration=3, max_duration=10)
|
| 34 |
+
|
| 35 |
+
tts_obj.tts_to_file(text=text, prompt_speech_16k=prompt_speech_16k, output_path=output_path, speed=speed)
|
| 36 |
+
|
| 37 |
+
print("END TTS worker")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
speed = sys.argv[1]
|
| 42 |
+
voice = sys.argv[2]
|
| 43 |
+
text = sys.argv[3]
|
| 44 |
+
output_path = sys.argv[4]
|
| 45 |
+
output_format = sys.argv[5] if len(sys.argv) > 5 else "wav"
|
| 46 |
+
|
| 47 |
+
load_model(speed, voice, text, output_path, output_format)
|