File size: 5,607 Bytes
4de1fc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8226d13
4de1fc0
 
 
 
 
 
 
 
 
 
 
8226d13
4de1fc0
 
 
8226d13
4de1fc0
 
8226d13
4de1fc0
 
 
 
 
8226d13
4de1fc0
 
 
8226d13
4de1fc0
 
 
 
 
 
8226d13
4de1fc0
 
 
 
8226d13
4de1fc0
 
 
 
 
 
8226d13
4de1fc0
8226d13
4de1fc0
8226d13
4de1fc0
 
 
 
 
 
 
8226d13
4de1fc0
 
8226d13
4de1fc0
8226d13
4de1fc0
6159823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4de1fc0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import shutil
import urllib.request
import zipfile
import gdown
import requests
import gradio as gr
from mega import Mega

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
rvc_models_dir = os.path.join(BASE_DIR, 'rvc_models')

def ignore_files(models_dir):
    models_list = os.listdir(models_dir)
    items_to_remove = ['hubert_base.pt', 'MODELS.txt', 'rmvpe.pt', 'fcpe.pt']
    return [item for item in models_list if item not in items_to_remove]

def update_models_list():
    models_l = ignore_files(rvc_models_dir)
    return gr.update(choices=models_l)

def extract_zip(extraction_folder, zip_name):
    os.makedirs(extraction_folder, exist_ok=True)
    with zipfile.ZipFile(zip_name, 'r') as zip_ref:
        zip_ref.extractall(extraction_folder)
    os.remove(zip_name)

    index_filepath, model_filepath = None, None
    for root, _, files in os.walk(extraction_folder):
        for name in files:
            if name.endswith('.index') and os.stat(os.path.join(root, name)).st_size > 1024 * 100:
                index_filepath = os.path.join(root, name)
            if name.endswith('.pth') and os.stat(os.path.join(root, name)).st_size > 1024 * 1024 * 40:
                model_filepath = os.path.join(root, name)

    if not model_filepath:
        raise gr.Error(f'The .pth model file was not found in the unzipped zip file. Please check the {extraction_folder}.')

    os.rename(model_filepath, os.path.join(extraction_folder, os.path.basename(model_filepath)))
    if index_filepath:
        os.rename(index_filepath, os.path.join(extraction_folder, os.path.basename(index_filepath)))

    for filepath in os.listdir(extraction_folder):
        if os.path.isdir(os.path.join(extraction_folder, filepath)):
            shutil.rmtree(os.path.join(extraction_folder, filepath))

def download_from_url(url, dir_name, progress=gr.Progress()):
    try:
        progress(0, desc=f'[~] Downloading voice model named {dir_name}...')
        zip_name = os.path.join(rvc_models_dir, dir_name + '.zip')
        extraction_folder = os.path.join(rvc_models_dir, dir_name)
        if os.path.exists(extraction_folder):
            raise gr.Error(f'The voice model directory {dir_name} already exists! Please choose a different name for your voice model.')

        if 'drive.google.com' in url:
            progress(0.5, desc='[~] Downloading model from Google Grive...')
            file_id = url.split("file/d/")[1].split("/")[0] if "file/d/" in url else url.split("id=")[1].split("&")[0]
            output = zip_name
            gdown.download(id=file_id, output=output, quiet=False)

        elif 'huggingface.co' in url:
            progress(0.5, desc='[~] Downloading model from HuggingFace...')
            urllib.request.urlretrieve(url, zip_name)

        elif 'pixeldrain.com' in url:
            progress(0.5, desc='[~] Downloading model from Pixeldrain...')
            file_id = url.split("pixeldrain.com/u/")[1]
            response = requests.get(f"https://pixeldrain.com/api/file/{file_id}")
            with open(zip_name, 'wb') as f:
                f.write(response.content)

        elif 'mega.nz' in url:
            progress(0.5, desc='[~] Downloading model from Mega...')
            m = Mega()
            m.download_url(url, dest_filename=zip_name)

        elif 'yadi.sk' in url or 'disk.yandex.ru' in url:
            progress(0.5, desc='[~] Downloading model from Yandex Disk...')
            yandex_api_url = "https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}".format(url)
            response = requests.get(yandex_api_url)
            if response.status_code == 200:
                download_link = response.json().get('href')
                urllib.request.urlretrieve(download_link, zip_name)
            else:
                raise gr.Error(f"Error when receiving a download link from Yandex Disk: {response.status_code}")

        progress(0.8, desc='[~] Extracting zip file...')
        extract_zip(extraction_folder, zip_name)
        return f'[+] The {dir_name} model was loaded successfully!'
    except Exception as e:
        raise gr.Error(str(e))

def upload_zip_model(zip_path, dir_name, progress=gr.Progress()):
    try:
        extraction_folder = os.path.join(rvc_models_dir, dir_name)
        if os.path.exists(extraction_folder):
            raise gr.Error(f'The voice model directory {dir_name} already exists! Choose a different name for your voice model.')

        zip_name = zip_path.name
        progress(0.8, desc='[~] Extracting zip file...')
        extract_zip(extraction_folder, zip_name)
        return f'[+] The {dir_name} model was loaded successfully!'

    except Exception as e:
        raise gr.Error(str(e))

def upload_separate_files(pth_file, index_file, dir_name, progress=gr.Progress()):
    try:
        extraction_folder = os.path.join(rvc_models_dir, dir_name)
        if os.path.exists(extraction_folder):
            raise gr.Error(f'The voice model directory {dir_name} already exists! Choose a different name for your voice model.')

        os.makedirs(extraction_folder, exist_ok=True)

        if pth_file:
            pth_path = os.path.join(extraction_folder, os.path.basename(pth_file.name))
            shutil.copyfile(pth_file.name, pth_path)

        if index_file:
            index_path = os.path.join(extraction_folder, os.path.basename(index_file.name))
            shutil.copyfile(index_file.name, index_path)

        return f'[+] The {dir_name} model was loaded successfully!'
    except Exception as e:
        raise gr.Error(str(e))