CoolFace
Apppublic

mosibi/man

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
core.py782 linesDownload Raw Back to root
1import os2import sys3import json4import argparse5import subprocess6import spaces7 8now_dir = os.getcwd()9sys.path.append(now_dir)10 11from rvc.configs.config import Config12 13from rvc.lib.tools.prerequisites_download import prequisites_download_pipeline14 15from rvc.infer.infer import infer_pipeline16 17from rvc.lib.tools.model_download import model_download_pipeline18 19config = Config()20current_script_directory = os.path.dirname(os.path.realpath(__file__))21logs_path = os.path.join(current_script_directory, "logs")22 23# Get TTS Voices24with open(os.path.join("rvc", "lib", "tools", "tts_voices.json"), "r") as f:25    voices_data = json.load(f)26 27locales = list({voice["Locale"] for voice in voices_data})28 29 30# Infer31@spaces.GPU32def run_infer_script(33    f0up_key,34    filter_radius,35    index_rate,36    rms_mix_rate,37    protect,38    hop_length,39    f0method,40    input_path,41    output_path,42    pth_path,43    index_path,44    split_audio,45    f0autotune,46    clean_audio,47    clean_strength,48    export_format,49    embedder_model,50    embedder_model_custom,51    upscale_audio,52):53    f0autotune = "True" if str(f0autotune) == "True" else "False"54    clean_audio = "True" if str(clean_audio) == "True" else "False"55    upscale_audio = "True" if str(upscale_audio) == "True" else "False"56    infer_pipeline(57        f0up_key,58        filter_radius,59        index_rate,60        rms_mix_rate,61        protect,62        hop_length,63        f0method,64        input_path,65        output_path,66        pth_path,67        index_path,68        split_audio,69        f0autotune,70        clean_audio,71        clean_strength,72        export_format,73        embedder_model,74        embedder_model_custom,75        upscale_audio,76    )77    return f"File {input_path} inferred successfully.", output_path.replace(78        ".wav", f".{export_format.lower()}"79    )80 81 82# Batch infer83@spaces.GPU84def run_batch_infer_script(85    f0up_key,86    filter_radius,87    index_rate,88    rms_mix_rate,89    protect,90    hop_length,91    f0method,92    input_folder,93    output_folder,94    pth_path,95    index_path,96    split_audio,97    f0autotune,98    clean_audio,99    clean_strength,100    export_format,101    embedder_model,102    embedder_model_custom,103    upscale_audio,104):105    f0autotune = "True" if str(f0autotune) == "True" else "False"106    clean_audio = "True" if str(clean_audio) == "True" else "False"107    upscale_audio = "True" if str(upscale_audio) == "True" else "False"108    audio_files = [109        f for f in os.listdir(input_folder) if f.endswith((".mp3", ".wav", ".flac"))110    ]111    print(f"Detected {len(audio_files)} audio files for inference.")112 113    for audio_file in audio_files:114        if "_output" in audio_file:115            pass116        else:117            input_path = os.path.join(input_folder, audio_file)118            output_file_name = os.path.splitext(os.path.basename(audio_file))[0]119            output_path = os.path.join(120                output_folder,121                f"{output_file_name}_output{os.path.splitext(audio_file)[1]}",122            )123            print(f"Inferring {input_path}...")124 125            infer_pipeline(126                f0up_key,127                filter_radius,128                index_rate,129                rms_mix_rate,130                protect,131                hop_length,132                f0method,133                input_path,134                output_path,135                pth_path,136                index_path,137                split_audio,138                f0autotune,139                clean_audio,140                clean_strength,141                export_format,142                embedder_model,143                embedder_model_custom,144                upscale_audio,145            )146 147    return f"Files from {input_folder} inferred successfully."148 149 150# TTS151@spaces.GPU152def run_tts_script(153    tts_text,154    tts_voice,155    tts_rate,156    f0up_key,157    filter_radius,158    index_rate,159    rms_mix_rate,160    protect,161    hop_length,162    f0method,163    output_tts_path,164    output_rvc_path,165    pth_path,166    index_path,167    split_audio,168    f0autotune,169    clean_audio,170    clean_strength,171    export_format,172    embedder_model,173    embedder_model_custom,174    upscale_audio,175):176    f0autotune = "True" if str(f0autotune) == "True" else "False"177    clean_audio = "True" if str(clean_audio) == "True" else "False"178    upscale_audio = "True" if str(upscale_audio) == "True" else "False"179    tts_script_path = os.path.join("rvc", "lib", "tools", "tts.py")180 181    if os.path.exists(output_tts_path):182        os.remove(output_tts_path)183 184    command_tts = [185        "python",186        tts_script_path,187        tts_text,188        tts_voice,189        str(tts_rate),190        output_tts_path,191    ]192    subprocess.run(command_tts)193 194    infer_pipeline(195        f0up_key,196        filter_radius,197        index_rate,198        rms_mix_rate,199        protect,200        hop_length,201        f0method,202        output_tts_path,203        output_rvc_path,204        pth_path,205        index_path,206        split_audio,207        f0autotune,208        clean_audio,209        clean_strength,210        export_format,211        embedder_model,212        embedder_model_custom,213        upscale_audio,214    )215 216    return f"Text {tts_text} synthesized successfully.", output_rvc_path.replace(217        ".wav", f".{export_format.lower()}"218    )219 220 221# Download222def run_download_script(model_link):223    model_download_pipeline(model_link)224    return f"Model downloaded successfully."225 226 227# Prerequisites228def run_prerequisites_script(pretraineds_v1, pretraineds_v2, models, exe):229    prequisites_download_pipeline(pretraineds_v1, pretraineds_v2, models, exe)230    return "Prerequisites installed successfully."231 232# Parse arguments233def parse_arguments():234    parser = argparse.ArgumentParser(235        description="Run the main.py script with specific parameters."236    )237    subparsers = parser.add_subparsers(238        title="subcommands", dest="mode", help="Choose a mode"239    )240 241    # Parser for 'infer' mode242    infer_parser = subparsers.add_parser("infer", help="Run inference")243    infer_parser.add_argument(244        "--f0up_key",245        type=str,246        help="Value for f0up_key",247        choices=[str(i) for i in range(-24, 25)],248        default="0",249    )250    infer_parser.add_argument(251        "--filter_radius",252        type=str,253        help="Value for filter_radius",254        choices=[str(i) for i in range(11)],255        default="3",256    )257    infer_parser.add_argument(258        "--index_rate",259        type=str,260        help="Value for index_rate",261        choices=[str(i / 10) for i in range(11)],262        default="0.3",263    )264    infer_parser.add_argument(265        "--rms_mix_rate",266        type=str,267        help="Value for rms_mix_rate",268        choices=[str(i / 10) for i in range(11)],269        default="1",270    )271    infer_parser.add_argument(272        "--protect",273        type=str,274        help="Value for protect",275        choices=[str(i / 10) for i in range(6)],276        default="0.33",277    )278    infer_parser.add_argument(279        "--hop_length",280        type=str,281        help="Value for hop_length",282        choices=[str(i) for i in range(1, 513)],283        default="128",284    )285    infer_parser.add_argument(286        "--f0method",287        type=str,288        help="Value for f0method",289        choices=[290            "pm",291            "harvest",292            "dio",293            "crepe",294            "crepe-tiny",295            "rmvpe",296            "fcpe",297            "hybrid[crepe+rmvpe]",298            "hybrid[crepe+fcpe]",299            "hybrid[rmvpe+fcpe]",300            "hybrid[crepe+rmvpe+fcpe]",301        ],302        default="rmvpe",303    )304    infer_parser.add_argument("--input_path", type=str, help="Input path")305    infer_parser.add_argument("--output_path", type=str, help="Output path")306    infer_parser.add_argument("--pth_path", type=str, help="Path to the .pth file")307    infer_parser.add_argument(308        "--index_path",309        type=str,310        help="Path to the .index file",311    )312    infer_parser.add_argument(313        "--split_audio",314        type=str,315        help="Enable split audio",316        choices=["True", "False"],317        default="False",318    )319    infer_parser.add_argument(320        "--f0autotune",321        type=str,322        help="Enable autotune",323        choices=["True", "False"],324        default="False",325    )326    infer_parser.add_argument(327        "--clean_audio",328        type=str,329        help="Enable clean audio",330        choices=["True", "False"],331        default="False",332    )333    infer_parser.add_argument(334        "--clean_strength",335        type=str,336        help="Value for clean_strength",337        choices=[str(i / 10) for i in range(11)],338        default="0.7",339    )340    infer_parser.add_argument(341        "--export_format",342        type=str,343        help="Export format",344        choices=["WAV", "MP3", "FLAC", "OGG", "M4A"],345        default="WAV",346    )347    infer_parser.add_argument(348        "--embedder_model",349        type=str,350        help="Embedder model",351        choices=["contentvec", "hubert", "custom"],352        default="hubert",353    )354    infer_parser.add_argument(355        "--embedder_model_custom",356        type=str,357        help="Custom Embedder model",358        default=None,359    )360    infer_parser.add_argument(361        "--upscale_audio",362        type=str,363        help="Enable audio upscaling",364        choices=["True", "False"],365        default="False",366    )367 368    # Parser for 'batch_infer' mode369    batch_infer_parser = subparsers.add_parser(370        "batch_infer", help="Run batch inference"371    )372    batch_infer_parser.add_argument(373        "--f0up_key",374        type=str,375        help="Value for f0up_key",376        choices=[str(i) for i in range(-24, 25)],377        default="0",378    )379    batch_infer_parser.add_argument(380        "--filter_radius",381        type=str,382        help="Value for filter_radius",383        choices=[str(i) for i in range(11)],384        default="3",385    )386    batch_infer_parser.add_argument(387        "--index_rate",388        type=str,389        help="Value for index_rate",390        choices=[str(i / 10) for i in range(11)],391        default="0.3",392    )393    batch_infer_parser.add_argument(394        "--rms_mix_rate",395        type=str,396        help="Value for rms_mix_rate",397        choices=[str(i / 10) for i in range(11)],398        default="1",399    )400    batch_infer_parser.add_argument(401        "--protect",402        type=str,403        help="Value for protect",404        choices=[str(i / 10) for i in range(6)],405        default="0.33",406    )407    batch_infer_parser.add_argument(408        "--hop_length",409        type=str,410        help="Value for hop_length",411        choices=[str(i) for i in range(1, 513)],412        default="128",413    )414    batch_infer_parser.add_argument(415        "--f0method",416        type=str,417        help="Value for f0method",418        choices=[419            "pm",420            "harvest",421            "dio",422            "crepe",423            "crepe-tiny",424            "rmvpe",425            "fcpe",426            "hybrid[crepe+rmvpe]",427            "hybrid[crepe+fcpe]",428            "hybrid[rmvpe+fcpe]",429            "hybrid[crepe+rmvpe+fcpe]",430        ],431        default="rmvpe",432    )433    batch_infer_parser.add_argument("--input_folder", type=str, help="Input folder")434    batch_infer_parser.add_argument("--output_folder", type=str, help="Output folder")435    batch_infer_parser.add_argument(436        "--pth_path", type=str, help="Path to the .pth file"437    )438    batch_infer_parser.add_argument(439        "--index_path",440        type=str,441        help="Path to the .index file",442    )443    batch_infer_parser.add_argument(444        "--split_audio",445        type=str,446        help="Enable split audio",447        choices=["True", "False"],448        default="False",449    )450    batch_infer_parser.add_argument(451        "--f0autotune",452        type=str,453        help="Enable autotune",454        choices=["True", "False"],455        default="False",456    )457    batch_infer_parser.add_argument(458        "--clean_audio",459        type=str,460        help="Enable clean audio",461        choices=["True", "False"],462        default="False",463    )464    batch_infer_parser.add_argument(465        "--clean_strength",466        type=str,467        help="Value for clean_strength",468        choices=[str(i / 10) for i in range(11)],469        default="0.7",470    )471    batch_infer_parser.add_argument(472        "--export_format",473        type=str,474        help="Export format",475        choices=["WAV", "MP3", "FLAC", "OGG", "M4A"],476        default="WAV",477    )478    batch_infer_parser.add_argument(479        "--embedder_model",480        type=str,481        help="Embedder model",482        choices=["contentvec", "hubert", "custom"],483        default="hubert",484    )485    batch_infer_parser.add_argument(486        "--embedder_model_custom",487        type=str,488        help="Custom Embedder model",489        default=None,490    )491    batch_infer_parser.add_argument(492        "--upscale_audio",493        type=str,494        help="Enable audio upscaling",495        choices=["True", "False"],496        default="False",497    )498 499    # Parser for 'tts' mode500    tts_parser = subparsers.add_parser("tts", help="Run TTS")501    tts_parser.add_argument(502        "--tts_text",503        type=str,504        help="Text to be synthesized",505    )506    tts_parser.add_argument(507        "--tts_voice",508        type=str,509        help="Voice to be used",510        choices=locales,511    )512    tts_parser.add_argument(513        "--tts_rate",514        type=str,515        help="Increase or decrease TTS speed",516        choices=[str(i) for i in range(-100, 100)],517        default="0",518    )519    tts_parser.add_argument(520        "--f0up_key",521        type=str,522        help="Value for f0up_key",523        choices=[str(i) for i in range(-24, 25)],524        default="0",525    )526    tts_parser.add_argument(527        "--filter_radius",528        type=str,529        help="Value for filter_radius",530        choices=[str(i) for i in range(11)],531        default="3",532    )533    tts_parser.add_argument(534        "--index_rate",535        type=str,536        help="Value for index_rate",537        choices=[str(i / 10) for i in range(11)],538        default="0.3",539    )540    tts_parser.add_argument(541        "--rms_mix_rate",542        type=str,543        help="Value for rms_mix_rate",544        choices=[str(i / 10) for i in range(11)],545        default="1",546    )547    tts_parser.add_argument(548        "--protect",549        type=str,550        help="Value for protect",551        choices=[str(i / 10) for i in range(6)],552        default="0.33",553    )554    tts_parser.add_argument(555        "--hop_length",556        type=str,557        help="Value for hop_length",558        choices=[str(i) for i in range(1, 513)],559        default="128",560    )561    tts_parser.add_argument(562        "--f0method",563        type=str,564        help="Value for f0method",565        choices=[566            "pm",567            "harvest",568            "dio",569            "crepe",570            "crepe-tiny",571            "rmvpe",572            "fcpe",573            "hybrid[crepe+rmvpe]",574            "hybrid[crepe+fcpe]",575            "hybrid[rmvpe+fcpe]",576            "hybrid[crepe+rmvpe+fcpe]",577        ],578        default="rmvpe",579    )580    tts_parser.add_argument("--output_tts_path", type=str, help="Output tts path")581    tts_parser.add_argument("--output_rvc_path", type=str, help="Output rvc path")582    tts_parser.add_argument("--pth_path", type=str, help="Path to the .pth file")583    tts_parser.add_argument(584        "--index_path",585        type=str,586        help="Path to the .index file",587    )588    tts_parser.add_argument(589        "--split_audio",590        type=str,591        help="Enable split audio",592        choices=["True", "False"],593        default="False",594    )595    tts_parser.add_argument(596        "--f0autotune",597        type=str,598        help="Enable autotune",599        choices=["True", "False"],600        default="False",601    )602    tts_parser.add_argument(603        "--clean_audio",604        type=str,605        help="Enable clean audio",606        choices=["True", "False"],607        default="False",608    )609    tts_parser.add_argument(610        "--clean_strength",611        type=str,612        help="Value for clean_strength",613        choices=[str(i / 10) for i in range(11)],614        default="0.7",615    )616    tts_parser.add_argument(617        "--export_format",618        type=str,619        help="Export format",620        choices=["WAV", "MP3", "FLAC", "OGG", "M4A"],621        default="WAV",622    )623    tts_parser.add_argument(624        "--embedder_model",625        type=str,626        help="Embedder model",627        choices=["contentvec", "hubert", "custom"],628        default="hubert",629    )630    tts_parser.add_argument(631        "--embedder_model_custom",632        type=str,633        help="Custom Embedder model",634        default=None,635    )636    tts_parser.add_argument(637        "--upscale_audio",638        type=str,639        help="Enable audio upscaling",640        choices=["True", "False"],641        default="False",642    )643 644    # Parser for 'download' mode645    download_parser = subparsers.add_parser("download", help="Download models")646    download_parser.add_argument(647        "--model_link",648        type=str,649        help="Link of the model",650    )651 652    # Parser for 'prerequisites' mode653    prerequisites_parser = subparsers.add_parser(654        "prerequisites", help="Install prerequisites"655    )656    prerequisites_parser.add_argument(657        "--pretraineds_v1",658        type=str,659        choices=["True", "False"],660        default="True",661        help="Download pretrained models for v1",662    )663    prerequisites_parser.add_argument(664        "--pretraineds_v2",665        type=str,666        choices=["True", "False"],667        default="True",668        help="Download pretrained models for v2",669    )670    prerequisites_parser.add_argument(671        "--models",672        type=str,673        choices=["True", "False"],674        default="True",675        help="Donwload models",676    )677    prerequisites_parser.add_argument(678        "--exe",679        type=str,680        choices=["True", "False"],681        default="True",682        help="Download executables",683    )684 685    return parser.parse_args()686 687 688def main():689    if len(sys.argv) == 1:690        print("Please run the script with '-h' for more information.")691        sys.exit(1)692 693    args = parse_arguments()694 695    try:696        if args.mode == "infer":697            run_infer_script(698                str(args.f0up_key),699                str(args.filter_radius),700                str(args.index_rate),701                str(args.rms_mix_rate),702                str(args.protect),703                str(args.hop_length),704                str(args.f0method),705                str(args.input_path),706                str(args.output_path),707                str(args.pth_path),708                str(args.index_path),709                str(args.split_audio),710                str(args.f0autotune),711                str(args.clean_audio),712                str(args.clean_strength),713                str(args.export_format),714                str(args.embedder_model),715                str(args.embedder_model_custom),716                str(args.upscale_audio),717            )718        elif args.mode == "batch_infer":719            run_batch_infer_script(720                str(args.f0up_key),721                str(args.filter_radius),722                str(args.index_rate),723                str(args.rms_mix_rate),724                str(args.protect),725                str(args.hop_length),726                str(args.f0method),727                str(args.input_folder),728                str(args.output_folder),729                str(args.pth_path),730                str(args.index_path),731                str(args.split_audio),732                str(args.f0autotune),733                str(args.clean_audio),734                str(args.clean_strength),735                str(args.export_format),736                str(args.embedder_model),737                str(args.embedder_model_custom),738                str(args.upscale_audio),739            )740        elif args.mode == "tts":741            run_tts_script(742                str(args.tts_text),743                str(args.tts_voice),744                str(args.tts_rate),745                str(args.f0up_key),746                str(args.filter_radius),747                str(args.index_rate),748                str(args.rms_mix_rate),749                str(args.protect),750                str(args.hop_length),751                str(args.f0method),752                str(args.output_tts_path),753                str(args.output_rvc_path),754                str(args.pth_path),755                str(args.index_path),756                str(args.split_audio),757                str(args.f0autotune),758                str(args.clean_audio),759                str(args.clean_strength),760                str(args.export_format),761                str(args.embedder_model),762                str(args.embedder_model_custom),763                str(args.upscale_audio),764            )765        elif args.mode == "download":766            run_download_script(767                str(args.model_link),768            )769        elif args.mode == "prerequisites":770            run_prerequisites_script(771                str(args.pretraineds_v1),772                str(args.pretraineds_v2),773                str(args.models),774                str(args.exe),775            )776    except Exception as error:777        print(f"Error: {error}")778 779 780if __name__ == "__main__":781    main()782