|
| 1 | +import gradio as gr |
| 2 | +import shutil |
| 3 | +import os, sys |
| 4 | +import regex as re |
| 5 | + |
| 6 | +from core import download_model |
| 7 | +from programs.applio_code.rvc.lib.utils import format_title |
| 8 | +from assets.i18n.i18n import I18nAuto |
| 9 | + |
| 10 | +now_dir = os.getcwd() |
| 11 | +sys.path.append(now_dir) |
| 12 | + |
| 13 | +i18n = I18nAuto() |
| 14 | + |
| 15 | + |
| 16 | +def save_drop_model(dropbox): |
| 17 | + if "pth" not in dropbox and "index" not in dropbox: |
| 18 | + raise gr.Error( |
| 19 | + message="The file you dropped is not a valid model file. Please try again." |
| 20 | + ) |
| 21 | + else: |
| 22 | + file_name = format_title(os.path.basename(dropbox)) |
| 23 | + if ".pth" in dropbox: |
| 24 | + model_name = format_title(file_name.split(".pth")[0]) |
| 25 | + else: |
| 26 | + if ( |
| 27 | + "v2" not in dropbox |
| 28 | + and "added_" not in dropbox |
| 29 | + and "_nprobe_1_" not in dropbox |
| 30 | + ): |
| 31 | + model_name = format_title(file_name.split(".index")[0]) |
| 32 | + else: |
| 33 | + if "v2" not in dropbox: |
| 34 | + if "_nprobe_1_" in file_name and "_v1" in file_name: |
| 35 | + model_name = format_title( |
| 36 | + file_name.split("_nprobe_1_")[1].split("_v1")[0] |
| 37 | + ) |
| 38 | + elif "added_" in file_name and "_v1" in file_name: |
| 39 | + model_name = format_title( |
| 40 | + file_name.split("added_")[1].split("_v1")[0] |
| 41 | + ) |
| 42 | + else: |
| 43 | + if "_nprobe_1_" in file_name and "_v2" in file_name: |
| 44 | + model_name = format_title( |
| 45 | + file_name.split("_nprobe_1_")[1].split("_v2")[0] |
| 46 | + ) |
| 47 | + elif "added_" in file_name and "_v2" in file_name: |
| 48 | + model_name = format_title( |
| 49 | + file_name.split("added_")[1].split("_v2")[0] |
| 50 | + ) |
| 51 | + |
| 52 | + model_name = re.sub(r"\d+[se]", "", model_name) |
| 53 | + if "__" in model_name: |
| 54 | + model_name = model_name.replace("__", "") |
| 55 | + |
| 56 | + model_path = os.path.join(now_dir, "logs", model_name) |
| 57 | + if not os.path.exists(model_path): |
| 58 | + os.makedirs(model_path) |
| 59 | + if os.path.exists(os.path.join(model_path, file_name)): |
| 60 | + os.remove(os.path.join(model_path, file_name)) |
| 61 | + shutil.copy(dropbox, os.path.join(model_path, file_name)) |
| 62 | + print(f"{file_name} saved in {model_path}") |
| 63 | + gr.Info(f"{file_name} saved in {model_path}") |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +def download_model_tab(): |
| 68 | + with gr.Row(): |
| 69 | + link = gr.Textbox( |
| 70 | + label=i18n("Model URL"), |
| 71 | + lines=1, |
| 72 | + ) |
| 73 | + output = gr.Textbox( |
| 74 | + label=i18n("Output Information"), |
| 75 | + info=i18n("The output information will be displayed here."), |
| 76 | + ) |
| 77 | + download = gr.Button(i18n("Download")) |
| 78 | + |
| 79 | + download.click( |
| 80 | + download_model, |
| 81 | + inputs=[link], |
| 82 | + outputs=[output], |
| 83 | + ) |
| 84 | + gr.Markdown(value=i18n("## Drop files")) |
| 85 | + dropbox = gr.File( |
| 86 | + label=i18n( |
| 87 | + "Drag your .pth file and .index file into this space. Drag one and then the other." |
| 88 | + ), |
| 89 | + type="filepath", |
| 90 | + ) |
| 91 | + dropbox.upload( |
| 92 | + fn=save_drop_model, |
| 93 | + inputs=[dropbox], |
| 94 | + outputs=[dropbox], |
| 95 | + ) |
0 commit comments