Added accentification
Browse files- accentor.py +49 -0
- app.py +18 -5
accentor.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ukrainian_word_stress import Stressifier, StressSymbol
|
| 2 |
+
from ukrainian_accentor_transformer import Accentor
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
accentors = ["model", "vocab", "none"]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def stress_replace_and_shift(stressed: str):
|
| 11 |
+
stressed = stressed.replace(
|
| 12 |
+
StressSymbol.CombiningAcuteAccent, "+"
|
| 13 |
+
)
|
| 14 |
+
new_stressed = ""
|
| 15 |
+
start = 0
|
| 16 |
+
last = 0
|
| 17 |
+
|
| 18 |
+
# shift stress symbol by one "при+віт" -> "пр+ивіт"
|
| 19 |
+
while True:
|
| 20 |
+
plus_position = stressed.find("+", start)
|
| 21 |
+
if plus_position != -1:
|
| 22 |
+
new_stressed += (
|
| 23 |
+
stressed[last : plus_position - 1] + "+" + stressed[plus_position - 1]
|
| 24 |
+
)
|
| 25 |
+
start = plus_position + 1
|
| 26 |
+
last = start
|
| 27 |
+
else:
|
| 28 |
+
new_stressed += stressed[last:]
|
| 29 |
+
break
|
| 30 |
+
return new_stressed
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
stressify = Stressifier(stress_symbol=StressSymbol.CombiningAcuteAccent)
|
| 35 |
+
accentor_transformer = Accentor()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def accentification(sentence: str, mode: str):
|
| 39 |
+
sentence = sentence.replace("+", "")
|
| 40 |
+
sentence = sentence.replace(
|
| 41 |
+
StressSymbol.CombiningAcuteAccent, ""
|
| 42 |
+
)
|
| 43 |
+
if (mode == "vocab"):
|
| 44 |
+
accented_sentence = stressify(sentence)
|
| 45 |
+
elif (mode == "model"):
|
| 46 |
+
accented_sentence = accentor_transformer(sentence)
|
| 47 |
+
else:
|
| 48 |
+
accented_sentence = sentence
|
| 49 |
+
return accented_sentence
|
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from torch import no_grad, package
|
|
| 6 |
import ctypes
|
| 7 |
import gc
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
config = {
|
|
@@ -38,14 +39,18 @@ def init_models():
|
|
| 38 |
return models
|
| 39 |
|
| 40 |
|
| 41 |
-
def tts(text: str, voice: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
synt = models[voice]
|
| 43 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 44 |
with no_grad():
|
| 45 |
-
wav_data = synt.tts(
|
| 46 |
synt.save_wav(wav_data, fp)
|
| 47 |
trim_memory()
|
| 48 |
-
return fp.name
|
| 49 |
|
| 50 |
|
| 51 |
|
|
@@ -58,15 +63,23 @@ iface = gr.Interface(
|
|
| 58 |
inputs=[
|
| 59 |
gr.Textbox(
|
| 60 |
label="Input",
|
| 61 |
-
value="
|
| 62 |
),
|
| 63 |
gr.Radio(
|
| 64 |
label="Voice",
|
| 65 |
choices=voices,
|
| 66 |
value=voices[0],
|
| 67 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
],
|
| 69 |
-
outputs=gr.Audio(label="Output"),
|
| 70 |
title="🇺🇦 - Ukrainian Voices",
|
| 71 |
)
|
| 72 |
|
|
|
|
| 6 |
import ctypes
|
| 7 |
import gc
|
| 8 |
|
| 9 |
+
from accentor import accentification, stress_replace_and_shift, accentors
|
| 10 |
|
| 11 |
|
| 12 |
config = {
|
|
|
|
| 39 |
return models
|
| 40 |
|
| 41 |
|
| 42 |
+
def tts(text: str, voice: str, mode: str):
|
| 43 |
+
# accentor
|
| 44 |
+
accented_text = accentification(text, mode)
|
| 45 |
+
plussed_text = stress_replace_and_shift(accented_text)
|
| 46 |
+
# TTS
|
| 47 |
synt = models[voice]
|
| 48 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 49 |
with no_grad():
|
| 50 |
+
wav_data = synt.tts(plussed_text, **tts_kwargs)
|
| 51 |
synt.save_wav(wav_data, fp)
|
| 52 |
trim_memory()
|
| 53 |
+
return fp.name, accented_text
|
| 54 |
|
| 55 |
|
| 56 |
|
|
|
|
| 63 |
inputs=[
|
| 64 |
gr.Textbox(
|
| 65 |
label="Input",
|
| 66 |
+
value="Кам'янець-Подільський - місто в Хмельницькій області України, центр Кам'янець-Подільської міської об'єднаної територіальної громади і Кам'янець-Подільського району.",
|
| 67 |
),
|
| 68 |
gr.Radio(
|
| 69 |
label="Voice",
|
| 70 |
choices=voices,
|
| 71 |
value=voices[0],
|
| 72 |
),
|
| 73 |
+
gr.Radio(
|
| 74 |
+
label="Accentor",
|
| 75 |
+
choices=accentors,
|
| 76 |
+
value=accentors[0],
|
| 77 |
+
),
|
| 78 |
+
],
|
| 79 |
+
outputs=[
|
| 80 |
+
gr.Audio(label="Output"),
|
| 81 |
+
gr.Textbox(label="Stressed")
|
| 82 |
],
|
|
|
|
| 83 |
title="🇺🇦 - Ukrainian Voices",
|
| 84 |
)
|
| 85 |
|