Spaces:
Running
Running
Update src/modules/model_management.py
Browse files- src/modules/model_management.py +105 -89
src/modules/model_management.py
CHANGED
@@ -1,89 +1,105 @@
|
|
1 |
-
import os
|
2 |
-
import shutil
|
3 |
-
import urllib.request
|
4 |
-
import zipfile
|
5 |
-
import gdown
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
def ignore_files(models_dir):
|
14 |
-
models_list = os.listdir(models_dir)
|
15 |
-
items_to_remove = ['hubert_base.pt', 'MODELS.txt', 'rmvpe.pt', 'fcpe.pt']
|
16 |
-
return [item for item in models_list if item not in items_to_remove]
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
if name.endswith('.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
if
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
urllib.request.urlretrieve(url, zip_name)
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import urllib.request
|
4 |
+
import zipfile
|
5 |
+
import gdown
|
6 |
+
import requests
|
7 |
+
import gradio as gr
|
8 |
+
from mega import Mega
|
9 |
+
|
10 |
+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
11 |
+
rvc_models_dir = os.path.join(BASE_DIR, 'rvc_models')
|
12 |
+
|
13 |
+
def ignore_files(models_dir):
|
14 |
+
models_list = os.listdir(models_dir)
|
15 |
+
items_to_remove = ['hubert_base.pt', 'MODELS.txt', 'rmvpe.pt', 'fcpe.pt']
|
16 |
+
return [item for item in models_list if item not in items_to_remove]
|
17 |
+
|
18 |
+
def update_models_list():
|
19 |
+
models_l = ignore_files(rvc_models_dir)
|
20 |
+
return gr.update(choices=models_l)
|
21 |
+
|
22 |
+
def extract_zip(extraction_folder, zip_name):
|
23 |
+
os.makedirs(extraction_folder, exist_ok=True)
|
24 |
+
with zipfile.ZipFile(zip_name, 'r') as zip_ref:
|
25 |
+
zip_ref.extractall(extraction_folder)
|
26 |
+
os.remove(zip_name)
|
27 |
+
|
28 |
+
index_filepath, model_filepath = None, None
|
29 |
+
for root, _, files in os.walk(extraction_folder):
|
30 |
+
for name in files:
|
31 |
+
if name.endswith('.index') and os.stat(os.path.join(root, name)).st_size > 1024 * 100:
|
32 |
+
index_filepath = os.path.join(root, name)
|
33 |
+
if name.endswith('.pth') and os.stat(os.path.join(root, name)).st_size > 1024 * 1024 * 40:
|
34 |
+
model_filepath = os.path.join(root, name)
|
35 |
+
|
36 |
+
if not model_filepath:
|
37 |
+
raise gr.Error(f'Не найден файл модели .pth в распакованном zip-файле. Пожалуйста, проверьте {extraction_folder}.')
|
38 |
+
|
39 |
+
os.rename(model_filepath, os.path.join(extraction_folder, os.path.basename(model_filepath)))
|
40 |
+
if index_filepath:
|
41 |
+
os.rename(index_filepath, os.path.join(extraction_folder, os.path.basename(index_filepath)))
|
42 |
+
|
43 |
+
for filepath in os.listdir(extraction_folder):
|
44 |
+
if os.path.isdir(os.path.join(extraction_folder, filepath)):
|
45 |
+
shutil.rmtree(os.path.join(extraction_folder, filepath))
|
46 |
+
|
47 |
+
def download_from_url(url, dir_name, progress=gr.Progress()):
|
48 |
+
try:
|
49 |
+
progress(0, desc=f'[~] Загрузка голосовой модели с именем {dir_name}...')
|
50 |
+
zip_name = os.path.join(rvc_models_dir, dir_name + '.zip')
|
51 |
+
extraction_folder = os.path.join(rvc_models_dir, dir_name)
|
52 |
+
if os.path.exists(extraction_folder):
|
53 |
+
raise gr.Error(f'Директория голосовой модели {dir_name} уже существует! Выберите другое имя для вашей голосовой модели.')
|
54 |
+
|
55 |
+
if 'drive.google.com' in url:
|
56 |
+
progress(0.5, desc='[~] Загрузка модели с Google Grive...')
|
57 |
+
file_id = url.split("file/d/")[1].split("/")[0] if "file/d/" in url else url.split("id=")[1].split("&")[0]
|
58 |
+
output = zip_name
|
59 |
+
gdown.download(id=file_id, output=output, quiet=False)
|
60 |
+
|
61 |
+
elif 'huggingface.co' in url:
|
62 |
+
progress(0.5, desc='[~] Загрузка модели с HuggingFace...')
|
63 |
+
urllib.request.urlretrieve(url, zip_name)
|
64 |
+
|
65 |
+
elif 'pixeldrain.com' in url:
|
66 |
+
progress(0.5, desc='[~] Загрузка модели с Pixeldrain...')
|
67 |
+
file_id = url.split("pixeldrain.com/u/")[1]
|
68 |
+
response = requests.get(f"https://pixeldrain.com/api/file/{file_id}")
|
69 |
+
with open(zip_name, 'wb') as f:
|
70 |
+
f.write(response.content)
|
71 |
+
|
72 |
+
elif 'mega.nz' in url:
|
73 |
+
progress(0.5, desc='[~] Загрузка модели с Mega...')
|
74 |
+
m = Mega()
|
75 |
+
m.download_url(url, dest_filename=zip_name)
|
76 |
+
|
77 |
+
elif 'yadi.sk' in url or 'disk.yandex.ru' in url:
|
78 |
+
progress(0.5, desc='[~] Загрузка модели с Яндекс Диска...')
|
79 |
+
yandex_api_url = "https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}".format(url)
|
80 |
+
response = requests.get(yandex_api_url)
|
81 |
+
if response.status_code == 200:
|
82 |
+
download_link = response.json().get('href')
|
83 |
+
urllib.request.urlretrieve(download_link, zip_name)
|
84 |
+
else:
|
85 |
+
raise gr.Error(f"Ошибка при получении ссылки на скачивание с Яндекс Диск: {response.status_code}")
|
86 |
+
|
87 |
+
progress(0.8, desc='[~] Распаковка zip-файла...')
|
88 |
+
extract_zip(extraction_folder, zip_name)
|
89 |
+
return f'[+] Модель {dir_name} успешно загружена!'
|
90 |
+
except Exception as e:
|
91 |
+
raise gr.Error(str(e))
|
92 |
+
|
93 |
+
def upload_zip_model(zip_path, dir_name, progress=gr.Progress()):
|
94 |
+
try:
|
95 |
+
extraction_folder = os.path.join(rvc_models_dir, dir_name)
|
96 |
+
if os.path.exists(extraction_folder):
|
97 |
+
raise gr.Error(f'Директория голосовой модели {dir_name} уже существует! Выберите другое имя для вашей голосовой модели.')
|
98 |
+
|
99 |
+
zip_name = zip_path.name
|
100 |
+
progress(0.8, desc='[~] Распаковка zip-файла...')
|
101 |
+
extract_zip(extraction_folder, zip_name)
|
102 |
+
return f'[+] Модель {dir_name} успешно загружена!'
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
raise gr.Error(str(e))
|