File size: 3,904 Bytes
eb8c82c
 
b663da0
f9e5028
8c4d22a
64fcafd
8812d27
f9e5028
06a4abd
 
eb8c82c
49fc4a4
8812d27
3502c7a
 
 
2a3e50c
a163565
8812d27
57c5ab0
8812d27
 
 
 
f64a87d
57c5ab0
f64a87d
8812d27
 
349b2ad
 
8812d27
349b2ad
 
12ee3cc
 
 
a163565
 
 
 
 
 
 
 
8c4d22a
dbebef0
8812d27
 
 
8c4d22a
b75a2aa
eb8c82c
8c4d22a
9e96240
eb8c82c
 
3502c7a
 
 
 
 
eb8c82c
 
 
a163565
eb8c82c
a163565
eb8c82c
a163565
8812d27
 
a163565
8812d27
a163565
8812d27
f9e5028
a163565
f9e5028
eb8c82c
8812d27
a163565
 
8812d27
49fc4a4
a4249a1
3502c7a
f9e5028
77f184e
a4249a1
3502c7a
a4249a1
77f184e
 
a4249a1
3502c7a
a4249a1
77f184e
 
a4249a1
a163565
a4249a1
77f184e
 
 
a163565
77f184e
 
 
 
3502c7a
77f184e
 
8812d27
eb8c82c
3502c7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import tempfile
import gradio as gr
from datetime import datetime
from enum import Enum
from ukrainian_tts.tts import TTS
from torch.cuda import is_available

class StressOption(Enum):
    AutomaticStress = "Автоматичні наголоси (за словником) 📖"
    AutomaticStressWithModel = "Автоматичні наголоси (за допомогою моделі) 🧮"


class VoiceOption(Enum):
    Olena = "Олена (жіночий) 👩"
    Mykyta = "Микита (чоловічий) 👨"
    Lada = "Лада (жіночий) 👩"
    Dmytro = "Дмитро (чоловічий) 👨"
    Olga = "Ольга (жіночий) 👩"

print(f"CUDA available? {is_available()}")

badge = (
    "https://visitor-badge-reloaded.herokuapp.com/badge?page_id=robinhad.ukrainian-tts"
)

ukr_tts = TTS(use_cuda=is_available())


def tts(text: str, voice: str, stress: str):
    print("============================")
    print("Original text:", text)
    print("Voice", voice)
    print("Stress:", stress)
    print("Time:", datetime.utcnow())
    autostress_with_model = (
        True if stress == StressOption.AutomaticStressWithModel.value else False
    )
    voice_mapping = {
        VoiceOption.Olena.value: "olena",
        VoiceOption.Mykyta.value: "mykyta",
        VoiceOption.Lada.value: "lada",
        VoiceOption.Dmytro.value: "dmytro",
        VoiceOption.Olga.value: "olga",
    }
    speaker_name = voice_mapping[voice]

    text_limit = 7200
    text = (
        text if len(text) < text_limit else text[0:text_limit]
    )  # mitigate crashes on hf space
    

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
        ukr_tts.tts(text, speaker_name, autostress_with_model, fp)
        return fp.name, text


with open("README.md") as file:
    article = file.read()
    article = article[article.find("---\n", 4) + 5::]


iface = gr.Interface(
    fn=tts,
    inputs=[
        gr.components.Textbox(
            label="Input",
            value="Введіть, будь ласка, своє р+ечення.",
        ),
        gr.components.Radio(
            label="Голос",
            choices=[option.value for option in VoiceOption],
            value=VoiceOption.Olena.value,
        ),
        gr.components.Radio(
            label="Наголоси",
            choices=[option.value for option in StressOption],
            value=StressOption.AutomaticStress.value
        ),
    ],
    outputs=[
        gr.components.Audio(label="Output"),
        gr.components.Textbox(label="Наголошений текст"),
    ],
    title="🐸💬🇺🇦 - Coqui TTS",
    description="Україномовний🇺🇦 TTS за допомогою Coqui TTS (щоб вручну поставити наголос, використовуйте + перед голосною)",
    article=article + f'\n  <center><img src="{badge}" alt="visitors badge"/></center>',
    examples=[
        [
            "Введіть, будь ласка, своє речення.",
            VoiceOption.Olena.value,
            StressOption.AutomaticStress.value,
        ],
        [
            "Введіть, будь ласка, своє речення.",
            VoiceOption.Mykyta.value,
            StressOption.AutomaticStress.value,
        ],
        [
            "Вв+едіть, будь ласка, св+оє реч+ення.",
            VoiceOption.Dmytro.value,
            StressOption.AutomaticStress.value,
        ],
        [
            "Привіт, як тебе звати?",
            VoiceOption.Olga.value,
            StressOption.AutomaticStress.value,
        ],
        [
            "Договір підписано 4 квітня 1949 року.",
            VoiceOption.Lada.value,
            StressOption.AutomaticStress.value,
        ],
    ],
)
iface.launch(enable_queue=True)