Skip to content

Commit f5aa9c3

Browse files
committed
fix: handle exceptions directly in UI scripts Pt.3
1 parent 8498918 commit f5aa9c3

7 files changed

Lines changed: 115 additions & 86 deletions

File tree

core.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ def run_preprocess_script(
438438
],
439439
),
440440
]
441-
subprocess.run(command)
441+
result = subprocess.run(command)
442+
if result.returncode != 0:
443+
return f"Preprocessing failed for model {model_name}. Please check the console logs for more details."
444+
442445
return f"Model {model_name} preprocessed successfully."
443446

444447

@@ -474,7 +477,9 @@ def run_extract_script(
474477
),
475478
]
476479

477-
subprocess.run(command_1)
480+
result = subprocess.run(command_1)
481+
if result.returncode != 0:
482+
return f"Feature extraction failed for model {model_name}. Please check the console logs for more details."
478483

479484
return f"Model {model_name} extracted successfully."
480485

@@ -543,9 +548,8 @@ def run_train_script(
543548
]
544549
result = subprocess.run(command)
545550
if result.returncode != 0:
546-
raise RuntimeError(
547-
f"Training failed for model {model_name}. Please check the console logs for more details."
548-
)
551+
return f"Training failed for model {model_name}. Please check the console logs for more details."
552+
549553
run_index_script(model_name, index_algorithm)
550554
return f"Model {model_name} trained successfully."
551555

@@ -560,7 +564,10 @@ def run_index_script(model_name: str, index_algorithm: str):
560564
index_algorithm,
561565
]
562566

563-
subprocess.run(command)
567+
result = subprocess.run(command)
568+
if result.returncode != 0:
569+
return f"Index generation failed for model {model_name}. Make sure you have enough GPU available to generate the Index file. Please check the console logs for more details."
570+
564571
return f"Index file for {model_name} generated successfully."
565572

566573

@@ -587,7 +594,7 @@ def run_tensorboard_script():
587594
def run_download_script(model_link: str):
588595
result = model_download_pipeline(model_link)
589596
if result == "Error" or result is None:
590-
return "An error occurred downloading the model. Check the console for details."
597+
return "An error occurred downloading the model. Please check the console logs for more details."
591598
return "Model downloaded successfully."
592599

593600

rvc/train/extract/extract.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
import concurrent.futures
2+
import glob
3+
import json
4+
import multiprocessing as mp
15
import os
26
import sys
3-
import glob
47
import time
5-
import tqdm
6-
import torch
7-
import torchcrepe
8+
89
import numpy as np
9-
import concurrent.futures
10-
import multiprocessing as mp
11-
import json
10+
import torch
11+
import tqdm
1212

1313
now_dir = os.getcwd()
1414
sys.path.append(os.path.join(now_dir))
1515

1616
# Zluda hijack
1717
import rvc.lib.zluda
18-
18+
from rvc.configs.config import Config
19+
from rvc.lib.predictors.f0 import CREPE, FCPE, RMVPE
1920
from rvc.lib.utils import load_audio_16k, load_embedding
2021
from rvc.train.extract.preparing_files import generate_config, generate_filelist
21-
from rvc.lib.predictors.f0 import CREPE, FCPE, RMVPE
22-
from rvc.configs.config import Config
2322

2423
# Load config
2524
config = Config()
@@ -184,6 +183,13 @@ def run_embedding_extraction(
184183
include_mutes = int(sys.argv[8]) if len(sys.argv) > 8 else 2
185184

186185
wav_path = os.path.join(exp_dir, "sliced_audios_16k")
186+
187+
if not os.path.exists(wav_path):
188+
print(
189+
f"Folder for feature extraction not found at {wav_path}. Did you run the preprocessing step?"
190+
)
191+
sys.exit(1)
192+
187193
os.makedirs(os.path.join(exp_dir, "f0"), exist_ok=True)
188194
os.makedirs(os.path.join(exp_dir, "f0_voiced"), exist_ok=True)
189195
os.makedirs(os.path.join(exp_dir, "extracted"), exist_ok=True)
@@ -212,6 +218,12 @@ def run_embedding_extraction(
212218
]
213219
files.append(file_info)
214220

221+
if not files:
222+
print(
223+
f"Sliced audios not found at {wav_path}. Did you run the preprocessing step?"
224+
)
225+
sys.exit(1)
226+
215227
devices = ["cpu"] if gpus == "-" else [f"cuda:{idx}" for idx in gpus.split("-")]
216228

217229
run_pitch_extraction(files, devices, f0_method, num_processes)

rvc/train/preprocess/preprocess.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import concurrent.futures
2+
import json
3+
import multiprocessing
14
import os
25
import sys
36
import time
4-
from scipy import signal
5-
from scipy.io import wavfile
6-
import numpy as np
7-
import concurrent.futures
8-
from tqdm import tqdm
9-
import json
107
from distutils.util import strtobool
8+
119
import librosa
12-
import multiprocessing
1310
import noisereduce as nr
11+
import numpy as np
1412
import soxr
13+
from scipy import signal
14+
from scipy.io import wavfile
15+
from tqdm import tqdm
1516

1617
now_directory = os.getcwd()
1718
sys.path.append(now_directory)
1819

20+
import logging
21+
1922
from rvc.lib.utils import load_audio
2023
from rvc.train.preprocess.slicer import Slicer
2124

22-
import logging
23-
2425
logging.getLogger("numba.core.byteflow").setLevel(logging.WARNING)
2526
logging.getLogger("numba.core.ssa").setLevel(logging.WARNING)
2627
logging.getLogger("numba.core.interpreter").setLevel(logging.WARNING)
@@ -281,6 +282,13 @@ def preprocess_training_set(
281282
overlap_len: float,
282283
normalization_mode: str,
283284
):
285+
if not os.path.exists(input_root):
286+
print(f"The dataset path does not exist: '{input_root}'.")
287+
sys.exit(1)
288+
289+
if not os.path.isdir(input_root):
290+
print(f"The dataset path is not a directory: '{input_root}'.")
291+
sys.exit(1)
284292
start_time = time.time()
285293
pp = PreProcess(sr, exp_dir)
286294
print(f"Starting preprocess with {num_processes} processes...")
@@ -301,6 +309,11 @@ def preprocess_training_set(
301309
)
302310

303311
# print(f"Number of files: {len(files)}")
312+
if len(files) == 0:
313+
print(
314+
f"No audio files found in the dataset path: '{input_root}'. Please check that the path is correct and contains valid audio files."
315+
)
316+
sys.exit(1)
304317
audio_length = []
305318
with tqdm(total=len(files)) as pbar:
306319
with concurrent.futures.ProcessPoolExecutor(

rvc/train/process/extract_index.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,68 @@
1010
exp_dir = str(sys.argv[1])
1111
index_algorithm = str(sys.argv[2])
1212

13-
try:
14-
feature_dir = os.path.join(exp_dir, f"extracted")
15-
model_name = os.path.basename(exp_dir)
13+
feature_dir = os.path.join(exp_dir, "extracted")
14+
model_name = os.path.basename(exp_dir)
1615

17-
if not os.path.exists(feature_dir):
18-
print(
19-
f"Feature to generate index file not found at {feature_dir}. Did you run preprocessing and feature extraction steps?"
20-
)
21-
sys.exit(1)
16+
if not os.path.exists(feature_dir):
17+
print(
18+
f"Feature to generate index file not found at {feature_dir}. Did you run preprocessing and feature extraction steps?"
19+
)
20+
sys.exit(1)
2221

23-
index_filename_added = f"{model_name}.index"
24-
index_filepath_added = os.path.join(exp_dir, index_filename_added)
22+
index_filename_added = f"{model_name}.index"
23+
index_filepath_added = os.path.join(exp_dir, index_filename_added)
2524

26-
if os.path.exists(index_filepath_added):
27-
pass
28-
else:
29-
npys = []
30-
listdir_res = sorted(os.listdir(feature_dir))
25+
if os.path.exists(index_filepath_added):
26+
pass
27+
else:
28+
npys = []
29+
print(f"Generating index for '{model_name}', this may take a while...")
30+
listdir_res = sorted(os.listdir(feature_dir))
3131

32-
for name in listdir_res:
33-
file_path = os.path.join(feature_dir, name)
34-
phone = np.load(file_path)
35-
npys.append(phone)
32+
for name in listdir_res:
33+
file_path = os.path.join(feature_dir, name)
34+
phone = np.load(file_path)
35+
npys.append(phone)
3636

37-
big_npy = np.concatenate(npys, axis=0)
37+
if not npys:
38+
print(
39+
f"Feature files in {feature_dir} could not be loaded correctly. Did you run preprocessing and feature extraction steps?"
40+
)
41+
sys.exit(1)
3842

39-
big_npy_idx = np.arange(big_npy.shape[0])
40-
np.random.shuffle(big_npy_idx)
41-
big_npy = big_npy[big_npy_idx]
43+
big_npy = np.concatenate(npys, axis=0)
4244

43-
if big_npy.shape[0] > 2e5 and (
44-
index_algorithm == "Auto" or index_algorithm == "KMeans"
45-
):
46-
big_npy = (
47-
MiniBatchKMeans(
48-
n_clusters=10000,
49-
verbose=True,
50-
batch_size=256 * cpu_count(),
51-
compute_labels=False,
52-
init="random",
53-
)
54-
.fit(big_npy)
55-
.cluster_centers_
56-
)
45+
big_npy_idx = np.arange(big_npy.shape[0])
46+
np.random.shuffle(big_npy_idx)
47+
big_npy = big_npy[big_npy_idx]
5748

58-
n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
49+
if big_npy.shape[0] > 2e5 and (
50+
index_algorithm == "Auto" or index_algorithm == "KMeans"
51+
):
52+
big_npy = (
53+
MiniBatchKMeans(
54+
n_clusters=10000,
55+
verbose=True,
56+
batch_size=256 * cpu_count(),
57+
compute_labels=False,
58+
init="random",
59+
)
60+
.fit(big_npy)
61+
.cluster_centers_
62+
)
5963

60-
# index_added
61-
index_added = faiss.index_factory(768, f"IVF{n_ivf},Flat")
62-
index_ivf_added = faiss.extract_index_ivf(index_added)
63-
index_ivf_added.nprobe = 1
64-
index_added.train(big_npy)
64+
n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
6565

66-
batch_size_add = 8192
67-
for i in range(0, big_npy.shape[0], batch_size_add):
68-
index_added.add(big_npy[i : i + batch_size_add])
66+
# index_added
67+
index_added = faiss.index_factory(768, f"IVF{n_ivf},Flat")
68+
index_ivf_added = faiss.extract_index_ivf(index_added)
69+
index_ivf_added.nprobe = 1
70+
index_added.train(big_npy)
6971

70-
faiss.write_index(index_added, index_filepath_added)
71-
print(f"Saved index file '{index_filepath_added}'")
72+
batch_size_add = 8192
73+
for i in range(0, big_npy.shape[0], batch_size_add):
74+
index_added.add(big_npy[i : i + batch_size_add])
7275

73-
except Exception as error:
74-
print(f"An error occurred extracting the index: {error}")
75-
print(
76-
"If you are running this code in a virtual environment, make sure you have enough GPU available to generate the Index file."
77-
)
76+
faiss.write_index(index_added, index_filepath_added)
77+
print(f"Saved index file '{index_filepath_added}'")

tabs/inference/inference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ def enforce_terms(terms_accepted, *args):
11731173
except Exception:
11741174
traceback.print_exc()
11751175
return (
1176-
"An error occurred during audio conversion. Check the console for details.",
1176+
"An error occurred during audio conversion. Please check the console logs for more details.",
11771177
None,
11781178
)
11791179

@@ -1186,7 +1186,7 @@ def enforce_terms_batch(terms_accepted, *args):
11861186
return run_batch_infer_script(*args)
11871187
except Exception:
11881188
traceback.print_exc()
1189-
return "An error occurred during audio batch conversion. Check the console for details."
1189+
return "An error occurred during audio batch conversion. Please check the console logs for more details."
11901190

11911191
terms_checkbox = gr.Checkbox(
11921192
label=i18n("I agree to the terms of use"),

tabs/train/train.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,7 @@ def enforce_terms(terms_accepted, *args):
768768
message = "You must agree to the Terms of Use to proceed."
769769
gr.Info(message)
770770
return message
771-
try:
772-
return run_train_script(*args)
773-
except Exception as e:
774-
return e
771+
return run_train_script(*args)
775772

776773
terms_checkbox = gr.Checkbox(
777774
label=i18n("I agree to the terms of use"),

tabs/tts/tts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def enforce_terms(terms_accepted, *args):
363363
except Exception:
364364
traceback.print_exc()
365365
return (
366-
"An error occurred during TTS conversion. Check the console for details.",
366+
"An error occurred during TTS conversion. Please check the console logs for more details.",
367367
None,
368368
)
369369

0 commit comments

Comments
 (0)