File size: 17,072 Bytes
506e073
73140b0
 
 
b522165
73140b0
 
 
506e073
 
 
 
 
 
 
a36f6e8
73140b0
 
f222b7d
 
b522165
 
 
 
612b65a
b522165
 
 
 
 
 
 
 
612b65a
 
 
b522165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73140b0
 
 
 
b522165
 
73140b0
 
 
 
 
 
b522165
 
 
 
 
e58b2ee
b522165
 
 
f222b7d
b522165
 
 
 
 
 
 
3e7715d
b522165
 
 
 
 
 
 
f222b7d
b522165
 
 
f222b7d
b522165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612b65a
b522165
 
 
612b65a
b522165
 
f222b7d
612b65a
b522165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73140b0
 
 
 
b522165
73140b0
 
 
 
506e073
 
 
73140b0
 
b522165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4ed48a
 
b522165
 
 
 
 
 
d4ed48a
 
b522165
 
c25a639
d4ed48a
 
b522165
d4ed48a
b522165
 
 
 
 
 
 
d4ed48a
 
b522165
3604243
b522165
 
 
73140b0
b522165
73140b0
b522165
 
 
 
73140b0
 
 
b522165
 
 
 
 
 
 
 
 
 
 
3e7715d
b522165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import logging
logging.getLogger('numba').setLevel(logging.WARNING)
logging.getLogger('matplotlib').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
import romajitable
import re
import numpy as np
import IPython.display as ipd
import torch
import commons
import utils
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence
import gradio as gr
import time
import datetime
import os
import librosa
from mel_processing import spectrogram_torch
class VitsGradio:
    def __init__(self):
        self.dev = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.lan = ["中文","日文","自动","手动"]
        self.idols = ["chinese1","chinese2","chinese3","高咲侑","歩夢","かすみ","しずく","果林","愛","彼方","せつ菜","璃奈","栞子","エマ","ランジュ","ミア","華恋","まひる","なな","クロディーヌ","ひかり",'純那',"香子","真矢","双葉","ミチル","メイファン","やちよ","晶","いちえ","ゆゆ子","塁","珠緒","あるる","ララフィン","美空","静羽","あるる"]
        self.modelPaths = []
        for root,dirs,files in os.walk("checkpoints"):
            for dir in dirs:
                self.modelPaths.append(dir)
        with gr.Blocks() as self.Vits:
            gr.Markdown(
            "## <center> Lovelive虹团中日双语VITS\n"
            "### <center> 请不要生成会对个人以及企划造成侵害的内容\n"
            "<div align='center'>目前有虹团标贝普通话版(biaobei),虹团模型(default),少歌模型(ShojoKageki)以及混合模型(tmp)</div>"
            '<div align="center"><a>参数说明:默认参数适合汉语普通话,合成日语时建议将噪声比例调节至0.667,噪声偏差对应着每个字之间的间隔,对普通话影响较大,duration代表整体语速</div>'
            '<div align="center"><a>合成前请先选择模型,建议选择tmp模型,否则第一次合成不一定成功。长段落/小说合成建议colab或本地运行</div>')
            with gr.Tab("TTS合成"):
                with gr.Row():
                    with gr.Column():
                        with gr.Row():
                            with gr.Column():
                                input1 = gr.TextArea(label="Text", value="为什么你会那么熟练啊?你和雪菜亲过多少次了")
                                input2 = gr.Dropdown(label="Language", choices=self.lan, value="自动", interactive=True)
                                input3 = gr.Dropdown(label="Speaker", choices=self.idols, value="歩夢", interactive=True)
                                btnVC = gr.Button("Submit")
                            with gr.Column():
                                input4 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声比例(noise scale),以控制情感", value=0.267)
                                input5 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声偏差(noise scale w),以控制音素长短", value=0.7)
                                input6 = gr.Slider(minimum=0.1, maximum=10, label="duration", value=1)
                                output1 = gr.Audio(label="采样率22050")
                btnVC.click(self.infer, inputs=[input1, input2, input3, input4, input5, input6], outputs=[output1])
            with gr.Tab("选择模型"):
                with gr.Column():
                    modelstrs = gr.Dropdown(label = "模型", choices = self.modelPaths, value = self.modelPaths[0], type = "value")
                    btnMod = gr.Button("载入模型")
                    statusa = gr.TextArea()
                    btnMod.click(self.loadCk, inputs=[modelstrs], outputs = [statusa])
            with gr.Tab("Voice Conversion"):
                gr.Markdown("""
                                录制或上传声音,并选择要转换的音色。
                """)
                with gr.Column():
                    record_audio = gr.Audio(label="record your voice", source="microphone")
                    upload_audio = gr.Audio(label="or upload audio here", source="upload")
                    source_speaker = gr.Dropdown(choices=self.idols, value="歩夢", label="source speaker")
                    target_speaker = gr.Dropdown(choices=self.idols, value="歩夢", label="target speaker")
                with gr.Column():
                    message_box = gr.Textbox(label="Message")
                    converted_audio = gr.Audio(label='converted audio')
                btn = gr.Button("Convert!")
                btn.click(self.vc_fn, inputs=[source_speaker, target_speaker, record_audio, upload_audio],
                        outputs=[message_box, converted_audio])
            with gr.Tab("小说合成(带字幕)"):
                with gr.Row():
                    with gr.Column():
                        with gr.Row():
                            with gr.Column():
                                input1 = gr.TextArea(label="建议colab或本地克隆后运行本仓库", value="为什么你会那么熟练啊?你和雪菜亲过多少次了")
                                input2 = gr.Dropdown(label="Language", choices=self.lan, value="自动", interactive=True)
                                input3 = gr.Dropdown(label="Speaker", choices=self.idols, value="歩夢", interactive=True)
                                btnVC = gr.Button("Submit")
                            with gr.Column():
                                input4 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声比例(noise scale),以控制情感", value=0.267)
                                input5 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声偏差(noise scale w),以控制音素长短", value=0.7)
                                input6 = gr.Slider(minimum=0.1, maximum=10, label="Duration", value=1)
                                output1 = gr.Audio(label="采样率22050")
                                subtitle = gr.outputs.File(label="字幕文件:subtitles.srt")
                btnVC.click(self.infer2, inputs=[input1, input2, input3, input4, input5, input6], outputs=[output1,subtitle])
    
    def loadCk(self,path):
        self.hps = utils.get_hparams_from_file(f"checkpoints/{path}/config.json")
        self.net_g = SynthesizerTrn(
            len(symbols),
            self.hps.data.filter_length // 2 + 1,
            self.hps.train.segment_size // self.hps.data.hop_length,
            n_speakers=self.hps.data.n_speakers,
            **self.hps.model).to(self.dev)
        _ = self.net_g.eval()
        _ = utils.load_checkpoint(f"checkpoints/{path}/model.pth", self.net_g)
        return "success"

    def get_text(self,text):
        text_norm = text_to_sequence(text,self.hps.data.text_cleaners)
        if self.hps.data.add_blank:
            text_norm = commons.intersperse(text_norm, 0)
        text_norm = torch.LongTensor(text_norm)
        return text_norm
    
    def is_japanese(self,string):
        for ch in string:
            if ord(ch) > 0x3040 and ord(ch) < 0x30FF:
                return True
        return False
    
    def is_english(self,string):
        import re
        pattern = re.compile('^[A-Za-z0-9.,:;!?()_*"\' ]+$')
        if pattern.fullmatch(string):
            return True
        else:
            return False
    
    def selection(self,speaker):
        if speaker == "高咲侑":
            spk = 0
            return spk

        elif speaker == "歩夢":
            spk = 1
            return spk

        elif speaker == "かすみ":
            spk = 2
            return spk

        elif speaker == "しずく":
            spk = 3
            return spk

        elif speaker == "果林":
            spk = 4
            return spk
    
        elif speaker == "愛":
            spk = 5
            return spk

        elif speaker == "彼方":
            spk = 6
            return spk

        elif speaker == "せつ菜":
            spk = 7
            return spk
        elif speaker == "エマ":
            spk = 8
            return spk
        elif speaker == "璃奈":
            spk = 9
            return spk
        elif speaker == "栞子":
            spk = 10
            return spk
        elif speaker == "ランジュ":
            spk = 11
            return spk
        elif speaker == "ミア":
            spk = 12
            return spk
        
        elif speaker == "chinese1":
            spk = 16
            return spk
        
        elif speaker == "chinese2":
            spk = 18
            return spk

        elif speaker == "chinese3":
            spk = 19
            return spk

        elif speaker == "華恋":
            spk = 21
            return spk

        elif speaker == "まひる":
            spk = 22
            return spk
    
        elif speaker == "なな":
            spk = 23
            return spk
    
        elif speaker == "クロディーヌ":
            spk = 24
            return spk
    
        elif speaker == "ひかり":
            spk = 25
            return spk
    
        elif speaker == "純那":
            spk = 26
            return spk
    
        elif speaker == "香子":
            spk = 27
            return spk
    
        elif speaker == "真矢":
            spk = 28
            return spk
        elif speaker == "双葉":
            spk = 29
            return spk
        elif speaker == "ミチル":
            spk = 30
            return spk
        elif speaker == "メイファン":
            spk = 31
            return spk
        elif speaker == "やちよ":
            spk = 32
            return spk
        elif speaker == "晶":
            spk = 33
            return spk
        elif speaker == "いちえ":
            spk = 34
            return spk
        elif speaker == "ゆゆ子":
            spk = 35
            return spk
        elif speaker == "塁":
            spk = 36
            return spk
        elif speaker == "珠緒":
            spk = 37
            return spk
        elif speaker == "あるる":
            spk = 38
            return spk
        elif speaker == "ララフィン":
            spk = 39
            return spk
        elif speaker == "美空":
            spk = 40
            return spk
        elif speaker == "静羽":
            spk = 41
            return spk
        else:
            return 0
            
    
    def sle(self,language,text):
        text = text.replace('\n','。').replace(' ',',')
        if language == "中文":
            tts_input1 = "[ZH]" + text + "[ZH]"
            return tts_input1
        elif language == "自动":
            tts_input1 = f"[JA]{text}[JA]" if self.is_japanese(text) else f"[ZH]{text}[ZH]"
            return tts_input1
        elif language == "日文":
            tts_input1 = "[JA]" + text + "[JA]"
            return tts_input1
        elif language == "英文":
            tts_input1 = "[EN]" + text + "[EN]"
            return tts_input1
        elif language == "手动":
            return text
    
    def extrac(self,text):
        text = re.sub("<[^>]*>","",text)
        result_list = re.split(r'\n', text)
        final_list = []
        for i in result_list:
            if self.is_english(i):
                i = romajitable.to_kana(i).katakana
            i = i.replace('\n','').replace(' ','')
            #Current length of single sentence: 20 
            '''
            if len(i)>1:
                if len(i) > 20:
                    try:
                        cur_list = re.split(r'。|!', i)
                        for i in cur_list:
                            if len(i)>1:
                                final_list.append(i+'。')
                    except:
                        pass
                else:
                    final_list.append(i)
            '''
            try:
                final_list.append(i)
            except:
                pass
        final_list = [x for x in final_list if x != '']
        print(final_list)
        return final_list
    
    def vc_fn(self,original_speaker, target_speaker, record_audio, upload_audio):
        input_audio = record_audio if record_audio is not None else upload_audio
        if input_audio is None:
            return "You need to record or upload an audio", None
        sampling_rate, audio = input_audio
        original_speaker_id = self.selection(original_speaker)
        target_speaker_id = self.selection(target_speaker)

        audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
        if len(audio.shape) > 1:
            audio = librosa.to_mono(audio.transpose(1, 0))
        if sampling_rate != self.hps.data.sampling_rate:
            audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=self.hps.data.sampling_rate)
        with torch.no_grad():
            y = torch.FloatTensor(audio)
            y = y / max(-y.min(), y.max()) / 0.99
            y = y.to(self.dev)
            y = y.unsqueeze(0)
            spec = spectrogram_torch(y, self.hps.data.filter_length,
                                     self.hps.data.sampling_rate, self.hps.data.hop_length, self.hps.data.win_length,
                                     center=False).to(self.dev)
            spec_lengths = torch.LongTensor([spec.size(-1)]).to(self.dev)
            sid_src = torch.LongTensor([original_speaker_id]).to(self.dev)
            sid_tgt = torch.LongTensor([target_speaker_id]).to(self.dev)
            audio = self.net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt)[0][
                0, 0].data.cpu().float().numpy()
        del y, spec, spec_lengths, sid_src, sid_tgt
        return "Success", (self.hps.data.sampling_rate, audio)
    
    def infer(self, text ,language, speaker_id,n_scale= 0.667,n_scale_w = 0.8, l_scale = 1):
        try:
            speaker_id = int(self.selection(speaker_id))
            t1 = time.time()
            stn_tst = self.get_text(self.sle(language,text))
            with torch.no_grad():
                x_tst = stn_tst.unsqueeze(0).to(self.dev)
                x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(self.dev)
                sid = torch.LongTensor([speaker_id]).to(self.dev)
                audio = self.net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=n_scale, noise_scale_w=n_scale_w, length_scale=l_scale)[0][0,0].data.cpu().float().numpy()
                t2 = time.time()
                spending_time = "推理时间为:"+str(t2-t1)+"s"
                print(spending_time)
            return (self.hps.data.sampling_rate, audio)
        except:
            self.hps = utils.get_hparams_from_file(f"checkpoints/biaobei/config.json")
            self.net_g = SynthesizerTrn(
                len(symbols),
                self.hps.data.filter_length // 2 + 1,
                self.hps.train.segment_size // self.hps.data.hop_length,
                n_speakers=self.hps.data.n_speakers,
                **self.hps.model).to(self.dev)
            _ = self.net_g.eval()
            _ = utils.load_checkpoint(f"checkpoints/biaobei/model.pth", self.net_g)

    def infer2(self, text ,language, speaker_id,n_scale= 0.667,n_scale_w = 0.8, l_scale = 1):
        speaker_id = int(self.selection(speaker_id))
        a = ['【','[','(','(']
        b = ['】',']',')',')']
        for i in a:
            text = text.replace(i,'<')
        for i in b:
            text = text.replace(i,'>')
        final_list = self.extrac(text.replace('“','').replace('”',''))
        audio_fin = []
        c = 0
        t = datetime.timedelta(seconds=0)
        f1 = open("subtitles.srt",'w',encoding='utf-8')
        for sentence in final_list:
            c +=1
            stn_tst = self.get_text(self.sle(language,sentence))
            with torch.no_grad():
                x_tst = stn_tst.unsqueeze(0).to(self.dev)
                x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(self.dev)
                sid = torch.LongTensor([speaker_id]).to(self.dev)
                t1 = time.time()
                audio = self.net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=n_scale, noise_scale_w=n_scale_w, length_scale=l_scale)[0][0,0].data.cpu().float().numpy()
                t2 = time.time()
                spending_time = "第"+str(c)+"句的推理时间为:"+str(t2-t1)+"s"
                print(spending_time)
                time_start = str(t).split(".")[0] + "," + str(t.microseconds)[:3]
                last_time = datetime.timedelta(seconds=len(audio)/float(22050))
                t+=last_time
                time_end = str(t).split(".")[0] + "," + str(t.microseconds)[:3]
                print(time_end)
                f1.write(str(c-1)+'\n'+time_start+' --> '+time_end+'\n'+sentence+'\n\n')
                audio_fin.append(audio)
        file_path = "subtitles.srt"
        return (self.hps.data.sampling_rate, np.concatenate(audio_fin)),file_path
print("开始部署")
grVits = VitsGradio()
grVits.Vits.launch()