CoolFace
Apppublic

surfmore/SimpleRVC

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
infer_web.py201 linesDownload Raw Back to root
1from vc_infer_pipeline import VC2from myutils import Audio3from infer_pack.models import (4    SynthesizerTrnMs256NSFsid,5    SynthesizerTrnMs256NSFsid_nono,6    SynthesizerTrnMs768NSFsid,7    SynthesizerTrnMs768NSFsid_nono,8)9from fairseq import checkpoint_utils10from config import Config11import torch12import numpy as np13import traceback14import os15import sys16import warnings17 18now_dir = os.getcwd()19sys.path.append(now_dir)20os.makedirs(os.path.join(now_dir, "audios"), exist_ok=True)21os.makedirs(os.path.join(now_dir, "audio-outputs"), exist_ok=True)22os.makedirs(os.path.join(now_dir, "weights"), exist_ok=True)23warnings.filterwarnings("ignore")24torch.manual_seed(114514)25 26config = Config()27 28hubert_model = None29weight_root = "weights"30 31def load_hubert():32    # Determinar si existe una tarjeta N que pueda usarse para entrenar y acelerar la inferencia.33    global hubert_model34    models, _, _ = checkpoint_utils.load_model_ensemble_and_task(35        ["hubert_base.pt"],36        suffix="",37    )38    hubert_model = models[0]39    hubert_model = hubert_model.to(config.device)40    if config.is_half:41        hubert_model = hubert_model.half()42    else:43        hubert_model = hubert_model.float()44    hubert_model.eval()45 46def vc_single(47    sid,48    input_audio_path0,49    input_audio_path1,50    f0_up_key,51    f0_file,52    f0_method,53    file_index,54    file_index2,55    # file_big_npy,56    index_rate,57    filter_radius,58    resample_sr,59    rms_mix_rate,60    protect,61    crepe_hop_length,62):63    global tgt_sr, net_g, vc, hubert_model, version64    if input_audio_path0 is None or input_audio_path0 is None:65        return "You need to upload an audio", None66    f0_up_key = int(f0_up_key)67    try:68        if input_audio_path0 == "":69            audio = Audio.load_audio(input_audio_path1, 16000)70        else:71            audio = Audio.load_audio(input_audio_path0, 16000)72 73        audio_max = np.abs(audio).max() / 0.9574        if audio_max > 1:75            audio /= audio_max76        times = [0, 0, 0]77        if not hubert_model:78            load_hubert()79        if_f0 = cpt.get("f0", 1)80        file_index = (81            (82                file_index.strip(" ")83                .strip('"')84                .strip("\n")85                .strip('"')86                .strip(" ")87                .replace("trained", "added")88            )89            if file_index != ""90            else file_index291        )92 93        audio_opt = vc.pipeline(94            hubert_model,95            net_g,96            sid,97            audio,98            input_audio_path1,99            times,100            f0_up_key,101            f0_method,102            file_index,103            # file_big_npy,104            index_rate,105            if_f0,106            filter_radius,107            tgt_sr,108            resample_sr,109            rms_mix_rate,110            version,111            protect,112            crepe_hop_length,113            f0_file=f0_file,114        )115        if tgt_sr != resample_sr >= 16000:116            tgt_sr = resample_sr117        index_info = (118            "Using index:%s." % file_index119            if os.path.exists(file_index)120            else "Index not used."121        )122        print(index_info)123        return "Success.\n %s\nTime:\n npy:%ss, f0:%ss, infer:%ss" % (124            index_info,125            times[0],126            times[1],127            times[2],128        ), (tgt_sr, audio_opt)129    except:130        info = traceback.format_exc()131        print(info)132        return info, (None, None)133 134def get_vc(model_name):135    global tgt_sr, net_g, vc, cpt, version136 137    # Comprobar si se pasó uno o varios modelos138    if model_name == "" or model_name == []:139        global hubert_model140        if hubert_model is not None:  # 考虑到轮询, 需要加个判断看是否 sid 是由有模型切换到无模型的141            print("Limpiar caché")142            del net_g, vc, hubert_model, tgt_sr  # ,cpt143            hubert_model = net_g = vc = hubert_model = tgt_sr = None144 145            # Si hay una GPU disponible, libera la memoria de la GPU146            if torch.cuda.is_available():147                torch.cuda.empty_cache()148 149            # Bloque de abajo no limpia completamente150            if_f0 = cpt.get("f0", 1)151            version = cpt.get("version", "v1")152            if version == "v1":153                if if_f0 == 1:154                    net_g = SynthesizerTrnMs256NSFsid(155                        *cpt["config"], is_half=config.is_half156                    )157                else:158                    net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])159            elif version == "v2":160                if if_f0 == 1:161                    net_g = SynthesizerTrnMs768NSFsid(162                        *cpt["config"], is_half=config.is_half163                    )164            else:165                net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])166 167            del net_g, cpt168            if torch.cuda.is_available():169                torch.cuda.empty_cache()170            cpt = None171        return {"success": False, "message": "No se proporcionó un sid"}172 173    person = "%s/%s" % (weight_root, model_name)174    print("Cargando %s" % person)175    cpt = torch.load(person, map_location="cpu")176    tgt_sr = cpt["config"][-1]177    cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]178    if_f0 = cpt.get("f0", 1)179    version = cpt.get("version", "v1")180 181    if version == "v1":182        if if_f0 == 1:183            net_g = SynthesizerTrnMs256NSFsid(184                *cpt["config"], is_half=config.is_half)185        else:186            net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])187    elif version == "v2":188        if if_f0 == 1:189            net_g = SynthesizerTrnMs768NSFsid(190                *cpt["config"], is_half=config.is_half)191    else:192        net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])193    del net_g.enc_q194 195    print(net_g.load_state_dict(cpt["weight"], strict=False))196    net_g.eval().to(config.device)197    if config.is_half:198        net_g = net_g.half()199    else:200        net_g = net_g.float()201    vc = VC(tgt_sr, config)