Paolify/RVC_v2
0
1import subprocess2import os3import sys4import errno5import shutil6import yt_dlp7from mega import Mega8import datetime9import unicodedata10import torch11import glob12import gradio as gr13import gdown14import zipfile15import traceback16import json17import mdx18from mdx_processing_script import get_model_list,id_to_ptm,prepare_mdx,run_mdx19import requests20import wget21import ffmpeg22import hashlib23now_dir = os.getcwd()24sys.path.append(now_dir)25from unidecode import unidecode26import re27import time28from lib.infer_pack.models_onnx import SynthesizerTrnMsNSFsidM29from infer.modules.vc.pipeline import Pipeline30VC = Pipeline31from lib.infer_pack.models import (32 SynthesizerTrnMs256NSFsid,33 SynthesizerTrnMs256NSFsid_nono,34 SynthesizerTrnMs768NSFsid,35 SynthesizerTrnMs768NSFsid_nono,36)37from MDXNet import MDXNetDereverb38from configs.config import Config39from infer_uvr5 import _audio_pre_, _audio_pre_new40from huggingface_hub import HfApi, list_models41from huggingface_hub import login42from i18n import I18nAuto43i18n = I18nAuto()44from bs4 import BeautifulSoup45from sklearn.cluster import MiniBatchKMeans46from dotenv import load_dotenv47load_dotenv()48config = Config()49tmp = os.path.join(now_dir, "TEMP")50shutil.rmtree(tmp, ignore_errors=True)51os.environ["TEMP"] = tmp52weight_root = os.getenv("weight_root")53weight_uvr5_root = os.getenv("weight_uvr5_root")54index_root = os.getenv("index_root")55audio_root = "audios"56names = []57for name in os.listdir(weight_root):58 if name.endswith(".pth"):59 names.append(name)60index_paths = []61 62global indexes_list63indexes_list = []64 65audio_paths = []66for root, dirs, files in os.walk(index_root, topdown=False):67 for name in files:68 if name.endswith(".index") and "trained" not in name:69 index_paths.append("%s\\%s" % (root, name))70 71for root, dirs, files in os.walk(audio_root, topdown=False):72 for name in files:73 audio_paths.append("%s/%s" % (root, name))74 75uvr5_names = []76for name in os.listdir(weight_uvr5_root):77 if name.endswith(".pth") or "onnx" in name:78 uvr5_names.append(name.replace(".pth", ""))79 80def calculate_md5(file_path):81 hash_md5 = hashlib.md5()82 with open(file_path, "rb") as f:83 for chunk in iter(lambda: f.read(4096), b""):84 hash_md5.update(chunk)85 return hash_md5.hexdigest()86 87def format_title(title):88 formatted_title = re.sub(r'[^\w\s-]', '', title)89 formatted_title = formatted_title.replace(" ", "_")90 return formatted_title91 92def silentremove(filename):93 try:94 os.remove(filename)95 except OSError as e: 96 if e.errno != errno.ENOENT: 97 raise 98def get_md5(temp_folder):99 for root, subfolders, files in os.walk(temp_folder):100 for file in files:101 if not file.startswith("G_") and not file.startswith("D_") and file.endswith(".pth") and not "_G_" in file and not "_D_" in file:102 md5_hash = calculate_md5(os.path.join(root, file))103 return md5_hash104 105 return None106 107def find_parent(search_dir, file_name):108 for dirpath, dirnames, filenames in os.walk(search_dir):109 if file_name in filenames:110 return os.path.abspath(dirpath)111 return None112 113def find_folder_parent(search_dir, folder_name):114 for dirpath, dirnames, filenames in os.walk(search_dir):115 if folder_name in dirnames:116 return os.path.abspath(dirpath)117 return None118 119 120def delete_large_files(directory_path, max_size_megabytes):121 for filename in os.listdir(directory_path):122 file_path = os.path.join(directory_path, filename)123 if os.path.isfile(file_path):124 size_in_bytes = os.path.getsize(file_path)125 size_in_megabytes = size_in_bytes / (1024 * 1024) # Convert bytes to megabytes126 127 if size_in_megabytes > max_size_megabytes:128 print("###################################")129 print(f"Deleting s*** {filename} (Size: {size_in_megabytes:.2f} MB)")130 os.remove(file_path)131 print("###################################") 132 133def download_from_url(url):134 parent_path = find_folder_parent(".", "pretrained_v2")135 zips_path = os.path.join(parent_path, 'zips')136 print(f"Limit download size in MB {os.getenv('MAX_DOWNLOAD_SIZE')}, duplicate the space for modify the limit")137 138 if url != '':139 print(i18n("Downloading the file: ") + f"{url}")140 if "drive.google.com" in url:141 if "file/d/" in url:142 file_id = url.split("file/d/")[1].split("/")[0]143 elif "id=" in url:144 file_id = url.split("id=")[1].split("&")[0]145 else:146 return None147 148 if file_id:149 os.chdir('./zips')150 result = subprocess.run(["gdown", f"https://drive.google.com/uc?id={file_id}", "--fuzzy"], capture_output=True, text=True, encoding='utf-8')151 if "Too many users have viewed or downloaded this file recently" in str(result.stderr):152 return "too much use"153 if "Cannot retrieve the public link of the file." in str(result.stderr):154 return "private link"155 print(result.stderr)156 157 elif "/blob/" in url:158 os.chdir('./zips')159 url = url.replace("blob", "resolve")160 response = requests.get(url)161 if response.status_code == 200:162 file_name = url.split('/')[-1]163 with open(os.path.join(zips_path, file_name), "wb") as newfile:164 newfile.write(response.content)165 else:166 os.chdir(parent_path)167 elif "mega.nz" in url:168 if "#!" in url:169 file_id = url.split("#!")[1].split("!")[0]170 elif "file/" in url:171 file_id = url.split("file/")[1].split("/")[0]172 else:173 return None174 if file_id:175 m = Mega()176 m.download_url(url, zips_path)177 elif "/tree/main" in url:178 response = requests.get(url)179 soup = BeautifulSoup(response.content, 'html.parser')180 temp_url = ''181 for link in soup.find_all('a', href=True):182 if link['href'].endswith('.zip'):183 temp_url = link['href']184 break185 if temp_url:186 url = temp_url187 url = url.replace("blob", "resolve")188 if "huggingface.co" not in url:189 url = "https://huggingface.co" + url190 191 wget.download(url)192 else:193 print("No .zip file found on the page.")194 elif "cdn.discordapp.com" in url:195 file = requests.get(url)196 if file.status_code == 200:197 name = url.split('/')198 with open(os.path.join(zips_path, name[len(name)-1]), "wb") as newfile:199 newfile.write(file.content)200 else:201 return None202 elif "pixeldrain.com" in url:203 try:204 file_id = url.split("pixeldrain.com/u/")[1]205 os.chdir('./zips')206 print(file_id)207 response = requests.get(f"https://pixeldrain.com/api/file/{file_id}")208 if response.status_code == 200:209 file_name = response.headers.get("Content-Disposition").split('filename=')[-1].strip('";')210 if not os.path.exists(zips_path):211 os.makedirs(zips_path)212 with open(os.path.join(zips_path, file_name), "wb") as newfile:213 newfile.write(response.content)214 os.chdir(parent_path)215 return "downloaded"216 else:217 os.chdir(parent_path)218 return None219 except Exception as e:220 print(e)221 os.chdir(parent_path)222 return None223 else:224 os.chdir('./zips')225 wget.download(url)226 227 #os.chdir('./zips') 228 delete_large_files(zips_path, int(os.getenv("MAX_DOWNLOAD_SIZE"))) 229 os.chdir(parent_path)230 print(i18n("Full download"))231 return "downloaded"232 else:233 return None234 235class error_message(Exception):236 def __init__(self, mensaje):237 self.mensaje = mensaje238 super().__init__(mensaje)239 240def get_vc(sid, to_return_protect0, to_return_protect1):241 global n_spk, tgt_sr, net_g, vc, cpt, version242 if sid == "" or sid == []:243 global hubert_model244 if hubert_model is not None: 245 print("clean_empty_cache")246 del net_g, n_spk, vc, hubert_model, tgt_sr 247 hubert_model = net_g = n_spk = vc = hubert_model = tgt_sr = None248 if torch.cuda.is_available():249 torch.cuda.empty_cache()250 if_f0 = cpt.get("f0", 1)251 version = cpt.get("version", "v1")252 if version == "v1":253 if if_f0 == 1:254 net_g = SynthesizerTrnMs256NSFsid(255 *cpt["config"], is_half=config.is_half256 )257 else:258 net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])259 elif version == "v2":260 if if_f0 == 1:261 net_g = SynthesizerTrnMs768NSFsid(262 *cpt["config"], is_half=config.is_half263 )264 else:265 net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])266 del net_g, cpt267 if torch.cuda.is_available():268 torch.cuda.empty_cache()269 cpt = None270 return (271 {"visible": False, "__type__": "update"},272 {"visible": False, "__type__": "update"},273 {"visible": False, "__type__": "update"},274 )275 person = "%s/%s" % (weight_root, sid)276 print("loading %s" % person)277 cpt = torch.load(person, map_location="cpu")278 tgt_sr = cpt["config"][-1]279 cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] 280 if_f0 = cpt.get("f0", 1)281 if if_f0 == 0:282 to_return_protect0 = to_return_protect1 = {283 "visible": False,284 "value": 0.5,285 "__type__": "update",286 }287 else:288 to_return_protect0 = {289 "visible": True,290 "value": to_return_protect0,291 "__type__": "update",292 }293 to_return_protect1 = {294 "visible": True,295 "value": to_return_protect1,296 "__type__": "update",297 }298 version = cpt.get("version", "v1")299 if version == "v1":300 if if_f0 == 1:301 net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)302 else:303 net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])304 elif version == "v2":305 if if_f0 == 1:306 net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=config.is_half)307 else:308 net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])309 del net_g.enc_q310 print(net_g.load_state_dict(cpt["weight"], strict=False))311 net_g.eval().to(config.device)312 if config.is_half:313 net_g = net_g.half()314 else:315 net_g = net_g.float()316 vc = VC(tgt_sr, config)317 n_spk = cpt["config"][-3]318 return (319 {"visible": True, "maximum": n_spk, "__type__": "update"},320 to_return_protect0,321 to_return_protect1,322 )323 324def load_downloaded_model(url):325 parent_path = find_folder_parent(".", "pretrained_v2")326 try:327 infos = []328 logs_folders = ['0_gt_wavs','1_16k_wavs','2a_f0','2b-f0nsf','3_feature256','3_feature768']329 zips_path = os.path.join(parent_path, 'zips')330 unzips_path = os.path.join(parent_path, 'unzips')331 weights_path = os.path.join(parent_path, 'weights')332 logs_dir = ""333 334 if os.path.exists(zips_path):335 shutil.rmtree(zips_path)336 if os.path.exists(unzips_path):337 shutil.rmtree(unzips_path)338 339 os.mkdir(zips_path)340 os.mkdir(unzips_path)341 342 download_file = download_from_url(url)343 if not download_file:344 print(i18n("The file could not be downloaded."))345 infos.append(i18n("The file could not be downloaded."))346 yield "\n".join(infos)347 elif download_file == "downloaded":348 print(i18n("It has been downloaded successfully."))349 infos.append(i18n("It has been downloaded successfully."))350 yield "\n".join(infos)351 elif download_file == "too much use":352 raise Exception(i18n("Too many users have recently viewed or downloaded this file"))353 elif download_file == "private link":354 raise Exception(i18n("Cannot get file from this private link"))355 356 for filename in os.listdir(zips_path):357 if filename.endswith(".zip"):358 zipfile_path = os.path.join(zips_path,filename)359 print(i18n("Proceeding with the extraction..."))360 infos.append(i18n("Proceeding with the extraction..."))361 shutil.unpack_archive(zipfile_path, unzips_path, 'zip')362 model_name = os.path.basename(zipfile_path)363 logs_dir = os.path.join(parent_path,'logs', os.path.normpath(str(model_name).replace(".zip","")))364 yield "\n".join(infos)365 else:366 print(i18n("Unzip error."))367 infos.append(i18n("Unzip error."))368 yield "\n".join(infos)369 370 index_file = False371 model_file = False372 D_file = False373 G_file = False374 375 for path, subdirs, files in os.walk(unzips_path):376 for item in files:377 item_path = os.path.join(path, item)378 if not 'G_' in item and not 'D_' in item and item.endswith('.pth'):379 model_file = True380 model_name = item.replace(".pth","")381 logs_dir = os.path.join(parent_path,'logs', model_name)382 if os.path.exists(logs_dir):383 shutil.rmtree(logs_dir)384 os.mkdir(logs_dir)385 if not os.path.exists(weights_path):386 os.mkdir(weights_path)387 if os.path.exists(os.path.join(weights_path, item)):388 os.remove(os.path.join(weights_path, item))389 if os.path.exists(item_path):390 shutil.move(item_path, weights_path)391 392 if not model_file and not os.path.exists(logs_dir):393 os.mkdir(logs_dir)394 for path, subdirs, files in os.walk(unzips_path):395 for item in files:396 item_path = os.path.join(path, item)397 if item.startswith('added_') and item.endswith('.index'):398 index_file = True399 if os.path.exists(item_path):400 if os.path.exists(os.path.join(logs_dir, item)):401 os.remove(os.path.join(logs_dir, item))402 shutil.move(item_path, logs_dir)403 if item.startswith('total_fea.npy') or item.startswith('events.'):404 if os.path.exists(item_path):405 if os.path.exists(os.path.join(logs_dir, item)):406 os.remove(os.path.join(logs_dir, item))407 shutil.move(item_path, logs_dir)408 409 410 result = ""411 if model_file:412 if index_file:413 print(i18n("The model works for inference, and has the .index file."))414 infos.append("\n" + i18n("The model works for inference, and has the .index file."))415 yield "\n".join(infos)416 else:417 print(i18n("The model works for inference, but it doesn't have the .index file."))418 infos.append("\n" + i18n("The model works for inference, but it doesn't have the .index file."))419 yield "\n".join(infos)420 421 if not index_file and not model_file:422 print(i18n("No relevant file was found to upload."))423 infos.append(i18n("No relevant file was found to upload."))424 yield "\n".join(infos)425 426 if os.path.exists(zips_path):427 shutil.rmtree(zips_path)428 if os.path.exists(unzips_path):429 shutil.rmtree(unzips_path)430 os.chdir(parent_path) 431 return result432 except Exception as e:433 os.chdir(parent_path)434 if "too much use" in str(e):435 print(i18n("Too many users have recently viewed or downloaded this file"))436 yield i18n("Too many users have recently viewed or downloaded this file")437 elif "private link" in str(e):438 print(i18n("Cannot get file from this private link"))439 yield i18n("Cannot get file from this private link")440 else:441 print(e)442 yield i18n("An error occurred downloading")443 finally:444 os.chdir(parent_path)445 446def load_dowloaded_dataset(url):447 parent_path = find_folder_parent(".", "pretrained_v2")448 infos = []449 try:450 zips_path = os.path.join(parent_path, 'zips')451 unzips_path = os.path.join(parent_path, 'unzips')452 datasets_path = os.path.join(parent_path, 'datasets')453 audio_extenions =['wav', 'mp3', 'flac', 'ogg', 'opus',454 'm4a', 'mp4', 'aac', 'alac', 'wma',455 'aiff', 'webm', 'ac3']456 457 if os.path.exists(zips_path):458 shutil.rmtree(zips_path)459 if os.path.exists(unzips_path):460 shutil.rmtree(unzips_path)461 462 if not os.path.exists(datasets_path):463 os.mkdir(datasets_path)464 465 os.mkdir(zips_path)466 os.mkdir(unzips_path)467 468 download_file = download_from_url(url)469 470 if not download_file:471 print(i18n("An error occurred downloading"))472 infos.append(i18n("An error occurred downloading"))473 yield "\n".join(infos)474 raise Exception(i18n("An error occurred downloading"))475 elif download_file == "downloaded":476 print(i18n("It has been downloaded successfully."))477 infos.append(i18n("It has been downloaded successfully."))478 yield "\n".join(infos)479 elif download_file == "too much use":480 raise Exception(i18n("Too many users have recently viewed or downloaded this file"))481 elif download_file == "private link":482 raise Exception(i18n("Cannot get file from this private link"))483 484 zip_path = os.listdir(zips_path)485 foldername = ""486 for file in zip_path:487 if file.endswith('.zip'):488 file_path = os.path.join(zips_path, file)489 print("....")490 foldername = file.replace(".zip","").replace(" ","").replace("-","_")491 dataset_path = os.path.join(datasets_path, foldername)492 print(i18n("Proceeding with the extraction..."))493 infos.append(i18n("Proceeding with the extraction..."))494 yield "\n".join(infos)495 shutil.unpack_archive(file_path, unzips_path, 'zip')496 if os.path.exists(dataset_path):497 shutil.rmtree(dataset_path)498 499 os.mkdir(dataset_path)500 501 for root, subfolders, songs in os.walk(unzips_path):502 for song in songs:503 song_path = os.path.join(root, song)504 if song.endswith(tuple(audio_extenions)):505 formatted_song_name = format_title(os.path.splitext(song)[0])506 extension = os.path.splitext(song)[1]507 new_song_path = os.path.join(dataset_path, f"{formatted_song_name}{extension}")508 shutil.move(song_path, new_song_path)509 else:510 print(i18n("Unzip error."))511 infos.append(i18n("Unzip error."))512 yield "\n".join(infos)513 514 515 516 if os.path.exists(zips_path):517 shutil.rmtree(zips_path)518 if os.path.exists(unzips_path):519 shutil.rmtree(unzips_path)520 521 print(i18n("The Dataset has been loaded successfully."))522 infos.append(i18n("The Dataset has been loaded successfully."))523 yield "\n".join(infos)524 except Exception as e:525 os.chdir(parent_path)526 if "too much use" in str(e):527 print(i18n("Too many users have recently viewed or downloaded this file"))528 yield i18n("Too many users have recently viewed or downloaded this file") 529 elif "private link" in str(e):530 print(i18n("Cannot get file from this private link"))531 yield i18n("Cannot get file from this private link")532 else:533 print(e)534 yield i18n("An error occurred downloading")535 finally:536 os.chdir(parent_path)537 538def save_model(modelname, save_action):539 540 parent_path = find_folder_parent(".", "pretrained_v2")541 zips_path = os.path.join(parent_path, 'zips')542 dst = os.path.join(zips_path,modelname)543 logs_path = os.path.join(parent_path, 'logs', modelname)544 weights_path = os.path.join(parent_path, 'weights', f"{modelname}.pth")545 save_folder = parent_path546 infos = [] 547 548 try:549 if not os.path.exists(logs_path):550 raise Exception("No model found.")551 552 if not 'content' in parent_path:553 save_folder = os.path.join(parent_path, 'RVC_Backup')554 else:555 save_folder = '/content/drive/MyDrive/RVC_Backup'556 557 infos.append(i18n("Save model"))558 yield "\n".join(infos)559 560 if not os.path.exists(save_folder):561 os.mkdir(save_folder)562 if not os.path.exists(os.path.join(save_folder, 'ManualTrainingBackup')):563 os.mkdir(os.path.join(save_folder, 'ManualTrainingBackup'))564 if not os.path.exists(os.path.join(save_folder, 'Finished')):565 os.mkdir(os.path.join(save_folder, 'Finished'))566 567 if os.path.exists(zips_path):568 shutil.rmtree(zips_path)569 570 os.mkdir(zips_path)571 added_file = glob.glob(os.path.join(logs_path, "added_*.index"))572 d_file = glob.glob(os.path.join(logs_path, "D_*.pth"))573 g_file = glob.glob(os.path.join(logs_path, "G_*.pth"))574 575 if save_action == i18n("Choose the method"):576 raise Exception("No method choosen.")577 578 if save_action == i18n("Save all"):579 print(i18n("Save all"))580 save_folder = os.path.join(save_folder, 'ManualTrainingBackup')581 shutil.copytree(logs_path, dst)582 else:583 if not os.path.exists(dst):584 os.mkdir(dst)585 586 if save_action == i18n("Save D and G"):587 print(i18n("Save D and G"))588 save_folder = os.path.join(save_folder, 'ManualTrainingBackup')589 if len(d_file) > 0:590 shutil.copy(d_file[0], dst)591 if len(g_file) > 0:592 shutil.copy(g_file[0], dst) 593 594 if len(added_file) > 0:595 shutil.copy(added_file[0], dst)596 else:597 infos.append(i18n("Saved without index..."))598 599 if save_action == i18n("Save voice"):600 print(i18n("Save voice"))601 save_folder = os.path.join(save_folder, 'Finished')602 if len(added_file) > 0:603 shutil.copy(added_file[0], dst)604 else:605 infos.append(i18n("Saved without index..."))606 607 yield "\n".join(infos)608 if not os.path.exists(weights_path):609 infos.append(i18n("Saved without inference model..."))610 else:611 shutil.copy(weights_path, dst)612 613 yield "\n".join(infos)614 infos.append("\n" + i18n("This may take a few minutes, please wait..."))615 yield "\n".join(infos)616 617 shutil.make_archive(os.path.join(zips_path,f"{modelname}"), 'zip', zips_path)618 shutil.move(os.path.join(zips_path,f"{modelname}.zip"), os.path.join(save_folder, f'{modelname}.zip'))619 620 shutil.rmtree(zips_path) 621 infos.append("\n" + i18n("Model saved successfully"))622 yield "\n".join(infos)623 624 except Exception as e:625 print(e)626 if "No model found." in str(e):627 infos.append(i18n("The model you want to save does not exist, be sure to enter the correct name."))628 else:629 infos.append(i18n("An error occurred saving the model"))630 631 yield "\n".join(infos)632 633def load_downloaded_backup(url):634 parent_path = find_folder_parent(".", "pretrained_v2")635 try:636 infos = []637 logs_folders = ['0_gt_wavs','1_16k_wavs','2a_f0','2b-f0nsf','3_feature256','3_feature768']638 zips_path = os.path.join(parent_path, 'zips')639 unzips_path = os.path.join(parent_path, 'unzips')640 weights_path = os.path.join(parent_path, 'weights')641 logs_dir = os.path.join(parent_path, 'logs')642 643 if os.path.exists(zips_path):644 shutil.rmtree(zips_path)645 if os.path.exists(unzips_path):646 shutil.rmtree(unzips_path)647 648 os.mkdir(zips_path)649 os.mkdir(unzips_path)650 651 download_file = download_from_url(url)652 if not download_file:653 print(i18n("The file could not be downloaded."))654 infos.append(i18n("The file could not be downloaded."))655 yield "\n".join(infos)656 elif download_file == "downloaded":657 print(i18n("It has been downloaded successfully."))658 infos.append(i18n("It has been downloaded successfully."))659 yield "\n".join(infos)660 elif download_file == "too much use":661 raise Exception(i18n("Too many users have recently viewed or downloaded this file"))662 elif download_file == "private link":663 raise Exception(i18n("Cannot get file from this private link"))664 665 for filename in os.listdir(zips_path):666 if filename.endswith(".zip"):667 zipfile_path = os.path.join(zips_path,filename)668 zip_dir_name = os.path.splitext(filename)[0]669 unzip_dir = unzips_path670 print(i18n("Proceeding with the extraction..."))671 infos.append(i18n("Proceeding with the extraction..."))672 shutil.unpack_archive(zipfile_path, unzip_dir, 'zip')673 674 if os.path.exists(os.path.join(unzip_dir, zip_dir_name)):675 shutil.move(os.path.join(unzip_dir, zip_dir_name), logs_dir)676 else:677 new_folder_path = os.path.join(logs_dir, zip_dir_name)678 os.mkdir(new_folder_path)679 for item_name in os.listdir(unzip_dir):680 item_path = os.path.join(unzip_dir, item_name)681 if os.path.isfile(item_path):682 shutil.move(item_path, new_folder_path)683 elif os.path.isdir(item_path):684 shutil.move(item_path, new_folder_path)685 686 yield "\n".join(infos)687 else:688 print(i18n("Unzip error."))689 infos.append(i18n("Unzip error."))690 yield "\n".join(infos)691 692 result = ""693 694 for filename in os.listdir(unzips_path):695 if filename.endswith(".zip"):696 silentremove(filename)697 698 if os.path.exists(zips_path):699 shutil.rmtree(zips_path)700 if os.path.exists(os.path.join(parent_path, 'unzips')):701 shutil.rmtree(os.path.join(parent_path, 'unzips'))702 print(i18n("The Backup has been uploaded successfully."))703 infos.append("\n" + i18n("The Backup has been uploaded successfully."))704 yield "\n".join(infos)705 os.chdir(parent_path) 706 return result707 except Exception as e:708 os.chdir(parent_path)709 if "too much use" in str(e):710 print(i18n("Too many users have recently viewed or downloaded this file"))711 yield i18n("Too many users have recently viewed or downloaded this file")712 elif "private link" in str(e):713 print(i18n("Cannot get file from this private link"))714 yield i18n("Cannot get file from this private link") 715 else:716 print(e)717 yield i18n("An error occurred downloading")718 finally:719 os.chdir(parent_path)720 721def save_to_wav(record_button):722 if record_button is None:723 pass724 else:725 path_to_file=record_button726 new_name = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+'.wav'727 new_path='./audios/'+new_name728 shutil.move(path_to_file,new_path)729 return new_name730 731 732def change_choices2():733 audio_paths=[]734 for filename in os.listdir("./audios"):735 if filename.endswith(('wav', 'mp3', 'flac', 'ogg', 'opus',736 'm4a', 'mp4', 'aac', 'alac', 'wma',737 'aiff', 'webm', 'ac3')):738 audio_paths.append(os.path.join('./audios',filename).replace('\\', '/'))739 return {"choices": sorted(audio_paths), "__type__": "update"}, {"__type__": "update"}740 741 742 743 744 745def uvr(input_url, output_path, model_name, inp_root, save_root_vocal, paths, save_root_ins, agg, format0, architecture):746 carpeta_a_eliminar = "yt_downloads"747 if os.path.exists(carpeta_a_eliminar) and os.path.isdir(carpeta_a_eliminar):748 for archivo in os.listdir(carpeta_a_eliminar):749 ruta_archivo = os.path.join(carpeta_a_eliminar, archivo)750 if os.path.isfile(ruta_archivo):751 os.remove(ruta_archivo)752 elif os.path.isdir(ruta_archivo):753 shutil.rmtree(ruta_archivo) 754 755 756 757 ydl_opts = {758 'no-windows-filenames': True,759 'restrict-filenames': True,760 'extract_audio': True,761 'format': 'bestaudio',762 'quiet': True,763 'no-warnings': True,764 }765 766 try:767 print(i18n("Downloading audio from the video..."))768 with yt_dlp.YoutubeDL(ydl_opts) as ydl:769 info_dict = ydl.extract_info(input_url, download=False)770 formatted_title = format_title(info_dict.get('title', 'default_title'))771 formatted_outtmpl = output_path + '/' + formatted_title + '.wav'772 ydl_opts['outtmpl'] = formatted_outtmpl773 ydl = yt_dlp.YoutubeDL(ydl_opts)774 ydl.download([input_url])775 print(i18n("Audio downloaded!"))776 except Exception as error:777 print(i18n("An error occurred:"), error)778 779 actual_directory = os.path.dirname(__file__)780 781 vocal_directory = os.path.join(actual_directory, save_root_vocal)782 instrumental_directory = os.path.join(actual_directory, save_root_ins)783 784 vocal_formatted = f"vocal_{formatted_title}.wav.reformatted.wav_10.wav"785 instrumental_formatted = f"instrument_{formatted_title}.wav.reformatted.wav_10.wav" 786 787 vocal_audio_path = os.path.join(vocal_directory, vocal_formatted)788 instrumental_audio_path = os.path.join(instrumental_directory, instrumental_formatted)789 790 vocal_formatted_mdx = f"{formatted_title}_vocal_.wav"791 instrumental_formatted_mdx = f"{formatted_title}_instrument_.wav"792 793 vocal_audio_path_mdx = os.path.join(vocal_directory, vocal_formatted_mdx)794 instrumental_audio_path_mdx = os.path.join(instrumental_directory, instrumental_formatted_mdx)795 796 if architecture == "VR":797 try:798 print(i18n("Starting audio conversion... (This might take a moment)"))799 inp_root, save_root_vocal, save_root_ins = [x.strip(" ").strip('"').strip("\n").strip('"').strip(" ") for x in [inp_root, save_root_vocal, save_root_ins]]800 usable_files = [os.path.join(inp_root, file) 801 for file in os.listdir(inp_root) 802 if file.endswith(tuple(sup_audioext))] 803 804 805 pre_fun = MDXNetDereverb(15) if model_name == "onnx_dereverb_By_FoxJoy" else (_audio_pre_ if "DeEcho" not in model_name else _audio_pre_new)(806 agg=int(agg),807 model_path=os.path.join(weight_uvr5_root, model_name + ".pth"),808 device=config.device,809 is_half=config.is_half,810 )811 812 try:813 if paths != None:814 paths = [path.name for path in paths]815 else:816 paths = usable_files817 818 except:819 traceback.print_exc()820 paths = usable_files821 print(paths) 822 for path in paths:823 inp_path = os.path.join(inp_root, path)824 need_reformat, done = 1, 0825 826 try:827 info = ffmpeg.probe(inp_path, cmd="ffprobe")828 if info["streams"][0]["channels"] == 2 and info["streams"][0]["sample_rate"] == "44100":829 need_reformat = 0830 pre_fun._path_audio_(inp_path, save_root_ins, save_root_vocal, format0)831 done = 1832 except:833 traceback.print_exc()834 835 if need_reformat:836 tmp_path = f"{tmp}/{os.path.basename(inp_path)}.reformatted.wav"837 os.system(f"ffmpeg -i {inp_path} -vn -acodec pcm_s16le -ac 2 -ar 44100 {tmp_path} -y")838 inp_path = tmp_path839 840 try:841 if not done:842 pre_fun._path_audio_(inp_path, save_root_ins, save_root_vocal, format0)843 print(f"{os.path.basename(inp_path)}->Success")844 except:845 print(f"{os.path.basename(inp_path)}->{traceback.format_exc()}")846 except:847 traceback.print_exc()848 finally:849 try:850 if model_name == "onnx_dereverb_By_FoxJoy":851 del pre_fun.pred.model852 del pre_fun.pred.model_853 else:854 del pre_fun.model855 856 del pre_fun857 return i18n("Finished"), vocal_audio_path, instrumental_audio_path858 except: traceback.print_exc()859 860 if torch.cuda.is_available(): torch.cuda.empty_cache()861 862 elif architecture == "MDX":863 try:864 print(i18n("Starting audio conversion... (This might take a moment)"))865 inp_root, save_root_vocal, save_root_ins = [x.strip(" ").strip('"').strip("\n").strip('"').strip(" ") for x in [inp_root, save_root_vocal, save_root_ins]]866 867 usable_files = [os.path.join(inp_root, file) 868 for file in os.listdir(inp_root) 869 if file.endswith(tuple(sup_audioext))] 870 try:871 if paths != None:872 paths = [path.name for path in paths]873 else:874 paths = usable_files875 876 except:877 traceback.print_exc()878 paths = usable_files879 print(paths) 880 invert=True881 denoise=True882 use_custom_parameter=True883 dim_f=2048884 dim_t=256885 n_fft=7680886 use_custom_compensation=True887 compensation=1.025888 suffix = "vocal_" #@param ["Vocals", "Drums", "Bass", "Other"]{allow-input: true}889 suffix_invert = "instrument_" #@param ["Instrumental", "Drumless", "Bassless", "Instruments"]{allow-input: true}890 print_settings = True # @param{type:"boolean"}891 onnx = id_to_ptm(model_name)892 compensation = compensation if use_custom_compensation or use_custom_parameter else None893 mdx_model = prepare_mdx(onnx,use_custom_parameter, dim_f, dim_t, n_fft, compensation=compensation)894 895 896 for path in paths:897 #inp_path = os.path.join(inp_root, path)898 suffix_naming = suffix if use_custom_parameter else None899 diff_suffix_naming = suffix_invert if use_custom_parameter else None900 run_mdx(onnx, mdx_model, path, format0, diff=invert,suffix=suffix_naming,diff_suffix=diff_suffix_naming,denoise=denoise)901 902 if print_settings:903 print()904 print('[MDX-Net_Colab settings used]')905 print(f'Model used: {onnx}')906 print(f'Model MD5: {mdx.MDX.get_hash(onnx)}')907 print(f'Model parameters:')908 print(f' -dim_f: {mdx_model.dim_f}')909 print(f' -dim_t: {mdx_model.dim_t}')910 print(f' -n_fft: {mdx_model.n_fft}')911 print(f' -compensation: {mdx_model.compensation}')912 print()913 print('[Input file]')914 print('filename(s): ')915 for filename in paths:916 print(f' -{filename}')917 print(f"{os.path.basename(filename)}->Success")918 except:919 traceback.print_exc()920 finally:921 try:922 del mdx_model923 return i18n("Finished"), vocal_audio_path_mdx, instrumental_audio_path_mdx924 except: traceback.print_exc()925 926 print("clean_empty_cache")927 928 if torch.cuda.is_available(): torch.cuda.empty_cache()929sup_audioext = {'wav', 'mp3', 'flac', 'ogg', 'opus',930 'm4a', 'mp4', 'aac', 'alac', 'wma',931 'aiff', 'webm', 'ac3'}932 933def load_downloaded_audio(url):934 parent_path = find_folder_parent(".", "pretrained_v2")935 try:936 infos = []937 audios_path = os.path.join(parent_path, 'audios')938 zips_path = os.path.join(parent_path, 'zips')939 940 if not os.path.exists(audios_path):941 os.mkdir(audios_path)942 943 download_file = download_from_url(url)944 if not download_file:945 print(i18n("The file could not be downloaded."))946 infos.append(i18n("The file could not be downloaded."))947 yield "\n".join(infos)948 elif download_file == "downloaded":949 print(i18n("It has been downloaded successfully."))950 infos.append(i18n("It has been downloaded successfully."))951 yield "\n".join(infos)952 elif download_file == "too much use":953 raise Exception(i18n("Too many users have recently viewed or downloaded this file"))954 elif download_file == "private link":955 raise Exception(i18n("Cannot get file from this private link"))956 957 for filename in os.listdir(zips_path):958 item_path = os.path.join(zips_path, filename)959 if item_path.split('.')[-1] in sup_audioext:960 if os.path.exists(item_path):961 shutil.move(item_path, audios_path)962 963 result = ""964 print(i18n("Audio files have been moved to the 'audios' folder."))965 infos.append(i18n("Audio files have been moved to the 'audios' folder."))966 yield "\n".join(infos)967 968 os.chdir(parent_path) 969 return result970 except Exception as e:971 os.chdir(parent_path)972 if "too much use" in str(e):973 print(i18n("Too many users have recently viewed or downloaded this file"))974 yield i18n("Too many users have recently viewed or downloaded this file")975 elif "private link" in str(e):976 print(i18n("Cannot get file from this private link"))977 yield i18n("Cannot get file from this private link")978 else:979 print(e)980 yield i18n("An error occurred downloading")981 finally:982 os.chdir(parent_path)983 984 985class error_message(Exception):986 def __init__(self, mensaje):987 self.mensaje = mensaje988 super().__init__(mensaje)989 990def get_vc(sid, to_return_protect0, to_return_protect1):991 global n_spk, tgt_sr, net_g, vc, cpt, version992 if sid == "" or sid == []:993 global hubert_model994 if hubert_model is not None: 995 print("clean_empty_cache")996 del net_g, n_spk, vc, hubert_model, tgt_sr 997 hubert_model = net_g = n_spk = vc = hubert_model = tgt_sr = None998 if torch.cuda.is_available():999 torch.cuda.empty_cache()1000 if_f0 = cpt.get("f0", 1)1001 version = cpt.get("version", "v1")1002 if version == "v1":1003 if if_f0 == 1:1004 net_g = SynthesizerTrnMs256NSFsid(1005 *cpt["config"], is_half=config.is_half1006 )1007 else:1008 net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])1009 elif version == "v2":1010 if if_f0 == 1:1011 net_g = SynthesizerTrnMs768NSFsid(1012 *cpt["config"], is_half=config.is_half1013 )1014 else:1015 net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])1016 del net_g, cpt1017 if torch.cuda.is_available():1018 torch.cuda.empty_cache()1019 cpt = None1020 return (1021 {"visible": False, "__type__": "update"},1022 {"visible": False, "__type__": "update"},1023 {"visible": False, "__type__": "update"},1024 )1025 person = "%s/%s" % (weight_root, sid)1026 print("loading %s" % person)1027 cpt = torch.load(person, map_location="cpu")1028 tgt_sr = cpt["config"][-1]1029 cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] 1030 if_f0 = cpt.get("f0", 1)1031 if if_f0 == 0:1032 to_return_protect0 = to_return_protect1 = {1033 "visible": False,1034 "value": 0.5,1035 "__type__": "update",1036 }1037 else:1038 to_return_protect0 = {1039 "visible": True,1040 "value": to_return_protect0,1041 "__type__": "update",1042 }1043 to_return_protect1 = {1044 "visible": True,1045 "value": to_return_protect1,1046 "__type__": "update",1047 }1048 version = cpt.get("version", "v1")1049 if version == "v1":1050 if if_f0 == 1:1051 net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)1052 else:1053 net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])1054 elif version == "v2":1055 if if_f0 == 1:1056 net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=config.is_half)1057 else:1058 net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])1059 del net_g.enc_q1060 print(net_g.load_state_dict(cpt["weight"], strict=False))1061 net_g.eval().to(config.device)1062 if config.is_half:1063 net_g = net_g.half()1064 else:1065 net_g = net_g.float()1066 vc = VC(tgt_sr, config)1067 n_spk = cpt["config"][-3]1068 return (1069 {"visible": True, "maximum": n_spk, "__type__": "update"},1070 to_return_protect0,1071 to_return_protect1,1072 ) 1073 1074def update_model_choices(select_value):1075 model_ids = get_model_list()1076 model_ids_list = list(model_ids)1077 if select_value == "VR":1078 return {"choices": uvr5_names, "__type__": "update"}1079 elif select_value == "MDX":1080 return {"choices": model_ids_list, "__type__": "update"}1081 1082def download_model():1083 gr.Markdown(value="# " + i18n("Download Model"))1084 gr.Markdown(value=i18n("It is used to download your inference models."))1085 with gr.Row():1086 model_url=gr.Textbox(label=i18n("Url:"))1087 with gr.Row():1088 download_model_status_bar=gr.Textbox(label=i18n("Status:"))1089 with gr.Row():1090 download_button=gr.Button(i18n("Download"))1091 download_button.click(fn=load_downloaded_model, inputs=[model_url], outputs=[download_model_status_bar])1092 1093def download_backup():1094 gr.Markdown(value="# " + i18n("Download Backup"))1095 gr.Markdown(value=i18n("It is used to download your training backups."))1096 with gr.Row():1097 model_url=gr.Textbox(label=i18n("Url:"))1098 with gr.Row():1099 download_model_status_bar=gr.Textbox(label=i18n("Status:"))1100 with gr.Row():1101 download_button=gr.Button(i18n("Download"))1102 download_button.click(fn=load_downloaded_backup, inputs=[model_url], outputs=[download_model_status_bar])1103 1104def update_dataset_list(name):1105 new_datasets = []1106 for foldername in os.listdir("./datasets"):1107 if "." not in foldername:1108 new_datasets.append(os.path.join(find_folder_parent(".","pretrained"),"datasets",foldername))1109 return gr.Dropdown.update(choices=new_datasets)1110 1111def download_dataset(trainset_dir4):1112 gr.Markdown(value="# " + i18n("Download Dataset"))1113 gr.Markdown(value=i18n("Download the dataset with the audios in a compatible format (.wav/.flac) to train your model."))1114 with gr.Row():1115 dataset_url=gr.Textbox(label=i18n("Url:"))1116 with gr.Row():1117 load_dataset_status_bar=gr.Textbox(label=i18n("Status:"))1118 with gr.Row():1119 load_dataset_button=gr.Button(i18n("Download"))1120 load_dataset_button.click(fn=load_dowloaded_dataset, inputs=[dataset_url], outputs=[load_dataset_status_bar])1121 load_dataset_status_bar.change(update_dataset_list, dataset_url, trainset_dir4)1122 1123def download_audio():1124 gr.Markdown(value="# " + i18n("Download Audio"))1125 gr.Markdown(value=i18n("Download audios of any format for use in inference (recommended for mobile users)."))1126 with gr.Row():1127 audio_url=gr.Textbox(label=i18n("Url:"))1128 with gr.Row():1129 download_audio_status_bar=gr.Textbox(label=i18n("Status:"))1130 with gr.Row():1131 download_button2=gr.Button(i18n("Download"))1132 download_button2.click(fn=load_downloaded_audio, inputs=[audio_url], outputs=[download_audio_status_bar])1133 1134def youtube_separator():1135 gr.Markdown(value="# " + i18n("Separate YouTube tracks"))1136 gr.Markdown(value=i18n("Download audio from a YouTube video and automatically separate the vocal and instrumental tracks"))1137 with gr.Row():1138 input_url = gr.inputs.Textbox(label=i18n("Enter the YouTube link:"))1139 output_path = gr.Textbox(1140 label=i18n("Enter the path of the audio folder to be processed (copy it from the address bar of the file manager):"),1141 value=os.path.abspath(os.getcwd()).replace('\\', '/') + "/yt_downloads",1142 visible=False,1143 )1144 advanced_settings_checkbox = gr.Checkbox(1145 value=False,1146 label=i18n("Advanced Settings"),1147 interactive=True,1148 )1149 with gr.Row(label = i18n("Advanced Settings"), visible=False, variant='compact') as advanced_settings:1150 with gr.Column(): 1151 model_select = gr.Radio(1152 label=i18n("Model Architecture:"),1153 choices=["VR", "MDX"],1154 value="VR",1155 interactive=True,1156 )1157 model_choose = gr.Dropdown(label=i18n("Model: (Be aware that in some models the named vocal will be the instrumental)"), 1158 choices=uvr5_names,1159 value="HP5_only_main_vocal" 1160 )1161 with gr.Row():1162 agg = gr.Slider(1163 minimum=0,1164 maximum=20,1165 step=1,1166 label=i18n("Vocal Extraction Aggressive"),1167 value=10,1168 interactive=True,1169 )1170 with gr.Row(): 1171 opt_vocal_root = gr.Textbox(1172 label=i18n("Specify the output folder for vocals:"), value="audios",1173 )1174 opt_ins_root = gr.Textbox(1175 label=i18n("Specify the output folder for accompaniment:"), value="audio-others",1176 ) 1177 dir_wav_input = gr.Textbox(1178 label=i18n("Enter the path of the audio folder to be processed:"),1179 value=((os.getcwd()).replace('\\', '/') + "/yt_downloads"),1180 visible=False,1181 )1182 format0 = gr.Radio(1183 label=i18n("Export file format"),1184 choices=["wav", "flac", "mp3", "m4a"],1185 value="wav",1186 visible=False,1187 interactive=True,1188 )1189 wav_inputs = gr.File(1190 file_count="multiple", label=i18n("You can also input audio files in batches. Choose one of the two options. Priority is given to reading from the folder."),1191 visible=False,1192 )1193 model_select.change(1194 fn=update_model_choices,1195 inputs=model_select,1196 outputs=model_choose,1197 )1198 with gr.Row():1199 vc_output4 = gr.Textbox(label=i18n("Status:"))1200 vc_output5 = gr.Audio(label=i18n("Vocal"), type='filepath')