Files
viralfactory/src/engines/TTSEngine/CoquiTTSEngine.py

149 lines
4.2 KiB
Python
Raw Normal View History

2024-02-13 14:15:27 +01:00
import gradio as gr
2024-02-14 17:49:51 +01:00
2024-02-15 14:11:16 +01:00
import TTS
2024-02-13 14:15:27 +01:00
import os
2024-02-14 17:49:51 +01:00
2024-02-15 14:11:16 +01:00
import torch
2024-02-13 14:15:27 +01:00
from .BaseTTSEngine import BaseTTSEngine
class CoquiTTSEngine(BaseTTSEngine):
voices = [
"Claribel Dervla",
"Daisy Studious",
"Gracie Wise",
"Tammie Ema",
"Alison Dietlinde",
"Ana Florence",
"Annmarie Nele",
"Asya Anara",
"Brenda Stern",
"Gitta Nikolina",
"Henriette Usha",
"Sofia Hellen",
"Tammy Grit",
"Tanja Adelina",
"Vjollca Johnnie",
"Andrew Chipper",
"Badr Odhiambo",
"Dionisio Schuyler",
"Royston Min",
"Viktor Eka",
"Abrahan Mack",
"Adde Michal",
"Baldur Sanjin",
"Craig Gutsy",
"Damien Black",
"Gilberto Mathias",
"Ilkin Urbano",
"Kazuhiko Atallah",
"Ludvig Milivoj",
"Suad Qasim",
"Torcull Diarmuid",
"Viktor Menelaos",
"Zacharie Aimilios",
"Nova Hogarth",
"Maja Ruoho",
"Uta Obando",
"Lidiya Szekeres",
"Chandra MacFarland",
"Szofi Granger",
"Camilla Holmström",
"Lilya Stainthorpe",
"Zofija Kendrick",
"Narelle Moon",
"Barbora MacLean",
"Alexandra Hisakawa",
"Alma María",
"Rosemary Okafor",
"Ige Behringer",
"Filip Traverse",
"Damjan Chapman",
"Wulf Carlevaro",
"Aaron Dreschner",
"Kumar Dahl",
"Eugenio Mataracı",
"Ferran Simen",
"Xavier Hayasaka",
"Luis Moray",
"Marcos Rudaski",
]
name = "Coqui TTS"
description = "Coqui TTS engine."
languages = [
"en", # English
"es", # Spanish
"fr", # French
"de", # German
"it", # Italian
"pt", # Portuguese
"pl", # Polish
"tr", # Turkish
"ru", # Russian
"nl", # Dutch
"cs", # Czech
"ar", # Arabic
"zh-cn", # Chinese (Simplified)
"ja", # Japanese
"hu", # Hungarian
"ko", # Korean
"hi", # Hindi
]
num_options = 4
2024-02-13 14:29:49 +01:00
def __init__(self, options: list):
2024-02-13 14:15:27 +01:00
super().__init__()
2024-02-13 14:29:49 +01:00
self.voice = options[0][0]
self.language = options[1][0]
self.to_force_duration = options[2][0]
self.duration = options[3]
2024-02-13 14:29:49 +01:00
2024-02-13 14:15:27 +01:00
os.environ["COQUI_TOS_AGREED"] = "1"
2024-02-14 17:49:51 +01:00
2024-02-15 14:11:16 +01:00
self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
device = "cuda" if torch.cuda.is_available() else "cpu"
self.tts.to(device)
2024-02-13 14:15:27 +01:00
2024-02-15 14:11:16 +01:00
def synthesize(self, text: str, path: str):
"""
Synthesizes the given text into speech and saves it to the specified file path.
Args:
text (str): The text to synthesize into speech.
path (str): The file path to save the synthesized speech.
Returns:
float: The time taken to synthesize the speech with whispering effect.
"""
self.tts.tts_to_file(text=text, file_path=path, lang=self.language, speaker=self.voice)
if self.to_force_duration:
self.force_duration(float(self.duration), path)
return self.time_with_whisper(path)
2024-02-14 17:49:51 +01:00
@classmethod
def get_options(cls) -> list:
options = [
2024-02-14 17:49:51 +01:00
gr.Dropdown(
label="Voice",
choices=cls.voices,
max_choices=1,
value=cls.voices[0],
),
gr.Dropdown(
label="Language",
choices=cls.languages,
max_choices=1,
value=cls.languages[0],
),
]
2024-02-15 14:11:16 +01:00
duration_checkbox = gr.Checkbox(label="Force duration", info="Force the duration of the generated audio to be at most the specified value", value=False)
duration = gr.Number(label="Duration [s]", value=57, step=1, minimum=10, visible=False)
duration_switch = lambda x: gr.update(visible=x)
duration_checkbox.change(duration_switch, inputs=[duration_checkbox], outputs=[duration])
2024-02-15 14:11:16 +01:00
options.append(duration_checkbox)
options.append(duration)
return options