Update demo.py
Browse files
demo.py
CHANGED
|
@@ -10,6 +10,10 @@ from pathlib import Path
|
|
| 10 |
from sklearn.cluster import MiniBatchKMeans
|
| 11 |
import traceback
|
| 12 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Function to preprocess data
|
| 15 |
def preprocess_data(model_name, dataset_folder):
|
|
@@ -102,6 +106,160 @@ def train_index(exp_dir1, version19):
|
|
| 102 |
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def run_inference(model_name, pitch, input_path, f0_method, save_as, index_rate, volume_normalization, consonant_protection):
|
| 106 |
# Setting paths for model and index files
|
| 107 |
model_filename = model_name + '.pth'
|
|
@@ -148,6 +306,9 @@ def run_inference(model_name, pitch, input_path, f0_method, save_as, index_rate,
|
|
| 148 |
|
| 149 |
return f"Inference completed, output saved at {save_as}.", save_as
|
| 150 |
|
|
|
|
|
|
|
|
|
|
| 151 |
# Gradio Interface
|
| 152 |
|
| 153 |
with gr.Blocks() as demo:
|
|
@@ -173,22 +334,32 @@ with gr.Blocks() as demo:
|
|
| 173 |
with gr.Row():
|
| 174 |
output_message = gr.Textbox(label="Output Message",interactive=False)
|
| 175 |
output_audio = gr.Audio(label="Output Audio",interactive=False)
|
| 176 |
-
run_btn.click(run_inference, [model_name, pitch, input_path, f0_method, save_as, index_rate, volume_normalization, consonant_protection], output_message)
|
| 177 |
|
| 178 |
with gr.Tab("Training"):
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
demo.launch()
|
|
|
|
| 10 |
from sklearn.cluster import MiniBatchKMeans
|
| 11 |
import traceback
|
| 12 |
import gradio as gr
|
| 13 |
+
import pathlib
|
| 14 |
+
import json
|
| 15 |
+
from random import shuffle
|
| 16 |
+
from subprocess import Popen, PIPE, STDOUT
|
| 17 |
|
| 18 |
# Function to preprocess data
|
| 19 |
def preprocess_data(model_name, dataset_folder):
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
+
|
| 110 |
+
now_dir = os.getcwd()
|
| 111 |
+
|
| 112 |
+
def click_train(exp_dir1, sr2, if_f0_3, spk_id5, save_epoch10, total_epoch11, batch_size12,
|
| 113 |
+
if_save_latest13, pretrained_G14, pretrained_D15, gpus16, if_cache_gpu17,
|
| 114 |
+
if_save_every_weights18, version19):
|
| 115 |
+
exp_dir = "%s/logs/%s" % (now_dir, exp_dir1)
|
| 116 |
+
os.makedirs(exp_dir, exist_ok=True)
|
| 117 |
+
gt_wavs_dir = "%s/0_gt_wavs" % (exp_dir)
|
| 118 |
+
feature_dir = (
|
| 119 |
+
"%s/3_feature256" % (exp_dir)
|
| 120 |
+
if version19 == "v1"
|
| 121 |
+
else "%s/3_feature768" % (exp_dir)
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
if if_f0_3:
|
| 125 |
+
f0_dir = "%s/2a_f0" % (exp_dir)
|
| 126 |
+
f0nsf_dir = "%s/2b-f0nsf" % (exp_dir)
|
| 127 |
+
names = (
|
| 128 |
+
set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)])
|
| 129 |
+
& set([name.split(".")[0] for name in os.listdir(feature_dir)])
|
| 130 |
+
& set([name.split(".")[0] for name in os.listdir(f0_dir)])
|
| 131 |
+
& set([name.split(".")[0] for name in os.listdir(f0nsf_dir)])
|
| 132 |
+
)
|
| 133 |
+
else:
|
| 134 |
+
names = set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)]) & set(
|
| 135 |
+
[name.split(".")[0] for name in os.listdir(feature_dir)]
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
opt = []
|
| 139 |
+
for name in names:
|
| 140 |
+
if if_f0_3:
|
| 141 |
+
opt.append(
|
| 142 |
+
"%s/%s.wav|%s/%s.npy|%s/%s.wav.npy|%s/%s.wav.npy|%s"
|
| 143 |
+
% (
|
| 144 |
+
gt_wavs_dir.replace("\\", "\\\\"),
|
| 145 |
+
name,
|
| 146 |
+
feature_dir.replace("\\", "\\\\"),
|
| 147 |
+
name,
|
| 148 |
+
f0_dir.replace("\\", "\\\\"),
|
| 149 |
+
name,
|
| 150 |
+
f0nsf_dir.replace("\\", "\\\\"),
|
| 151 |
+
name,
|
| 152 |
+
spk_id5,
|
| 153 |
+
)
|
| 154 |
+
)
|
| 155 |
+
else:
|
| 156 |
+
opt.append(
|
| 157 |
+
"%s/%s.wav|%s/%s.npy|%s"
|
| 158 |
+
% (
|
| 159 |
+
gt_wavs_dir.replace("\\", "\\\\"),
|
| 160 |
+
name,
|
| 161 |
+
feature_dir.replace("\\", "\\\\"),
|
| 162 |
+
name,
|
| 163 |
+
spk_id5,
|
| 164 |
+
)
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
fea_dim = 256 if version19 == "v1" else 768
|
| 168 |
+
if if_f0_3:
|
| 169 |
+
for _ in range(2):
|
| 170 |
+
opt.append(
|
| 171 |
+
"%s/logs/mute/0_gt_wavs/mute%s.wav|%s/logs/mute/3_feature%s/mute.npy|%s/logs/mute/2a_f0/mute.wav.npy|%s/logs/mute/2b-f0nsf/mute.wav.npy|%s"
|
| 172 |
+
% (now_dir, sr2, now_dir, fea_dim, now_dir, now_dir, spk_id5)
|
| 173 |
+
)
|
| 174 |
+
else:
|
| 175 |
+
for _ in range(2):
|
| 176 |
+
opt.append(
|
| 177 |
+
"%s/logs/mute/0_gt_wavs/mute%s.wav|%s/logs/mute/3_feature%s/mute.npy|%s"
|
| 178 |
+
% (now_dir, sr2, now_dir, fea_dim, spk_id5)
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
shuffle(opt)
|
| 182 |
+
with open("%s/filelist.txt" % exp_dir, "w") as f:
|
| 183 |
+
f.write("\n".join(opt))
|
| 184 |
+
|
| 185 |
+
print("Filelist generated")
|
| 186 |
+
print("Using gpus:", gpus16)
|
| 187 |
+
|
| 188 |
+
if pretrained_G14 == "":
|
| 189 |
+
print("No pretrained Generator")
|
| 190 |
+
if pretrained_D15 == "":
|
| 191 |
+
print("No pretrained Discriminator")
|
| 192 |
+
|
| 193 |
+
if version19 == "v1" or sr2 == "40k":
|
| 194 |
+
config_path = "configs/v1/%s.json" % sr2
|
| 195 |
+
else:
|
| 196 |
+
config_path = "configs/v2/%s.json" % sr2
|
| 197 |
+
config_save_path = os.path.join(exp_dir, "config.json")
|
| 198 |
+
if not pathlib.Path(config_save_path).exists():
|
| 199 |
+
with open(config_save_path, "w", encoding="utf-8") as f:
|
| 200 |
+
with open(config_path, "r") as config_file:
|
| 201 |
+
config_data = json.load(config_file)
|
| 202 |
+
json.dump(
|
| 203 |
+
config_data,
|
| 204 |
+
f,
|
| 205 |
+
ensure_ascii=False,
|
| 206 |
+
indent=4,
|
| 207 |
+
sort_keys=True,
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
cmd = (
|
| 211 |
+
'python infer/modules/train/train.py -e "%s" -sr %s -f0 %s -bs %s -g %s -te %s -se %s %s %s -l %s -c %s -sw %s -v %s'
|
| 212 |
+
% (
|
| 213 |
+
exp_dir1,
|
| 214 |
+
sr2,
|
| 215 |
+
1 if if_f0_3 else 0,
|
| 216 |
+
batch_size12,
|
| 217 |
+
gpus16,
|
| 218 |
+
total_epoch11,
|
| 219 |
+
save_epoch10,
|
| 220 |
+
"-pg %s" % pretrained_G14 if pretrained_G14 != "" else "",
|
| 221 |
+
"-pd %s" % pretrained_D15 if pretrained_D15 != "" else "",
|
| 222 |
+
1 if if_save_latest13 == True else 0,
|
| 223 |
+
1 if if_cache_gpu17 == True else 0,
|
| 224 |
+
1 if if_save_every_weights18 == True else 0,
|
| 225 |
+
version19,
|
| 226 |
+
)
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
# Capture output
|
| 230 |
+
p = Popen(cmd, shell=True, cwd=now_dir, stdout=PIPE, stderr=STDOUT, bufsize=1, universal_newlines=True)
|
| 231 |
+
|
| 232 |
+
# Print output
|
| 233 |
+
output_log = ""
|
| 234 |
+
for line in p.stdout:
|
| 235 |
+
print(line.strip())
|
| 236 |
+
output_log += line.strip() + "\n"
|
| 237 |
+
|
| 238 |
+
p.wait()
|
| 239 |
+
return output_log
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def launch_training(model_name, epochs, save_frequency, batch_size):
|
| 243 |
+
sample_rate = '32k'
|
| 244 |
+
OV2 = True
|
| 245 |
+
G_file = f'assets/pretrained_v2/f0Ov2Super{sample_rate}G.pth' if OV2 else f'assets/pretrained_v2/f0G{sample_rate}.pth'
|
| 246 |
+
D_file = f'assets/pretrained_v2/f0Ov2Super{sample_rate}D.pth' if OV2 else f'assets/pretrained_v2/f0D{sample_rate}.pth'
|
| 247 |
+
|
| 248 |
+
# Call the training function
|
| 249 |
+
training_log = click_train(
|
| 250 |
+
model_name,
|
| 251 |
+
sample_rate,
|
| 252 |
+
True, 0, save_frequency,
|
| 253 |
+
epochs, batch_size, True,
|
| 254 |
+
G_file, D_file, 0, False,
|
| 255 |
+
True, 'v2'
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
return training_log
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
|
| 263 |
def run_inference(model_name, pitch, input_path, f0_method, save_as, index_rate, volume_normalization, consonant_protection):
|
| 264 |
# Setting paths for model and index files
|
| 265 |
model_filename = model_name + '.pth'
|
|
|
|
| 306 |
|
| 307 |
return f"Inference completed, output saved at {save_as}.", save_as
|
| 308 |
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
|
| 312 |
# Gradio Interface
|
| 313 |
|
| 314 |
with gr.Blocks() as demo:
|
|
|
|
| 334 |
with gr.Row():
|
| 335 |
output_message = gr.Textbox(label="Output Message",interactive=False)
|
| 336 |
output_audio = gr.Audio(label="Output Audio",interactive=False)
|
| 337 |
+
#run_btn.click(run_inference, [model_name, pitch, input_path, f0_method, save_as, index_rate, volume_normalization, consonant_protection], output_message)
|
| 338 |
|
| 339 |
with gr.Tab("Training"):
|
| 340 |
+
with gr.TabItem("Create Index and stuff"):
|
| 341 |
+
model_name = gr.Textbox(label="Model Name (No spaces or symbols)")
|
| 342 |
+
dataset_folder = gr.Textbox(label="Dataset Folder", value="/content/dataset")
|
| 343 |
+
f0method = gr.Dropdown(["pm", "harvest", "rmvpe", "rmvpe_gpu"], label="F0 Method", value="rmvpe_gpu")
|
| 344 |
+
preprocess_btn = gr.Button("Start Preprocessing")
|
| 345 |
+
f0_btn = gr.Button("Extract F0 Feature")
|
| 346 |
+
train_btn = gr.Button("Train Index")
|
| 347 |
+
preprocess_output = gr.Textbox(label="Preprocessing Log")
|
| 348 |
+
f0_output = gr.Textbox(label="F0 Feature Extraction Log")
|
| 349 |
+
train_output = gr.Textbox(label="Training Log")
|
| 350 |
+
|
| 351 |
+
#preprocess_btn.click(preprocess_data, inputs=[model_name, dataset_folder], outputs=preprocess_output)
|
| 352 |
+
#f0_btn.click(extract_f0_feature, inputs=[model_name, f0method], outputs=f0_output)
|
| 353 |
+
#train_btn.click(train_index, inputs=[model_name, "v2"], outputs=train_output)
|
| 354 |
+
with gr.TabItem("Train Your Model"):
|
| 355 |
+
model_name_input = gr.Textbox(label="Model Name", placeholder="Enter the model name", interactive=True)
|
| 356 |
+
epochs_slider = gr.Slider(minimum=50, maximum=2000, value=200, step=10, label="Epochs")
|
| 357 |
+
save_frequency_slider = gr.Slider(minimum=10, maximum=100, value=50, step=10, label="Save Frequency")
|
| 358 |
+
batch_size_slider = gr.Slider(minimum=1, maximum=20, value=8, step=1, label="Batch Size")
|
| 359 |
+
|
| 360 |
+
train_button = gr.Button("Train Model")
|
| 361 |
+
training_output = gr.Textbox(label="Training Log", interactive=False)
|
| 362 |
+
|
| 363 |
+
#train_button.click(launch_training, inputs=[model_name_input, epochs_slider, save_frequency_slider, batch_size_slider], outputs=training_output)
|
| 364 |
|
| 365 |
demo.launch()
|