wonkitty/apple_oh
0
1import os2import subprocess3import shutil4from concurrent.futures import ThreadPoolExecutor, as_completed5from tqdm.notebook import tqdm6from pathlib import Path7import requests8 9def run_script():10 def run_cmd(cmd):11 process = subprocess.run(cmd, shell=True, check=True, text=True)12 return process.stdout13 14 # Change the current directory to /content/15 os.chdir('/content/')16 print("Changing dir to /content/")17 18 # Your function to edit the file19 def edit_file(file_path):20 temp_file_path = "/tmp/temp_file.py"21 changes_made = False22 with open(file_path, "r") as file, open(temp_file_path, "w") as temp_file:23 previous_line = ""24 second_previous_line = ""25 for line in file:26 new_line = line.replace("value=160", "value=128")27 if new_line != line:28 print("Replaced 'value=160' with 'value=128'")29 changes_made = True30 line = new_line31 32 new_line = line.replace("crepe hop length: 160", "crepe hop length: 128")33 if new_line != line:34 print("Replaced 'crepe hop length: 160' with 'crepe hop length: 128'")35 changes_made = True36 line = new_line37 38 new_line = line.replace("value=0.88", "value=0.75")39 if new_line != line:40 print("Replaced 'value=0.88' with 'value=0.75'")41 changes_made = True42 line = new_line43 44 if "label=i18n(\"输入源音量包络替换输出音量包络融合比例,越靠近1越使用输出包络\")" in previous_line and "value=1," in line:45 new_line = line.replace("value=1,", "value=0.25,")46 if new_line != line:47 print("Replaced 'value=1,' with 'value=0.25,' based on the condition")48 changes_made = True49 line = new_line50 51 if "label=i18n(\"总训练轮数total_epoch\")" in previous_line and "value=20," in line:52 new_line = line.replace("value=20,", "value=500,")53 if new_line != line:54 print("Replaced 'value=20,' with 'value=500,' based on the condition for DEFAULT EPOCH")55 changes_made = True56 line = new_line57 58 if 'choices=["pm", "harvest", "dio", "crepe", "crepe-tiny", "mangio-crepe", "mangio-crepe-tiny"], # Fork Feature. Add Crepe-Tiny' in previous_line:59 if 'value="pm",' in line:60 new_line = line.replace('value="pm",', 'value="mangio-crepe",')61 if new_line != line:62 print("Replaced 'value=\"pm\",' with 'value=\"mangio-crepe\",' based on the condition")63 changes_made = True64 line = new_line65 66 new_line = line.replace('label=i18n("输入训练文件夹路径"), value="E:\\\\语音音频+标注\\\\米津玄师\\\\src"', 'label=i18n("输入训练文件夹路径"), value="/content/dataset/"')67 if new_line != line:68 print("Replaced 'label=i18n(\"输入训练文件夹路径\"), value=\"E:\\\\语音音频+标注\\\\米津玄师\\\\src\"' with 'label=i18n(\"输入训练文件夹路径\"), value=\"/content/dataset/\"'")69 changes_made = True70 line = new_line71 72 if 'label=i18n("是否仅保存最新的ckpt文件以节省硬盘空间"),' in second_previous_line:73 if 'value=i18n("否"),' in line:74 new_line = line.replace('value=i18n("否"),', 'value=i18n("是"),')75 if new_line != line:76 print("Replaced 'value=i18n(\"否\"),' with 'value=i18n(\"是\"),' based on the condition for SAVE ONLY LATEST")77 changes_made = True78 line = new_line79 80 if 'label=i18n("是否在每次保存时间点将最终小模型保存至weights文件夹"),' in second_previous_line:81 if 'value=i18n("否"),' in line:82 new_line = line.replace('value=i18n("否"),', 'value=i18n("是"),')83 if new_line != line:84 print("Replaced 'value=i18n(\"否\"),' with 'value=i18n(\"是\"),' based on the condition for SAVE SMALL WEIGHTS")85 changes_made = True86 line = new_line87 88 temp_file.write(line)89 second_previous_line = previous_line90 previous_line = line91 92 # After finished, we replace the original file with the temp one93 import shutil94 shutil.move(temp_file_path, file_path)95 96 if changes_made:97 print("Changes made and file saved successfully.")98 else:99 print("No changes were needed.")100 101 # Define the repo path102 repo_path = '/content/Applio-RVC-Fork'103 104 def copy_all_files_in_directory(src_dir, dest_dir):105 # Iterate over all files in source directory106 for item in Path(src_dir).glob('*'):107 if item.is_file():108 # Copy each file to destination directory109 shutil.copy(item, dest_dir)110 else:111 # If it's a directory, make a new directory in the destination and copy the files recursively112 new_dest = Path(dest_dir) / item.name113 new_dest.mkdir(exist_ok=True)114 copy_all_files_in_directory(str(item), str(new_dest))115 116 def clone_and_copy_repo(repo_path):117 # New repository link118 new_repo_link = "https://github.com/IAHispano/Applio-RVC-Fork/"119 # Temporary path to clone the repository120 temp_repo_path = "/content/temp_Applio-RVC-Fork"121 # New folder name122 new_folder_name = "Applio-RVC-Fork"123 124 # Clone the latest code from the new repository to a temporary location125 run_cmd(f"git clone {new_repo_link} {temp_repo_path}")126 os.chdir(temp_repo_path)127 128 run_cmd(f"git checkout 3fa4dad3d8961e5ca2522e9e12c0b4ddb71ad402")129 run_cmd(f"git checkout f9e606c279cb49420597519b0a83b92be81e42e4")130 run_cmd(f"git checkout 9e305588844c5442d58add1061b29beeca89d679")131 run_cmd(f"git checkout bf92dc1eb54b4f28d6396a4d1820a25896cc9af8")132 run_cmd(f"git checkout c3810e197d3cb98039973b2f723edf967ecd9e61")133 run_cmd(f"git checkout a33159efd134c2413b0afe26a76b7dc87926d2de")134 run_cmd(f"git checkout 24e251fb62c662e39ac5cf9253cc65deb9be94ec")135 run_cmd(f"git checkout ad5667d3017e93232dba85969cddac1322ba2902")136 run_cmd(f"git checkout ce9715392cf52dd5a0e18e00d1b5e408f08dbf27")137 run_cmd(f"git checkout 7c7da3f2ac68f3bd8f3ad5ca5c700f18ab9f90eb")138 run_cmd(f"git checkout 4ac395eab101955e8960b50d772c26f592161764")139 run_cmd(f"git checkout b15b358702294c7375761584e5276c811ffab5e8")140 run_cmd(f"git checkout 1501793dc490982db9aca84a50647764caa66e51")141 run_cmd(f"git checkout 21f7faf57219c75e6ba837062350391a803e9ae2")142 run_cmd(f"git checkout b5eb689fbc409b49f065a431817f822f554cebe7")143 run_cmd(f"git checkout 7e02fae1ebf24cb151bf6cbe787d06734aa65862")144 run_cmd(f"git checkout 6aea5ea18ed0b9a1e03fa5d268d6bc3c616672a9")145 run_cmd(f"git checkout f0f9b25717e59116473fb42bd7f9252cfc32b398")146 run_cmd(f"git checkout b394de424088a81fc081224bc27338a8651ad3b2")147 run_cmd(f"git checkout f1999406a88b80c965d2082340f5ea2bfa9ab67a")148 run_cmd(f"git checkout d98a0fa8dc715308dfc73eac5c553b69c6ee072b")149 run_cmd(f"git checkout d73267a415fb0eba98477afa43ef71ffd82a7157")150 run_cmd(f"git checkout 1a03d01356ae79179e1fb8d8915dc9cc79925742")151 run_cmd(f"git checkout 81497bb3115e92c754300c9b3992df428886a3e9")152 run_cmd(f"git checkout c5af1f8edcf79cb70f065c0110e279e78e48caf9")153 run_cmd(f"git checkout cdb3c90109387fa4dfa92f53c3864c71170ffc77")154 155 # Edit the file here, before copying156 #edit_file(f"{temp_repo_path}/infer-web.py")157 158 # Copy all files from the cloned repository to the existing path159 copy_all_files_in_directory(temp_repo_path, repo_path)160 print(f"Copying all {new_folder_name} files from GitHub.")161 162 # Change working directory back to /content/163 os.chdir('/content/')164 print("Changed path back to /content/")165 166 # Remove the temporary cloned repository167 shutil.rmtree(temp_repo_path)168 169 # Call the function170 clone_and_copy_repo(repo_path)171 172 # Download the credentials file for RVC archive sheet173 os.makedirs('/content/Applio-RVC-Fork/stats/', exist_ok=True)174 run_cmd("wget -q https://cdn.discordapp.com/attachments/945486970883285045/1114717554481569802/peppy-generator-388800-07722f17a188.json -O /content/Applio-RVC-Fork/stats/peppy-generator-388800-07722f17a188.json")175 176 # Forcefully delete any existing torchcrepe dependencies downloaded from an earlier run just in case177 shutil.rmtree('/content/Applio-RVC-Fork/torchcrepe', ignore_errors=True)178 shutil.rmtree('/content/torchcrepe', ignore_errors=True)179 180 # Download the torchcrepe folder from the maxrmorrison/torchcrepe repository181 run_cmd("git clone https://github.com/maxrmorrison/torchcrepe.git")182 shutil.move('/content/torchcrepe/torchcrepe', '/content/Applio-RVC-Fork/')183 shutil.rmtree('/content/torchcrepe', ignore_errors=True) # Delete the torchcrepe repository folder184 185 # Change the current directory to /content/Applio-RVC-Fork186 os.chdir('/content/Applio-RVC-Fork')187 os.makedirs('pretrained', exist_ok=True)188 os.makedirs('uvr5_weights', exist_ok=True)189 190def download_file(url, filepath):191 response = requests.get(url, stream=True)192 response.raise_for_status()193 194 with open(filepath, "wb") as file:195 for chunk in response.iter_content(chunk_size=8192):196 if chunk:197 file.write(chunk)198 199def download_pretrained_models():200 pretrained_models = {201 "pretrained": [202 "D40k.pth",203 "G40k.pth",204 "f0D40k.pth",205 "f0G40k.pth"206 ],207 "pretrained_v2": [208 "D40k.pth",209 "G40k.pth",210 "f0D40k.pth",211 "f0G40k.pth",212 "f0G48k.pth",213 "f0D48k.pth"214 ],215 "uvr5_weights": [216 "HP2-人声vocals+非人声instrumentals.pth",217 "HP5-主旋律人声vocals+其他instrumentals.pth",218 "VR-DeEchoNormal.pth",219 "VR-DeEchoDeReverb.pth",220 "VR-DeEchoAggressive.pth",221 "HP5_only_main_vocal.pth",222 "HP3_all_vocals.pth",223 "HP2_all_vocals.pth"224 ]225 }226 part2 = "I"227 base_url = "https://huggingface.co/lj1995/VoiceConversionWebU" + part2 + "/resolve/main/"228 base_path = "/content/Applio-RVC-Fork/"229 base_pathm = base_path230 231 # Calculate total number of files to download232 total_files = sum(len(files) for files in pretrained_models.values()) + 1 # +1 for hubert_base.pt233 234 with tqdm(total=total_files, desc="Downloading files") as pbar:235 for folder, models in pretrained_models.items():236 folder_path = os.path.join(base_path, folder)237 os.makedirs(folder_path, exist_ok=True)238 for model in models:239 url = base_url + folder + "/" + model240 filepath = os.path.join(folder_path, model)241 download_file(url, filepath)242 pbar.update()243 244 # Download hubert_base.pt to the base path245 hubert_url = base_url + "hubert_base.pt"246 hubert_filepath = os.path.join(base_pathm, "hubert_base.pt")247 download_file(hubert_url, hubert_filepath)248 pbar.update()249def clone_repository(run_download):250 with ThreadPoolExecutor(max_workers=2) as executor:251 executor.submit(run_script)252 if run_download:253 executor.submit(download_pretrained_models)254 