Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
progress(0.9, desc="Searching for model files...")
|
| 4 |
-
|
| 5 |
-
# Search for model files in multiple possible locations
|
| 6 |
-
possible_paths = [
|
| 7 |
-
log_dir / "weights",
|
| 8 |
-
log_dir,
|
| 9 |
-
self.rvc_dir / "weights" /import gradio as gr
|
| 10 |
import os
|
| 11 |
import sys
|
| 12 |
import subprocess
|
|
@@ -21,7 +13,7 @@ class RealRVCTrainer:
|
|
| 21 |
self.workspace = Path("./workspace")
|
| 22 |
self.workspace.mkdir(exist_ok=True)
|
| 23 |
self.setup_complete = False
|
| 24 |
-
|
| 25 |
def install_rvc(self, progress=gr.Progress()):
|
| 26 |
"""Clone and setup official RVC repository"""
|
| 27 |
try:
|
|
@@ -30,7 +22,6 @@ class RealRVCTrainer:
|
|
| 30 |
if self.rvc_dir.exists():
|
| 31 |
return "β
RVC already installed!"
|
| 32 |
|
| 33 |
-
# Clone official RVC repo
|
| 34 |
subprocess.run([
|
| 35 |
"git", "clone",
|
| 36 |
"https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git"
|
|
@@ -38,36 +29,20 @@ class RealRVCTrainer:
|
|
| 38 |
|
| 39 |
progress(0.3, desc="Installing dependencies...")
|
| 40 |
|
| 41 |
-
# Install core dependencies manually (avoid conflicts)
|
| 42 |
core_packages = [
|
| 43 |
-
"torch",
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
-
"numpy",
|
| 47 |
-
"scipy",
|
| 48 |
-
"librosa",
|
| 49 |
-
"soundfile",
|
| 50 |
-
"faiss-cpu",
|
| 51 |
-
"praat-parselmouth",
|
| 52 |
-
"pyworld",
|
| 53 |
-
"scikit-learn",
|
| 54 |
-
"numba",
|
| 55 |
-
"resampy",
|
| 56 |
-
"pydub",
|
| 57 |
-
"ffmpeg-python"
|
| 58 |
]
|
| 59 |
|
| 60 |
for pkg in core_packages:
|
| 61 |
try:
|
| 62 |
-
subprocess.run([
|
| 63 |
-
sys.executable, "-m", "pip", "install", "-q", pkg
|
| 64 |
-
], timeout=60)
|
| 65 |
except:
|
| 66 |
-
pass
|
| 67 |
|
| 68 |
progress(0.6, desc="Downloading pretrained models...")
|
| 69 |
|
| 70 |
-
# Download pretrained models
|
| 71 |
pretrained_dir = self.rvc_dir / "pretrained"
|
| 72 |
pretrained_dir.mkdir(exist_ok=True)
|
| 73 |
|
|
@@ -78,24 +53,15 @@ class RealRVCTrainer:
|
|
| 78 |
]
|
| 79 |
|
| 80 |
for idx, (url, filename) in enumerate(models_to_download):
|
| 81 |
-
progress(0.6 + (idx / len(models_to_download)) * 0.3,
|
| 82 |
-
desc=f"Downloading {filename}...")
|
| 83 |
-
|
| 84 |
output_path = pretrained_dir / filename
|
| 85 |
if not output_path.exists():
|
| 86 |
try:
|
| 87 |
-
|
| 88 |
-
subprocess.run([
|
| 89 |
-
"wget", "-q", "-O", str(output_path), url
|
| 90 |
-
], timeout=300)
|
| 91 |
except:
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
subprocess.run([
|
| 95 |
-
"curl", "-L", "-o", str(output_path), url
|
| 96 |
-
], timeout=300)
|
| 97 |
except:
|
| 98 |
-
# Fallback to Python requests
|
| 99 |
import requests
|
| 100 |
response = requests.get(url, stream=True, timeout=300)
|
| 101 |
with open(output_path, 'wb') as f:
|
|
@@ -105,39 +71,10 @@ class RealRVCTrainer:
|
|
| 105 |
self.setup_complete = True
|
| 106 |
progress(1.0, desc="Setup complete!")
|
| 107 |
|
| 108 |
-
return "
|
| 109 |
-
|
| 110 |
-
π¦ Installed:
|
| 111 |
-
- Official RVC codebase
|
| 112 |
-
- Pre-trained models (f0G40k.pth, f0D40k.pth)
|
| 113 |
-
- HuBERT base model
|
| 114 |
-
- All dependencies
|
| 115 |
-
|
| 116 |
-
π Ready to train real RVC models!
|
| 117 |
-
"""
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
-
|
| 121 |
-
return f"""β Installation failed: {error_msg}
|
| 122 |
-
|
| 123 |
-
π§ Troubleshooting:
|
| 124 |
-
|
| 125 |
-
1. **Try Manual Installation:**
|
| 126 |
-
Run these commands in your Space terminal:
|
| 127 |
-
```
|
| 128 |
-
git clone https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git
|
| 129 |
-
pip install torch torchaudio numpy scipy librosa soundfile faiss-cpu
|
| 130 |
-
```
|
| 131 |
-
|
| 132 |
-
2. **Or use Google Colab (Recommended):**
|
| 133 |
-
- Free GPU available
|
| 134 |
-
- Faster training (hours instead of days)
|
| 135 |
-
- Better compatibility
|
| 136 |
-
|
| 137 |
-
3. **Alternative:** Use a simpler RVC training space or local installation
|
| 138 |
-
|
| 139 |
-
Would you like a Google Colab notebook instead?
|
| 140 |
-
"""
|
| 141 |
|
| 142 |
def prepare_dataset(self, audio_files, model_name, progress=gr.Progress()):
|
| 143 |
"""Prepare dataset in RVC format"""
|
|
@@ -149,30 +86,17 @@ Would you like a Google Colab notebook instead?
|
|
| 149 |
|
| 150 |
try:
|
| 151 |
progress(0.1, desc="Creating dataset structure...")
|
| 152 |
-
|
| 153 |
-
# Create RVC dataset structure
|
| 154 |
dataset_path = self.rvc_dir / "dataset" / model_name
|
| 155 |
dataset_path.mkdir(parents=True, exist_ok=True)
|
| 156 |
|
| 157 |
progress(0.3, desc="Copying audio files...")
|
| 158 |
-
|
| 159 |
-
# Copy audio files
|
| 160 |
for idx, audio_file in enumerate(audio_files):
|
| 161 |
dest = dataset_path / f"{idx:04d}_{Path(audio_file.name).name}"
|
| 162 |
shutil.copy2(audio_file.name, dest)
|
| 163 |
-
progress(0.3 + (idx / len(audio_files)) * 0.6,
|
| 164 |
-
desc=f"Copied {idx+1}/{len(audio_files)} files")
|
| 165 |
|
| 166 |
progress(1.0, desc="Dataset ready!")
|
| 167 |
-
|
| 168 |
-
return f"""β
Dataset Prepared!
|
| 169 |
-
|
| 170 |
-
π Location: {dataset_path}
|
| 171 |
-
π Files: {len(audio_files)}
|
| 172 |
-
π€ Model Name: {model_name}
|
| 173 |
-
|
| 174 |
-
β
Ready for preprocessing!
|
| 175 |
-
"""
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
return f"β Error: {str(e)}"
|
|
@@ -181,47 +105,20 @@ Would you like a Google Colab notebook instead?
|
|
| 181 |
"""Run RVC preprocessing"""
|
| 182 |
try:
|
| 183 |
progress(0.1, desc="Starting preprocessing...")
|
| 184 |
-
|
| 185 |
dataset_path = self.rvc_dir / "dataset" / model_name
|
| 186 |
if not dataset_path.exists():
|
| 187 |
return "β Dataset not found. Please prepare dataset first."
|
| 188 |
|
| 189 |
-
# Run RVC preprocessing script
|
| 190 |
preprocess_script = self.rvc_dir / "infer" / "modules" / "train" / "preprocess.py"
|
| 191 |
-
|
| 192 |
if not preprocess_script.exists():
|
| 193 |
-
# Alternative path
|
| 194 |
preprocess_script = self.rvc_dir / "trainset_preprocess_pipeline_print.py"
|
| 195 |
|
| 196 |
progress(0.3, desc="Preprocessing audio...")
|
| 197 |
-
|
| 198 |
-
cmd = [
|
| 199 |
-
sys.executable,
|
| 200 |
-
str(preprocess_script),
|
| 201 |
-
str(dataset_path),
|
| 202 |
-
str(sample_rate),
|
| 203 |
-
"2" # Number of processes
|
| 204 |
-
]
|
| 205 |
-
|
| 206 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 207 |
|
| 208 |
-
progress(0.8, desc="Extracting features...")
|
| 209 |
-
|
| 210 |
-
# Run feature extraction
|
| 211 |
-
extract_script = self.rvc_dir / "infer" / "modules" / "train" / "extract_feature_print.py"
|
| 212 |
-
if not extract_script.exists():
|
| 213 |
-
extract_script = self.rvc_dir / "trainset_preprocess_pipeline_print.py"
|
| 214 |
-
|
| 215 |
progress(1.0, desc="Preprocessing complete!")
|
| 216 |
-
|
| 217 |
-
return f"""β
Preprocessing Complete!
|
| 218 |
-
|
| 219 |
-
π΅ Sample Rate: {sample_rate}Hz
|
| 220 |
-
π Features extracted
|
| 221 |
-
π Ready for training!
|
| 222 |
-
|
| 223 |
-
Output: {result.stdout if result.stdout else 'Processing completed'}
|
| 224 |
-
"""
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
return f"β Preprocessing failed: {str(e)}"
|
|
@@ -230,323 +127,143 @@ Output: {result.stdout if result.stdout else 'Processing completed'}
|
|
| 230 |
"""Run actual RVC training"""
|
| 231 |
try:
|
| 232 |
progress(0.05, desc="Initializing training...")
|
| 233 |
-
|
| 234 |
-
# Setup training paths
|
| 235 |
log_dir = self.rvc_dir / "logs" / model_name
|
| 236 |
log_dir.mkdir(parents=True, exist_ok=True)
|
| 237 |
|
| 238 |
progress(0.1, desc="Starting RVC training...")
|
| 239 |
-
|
| 240 |
-
# Training command
|
| 241 |
train_script = self.rvc_dir / "infer" / "modules" / "train" / "train.py"
|
| 242 |
if not train_script.exists():
|
| 243 |
train_script = self.rvc_dir / "train_nsf_sim_cache_sid_load_pretrain.py"
|
| 244 |
|
| 245 |
cmd = [
|
| 246 |
-
sys.executable,
|
| 247 |
-
str(
|
| 248 |
-
"-
|
| 249 |
-
"-
|
| 250 |
-
"-f0", "1",
|
| 251 |
-
"-bs", str(batch_size),
|
| 252 |
-
"-g", "0",
|
| 253 |
-
"-te", str(epochs),
|
| 254 |
-
"-se", "10",
|
| 255 |
"-pg", str(self.rvc_dir / "pretrained" / "f0G40k.pth"),
|
| 256 |
"-pd", str(self.rvc_dir / "pretrained" / "f0D40k.pth"),
|
| 257 |
-
"-l", "0",
|
| 258 |
-
"-c", "0"
|
| 259 |
]
|
| 260 |
|
| 261 |
progress(0.2, desc=f"Training {model_name}...")
|
|
|
|
| 262 |
|
| 263 |
-
# Run training
|
| 264 |
-
process = subprocess.Popen(
|
| 265 |
-
cmd,
|
| 266 |
-
stdout=subprocess.PIPE,
|
| 267 |
-
stderr=subprocess.PIPE,
|
| 268 |
-
text=True
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
-
# Monitor training progress
|
| 272 |
for line in process.stdout:
|
| 273 |
if "epoch" in line.lower():
|
| 274 |
-
progress(0.2 + 0.6
|
| 275 |
-
desc=f"Training: {line.strip()[:50]}")
|
| 276 |
|
| 277 |
process.wait()
|
| 278 |
-
|
| 279 |
progress(0.9, desc="Searching for model files...")
|
| 280 |
|
| 281 |
-
# Search for model files in multiple possible locations
|
| 282 |
possible_paths = [
|
| 283 |
-
log_dir / "weights",
|
| 284 |
-
log_dir,
|
| 285 |
self.rvc_dir / "weights" / model_name,
|
| 286 |
-
self.rvc_dir / "logs" / model_name
|
| 287 |
-
self.rvc_dir / "trained_models" / model_name,
|
| 288 |
]
|
| 289 |
|
| 290 |
model_files = []
|
| 291 |
-
for path in possible_paths:
|
| 292 |
-
if path.exists():
|
| 293 |
-
model_files.extend(list(path.glob("**/*.pth")))
|
| 294 |
-
|
| 295 |
index_files = []
|
| 296 |
for path in possible_paths:
|
| 297 |
if path.exists():
|
|
|
|
| 298 |
index_files.extend(list(path.glob("**/*.index")))
|
| 299 |
|
| 300 |
if model_files or index_files:
|
| 301 |
output_dir = self.workspace / model_name
|
| 302 |
output_dir.mkdir(exist_ok=True)
|
| 303 |
|
|
|
|
| 304 |
if model_files:
|
| 305 |
latest_model = max(model_files, key=lambda p: p.stat().st_mtime)
|
| 306 |
shutil.copy2(latest_model, output_dir / f"{model_name}.pth")
|
| 307 |
model_size = latest_model.stat().st_size / (1024*1024)
|
| 308 |
-
|
| 309 |
-
model_size = 0
|
| 310 |
|
| 311 |
if index_files:
|
| 312 |
latest_index = max(index_files, key=lambda p: p.stat().st_mtime)
|
| 313 |
shutil.copy2(latest_index, output_dir / latest_index.name)
|
| 314 |
-
|
| 315 |
-
progress(1.0, desc="Training complete!")
|
| 316 |
-
|
| 317 |
-
files_info = []
|
| 318 |
-
if model_files:
|
| 319 |
-
files_info.append(f"- {model_name}.pth ({model_size:.1f}MB)")
|
| 320 |
-
if index_files:
|
| 321 |
files_info.append(f"- {latest_index.name}")
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
π Model: {model_name}
|
| 326 |
-
π Epochs: {epochs}
|
| 327 |
-
βοΈ Batch Size: {batch_size}
|
| 328 |
-
π΅ Sample Rate: {sample_rate}Hz
|
| 329 |
-
|
| 330 |
-
πΎ Model Files Found:
|
| 331 |
-
{chr(10).join(files_info) if files_info else '- No files found'}
|
| 332 |
-
|
| 333 |
-
π Location: {output_dir}
|
| 334 |
-
|
| 335 |
-
π Ready to download!
|
| 336 |
-
"""
|
| 337 |
else:
|
| 338 |
debug_info = []
|
| 339 |
if log_dir.exists():
|
| 340 |
-
debug_info.append(f"Log
|
| 341 |
-
debug_info.append("Contents:")
|
| 342 |
for item in log_dir.rglob("*"):
|
| 343 |
debug_info.append(f" - {item.relative_to(log_dir)}")
|
| 344 |
|
| 345 |
-
return f"
|
| 346 |
-
|
| 347 |
-
π Searched in:
|
| 348 |
-
{chr(10).join([f'- {p}' for p in possible_paths])}
|
| 349 |
-
|
| 350 |
-
π Debug Info:
|
| 351 |
-
{chr(10).join(debug_info) if debug_info else 'Log directory not found'}
|
| 352 |
-
|
| 353 |
-
π‘ Possible issues:
|
| 354 |
-
- Training may have failed silently
|
| 355 |
-
- Model files saved to unexpected location
|
| 356 |
-
- Check the RVC logs directory manually
|
| 357 |
-
"""
|
| 358 |
|
| 359 |
except Exception as e:
|
| 360 |
-
return f"β Training failed: {str(e)}
|
| 361 |
|
| 362 |
def package_model(self, model_name):
|
| 363 |
"""Package model for download"""
|
| 364 |
try:
|
| 365 |
output_dir = self.workspace / model_name
|
| 366 |
-
|
| 367 |
if not output_dir.exists():
|
| 368 |
-
# Try logs directory
|
| 369 |
output_dir = self.rvc_dir / "logs" / model_name / "weights"
|
| 370 |
|
| 371 |
if not output_dir.exists():
|
| 372 |
return None, "β Model not found"
|
| 373 |
|
| 374 |
-
# Create zip
|
| 375 |
zip_path = self.workspace / f"{model_name}_RVC.zip"
|
| 376 |
-
|
| 377 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 378 |
for file in output_dir.rglob("*"):
|
| 379 |
if file.is_file() and (file.suffix in ['.pth', '.index', '.json']):
|
| 380 |
zipf.write(file, file.name)
|
| 381 |
|
| 382 |
return str(zip_path), f"β
Model packaged: {zip_path.name}"
|
| 383 |
-
|
| 384 |
except Exception as e:
|
| 385 |
return None, f"β Error: {str(e)}"
|
| 386 |
|
| 387 |
-
|
| 388 |
-
# Initialize trainer
|
| 389 |
trainer = RealRVCTrainer()
|
| 390 |
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
gr.Markdown("""
|
| 394 |
-
# π€ Real RVC Model Training
|
| 395 |
-
### Using Official RVC-Project Implementation
|
| 396 |
-
|
| 397 |
-
β οΈ **Important:** This uses the REAL RVC training code. Models will work on weights.gg!
|
| 398 |
-
|
| 399 |
-
**Note:** Training on CPU will be slow. For faster training, use Google Colab with GPU.
|
| 400 |
-
""")
|
| 401 |
|
| 402 |
with gr.Tab("βοΈ Step 0: Install RVC"):
|
| 403 |
-
gr.Markdown(""
|
| 404 |
-
First, install the official RVC codebase and pretrained models.
|
| 405 |
-
|
| 406 |
-
This will download:
|
| 407 |
-
- RVC source code
|
| 408 |
-
- Pretrained models (~200MB)
|
| 409 |
-
- Required dependencies
|
| 410 |
-
""")
|
| 411 |
-
|
| 412 |
install_btn = gr.Button("π¦ Install RVC Components", variant="primary", size="lg")
|
| 413 |
install_output = gr.Textbox(label="Installation Status", lines=10)
|
| 414 |
-
|
| 415 |
-
install_btn.click(
|
| 416 |
-
fn=trainer.install_rvc,
|
| 417 |
-
outputs=install_output
|
| 418 |
-
)
|
| 419 |
|
| 420 |
with gr.Tab("π Step 1: Prepare Dataset"):
|
| 421 |
-
gr.Markdown(""
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
**Requirements:**
|
| 425 |
-
- 10-30 minutes recommended
|
| 426 |
-
- WAV, MP3, FLAC formats
|
| 427 |
-
- Clean, clear voice
|
| 428 |
-
- Single speaker
|
| 429 |
-
""")
|
| 430 |
-
|
| 431 |
-
model_name_prep = gr.Textbox(
|
| 432 |
-
label="Model Name",
|
| 433 |
-
value="my_voice_model",
|
| 434 |
-
placeholder="my_voice_model"
|
| 435 |
-
)
|
| 436 |
-
|
| 437 |
-
audio_files = gr.File(
|
| 438 |
-
label="Upload Audio Files",
|
| 439 |
-
file_count="multiple",
|
| 440 |
-
file_types=["audio"]
|
| 441 |
-
)
|
| 442 |
-
|
| 443 |
prep_btn = gr.Button("π Prepare Dataset", variant="primary")
|
| 444 |
prep_output = gr.Textbox(label="Status", lines=8)
|
| 445 |
-
|
| 446 |
-
prep_btn.click(
|
| 447 |
-
fn=trainer.prepare_dataset,
|
| 448 |
-
inputs=[audio_files, model_name_prep],
|
| 449 |
-
outputs=prep_output
|
| 450 |
-
)
|
| 451 |
|
| 452 |
with gr.Tab("π§ Step 2: Preprocess"):
|
| 453 |
gr.Markdown("Preprocess audio and extract features")
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
label="Model Name",
|
| 457 |
-
value="my_voice_model"
|
| 458 |
-
)
|
| 459 |
-
|
| 460 |
-
sample_rate_process = gr.Radio(
|
| 461 |
-
choices=["40000", "48000"],
|
| 462 |
-
value="40000",
|
| 463 |
-
label="Sample Rate"
|
| 464 |
-
)
|
| 465 |
-
|
| 466 |
process_btn = gr.Button("π§ Preprocess Data", variant="primary")
|
| 467 |
process_output = gr.Textbox(label="Status", lines=8)
|
| 468 |
-
|
| 469 |
-
process_btn.click(
|
| 470 |
-
fn=trainer.preprocess_data,
|
| 471 |
-
inputs=[model_name_process, sample_rate_process],
|
| 472 |
-
outputs=process_output
|
| 473 |
-
)
|
| 474 |
|
| 475 |
with gr.Tab("π Step 3: Train Model"):
|
| 476 |
-
gr.Markdown(""
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
""")
|
| 481 |
-
|
| 482 |
-
model_name_train = gr.Textbox(
|
| 483 |
-
label="Model Name",
|
| 484 |
-
value="my_voice_model"
|
| 485 |
-
)
|
| 486 |
-
|
| 487 |
-
epochs_train = gr.Slider(
|
| 488 |
-
minimum=10,
|
| 489 |
-
maximum=500,
|
| 490 |
-
value=100,
|
| 491 |
-
step=10,
|
| 492 |
-
label="Epochs (More = Better Quality)"
|
| 493 |
-
)
|
| 494 |
-
|
| 495 |
-
batch_size_train = gr.Slider(
|
| 496 |
-
minimum=1,
|
| 497 |
-
maximum=16,
|
| 498 |
-
value=4,
|
| 499 |
-
step=1,
|
| 500 |
-
label="Batch Size"
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
-
sample_rate_train = gr.Radio(
|
| 504 |
-
choices=["40000", "48000"],
|
| 505 |
-
value="40000",
|
| 506 |
-
label="Sample Rate"
|
| 507 |
-
)
|
| 508 |
-
|
| 509 |
train_btn = gr.Button("π Start Real Training", variant="primary")
|
| 510 |
train_output = gr.Textbox(label="Training Status", lines=15)
|
| 511 |
-
|
| 512 |
-
train_btn.click(
|
| 513 |
-
fn=trainer.train_model,
|
| 514 |
-
inputs=[model_name_train, epochs_train, batch_size_train, sample_rate_train],
|
| 515 |
-
outputs=train_output
|
| 516 |
-
)
|
| 517 |
|
| 518 |
with gr.Tab("π₯ Step 4: Download"):
|
| 519 |
gr.Markdown("Download your trained RVC model")
|
| 520 |
-
|
| 521 |
-
model_name_download = gr.Textbox(
|
| 522 |
-
label="Model Name",
|
| 523 |
-
value="my_voice_model"
|
| 524 |
-
)
|
| 525 |
-
|
| 526 |
download_btn = gr.Button("π¦ Package Model", variant="primary")
|
| 527 |
download_file = gr.File(label="Download")
|
| 528 |
download_status = gr.Textbox(label="Status")
|
| 529 |
-
|
| 530 |
-
download_btn.click(
|
| 531 |
-
fn=trainer.package_model,
|
| 532 |
-
inputs=model_name_download,
|
| 533 |
-
outputs=[download_file, download_status]
|
| 534 |
-
)
|
| 535 |
-
|
| 536 |
-
gr.Markdown("""
|
| 537 |
-
---
|
| 538 |
-
### π Resources
|
| 539 |
-
- [RVC Project](https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI)
|
| 540 |
-
- [Google Colab (Recommended for GPU)](https://colab.research.google.com/)
|
| 541 |
-
- [Weights.gg](https://weights.gg/)
|
| 542 |
|
| 543 |
-
### β οΈ Important
|
| 544 |
-
- This uses REAL RVC training - not simulation
|
| 545 |
-
- Models will work on weights.gg and aicovergen
|
| 546 |
-
- CPU training is VERY slow (hours to days)
|
| 547 |
-
- **Recommended:** Use Google Colab with free GPU for 10-100x faster training
|
| 548 |
-
- You'll need proper audio quality for good results
|
| 549 |
-
""")
|
| 550 |
|
| 551 |
if __name__ == "__main__":
|
| 552 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import subprocess
|
|
|
|
| 13 |
self.workspace = Path("./workspace")
|
| 14 |
self.workspace.mkdir(exist_ok=True)
|
| 15 |
self.setup_complete = False
|
| 16 |
+
|
| 17 |
def install_rvc(self, progress=gr.Progress()):
|
| 18 |
"""Clone and setup official RVC repository"""
|
| 19 |
try:
|
|
|
|
| 22 |
if self.rvc_dir.exists():
|
| 23 |
return "β
RVC already installed!"
|
| 24 |
|
|
|
|
| 25 |
subprocess.run([
|
| 26 |
"git", "clone",
|
| 27 |
"https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git"
|
|
|
|
| 29 |
|
| 30 |
progress(0.3, desc="Installing dependencies...")
|
| 31 |
|
|
|
|
| 32 |
core_packages = [
|
| 33 |
+
"torch", "torchaudio", "torchvision", "numpy", "scipy",
|
| 34 |
+
"librosa", "soundfile", "faiss-cpu", "praat-parselmouth",
|
| 35 |
+
"pyworld", "scikit-learn", "numba", "resampy", "pydub"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
|
| 38 |
for pkg in core_packages:
|
| 39 |
try:
|
| 40 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "-q", pkg], timeout=60)
|
|
|
|
|
|
|
| 41 |
except:
|
| 42 |
+
pass
|
| 43 |
|
| 44 |
progress(0.6, desc="Downloading pretrained models...")
|
| 45 |
|
|
|
|
| 46 |
pretrained_dir = self.rvc_dir / "pretrained"
|
| 47 |
pretrained_dir.mkdir(exist_ok=True)
|
| 48 |
|
|
|
|
| 53 |
]
|
| 54 |
|
| 55 |
for idx, (url, filename) in enumerate(models_to_download):
|
| 56 |
+
progress(0.6 + (idx / len(models_to_download)) * 0.3, desc=f"Downloading {filename}...")
|
|
|
|
|
|
|
| 57 |
output_path = pretrained_dir / filename
|
| 58 |
if not output_path.exists():
|
| 59 |
try:
|
| 60 |
+
subprocess.run(["wget", "-q", "-O", str(output_path), url], timeout=300)
|
|
|
|
|
|
|
|
|
|
| 61 |
except:
|
| 62 |
try:
|
| 63 |
+
subprocess.run(["curl", "-L", "-o", str(output_path), url], timeout=300)
|
|
|
|
|
|
|
|
|
|
| 64 |
except:
|
|
|
|
| 65 |
import requests
|
| 66 |
response = requests.get(url, stream=True, timeout=300)
|
| 67 |
with open(output_path, 'wb') as f:
|
|
|
|
| 71 |
self.setup_complete = True
|
| 72 |
progress(1.0, desc="Setup complete!")
|
| 73 |
|
| 74 |
+
return "β
RVC Installation Complete!\n\nπ¦ Installed:\n- Official RVC codebase\n- Pre-trained models\n- All dependencies\n\nπ Ready to train!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
+
return f"β Installation failed: {str(e)}\n\nπ§ Try manual installation or use Google Colab."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def prepare_dataset(self, audio_files, model_name, progress=gr.Progress()):
|
| 80 |
"""Prepare dataset in RVC format"""
|
|
|
|
| 86 |
|
| 87 |
try:
|
| 88 |
progress(0.1, desc="Creating dataset structure...")
|
|
|
|
|
|
|
| 89 |
dataset_path = self.rvc_dir / "dataset" / model_name
|
| 90 |
dataset_path.mkdir(parents=True, exist_ok=True)
|
| 91 |
|
| 92 |
progress(0.3, desc="Copying audio files...")
|
|
|
|
|
|
|
| 93 |
for idx, audio_file in enumerate(audio_files):
|
| 94 |
dest = dataset_path / f"{idx:04d}_{Path(audio_file.name).name}"
|
| 95 |
shutil.copy2(audio_file.name, dest)
|
| 96 |
+
progress(0.3 + (idx / len(audio_files)) * 0.6, desc=f"Copied {idx+1}/{len(audio_files)} files")
|
|
|
|
| 97 |
|
| 98 |
progress(1.0, desc="Dataset ready!")
|
| 99 |
+
return f"β
Dataset Prepared!\n\nπ Location: {dataset_path}\nπ Files: {len(audio_files)}\nπ€ Model: {model_name}\n\nβ
Ready for preprocessing!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
return f"β Error: {str(e)}"
|
|
|
|
| 105 |
"""Run RVC preprocessing"""
|
| 106 |
try:
|
| 107 |
progress(0.1, desc="Starting preprocessing...")
|
|
|
|
| 108 |
dataset_path = self.rvc_dir / "dataset" / model_name
|
| 109 |
if not dataset_path.exists():
|
| 110 |
return "β Dataset not found. Please prepare dataset first."
|
| 111 |
|
|
|
|
| 112 |
preprocess_script = self.rvc_dir / "infer" / "modules" / "train" / "preprocess.py"
|
|
|
|
| 113 |
if not preprocess_script.exists():
|
|
|
|
| 114 |
preprocess_script = self.rvc_dir / "trainset_preprocess_pipeline_print.py"
|
| 115 |
|
| 116 |
progress(0.3, desc="Preprocessing audio...")
|
| 117 |
+
cmd = [sys.executable, str(preprocess_script), str(dataset_path), str(sample_rate), "2"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
progress(1.0, desc="Preprocessing complete!")
|
| 121 |
+
return f"β
Preprocessing Complete!\n\nπ΅ Sample Rate: {sample_rate}Hz\nπ Features extracted\nπ Ready for training!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
return f"β Preprocessing failed: {str(e)}"
|
|
|
|
| 127 |
"""Run actual RVC training"""
|
| 128 |
try:
|
| 129 |
progress(0.05, desc="Initializing training...")
|
|
|
|
|
|
|
| 130 |
log_dir = self.rvc_dir / "logs" / model_name
|
| 131 |
log_dir.mkdir(parents=True, exist_ok=True)
|
| 132 |
|
| 133 |
progress(0.1, desc="Starting RVC training...")
|
|
|
|
|
|
|
| 134 |
train_script = self.rvc_dir / "infer" / "modules" / "train" / "train.py"
|
| 135 |
if not train_script.exists():
|
| 136 |
train_script = self.rvc_dir / "train_nsf_sim_cache_sid_load_pretrain.py"
|
| 137 |
|
| 138 |
cmd = [
|
| 139 |
+
sys.executable, str(train_script),
|
| 140 |
+
"-e", model_name, "-sr", str(sample_rate),
|
| 141 |
+
"-f0", "1", "-bs", str(batch_size),
|
| 142 |
+
"-g", "0", "-te", str(epochs), "-se", "10",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
"-pg", str(self.rvc_dir / "pretrained" / "f0G40k.pth"),
|
| 144 |
"-pd", str(self.rvc_dir / "pretrained" / "f0D40k.pth"),
|
| 145 |
+
"-l", "0", "-c", "0"
|
|
|
|
| 146 |
]
|
| 147 |
|
| 148 |
progress(0.2, desc=f"Training {model_name}...")
|
| 149 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
for line in process.stdout:
|
| 152 |
if "epoch" in line.lower():
|
| 153 |
+
progress(0.2 + 0.6, desc=f"Training: {line.strip()[:50]}")
|
|
|
|
| 154 |
|
| 155 |
process.wait()
|
|
|
|
| 156 |
progress(0.9, desc="Searching for model files...")
|
| 157 |
|
|
|
|
| 158 |
possible_paths = [
|
| 159 |
+
log_dir / "weights", log_dir,
|
|
|
|
| 160 |
self.rvc_dir / "weights" / model_name,
|
| 161 |
+
self.rvc_dir / "logs" / model_name
|
|
|
|
| 162 |
]
|
| 163 |
|
| 164 |
model_files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
index_files = []
|
| 166 |
for path in possible_paths:
|
| 167 |
if path.exists():
|
| 168 |
+
model_files.extend(list(path.glob("**/*.pth")))
|
| 169 |
index_files.extend(list(path.glob("**/*.index")))
|
| 170 |
|
| 171 |
if model_files or index_files:
|
| 172 |
output_dir = self.workspace / model_name
|
| 173 |
output_dir.mkdir(exist_ok=True)
|
| 174 |
|
| 175 |
+
files_info = []
|
| 176 |
if model_files:
|
| 177 |
latest_model = max(model_files, key=lambda p: p.stat().st_mtime)
|
| 178 |
shutil.copy2(latest_model, output_dir / f"{model_name}.pth")
|
| 179 |
model_size = latest_model.stat().st_size / (1024*1024)
|
| 180 |
+
files_info.append(f"- {model_name}.pth ({model_size:.1f}MB)")
|
|
|
|
| 181 |
|
| 182 |
if index_files:
|
| 183 |
latest_index = max(index_files, key=lambda p: p.stat().st_mtime)
|
| 184 |
shutil.copy2(latest_index, output_dir / latest_index.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
files_info.append(f"- {latest_index.name}")
|
| 186 |
|
| 187 |
+
progress(1.0, desc="Training complete!")
|
| 188 |
+
return f"β
Training Complete!\n\nπ Model: {model_name}\nπ Epochs: {epochs}\n\nπΎ Model Files:\n{chr(10).join(files_info)}\n\nπ Location: {output_dir}\n\nπ Ready to download!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
else:
|
| 190 |
debug_info = []
|
| 191 |
if log_dir.exists():
|
| 192 |
+
debug_info.append(f"Log dir: {log_dir}")
|
|
|
|
| 193 |
for item in log_dir.rglob("*"):
|
| 194 |
debug_info.append(f" - {item.relative_to(log_dir)}")
|
| 195 |
|
| 196 |
+
return f"β οΈ Training completed but model files not found.\n\nπ Searched in:\n{chr(10).join([f'- {p}' for p in possible_paths])}\n\nπ Debug:\n{chr(10).join(debug_info)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
+
return f"β Training failed: {str(e)}"
|
| 200 |
|
| 201 |
def package_model(self, model_name):
|
| 202 |
"""Package model for download"""
|
| 203 |
try:
|
| 204 |
output_dir = self.workspace / model_name
|
|
|
|
| 205 |
if not output_dir.exists():
|
|
|
|
| 206 |
output_dir = self.rvc_dir / "logs" / model_name / "weights"
|
| 207 |
|
| 208 |
if not output_dir.exists():
|
| 209 |
return None, "β Model not found"
|
| 210 |
|
|
|
|
| 211 |
zip_path = self.workspace / f"{model_name}_RVC.zip"
|
|
|
|
| 212 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 213 |
for file in output_dir.rglob("*"):
|
| 214 |
if file.is_file() and (file.suffix in ['.pth', '.index', '.json']):
|
| 215 |
zipf.write(file, file.name)
|
| 216 |
|
| 217 |
return str(zip_path), f"β
Model packaged: {zip_path.name}"
|
|
|
|
| 218 |
except Exception as e:
|
| 219 |
return None, f"β Error: {str(e)}"
|
| 220 |
|
|
|
|
|
|
|
| 221 |
trainer = RealRVCTrainer()
|
| 222 |
|
| 223 |
+
with gr.Blocks(title="Real RVC Training") as demo:
|
| 224 |
+
gr.Markdown("# π€ Real RVC Model Training\n### Using Official RVC-Project Implementation\n\nβ οΈ Uses REAL RVC training. Models work on weights.gg!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
with gr.Tab("βοΈ Step 0: Install RVC"):
|
| 227 |
+
gr.Markdown("Install official RVC codebase and pretrained models (~200MB)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
install_btn = gr.Button("π¦ Install RVC Components", variant="primary", size="lg")
|
| 229 |
install_output = gr.Textbox(label="Installation Status", lines=10)
|
| 230 |
+
install_btn.click(fn=trainer.install_rvc, outputs=install_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
with gr.Tab("π Step 1: Prepare Dataset"):
|
| 233 |
+
gr.Markdown("Upload voice audio files (10-30 min recommended, WAV/MP3/FLAC)")
|
| 234 |
+
model_name_prep = gr.Textbox(label="Model Name", value="my_voice_model")
|
| 235 |
+
audio_files = gr.File(label="Upload Audio Files", file_count="multiple", file_types=["audio"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
prep_btn = gr.Button("π Prepare Dataset", variant="primary")
|
| 237 |
prep_output = gr.Textbox(label="Status", lines=8)
|
| 238 |
+
prep_btn.click(fn=trainer.prepare_dataset, inputs=[audio_files, model_name_prep], outputs=prep_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
with gr.Tab("π§ Step 2: Preprocess"):
|
| 241 |
gr.Markdown("Preprocess audio and extract features")
|
| 242 |
+
model_name_process = gr.Textbox(label="Model Name", value="my_voice_model")
|
| 243 |
+
sample_rate_process = gr.Radio(choices=["40000", "48000"], value="40000", label="Sample Rate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
process_btn = gr.Button("π§ Preprocess Data", variant="primary")
|
| 245 |
process_output = gr.Textbox(label="Status", lines=8)
|
| 246 |
+
process_btn.click(fn=trainer.preprocess_data, inputs=[model_name_process, sample_rate_process], outputs=process_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
with gr.Tab("π Step 3: Train Model"):
|
| 249 |
+
gr.Markdown("Train RVC model (β οΈ CPU training takes hours/days)")
|
| 250 |
+
model_name_train = gr.Textbox(label="Model Name", value="my_voice_model")
|
| 251 |
+
epochs_train = gr.Slider(minimum=10, maximum=500, value=100, step=10, label="Epochs")
|
| 252 |
+
batch_size_train = gr.Slider(minimum=1, maximum=16, value=4, step=1, label="Batch Size")
|
| 253 |
+
sample_rate_train = gr.Radio(choices=["40000", "48000"], value="40000", label="Sample Rate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
train_btn = gr.Button("π Start Real Training", variant="primary")
|
| 255 |
train_output = gr.Textbox(label="Training Status", lines=15)
|
| 256 |
+
train_btn.click(fn=trainer.train_model, inputs=[model_name_train, epochs_train, batch_size_train, sample_rate_train], outputs=train_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
with gr.Tab("π₯ Step 4: Download"):
|
| 259 |
gr.Markdown("Download your trained RVC model")
|
| 260 |
+
model_name_download = gr.Textbox(label="Model Name", value="my_voice_model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
download_btn = gr.Button("π¦ Package Model", variant="primary")
|
| 262 |
download_file = gr.File(label="Download")
|
| 263 |
download_status = gr.Textbox(label="Status")
|
| 264 |
+
download_btn.click(fn=trainer.package_model, inputs=model_name_download, outputs=[download_file, download_status])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
+
gr.Markdown("---\n### π Resources\n- [RVC Project](https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI)\n- [Weights.gg](https://weights.gg/)\n\n### β οΈ Important\n- Uses REAL RVC training\n- Models work on weights.gg\n- CPU training is VERY slow\n- Recommended: Google Colab with GPU")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
if __name__ == "__main__":
|
| 269 |
demo.launch()
|