vits_yz / server /vits /run_old.py
byzp's picture
-
40fac91
raw
history blame
3.02 kB
import matplotlib.pyplot as plt
import os
import json
import math
import scipy
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.symbols1 import symbols1
from text import text_to_sequence
from text import text_to_sequence1
from scipy.io.wavfile import write
import io
"""
import argparse
parser = argparse.ArgumentParser(description='查看传参')
parser.add_argument("--text",type=str,default="你好。")
parser.add_argument("--character",type=int,default=0)
args = parser.parse_args()
"""
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 get_text1(text, hps):
text_norm = text_to_sequence1(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("./vits/configs/ys.json")
hps1= utils.get_hparams_from_file("./vits/configs/bh3.json")
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).cuda()
_ = net_g.eval()
net_g1 = SynthesizerTrn(
len(symbols1),
hps1.data.filter_length // 2 + 1,
hps1.train.segment_size // hps1.data.hop_length,
n_speakers=hps1.data.n_speakers,#
**hps1.model).cuda()
_ = net_g1.eval()
_ = utils.load_checkpoint("./vits/models/ys.pth", net_g, None)
_ = utils.load_checkpoint("./vits/models/bh3.pth", net_g1, None)
def ys(text,character):
#text=args.text
audio_bytes = io.BytesIO()
stn_tst = get_text(text, hps)
with torch.no_grad():
x_tst = stn_tst.cuda().unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()
#character=args.character
sid=torch.LongTensor([character]).cuda()
audio = net_g.infer(x_tst, x_tst_lengths, noise_scale=.667, sid = sid, noise_scale_w=0.8, length_scale=1.2)[0][0,0].data.cpu().float().numpy()
scipy.io.wavfile.write(audio_bytes, hps.data.sampling_rate, audio)
return audio_bytes
def bh3(text,character):
audio_bytes = io.BytesIO()
stn_tst = get_text1(text, hps1)
with torch.no_grad():
x_tst = stn_tst.cuda().unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()
#character=args.character
sid=torch.LongTensor([character]).cuda()
audio = net_g1.infer(x_tst, x_tst_lengths, noise_scale=.667, sid = sid, noise_scale_w=0.8, length_scale=1.2)[0][0,0].data.cpu().float().numpy()
scipy.io.wavfile.write(audio_bytes, hps1.data.sampling_rate, audio)
return audio_bytes