# import matplotlib.pyplot as plt import logging # logger = logging.getLogger(__name__) 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 import time def get_text(text, hps): # text_norm = requests.post("http://121.5.171.42:39001/texttosequence?text="+text).json()["text_norm"] text_norm = text_to_sequence(text, hps.data.text_cleaners) # print(hps.data.text_cleaners) # print(text_norm) if hps.data.add_blank: text_norm = commons.intersperse(text_norm, 0) text_norm = torch.LongTensor(text_norm) return text_norm def load_model(config_json, pth_path): dev = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") hps_ms = utils.get_hparams_from_file(f"./configs/{config_json}") global net_g net_g = SynthesizerTrn( len(symbols), hps_ms.data.filter_length // 2 + 1, hps_ms.train.segment_size // hps_ms.data.hop_length, **hps_ms.model).to(dev) _ = net_g.eval() _ = utils.load_checkpoint(pth_path, net_g) print("load_model:"+pth_path) return net_g def local_run(c_id, text): stn_tst = get_text(text, hps) with torch.no_grad(): x_tst = stn_tst.to(dev).unsqueeze(0) x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(dev) sid = torch.LongTensor([c_id]).to(dev) audio = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.cpu().float().numpy() return audio CONFIG_FILE = "configs/config.json" dev = torch.device("cpu") hps = utils.get_hparams_from_file(CONFIG_FILE)