File size: 2,982 Bytes
d2ce515
4ea20e3
f0dd59b
 
4ea20e3
 
 
 
 
d2ce515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84fd7d5
 
 
d2ce515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84fd7d5
 
d2ce515
 
 
a98d2b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2ce515
 
 
 
 
 
 
 
 
84fd7d5
d2ce515
84fd7d5
d2ce515
84fd7d5
 
 
 
 
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
import os

os.system('mkdir /home/user/app/monotonic_align/monotonic_align')
os.system('cd monotonic_align && python setup.py build_ext --inplace')


os.system("gdown 'https://drive.google.com/uc?id=1q86w74Ygw2hNzYP9cWkeClGT5X25PvBT'")


import matplotlib.pyplot as plt

import os
import json
import math
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader

import commons
import utils
from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence

from scipy.io.wavfile import write

import streamlit as st

def get_vi_audio(text):
    os.system(f"echo {text} | piper --model vi_VN-vivos-x_low --output_file vi_output.wav")


def get_text(text, hps):
    text_norm = text_to_sequence(text, hps.data.text_cleaners)
    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("./configs/ljs_base.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("pretrained_ljs.pth", net_g, None)





st.title("VITS Text-to-Speech Demo")

# Input text box for user to enter text
text_input = st.text_input("Enter text to convert to speech", value="Chào mừng các bạn đến với môn Xử lí tiếng nói")

##### A demo for the input text #####
# Convert the text to the appropriate format (e.g., phoneme or character representation)
stn_tst = get_text(text_input, 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=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.float().numpy()
    
# Use hps.data.sampling_rate for playing the audio
st.text("Before Fine-tuned:")
st.audio(audio, format="audio/wav", sample_rate=hps.data.sampling_rate)

get_vi_audio(text_input)

st.text("After Fine-tuned:")
st.audio("vi_output.wav", format="audio/wav")

##### User's Inference #####

if st.button("Generate Speech"):
    # Convert the text to the appropriate format (e.g., phoneme or character representation)
    stn_tst = get_text(text_input, 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=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.float().numpy()
        
    # Use hps.data.sampling_rate for playing the audio
    st.text("Before Fine-tuned:")
    st.audio(audio, format="audio/wav", sample_rate=hps.data.sampling_rate)

    get_vi_audio(text_input)

    st.text("After Fine-tuned:")
    st.audio("vi_output.wav", format="audio/wav")