File size: 4,886 Bytes
991e874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import commons
import utils
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence
import random
import os
import datetime
import numpy as np


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


def tts(txt, emotion, index, hps, net_g, random_emotion_root):
    """emotion为参考情感音频路径 或random_sample(随机抽取)"""
    stn_tst = get_text(txt, hps)
    with torch.no_grad():
        x_tst = stn_tst.unsqueeze(0)
        x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
        sid = torch.LongTensor([index])  ##appoint character
        if os.path.exists(f"{emotion}.emo.npy"):
            emo = torch.FloatTensor(np.load(f"{emotion}.emo.npy")).unsqueeze(0)
        elif emotion == "random_sample":
            while True:
                rand_wav = random.sample(os.listdir(random_emotion_root), 1)[0]
                if os.path.exists(f"{random_emotion_root}/{rand_wav}"):
                    break
            emo = torch.FloatTensor(np.load(f"{random_emotion_root}/{rand_wav}")).unsqueeze(0)
            print(f"{random_emotion_root}/{rand_wav}")
        else:
            print("emotion参数不正确")

        audio = \
            net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=0.667, noise_scale_w=0.8, length_scale=1, emo=emo)[
                0][
                0, 0].data.float().numpy()
        return audio


def random_generate(txt, index, hps, net_g, random_emotion_root):

    audio = tts(txt, emotion='random_sample', index=index, hps=hps, net_g=net_g,
                random_emotion_root=random_emotion_root)
    return audio


def charaterRoot(name):
    global random_emotion_root
    if name == '九条都':
        random_emotion_root = "./9nineEmo/my"
        index = 0
    elif name == '新海天':
        random_emotion_root = "./9nineEmo/sr"
        index = 1
    elif name == '结城希亚':
        random_emotion_root = "./9nineEmo/na"
        index = 2
    elif name == '蕾娜':
        random_emotion_root = "./9nineEmo/gt"
        index = 3
    elif name == '索菲':
        random_emotion_root = "./9nineEmo/sf"
        index = 4
    return random_emotion_root, index


def configSelect(config):
    global checkPonit, config_file
    if config == 'mul':
        config_file = "./configs/9nine_multi.json"
        checkPonit = "logs/9nineM/G_115600.pth"
    elif config == "single":
        config_file = "./configs/sora.json"
        checkPonit = "logs/sora/G_341200.pth"
    return config_file, checkPonit


def runVits(name, config, txt):
    config_file, checkPoint = configSelect(config)
    random_emotion_root, index = charaterRoot(name=name)
    checkPonit = checkPoint
    hps = utils.get_hparams_from_file(config_file)
    net_g = SynthesizerTrn(
        len(symbols),
        hps.data.filter_length // 2 + 1,
        hps.train.segment_size // hps.data.hop_length,
        n_speakers=hps.data.n_speakers,
        **hps.model)
    _ = net_g.eval()

    _ = utils.load_checkpoint(checkPonit, net_g, None)
    audio = random_generate(txt=txt, index=index, random_emotion_root=random_emotion_root,
                            net_g=net_g, hps=hps)
    return (hps.data.sampling_rate, audio)


def nineMul(name, txt):
    config = 'mul'
    audio = runVits(name, config, txt)
    return "multiple model success", audio


def nineSingle(name,txt):
    config = 'mul'
    # name = "新海天"
    audio = runVits(name, config, txt)
    return "single model success", audio

app = gr.Blocks()
with app:
    with gr.Tabs():
        with gr.TabItem("9nine multiple model"):
            character = gr.Radio(['九条都', '新海天', '结城希亚', '蕾娜', '索菲'], label='character',
                                 info="select character you want")

            text = gr.TextArea(label="input content", value="こんにちは。私わあやちねねです。")

            submit = gr.Button("generate", variant='privite')
            message = gr.Textbox(label="Message")
            audio = gr.Audio(label="output")
            submit.click(nineMul, [character, text], [message, audio])
        with gr.TabItem("9nine single model"):
            character = gr.Radio(['新海天'], label='character',
                                 info="select character you want")

            text = gr.TextArea(label="input content", value="こんにちは。私わあやちねねです。")

            submit = gr.Button("generate", variant='privite')
            message = gr.Textbox(label="Message")
            audio = gr.Audio(label="output")
            submit.click(nineSingle, [character, text], [message, audio])

app.launch(share=True)