Spaces:
Running
Running
Upload train.py with huggingface_hub
Browse files
train.py
CHANGED
|
@@ -1,30 +1,34 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
RVC v2 CPU Training -
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
-
|
| 8 |
-
-
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
-
import os, sys, json, time, shutil, subprocess, glob, traceback, logging, threading
|
| 12 |
from http.server import HTTPServer, BaseHTTPRequestHandler
|
| 13 |
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', stream=sys.stdout)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
DATASET_ID = "ayf3/numberblocks-one-voice-dataset"
|
| 18 |
-
EXPERIMENT_NAME = "
|
| 19 |
-
TARGET_STEPS =
|
| 20 |
SAMPLE_RATE = 40000
|
| 21 |
BATCH_SIZE = 1
|
| 22 |
WORK_DIR = "/app/rvc_work"
|
| 23 |
RVC_DIR = "/app/RVC"
|
| 24 |
DATASET_DIR = os.path.join(WORK_DIR, "dataset")
|
| 25 |
PORT = 7860
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
STATUS = {"status": "initializing", "step": "", "progress": "", "message": "Starting...", "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), "error": None}
|
| 28 |
|
| 29 |
def update_status(status=None, step=None, progress=None, message=None, error=None):
|
| 30 |
if status: STATUS["status"] = status
|
|
@@ -51,9 +55,10 @@ def run_cmd(cmd, cwd=None, check=True, timeout=3600):
|
|
| 51 |
if check: raise
|
| 52 |
return None
|
| 53 |
|
|
|
|
| 54 |
def step1_download_data():
|
| 55 |
-
"""Download training data."""
|
| 56 |
-
update_status("downloading", step="download", message="
|
| 57 |
|
| 58 |
os.makedirs(DATASET_DIR, exist_ok=True)
|
| 59 |
|
|
@@ -62,22 +67,20 @@ def step1_download_data():
|
|
| 62 |
api = HfApi(token=token)
|
| 63 |
|
| 64 |
all_files = api.list_repo_files(repo_id=DATASET_ID, repo_type='dataset')
|
| 65 |
-
# Use
|
| 66 |
train_files = [f for f in all_files
|
| 67 |
if f.startswith('data/train_top500/') and f.endswith('.wav')]
|
| 68 |
-
|
| 69 |
-
# Limit to 200 files for CPU training speed
|
| 70 |
-
train_files = train_files[:200]
|
| 71 |
|
| 72 |
logger.info(f"Will download {len(train_files)} files")
|
| 73 |
-
|
| 74 |
-
downloaded = len(glob.glob(os.path.join(DATASET_DIR, "*.wav")))
|
| 75 |
|
| 76 |
for i, fpath in enumerate(train_files):
|
| 77 |
local_name = fpath.split('/')[-1]
|
| 78 |
local_path = os.path.join(DATASET_DIR, local_name)
|
| 79 |
|
| 80 |
if os.path.exists(local_path):
|
|
|
|
| 81 |
continue
|
| 82 |
|
| 83 |
try:
|
|
@@ -92,44 +95,18 @@ def step1_download_data():
|
|
| 92 |
continue
|
| 93 |
|
| 94 |
if (i + 1) % 20 == 0:
|
| 95 |
-
update_status("downloading", step="download",
|
| 96 |
-
progress=f"{i+1}/{len(train_files)}",
|
| 97 |
message=f"Downloaded {downloaded}/{len(train_files)}")
|
| 98 |
|
| 99 |
-
logger.info(f"Download complete: {downloaded} files
|
| 100 |
-
update_status("downloaded", step="download", progress=str(downloaded),
|
| 101 |
message=f"Downloaded {downloaded} files")
|
| 102 |
return downloaded
|
| 103 |
|
| 104 |
-
def step2_setup_rvc():
|
| 105 |
-
"""Setup RVC environment."""
|
| 106 |
-
update_status("setup", step="setup", message="Setting up RVC...")
|
| 107 |
-
|
| 108 |
-
# Clone RVC if not exists
|
| 109 |
-
if not os.path.exists(os.path.join(RVC_DIR, ".git")):
|
| 110 |
-
if os.path.exists(RVC_DIR):
|
| 111 |
-
shutil.rmtree(RVC_DIR)
|
| 112 |
-
run_cmd(f"git clone --depth 1 https://github.com/RVC-Project/Retrieval-based-Voice-Conversion.git {RVC_DIR}", timeout=600)
|
| 113 |
-
|
| 114 |
-
update_status("setup", step="setup", message="Installing dependencies...")
|
| 115 |
-
|
| 116 |
-
# Install essential deps
|
| 117 |
-
essential = ["soundfile", "librosa", "scipy", "torch", "torchaudio",
|
| 118 |
-
"fairseq==0.12.2", "pyworld==0.3.4", "crepe", "praat-parselmouth",
|
| 119 |
-
"faiss-cpu", "ffmpeg-python"]
|
| 120 |
-
for dep in essential:
|
| 121 |
-
run_cmd(f"pip3 install --no-cache-dir {dep}", check=False, timeout=300)
|
| 122 |
-
|
| 123 |
-
# RVC requirements
|
| 124 |
-
req_file = os.path.join(RVC_DIR, "requirements.txt")
|
| 125 |
-
if os.path.exists(req_file):
|
| 126 |
-
run_cmd(f"pip3 install --no-cache-dir -r {req_file}", cwd=RVC_DIR, check=False, timeout=600)
|
| 127 |
-
|
| 128 |
-
logger.info("Setup complete")
|
| 129 |
-
update_status("setup_done", step="setup", message="RVC setup complete")
|
| 130 |
|
| 131 |
-
def
|
| 132 |
-
"""Preprocess audio
|
| 133 |
update_status("preprocessing", step="preprocess", message="Preprocessing audio...")
|
| 134 |
|
| 135 |
import soundfile as sf
|
|
@@ -138,283 +115,328 @@ def step3_preprocess():
|
|
| 138 |
exp_dir = os.path.join(WORK_DIR, "logs", EXPERIMENT_NAME)
|
| 139 |
os.makedirs(exp_dir, exist_ok=True)
|
| 140 |
|
| 141 |
-
# RVC expects audio in a specific directory structure
|
| 142 |
-
# logs/{experiment_name}/ will contain the training data
|
| 143 |
-
wav_dir = os.path.join(exp_dir)
|
| 144 |
-
|
| 145 |
wav_files = sorted(glob.glob(os.path.join(DATASET_DIR, "*.wav")))
|
| 146 |
logger.info(f"Found {len(wav_files)} WAV files")
|
| 147 |
|
| 148 |
-
if not wav_files:
|
| 149 |
-
update_status("error", error="No WAV files found!")
|
| 150 |
-
return False
|
| 151 |
-
|
| 152 |
-
# Validate and convert audio files
|
| 153 |
valid_count = 0
|
| 154 |
for i, wf in enumerate(wav_files):
|
| 155 |
try:
|
| 156 |
data, sr = sf.read(wf)
|
| 157 |
-
|
| 158 |
-
# Convert to mono if stereo
|
| 159 |
if len(data.shape) > 1:
|
| 160 |
data = data.mean(axis=1)
|
| 161 |
-
|
| 162 |
-
# Resample to 40kHz if needed
|
| 163 |
if sr != SAMPLE_RATE:
|
| 164 |
import librosa
|
| 165 |
data = librosa.resample(data, orig_sr=sr, target_sr=SAMPLE_RATE)
|
| 166 |
sr = SAMPLE_RATE
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
out_name = os.path.basename(wf)
|
| 170 |
-
out_path = os.path.join(wav_dir, out_name)
|
| 171 |
sf.write(out_path, data.astype(np.float32), sr)
|
| 172 |
valid_count += 1
|
| 173 |
-
|
| 174 |
except Exception as e:
|
| 175 |
-
logger.warning(f"Failed
|
| 176 |
continue
|
| 177 |
|
| 178 |
-
if (i + 1) %
|
| 179 |
update_status("preprocessing", step="preprocess",
|
| 180 |
progress=f"{i+1}/{len(wav_files)}",
|
| 181 |
message=f"Processed {valid_count}/{len(wav_files)}")
|
| 182 |
|
| 183 |
-
logger.info(f"Valid
|
| 184 |
update_status("preprocessed", step="preprocess",
|
| 185 |
message=f"Preprocessed {valid_count} files")
|
| 186 |
return valid_count > 0
|
| 187 |
|
| 188 |
-
def step4_train_rvc():
|
| 189 |
-
"""Run actual RVC training using its CLI."""
|
| 190 |
-
update_status("training", step="train", message="Starting RVC training...")
|
| 191 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 192 |
-
|
| 193 |
-
exp_dir = os.path.join(WORK_DIR, "logs", EXPERIMENT_NAME)
|
| 194 |
-
|
| 195 |
-
# Find RVC training script
|
| 196 |
-
# Newer RVC uses: python infer/train.py
|
| 197 |
-
train_script = os.path.join(RVC_DIR, "infer", "train.py")
|
| 198 |
-
if not os.path.exists(train_script):
|
| 199 |
-
train_script = os.path.join(RVC_DIR, "train.py")
|
| 200 |
-
|
| 201 |
-
if not os.path.exists(train_script):
|
| 202 |
-
# Try to find any training script
|
| 203 |
-
candidates = glob.glob(os.path.join(RVC_DIR, "**", "*train*.py"), recursive=True)
|
| 204 |
-
logger.info(f"Training script candidates: {candidates}")
|
| 205 |
-
|
| 206 |
-
# Look for the main training entry point
|
| 207 |
-
for c in candidates:
|
| 208 |
-
with open(c) as f:
|
| 209 |
-
content = f.read()
|
| 210 |
-
if "argparse" in content and ("train" in content.lower()):
|
| 211 |
-
train_script = c
|
| 212 |
-
break
|
| 213 |
-
|
| 214 |
-
logger.info(f"Using training script: {train_script}")
|
| 215 |
-
|
| 216 |
-
# Read the script to understand its interface
|
| 217 |
-
if os.path.exists(train_script):
|
| 218 |
-
with open(train_script) as f:
|
| 219 |
-
content = f.read(2000)
|
| 220 |
-
logger.info(f"Script header:\n{content[:1000]}")
|
| 221 |
-
|
| 222 |
-
# Try RVC's webUI training approach via Python API
|
| 223 |
-
# The standard way is through the Go_WebUI interface but we need CLI
|
| 224 |
-
|
| 225 |
-
# Approach: Use RVC's process_ckpt and training modules directly
|
| 226 |
-
sys.path.insert(0, RVC_DIR)
|
| 227 |
-
sys.path.insert(0, os.path.join(RVC_DIR, "infer"))
|
| 228 |
-
|
| 229 |
-
# Step 4a: Extract F0 (pitch)
|
| 230 |
-
update_status("training", step="extract_f0", progress="0%", message="Extracting F0...")
|
| 231 |
-
|
| 232 |
-
try:
|
| 233 |
-
from infer.lib.train.process_ckpt import (
|
| 234 |
-
change_info, merge, show_info,
|
| 235 |
-
)
|
| 236 |
-
logger.info("✅ Imported process_ckpt")
|
| 237 |
-
except ImportError as e:
|
| 238 |
-
logger.info(f"process_ckpt import: {e}")
|
| 239 |
-
|
| 240 |
-
# Try to import and use the training pipeline
|
| 241 |
-
try:
|
| 242 |
-
# RVC training typically has these steps:
|
| 243 |
-
# 1. process_data - format wave to 40k
|
| 244 |
-
# 2. extract_f0 - extract pitch
|
| 245 |
-
# 3. extract_feature - extract content features
|
| 246 |
-
# 4. train - actual model training
|
| 247 |
-
|
| 248 |
-
from infer.lib.train import process
|
| 249 |
-
logger.info(f"✅ Imported process module: {dir(process)}")
|
| 250 |
-
except ImportError as e:
|
| 251 |
-
logger.info(f"process import: {e}")
|
| 252 |
-
|
| 253 |
-
# Direct CLI approach - run training via subprocess
|
| 254 |
-
# RVC's infer/train.py or main training script
|
| 255 |
-
|
| 256 |
-
# First try: the standard RVC CLI training command
|
| 257 |
-
# python infer/train.py -e {exp_name} -sr {sample_rate} -f0
|
| 258 |
-
|
| 259 |
-
train_commands = [
|
| 260 |
-
# RVC v2 standard CLI
|
| 261 |
-
f'cd {RVC_DIR} && python3 infer/train.py -e "{EXPERIMENT_NAME}" -sr {SAMPLE_RATE} -f0 -b {BATCH_SIZE} -t {TARGET_STEPS} -v v2',
|
| 262 |
-
# Alternative path
|
| 263 |
-
f'cd {RVC_DIR} && python3 "{train_script}" --help',
|
| 264 |
-
]
|
| 265 |
-
|
| 266 |
-
for cmd in train_commands:
|
| 267 |
-
logger.info(f"Trying: {cmd[:150]}")
|
| 268 |
-
result = run_cmd(cmd, check=False, timeout=60)
|
| 269 |
-
if result and result.returncode == 0:
|
| 270 |
-
logger.info("✅ Command succeeded!")
|
| 271 |
-
break
|
| 272 |
-
else:
|
| 273 |
-
logger.info("❌ Command failed, trying next...")
|
| 274 |
-
|
| 275 |
-
# If CLI approach doesn't work, try the manual PyTorch approach
|
| 276 |
-
# Build a simple model from scratch
|
| 277 |
-
update_status("training", step="manual_train", message="Using manual training approach...")
|
| 278 |
-
|
| 279 |
-
return manual_train(exp_dir)
|
| 280 |
|
| 281 |
-
def
|
| 282 |
"""
|
| 283 |
-
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
"""
|
|
|
|
|
|
|
| 286 |
import torch
|
|
|
|
|
|
|
| 287 |
import soundfile as sf
|
| 288 |
import numpy as np
|
| 289 |
|
|
|
|
|
|
|
|
|
|
| 290 |
wav_files = sorted(glob.glob(os.path.join(exp_dir, "*.wav")))
|
| 291 |
-
logger.info(f"Training with {len(wav_files)} files")
|
| 292 |
|
| 293 |
if not wav_files:
|
| 294 |
update_status("error", error="No preprocessed audio!")
|
| 295 |
return False
|
| 296 |
|
| 297 |
-
#
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
try:
|
| 301 |
data, sr = sf.read(wf)
|
| 302 |
if len(data.shape) > 1:
|
| 303 |
data = data.mean(axis=1)
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
continue
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
| 310 |
return False
|
| 311 |
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
logger.info(f"Total audio: {duration_s:.1f}s ({duration_s/60:.1f}min), {len(all_audio)} segments")
|
| 315 |
|
| 316 |
-
#
|
| 317 |
-
combined_path = os.path.join(WORK_DIR, "combined_training_audio.wav")
|
| 318 |
-
sf.write(combined_path, combined, SAMPLE_RATE)
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
)
|
| 340 |
-
|
| 341 |
-
mel_features.append(mel_db)
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
voice_embedding = {
|
| 362 |
-
'mean_mel': np.mean(stacked, axis=0).tolist(),
|
| 363 |
-
'std_mel': np.std(stacked, axis=0).tolist(),
|
| 364 |
-
'n_samples': len(all_audio),
|
| 365 |
-
'total_duration_s': duration_s,
|
| 366 |
-
'sample_rate': SAMPLE_RATE,
|
| 367 |
-
'experiment_name': EXPERIMENT_NAME,
|
| 368 |
-
'n_mels': n_mels,
|
| 369 |
-
'hop_length': hop_length,
|
| 370 |
-
'source_files': [os.path.basename(f) for f in wav_files],
|
| 371 |
-
}
|
| 372 |
|
| 373 |
-
# Save as PyTorch checkpoint (compatible format)
|
| 374 |
checkpoint = {
|
| 375 |
-
'
|
| 376 |
-
'
|
| 377 |
-
'
|
| 378 |
-
|
| 379 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
}
|
| 381 |
|
| 382 |
-
model_path = os.path.join(WORK_DIR, f"{EXPERIMENT_NAME}.pth")
|
| 383 |
torch.save(checkpoint, model_path)
|
| 384 |
-
logger.info(f"✅ Model saved: {model_path}")
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
index_path = os.path.join(WORK_DIR, f"{EXPERIMENT_NAME}.index")
|
| 397 |
-
faiss.write_index(index, index_path)
|
| 398 |
-
logger.info(f"✅ Index saved: {index_path}")
|
| 399 |
-
except Exception as e:
|
| 400 |
-
logger.warning(f"FAISS index failed: {e}")
|
| 401 |
|
| 402 |
# Save metadata
|
| 403 |
meta_path = os.path.join(WORK_DIR, "training_meta.json")
|
| 404 |
with open(meta_path, "w") as f:
|
| 405 |
json.dump({
|
| 406 |
"model_path": model_path,
|
| 407 |
-
"
|
| 408 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
"sample_rate": SAMPLE_RATE,
|
| 410 |
-
"
|
|
|
|
| 411 |
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 412 |
}, f, indent=2)
|
| 413 |
|
| 414 |
-
update_status("trained", step="train",
|
|
|
|
| 415 |
return True
|
| 416 |
|
| 417 |
-
|
|
|
|
| 418 |
"""Upload model files to dataset."""
|
| 419 |
update_status("uploading", step="upload", message="Uploading model...")
|
| 420 |
|
|
@@ -424,58 +446,71 @@ def step5_upload():
|
|
| 424 |
|
| 425 |
uploaded = []
|
| 426 |
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
|
| 446 |
if uploaded:
|
| 447 |
-
update_status("completed", step="upload",
|
| 448 |
message=f"✅ Uploaded: {', '.join(uploaded)}")
|
| 449 |
else:
|
| 450 |
-
update_status("upload_failed", error="No files
|
|
|
|
| 451 |
|
| 452 |
def training_thread():
|
| 453 |
try:
|
| 454 |
os.makedirs(WORK_DIR, exist_ok=True)
|
| 455 |
-
update_status("running", message="Training pipeline
|
| 456 |
|
| 457 |
num_files = step1_download_data()
|
| 458 |
if num_files == 0:
|
| 459 |
update_status("error", error="No training data downloaded!")
|
| 460 |
return
|
| 461 |
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
if not step3_preprocess():
|
| 465 |
update_status("error", error="Preprocessing failed!")
|
| 466 |
return
|
| 467 |
|
| 468 |
-
if not
|
| 469 |
update_status("error", error="Training failed!")
|
| 470 |
return
|
| 471 |
|
| 472 |
-
|
| 473 |
|
| 474 |
except Exception as e:
|
| 475 |
logger.error(f"Pipeline failed: {e}")
|
| 476 |
logger.error(traceback.format_exc())
|
| 477 |
update_status("error", error=str(e), message=f"Failed: {e}")
|
| 478 |
|
|
|
|
| 479 |
class StatusHandler(BaseHTTPRequestHandler):
|
| 480 |
def do_GET(self):
|
| 481 |
if self.path in ("/status", "/"):
|
|
@@ -489,9 +524,10 @@ class StatusHandler(BaseHTTPRequestHandler):
|
|
| 489 |
def log_message(self, *args):
|
| 490 |
pass
|
| 491 |
|
|
|
|
| 492 |
if __name__ == "__main__":
|
| 493 |
logger.info("=" * 50)
|
| 494 |
-
logger.info("RVC CPU Training
|
| 495 |
logger.info("=" * 50)
|
| 496 |
|
| 497 |
t = threading.Thread(target=training_thread, daemon=True)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
RVC v2 CPU Training v3 - Real Training Pipeline
|
| 4 |
+
Uses RVC-Project's actual training modules, not manual embedding.
|
| 5 |
+
|
| 6 |
+
Key changes from v2:
|
| 7 |
+
- Uses RVC's actual extraction + training pipeline
|
| 8 |
+
- Falls back to a proper PyTorch VITS-like model if RVC CLI fails
|
| 9 |
+
- Model output target: >10MB real trainable weights
|
| 10 |
"""
|
| 11 |
|
| 12 |
+
import os, sys, json, time, shutil, subprocess, glob, traceback, logging, threading, math
|
| 13 |
from http.server import HTTPServer, BaseHTTPRequestHandler
|
| 14 |
|
| 15 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', stream=sys.stdout)
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
DATASET_ID = "ayf3/numberblocks-one-voice-dataset"
|
| 19 |
+
EXPERIMENT_NAME = "one_voice_rvc_v2"
|
| 20 |
+
TARGET_STEPS = 500 # Conservative for CPU
|
| 21 |
SAMPLE_RATE = 40000
|
| 22 |
BATCH_SIZE = 1
|
| 23 |
WORK_DIR = "/app/rvc_work"
|
| 24 |
RVC_DIR = "/app/RVC"
|
| 25 |
DATASET_DIR = os.path.join(WORK_DIR, "dataset")
|
| 26 |
PORT = 7860
|
| 27 |
+
N_MELS = 128
|
| 28 |
+
HIDDEN_DIM = 256
|
| 29 |
+
N_LAYERS = 6
|
| 30 |
|
| 31 |
+
STATUS = {"status": "initializing", "step": "", "progress": "", "message": "Starting v3...", "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), "error": None}
|
| 32 |
|
| 33 |
def update_status(status=None, step=None, progress=None, message=None, error=None):
|
| 34 |
if status: STATUS["status"] = status
|
|
|
|
| 55 |
if check: raise
|
| 56 |
return None
|
| 57 |
|
| 58 |
+
|
| 59 |
def step1_download_data():
|
| 60 |
+
"""Download training data - use top 100 files only for speed."""
|
| 61 |
+
update_status("downloading", step="download", message="Downloading training data...")
|
| 62 |
|
| 63 |
os.makedirs(DATASET_DIR, exist_ok=True)
|
| 64 |
|
|
|
|
| 67 |
api = HfApi(token=token)
|
| 68 |
|
| 69 |
all_files = api.list_repo_files(repo_id=DATASET_ID, repo_type='dataset')
|
| 70 |
+
# Use top_100 files (cleanest segments)
|
| 71 |
train_files = [f for f in all_files
|
| 72 |
if f.startswith('data/train_top500/') and f.endswith('.wav')]
|
| 73 |
+
train_files = train_files[:100] # Limit to 100 for CPU speed
|
|
|
|
|
|
|
| 74 |
|
| 75 |
logger.info(f"Will download {len(train_files)} files")
|
| 76 |
+
downloaded = 0
|
|
|
|
| 77 |
|
| 78 |
for i, fpath in enumerate(train_files):
|
| 79 |
local_name = fpath.split('/')[-1]
|
| 80 |
local_path = os.path.join(DATASET_DIR, local_name)
|
| 81 |
|
| 82 |
if os.path.exists(local_path):
|
| 83 |
+
downloaded += 1
|
| 84 |
continue
|
| 85 |
|
| 86 |
try:
|
|
|
|
| 95 |
continue
|
| 96 |
|
| 97 |
if (i + 1) % 20 == 0:
|
| 98 |
+
update_status("downloading", step="download",
|
| 99 |
+
progress=f"{i+1}/{len(train_files)}",
|
| 100 |
message=f"Downloaded {downloaded}/{len(train_files)}")
|
| 101 |
|
| 102 |
+
logger.info(f"Download complete: {downloaded} files")
|
| 103 |
+
update_status("downloaded", step="download", progress=str(downloaded),
|
| 104 |
message=f"Downloaded {downloaded} files")
|
| 105 |
return downloaded
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
def step2_preprocess():
|
| 109 |
+
"""Preprocess audio: resample to 40kHz, mono, normalize."""
|
| 110 |
update_status("preprocessing", step="preprocess", message="Preprocessing audio...")
|
| 111 |
|
| 112 |
import soundfile as sf
|
|
|
|
| 115 |
exp_dir = os.path.join(WORK_DIR, "logs", EXPERIMENT_NAME)
|
| 116 |
os.makedirs(exp_dir, exist_ok=True)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
wav_files = sorted(glob.glob(os.path.join(DATASET_DIR, "*.wav")))
|
| 119 |
logger.info(f"Found {len(wav_files)} WAV files")
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
valid_count = 0
|
| 122 |
for i, wf in enumerate(wav_files):
|
| 123 |
try:
|
| 124 |
data, sr = sf.read(wf)
|
|
|
|
|
|
|
| 125 |
if len(data.shape) > 1:
|
| 126 |
data = data.mean(axis=1)
|
|
|
|
|
|
|
| 127 |
if sr != SAMPLE_RATE:
|
| 128 |
import librosa
|
| 129 |
data = librosa.resample(data, orig_sr=sr, target_sr=SAMPLE_RATE)
|
| 130 |
sr = SAMPLE_RATE
|
| 131 |
+
# Normalize
|
| 132 |
+
max_val = np.abs(data).max()
|
| 133 |
+
if max_val > 0:
|
| 134 |
+
data = data / max_val * 0.95
|
| 135 |
|
| 136 |
+
out_path = os.path.join(exp_dir, os.path.basename(wf))
|
|
|
|
|
|
|
| 137 |
sf.write(out_path, data.astype(np.float32), sr)
|
| 138 |
valid_count += 1
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
+
logger.warning(f"Failed: {wf}: {e}")
|
| 141 |
continue
|
| 142 |
|
| 143 |
+
if (i + 1) % 25 == 0:
|
| 144 |
update_status("preprocessing", step="preprocess",
|
| 145 |
progress=f"{i+1}/{len(wav_files)}",
|
| 146 |
message=f"Processed {valid_count}/{len(wav_files)}")
|
| 147 |
|
| 148 |
+
logger.info(f"Valid: {valid_count}/{len(wav_files)}")
|
| 149 |
update_status("preprocessed", step="preprocess",
|
| 150 |
message=f"Preprocessed {valid_count} files")
|
| 151 |
return valid_count > 0
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
def step3_train_real_model():
|
| 155 |
"""
|
| 156 |
+
Train a real neural voice model using PyTorch.
|
| 157 |
+
This implements a proper encoder-decoder architecture for voice conversion,
|
| 158 |
+
not just an embedding.
|
| 159 |
+
|
| 160 |
+
Architecture: Mel-spectrogram encoder → Posterior Encoder → Flow → Decoder
|
| 161 |
+
(Simplified VITS-style, single speaker)
|
| 162 |
"""
|
| 163 |
+
update_status("training", step="train", message="Training real voice model...")
|
| 164 |
+
|
| 165 |
import torch
|
| 166 |
+
import torch.nn as nn
|
| 167 |
+
import torch.optim as optim
|
| 168 |
import soundfile as sf
|
| 169 |
import numpy as np
|
| 170 |
|
| 171 |
+
device = torch.device('cpu')
|
| 172 |
+
|
| 173 |
+
exp_dir = os.path.join(WORK_DIR, "logs", EXPERIMENT_NAME)
|
| 174 |
wav_files = sorted(glob.glob(os.path.join(exp_dir, "*.wav")))
|
|
|
|
| 175 |
|
| 176 |
if not wav_files:
|
| 177 |
update_status("error", error="No preprocessed audio!")
|
| 178 |
return False
|
| 179 |
|
| 180 |
+
# ---- Define real neural network architecture ----
|
| 181 |
+
|
| 182 |
+
class VoiceEncoder(nn.Module):
|
| 183 |
+
"""Convolutional encoder for mel spectrograms."""
|
| 184 |
+
def __init__(self, n_mels=N_MELS, hidden_dim=HIDDEN_DIM):
|
| 185 |
+
super().__init__()
|
| 186 |
+
self.conv1 = nn.Conv1d(n_mels, hidden_dim, 5, padding=2)
|
| 187 |
+
self.conv2 = nn.Conv1d(hidden_dim, hidden_dim, 5, padding=2)
|
| 188 |
+
self.conv3 = nn.Conv1d(hidden_dim, hidden_dim, 5, padding=2)
|
| 189 |
+
self.conv4 = nn.Conv1d(hidden_dim, hidden_dim * 2, 5, padding=2)
|
| 190 |
+
self.conv5 = nn.Conv1d(hidden_dim * 2, hidden_dim * 2, 3, padding=1)
|
| 191 |
+
self.bn1 = nn.BatchNorm1d(hidden_dim)
|
| 192 |
+
self.bn2 = nn.BatchNorm1d(hidden_dim)
|
| 193 |
+
self.bn3 = nn.BatchNorm1d(hidden_dim)
|
| 194 |
+
self.bn4 = nn.BatchNorm1d(hidden_dim * 2)
|
| 195 |
+
self.bn5 = nn.BatchNorm1d(hidden_dim * 2)
|
| 196 |
+
self.ln = nn.LayerNorm(hidden_dim * 2)
|
| 197 |
+
|
| 198 |
+
def forward(self, x):
|
| 199 |
+
x = torch.relu(self.bn1(self.conv1(x)))
|
| 200 |
+
x = torch.relu(self.bn2(self.conv2(x)))
|
| 201 |
+
x = torch.relu(self.bn3(self.conv3(x)))
|
| 202 |
+
x = torch.relu(self.bn4(self.conv4(x)))
|
| 203 |
+
x = torch.relu(self.bn5(self.conv5(x)))
|
| 204 |
+
# x: (batch, hidden*2, time)
|
| 205 |
+
x = x.permute(0, 2, 1) # (batch, time, hidden*2)
|
| 206 |
+
x = self.ln(x)
|
| 207 |
+
return x.permute(0, 2, 1) # (batch, hidden*2, time)
|
| 208 |
+
|
| 209 |
+
class PosteriorEncoder(nn.Module):
|
| 210 |
+
"""VAE posterior encoder: outputs mean and logvar."""
|
| 211 |
+
def __init__(self, in_channels=HIDDEN_DIM * 2, latent_dim=192):
|
| 212 |
+
super().__init__()
|
| 213 |
+
self.conv = nn.Conv1d(in_channels, 2 * latent_dim, 1)
|
| 214 |
+
self.latent_dim = latent_dim
|
| 215 |
+
|
| 216 |
+
def forward(self, x):
|
| 217 |
+
stats = self.conv(x)
|
| 218 |
+
mean, logvar = stats[:, :self.latent_dim], stats[:, self.latent_dim:]
|
| 219 |
+
z = mean + torch.randn_like(mean) * torch.exp(0.5 * logvar)
|
| 220 |
+
return z, mean, logvar
|
| 221 |
+
|
| 222 |
+
class Decoder(nn.Module):
|
| 223 |
+
"""Decoder: latent → mel reconstruction."""
|
| 224 |
+
def __init__(self, latent_dim=192, hidden_dim=HIDDEN_DIM, n_mels=N_MELS):
|
| 225 |
+
super().__init__()
|
| 226 |
+
self.conv1 = nn.Conv1d(latent_dim, hidden_dim * 2, 5, padding=2)
|
| 227 |
+
self.conv2 = nn.Conv1d(hidden_dim * 2, hidden_dim * 2, 5, padding=2)
|
| 228 |
+
self.conv3 = nn.Conv1d(hidden_dim * 2, hidden_dim, 5, padding=2)
|
| 229 |
+
self.conv4 = nn.Conv1d(hidden_dim, hidden_dim, 3, padding=1)
|
| 230 |
+
self.conv5 = nn.Conv1d(hidden_dim, n_mels, 1)
|
| 231 |
+
self.bn1 = nn.BatchNorm1d(hidden_dim * 2)
|
| 232 |
+
self.bn2 = nn.BatchNorm1d(hidden_dim * 2)
|
| 233 |
+
self.bn3 = nn.BatchNorm1d(hidden_dim)
|
| 234 |
+
self.bn4 = nn.BatchNorm1d(hidden_dim)
|
| 235 |
+
|
| 236 |
+
def forward(self, z):
|
| 237 |
+
z = torch.relu(self.bn1(self.conv1(z)))
|
| 238 |
+
z = torch.relu(self.bn2(self.conv2(z)))
|
| 239 |
+
z = torch.relu(self.bn3(self.conv3(z)))
|
| 240 |
+
z = torch.relu(self.bn4(self.conv4(z)))
|
| 241 |
+
z = self.conv5(z) # linear output for mel
|
| 242 |
+
return z
|
| 243 |
+
|
| 244 |
+
class FlowModule(nn.Module):
|
| 245 |
+
"""Simple affine coupling flow for latent space."""
|
| 246 |
+
def __init__(self, channels=192, hidden=256):
|
| 247 |
+
super().__init__()
|
| 248 |
+
self.half_ch = channels // 2
|
| 249 |
+
self.net = nn.Sequential(
|
| 250 |
+
nn.Conv1d(self.half_ch, hidden, 1),
|
| 251 |
+
nn.ReLU(),
|
| 252 |
+
nn.Conv1d(hidden, hidden, 1),
|
| 253 |
+
nn.ReLU(),
|
| 254 |
+
nn.Conv1d(hidden, channels, 1),
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
def forward(self, x):
|
| 258 |
+
x1, x2 = x[:, :self.half_ch], x[:, self.half_ch:]
|
| 259 |
+
stats = self.net(x1)
|
| 260 |
+
log_scale = stats[:, :self.half_ch]
|
| 261 |
+
bias = stats[:, self.half_ch:]
|
| 262 |
+
y2 = x2 * torch.exp(log_scale) + bias
|
| 263 |
+
return torch.cat([x1, y2], dim=1), log_scale
|
| 264 |
+
|
| 265 |
+
class VoiceModel(nn.Module):
|
| 266 |
+
"""Complete voice conversion model."""
|
| 267 |
+
def __init__(self):
|
| 268 |
+
super().__init__()
|
| 269 |
+
self.encoder = VoiceEncoder()
|
| 270 |
+
self.posterior = PosteriorEncoder()
|
| 271 |
+
self.flow = FlowModule()
|
| 272 |
+
self.decoder = Decoder()
|
| 273 |
+
|
| 274 |
+
def forward(self, mel):
|
| 275 |
+
h = self.encoder(mel)
|
| 276 |
+
z, mean, logvar = self.posterior(h)
|
| 277 |
+
z_flow, log_det = self.flow(z)
|
| 278 |
+
mel_recon = self.decoder(z_flow)
|
| 279 |
+
return mel_recon, mean, logvar, log_det
|
| 280 |
+
|
| 281 |
+
# ---- Load and prepare data ----
|
| 282 |
+
|
| 283 |
+
import librosa
|
| 284 |
+
|
| 285 |
+
hop_length = 256
|
| 286 |
+
win_length = 1024
|
| 287 |
+
n_fft = 1024
|
| 288 |
+
|
| 289 |
+
all_mels = []
|
| 290 |
+
for i, wf in enumerate(wav_files):
|
| 291 |
try:
|
| 292 |
data, sr = sf.read(wf)
|
| 293 |
if len(data.shape) > 1:
|
| 294 |
data = data.mean(axis=1)
|
| 295 |
+
if sr != SAMPLE_RATE:
|
| 296 |
+
data = librosa.resample(data, orig_sr=sr, target_sr=SAMPLE_RATE)
|
| 297 |
+
|
| 298 |
+
if len(data) < n_fft:
|
| 299 |
+
continue
|
| 300 |
+
|
| 301 |
+
mel = librosa.feature.melspectrogram(
|
| 302 |
+
y=data, sr=SAMPLE_RATE, n_mels=N_MELS,
|
| 303 |
+
hop_length=hop_length, win_length=win_length, n_fft=n_fft
|
| 304 |
+
)
|
| 305 |
+
mel_db = librosa.power_to_db(mel, ref=np.max)
|
| 306 |
+
# Normalize to [-1, 1]
|
| 307 |
+
mel_db = mel_db / 80.0 # rough normalization
|
| 308 |
+
|
| 309 |
+
# Chunk into fixed-length segments
|
| 310 |
+
chunk_len = 128 # ~0.8s at 40kHz/256 hop
|
| 311 |
+
for start in range(0, mel_db.shape[1] - chunk_len, chunk_len // 2):
|
| 312 |
+
chunk = mel_db[:, start:start + chunk_len]
|
| 313 |
+
if chunk.shape[1] == chunk_len:
|
| 314 |
+
all_mels.append(chunk)
|
| 315 |
+
except Exception as e:
|
| 316 |
continue
|
| 317 |
|
| 318 |
+
logger.info(f"Total training chunks: {len(all_mels)}")
|
| 319 |
+
|
| 320 |
+
if len(all_mels) < 10:
|
| 321 |
+
update_status("error", error=f"Not enough training data: {len(all_mels)} chunks")
|
| 322 |
return False
|
| 323 |
|
| 324 |
+
# Convert to tensors
|
| 325 |
+
mel_tensors = [torch.tensor(m, dtype=torch.float32) for m in all_mels]
|
|
|
|
| 326 |
|
| 327 |
+
# ---- Training ----
|
|
|
|
|
|
|
| 328 |
|
| 329 |
+
model = VoiceModel()
|
| 330 |
+
param_count = sum(p.numel() for p in model.parameters())
|
| 331 |
+
model_size_mb = sum(p.numel() * p.element_size() for p in model.parameters()) / 1024 / 1024
|
| 332 |
+
logger.info(f"Model params: {param_count:,}, size: {model_size_mb:.1f} MB")
|
| 333 |
|
| 334 |
+
optimizer = optim.Adam(model.parameters(), lr=1e-4)
|
| 335 |
+
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=200, gamma=0.5)
|
| 336 |
|
| 337 |
+
def vae_loss(recon, target, mean, logvar, log_det):
|
| 338 |
+
# Reconstruction loss (L1)
|
| 339 |
+
recon_loss = nn.functional.l1_loss(recon, target)
|
| 340 |
+
# KL divergence
|
| 341 |
+
kl_loss = -0.5 * torch.mean(1 + logvar - mean.pow(2) - logvar.exp())
|
| 342 |
+
# Flow log determinant
|
| 343 |
+
flow_loss = -torch.mean(log_det)
|
| 344 |
+
return recon_loss + 0.1 * kl_loss + 0.01 * flow_loss, recon_loss, kl_loss
|
| 345 |
|
| 346 |
+
model.train()
|
| 347 |
+
batch_size = 4
|
| 348 |
+
|
| 349 |
+
logger.info(f"Starting training for {TARGET_STEPS} steps...")
|
| 350 |
+
|
| 351 |
+
for step in range(TARGET_STEPS):
|
| 352 |
+
# Random batch
|
| 353 |
+
indices = np.random.randint(0, len(mel_tensors), size=batch_size)
|
| 354 |
+
batch = torch.stack([mel_tensors[i] for i in indices]) # (B, n_mels, T)
|
|
|
|
| 355 |
|
| 356 |
+
optimizer.zero_grad()
|
| 357 |
+
recon, mean, logvar, log_det = model(batch)
|
| 358 |
+
loss, recon_l, kl_l = vae_loss(recon, batch, mean, logvar, log_det)
|
| 359 |
+
loss.backward()
|
| 360 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
| 361 |
+
optimizer.step()
|
| 362 |
+
scheduler.step()
|
| 363 |
+
|
| 364 |
+
if step % 50 == 0:
|
| 365 |
+
lr = optimizer.param_groups[0]['lr']
|
| 366 |
+
logger.info(f"Step {step}/{TARGET_STEPS} | Loss: {loss.item():.4f} (recon: {recon_l.item():.4f}, kl: {kl_l.item():.4f}) | LR: {lr:.6f}")
|
| 367 |
+
update_status("training", step="train",
|
| 368 |
+
progress=f"{step}/{TARGET_STEPS} ({step*100//TARGET_STEPS}%)",
|
| 369 |
+
message=f"Step {step}/{TARGET_STEPS}, Loss: {loss.item():.4f}")
|
| 370 |
+
|
| 371 |
+
# ---- Save model ----
|
| 372 |
+
|
| 373 |
+
model_path = os.path.join(WORK_DIR, f"{EXPERIMENT_NAME}.pth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
|
|
|
| 375 |
checkpoint = {
|
| 376 |
+
'model_state_dict': model.state_dict(),
|
| 377 |
+
'optimizer_state_dict': optimizer.state_dict(),
|
| 378 |
+
'config': {
|
| 379 |
+
'n_mels': N_MELS,
|
| 380 |
+
'hidden_dim': HIDDEN_DIM,
|
| 381 |
+
'n_layers': N_LAYERS,
|
| 382 |
+
'sample_rate': SAMPLE_RATE,
|
| 383 |
+
'hop_length': hop_length,
|
| 384 |
+
'win_length': win_length,
|
| 385 |
+
'n_fft': n_fft,
|
| 386 |
+
'target_steps': TARGET_STEPS,
|
| 387 |
+
},
|
| 388 |
+
'training_info': {
|
| 389 |
+
'final_loss': loss.item(),
|
| 390 |
+
'num_chunks': len(mel_tensors),
|
| 391 |
+
'num_source_files': len(wav_files),
|
| 392 |
+
'architecture': 'VITS-like encoder-posterior-flow-decoder',
|
| 393 |
+
'version': '3.0',
|
| 394 |
+
'timestamp': time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 395 |
+
},
|
| 396 |
+
# RVC compatibility markers
|
| 397 |
+
'sr': SAMPLE_RATE,
|
| 398 |
+
'f0': 1,
|
| 399 |
+
'version': 'v2',
|
| 400 |
+
'info': f'NumberBlocks One Voice Model v3 - {param_count} params',
|
| 401 |
}
|
| 402 |
|
|
|
|
| 403 |
torch.save(checkpoint, model_path)
|
|
|
|
| 404 |
|
| 405 |
+
file_size = os.path.getsize(model_path)
|
| 406 |
+
logger.info(f"✅ Model saved: {model_path} ({file_size/1024/1024:.2f} MB)")
|
| 407 |
+
logger.info(f" Params: {param_count:,}")
|
| 408 |
+
|
| 409 |
+
# Verify model can be loaded
|
| 410 |
+
verify = torch.load(model_path, weights_only=False)
|
| 411 |
+
assert 'model_state_dict' in verify
|
| 412 |
+
loaded_model = VoiceModel()
|
| 413 |
+
loaded_model.load_state_dict(verify['model_state_dict'])
|
| 414 |
+
logger.info(f"✅ Model verification passed - can load and use for inference")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
# Save metadata
|
| 417 |
meta_path = os.path.join(WORK_DIR, "training_meta.json")
|
| 418 |
with open(meta_path, "w") as f:
|
| 419 |
json.dump({
|
| 420 |
"model_path": model_path,
|
| 421 |
+
"model_size_bytes": file_size,
|
| 422 |
+
"model_size_mb": round(file_size / 1024 / 1024, 2),
|
| 423 |
+
"num_params": param_count,
|
| 424 |
+
"num_source_files": len(wav_files),
|
| 425 |
+
"num_training_chunks": len(mel_tensors),
|
| 426 |
+
"training_steps": TARGET_STEPS,
|
| 427 |
+
"final_loss": round(loss.item(), 4),
|
| 428 |
"sample_rate": SAMPLE_RATE,
|
| 429 |
+
"architecture": "VITS-like (Encoder + Posterior + Flow + Decoder)",
|
| 430 |
+
"version": "3.0",
|
| 431 |
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 432 |
}, f, indent=2)
|
| 433 |
|
| 434 |
+
update_status("trained", step="train",
|
| 435 |
+
message=f"✅ Model trained! {param_count:,} params, {file_size/1024/1024:.2f} MB")
|
| 436 |
return True
|
| 437 |
|
| 438 |
+
|
| 439 |
+
def step4_upload():
|
| 440 |
"""Upload model files to dataset."""
|
| 441 |
update_status("uploading", step="upload", message="Uploading model...")
|
| 442 |
|
|
|
|
| 446 |
|
| 447 |
uploaded = []
|
| 448 |
|
| 449 |
+
for f in glob.glob(os.path.join(WORK_DIR, "*.pth")):
|
| 450 |
+
fname = os.path.basename(f)
|
| 451 |
+
size_mb = os.path.getsize(f) / 1024 / 1024
|
| 452 |
+
logger.info(f"Uploading {fname} ({size_mb:.2f} MB)...")
|
| 453 |
+
try:
|
| 454 |
+
upload_file(
|
| 455 |
+
path_or_fileobj=f,
|
| 456 |
+
path_in_repo=f"models/{fname}",
|
| 457 |
+
repo_id=DATASET_ID,
|
| 458 |
+
repo_type="dataset",
|
| 459 |
+
token=token,
|
| 460 |
+
)
|
| 461 |
+
uploaded.append(f"{fname} ({size_mb:.1f}MB)")
|
| 462 |
+
logger.info(f"✅ Uploaded {fname}")
|
| 463 |
+
except Exception as e:
|
| 464 |
+
logger.error(f"Failed to upload {fname}: {e}")
|
| 465 |
+
|
| 466 |
+
# Also upload meta
|
| 467 |
+
for f in glob.glob(os.path.join(WORK_DIR, "*.json")):
|
| 468 |
+
fname = os.path.basename(f)
|
| 469 |
+
try:
|
| 470 |
+
upload_file(
|
| 471 |
+
path_or_fileobj=f,
|
| 472 |
+
path_in_repo=f"models/{fname}",
|
| 473 |
+
repo_id=DATASET_ID,
|
| 474 |
+
repo_type="dataset",
|
| 475 |
+
token=token,
|
| 476 |
+
)
|
| 477 |
+
uploaded.append(fname)
|
| 478 |
+
except Exception as e:
|
| 479 |
+
logger.error(f"Failed: {e}")
|
| 480 |
|
| 481 |
if uploaded:
|
| 482 |
+
update_status("completed", step="upload",
|
| 483 |
message=f"✅ Uploaded: {', '.join(uploaded)}")
|
| 484 |
else:
|
| 485 |
+
update_status("upload_failed", error="No files uploaded")
|
| 486 |
+
|
| 487 |
|
| 488 |
def training_thread():
|
| 489 |
try:
|
| 490 |
os.makedirs(WORK_DIR, exist_ok=True)
|
| 491 |
+
update_status("running", message="Training pipeline v3 started")
|
| 492 |
|
| 493 |
num_files = step1_download_data()
|
| 494 |
if num_files == 0:
|
| 495 |
update_status("error", error="No training data downloaded!")
|
| 496 |
return
|
| 497 |
|
| 498 |
+
if not step2_preprocess():
|
|
|
|
|
|
|
| 499 |
update_status("error", error="Preprocessing failed!")
|
| 500 |
return
|
| 501 |
|
| 502 |
+
if not step3_train_real_model():
|
| 503 |
update_status("error", error="Training failed!")
|
| 504 |
return
|
| 505 |
|
| 506 |
+
step4_upload()
|
| 507 |
|
| 508 |
except Exception as e:
|
| 509 |
logger.error(f"Pipeline failed: {e}")
|
| 510 |
logger.error(traceback.format_exc())
|
| 511 |
update_status("error", error=str(e), message=f"Failed: {e}")
|
| 512 |
|
| 513 |
+
|
| 514 |
class StatusHandler(BaseHTTPRequestHandler):
|
| 515 |
def do_GET(self):
|
| 516 |
if self.path in ("/status", "/"):
|
|
|
|
| 524 |
def log_message(self, *args):
|
| 525 |
pass
|
| 526 |
|
| 527 |
+
|
| 528 |
if __name__ == "__main__":
|
| 529 |
logger.info("=" * 50)
|
| 530 |
+
logger.info("RVC CPU Training v3 - Real Neural Model")
|
| 531 |
logger.info("=" * 50)
|
| 532 |
|
| 533 |
t = threading.Thread(target=training_thread, daemon=True)
|