File size: 4,386 Bytes
eb8c82c
 
b663da0
f9e5028
c49c056
64fcafd
a1d768d
 
 
 
ae58991
a1d768d
 
 
2ffc7e7
 
 
a1d768d
ae58991
 
 
 
 
 
 
 
 
2ffc7e7
 
 
ae58991
 
a1d768d
2ffc7e7
a1d768d
 
 
 
 
8812d27
 
0ea31c1
3502c7a
 
2a3e50c
8812d27
2ffc7e7
57c5ab0
8812d27
f64a87d
0ea31c1
f64a87d
8812d27
0ea31c1
349b2ad
 
8812d27
349b2ad
c49c056
a163565
0ea31c1
c49c056
 
 
a163565
52408fe
c49c056
dbebef0
8812d27
 
 
2ffc7e7
a1d768d
9d153e7
0ea31c1
9d153e7
b75a2aa
eb8c82c
0ea31c1
9e96240
eb8c82c
 
3502c7a
 
2ffc7e7
3502c7a
 
eb8c82c
 
 
a163565
eb8c82c
0ea31c1
eb8c82c
a163565
8812d27
 
0ea31c1
f9e5028
a9c23eb
0ea31c1
9d153e7
eb8c82c
8812d27
a163565
 
8812d27
6449e88
 
 
f9e5028
498ce63
0ea31c1
 
 
 
 
 
498ce63
128f2fd
498ce63
 
0ea31c1
498ce63
128f2fd
498ce63
77f184e
a4249a1
498ce63
128f2fd
77f184e
 
a4249a1
3502c7a
128f2fd
77f184e
 
 
3502c7a
0ea31c1
77f184e
2ffc7e7
eb8c82c
1163e9d
0e41b64
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import tempfile
import gradio as gr
from datetime import datetime
from enum import Enum
from ukrainian_tts.tts import TTS, Stress, Voices
from torch.cuda import is_available
from os import getenv
from data_logger import log_data
from threading import Thread
from queue import Queue
from time import sleep


def check_thread(logging_queue: Queue):
    logging_callback = log_data(
        hf_token=getenv("HF_API_TOKEN"), dataset_name="uk-tts-output", private=True
    )
    while True:
        sleep(60)
        batch = []
        while not logging_queue.empty():
            batch.append(logging_queue.get())

        if len(batch) > 0:
            try:
                logging_callback(batch)
            except:
                print(
                    "Error happened while pushing data to HF. Puttting items back in queue..."
                )
                for item in batch:
                    logging_queue.put(item)


if getenv("HF_API_TOKEN") is not None:
    log_queue = Queue()
    t = Thread(target=check_thread, args=(log_queue,))
    t.start()


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


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


ukr_tts = TTS(device="cuda" if is_available() else "cpu")


def tts(text: str, voice: str, speed: float):
    print("============================")
    print("Original text:", text)
    print("Voice", voice)
    print("Time:", datetime.utcnow())

    voice_mapping = {
        VoiceOption.Tetiana.value: Voices.Tetiana.value,
        VoiceOption.Mykyta.value: Voices.Mykyta.value,
        VoiceOption.Lada.value: Voices.Lada.value,
        VoiceOption.Dmytro.value: Voices.Dmytro.value,
    }

    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

    if getenv("HF_API_TOKEN") is not None:
        log_queue.put(
            [text, speaker_name, Stress.Dictionary.value, speed, str(datetime.utcnow())]
        )

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
        _, text = ukr_tts.tts(text, speaker_name, Stress.Dictionary.value, fp, speed)
        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.Tetiana.value,
        ),
        gr.components.Slider(
            label="Швидкість", minimum=0.5, maximum=2, value=1, step=0.05
        ),
    ],
    outputs=[
        gr.components.Audio(label="Output"),
        gr.components.Textbox(label="Наголошений текст"),
    ],
    title="🤖💬🇺🇦 - ESPNET",
    description="Україномовний🇺🇦 TTS за допомогою ESPNET (щоб вручну поставити наголос, використовуйте + перед голосною)",
    article=article,
    examples=[
        [
            "Привіт, як тебе звати?",
            VoiceOption.Tetiana.value,
            1,
        ],
        [
            "Введіть, будь ласка, св+оє реч+ення.",
            VoiceOption.Dmytro.value,
            1,
        ],
        [
            "Введіть, будь ласка, своє речення.",
            VoiceOption.Dmytro.value,
            1.3,
        ],
        [
            "Введіть, будь ласка, своє речення.",
            VoiceOption.Mykyta.value,
            1,
        ],
        [
            "Введіть, будь ласка, своє речення.",
            VoiceOption.Mykyta.value,
            0.7,
        ],
        [
            "Договір підписано 4 квітня 1949 року.",
            VoiceOption.Lada.value,
            0.9,
        ],
    ],
)
iface.queue(concurrency_count=3)  # for HF specifically
iface.launch()