File size: 2,576 Bytes
d5ed1ca f6bf67e d5ed1ca ee9699f d5ed1ca f6bf67e d5ed1ca |
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 |
# -*- coding: utf-8 -*-
import gradio as gr
from models import SynthesizerTrn
from khmer_phonemizer import phonemize_single
import utils
import commons
import torch
import khmernormalizer
_pad = "_"
_punctuation = ". "
_letters_ipa = "acefhijklmnoprstuwzΔΕΕΕΙΙΙΙΙΙΙ‘Ι¨Ι²ΚΚΚ°Λ"
# Export all symbols:
symbols = [_pad] + list(_punctuation) + list(_letters_ipa)
# Special symbol ids
SPACE_ID = symbols.index(" ")
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
def text_to_sequence(text):
sequence = []
for symbol in text:
symbol_id = _symbol_to_id[symbol]
sequence += [symbol_id]
return sequence
def get_text(text, hps):
text_norm = text_to_sequence(text)
if hps.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
text_norm = torch.LongTensor(text_norm)
return text_norm
hps = utils.get_hparams_from_file("config.json")
net_g = SynthesizerTrn(
len(symbols),
hps.data.filter_length // 2 + 1,
hps.train.segment_size // hps.data.hop_length,
**hps.model
)
_ = net_g.eval()
_ = utils.load_checkpoint("G_60000.pth", net_g, None)
def generate_voice(text):
text = khmernormalizer.normalize(text)
text = " ".join(phonemize_single(text) + ["."])
stn_tst = get_text(text, hps)
with torch.no_grad():
x_tst = stn_tst.unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
audio = (
net_g.infer(
x_tst,
x_tst_lengths,
noise_scale=0.667,
noise_scale_w=0.8,
length_scale=1,
)[0][0, 0]
.data.cpu()
.float()
.numpy()
)
return (hps.data.sampling_rate, audio)
with gr.Blocks(
title="Khmer Word to Speech",
theme=gr.themes.Default(
font=[gr.themes.GoogleFont("Noto Sans Khmer"), "Arial", "sans-serif"]
),
) as blocks:
gr.Markdown("# Khmer Word to Speech")
input_text = gr.Text(label="ααΆαααααααΈ", lines=1)
examples = gr.Examples(examples=["ααα»αααααΆαα·", "αααααααα"], inputs=[input_text])
run_button = gr.Button(value="αααααΎα")
out_audio = gr.Audio(
label="ααα‘αααααααΆααααααΎα",
type="numpy",
)
inputs = [input_text]
outputs = [out_audio]
run_button.click(
fn=generate_voice,
inputs=inputs,
outputs=outputs,
queue=True,
)
blocks.queue(concurrency_count=1).launch(debug=True)
|