sugakrit6 commited on
Commit
9615f93
Β·
verified Β·
1 Parent(s): 4f2d1ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -123
app.py CHANGED
@@ -32,29 +32,38 @@ class RealRVCTrainer:
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
 
49
  models_to_download = [
50
- ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/pretrained/f0G40k.pth", "f0G40k.pth"),
51
- ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/pretrained/f0D40k.pth", "f0D40k.pth"),
52
- ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/hubert_base.pt", "hubert_base.pt"),
 
 
 
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)
@@ -71,13 +80,13 @@ class RealRVCTrainer:
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"""
81
  if not audio_files:
82
  return "❌ Please upload audio files"
83
 
@@ -102,23 +111,22 @@ class RealRVCTrainer:
102
  return f"❌ Error: {str(e)}"
103
 
104
  def preprocess_data(self, model_name, sample_rate, progress=gr.Progress()):
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)}"
@@ -132,72 +140,48 @@ class RealRVCTrainer:
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
-
150
- # Set up environment and working directory
151
- env = os.environ.copy()
152
- pythonpath = str(self.rvc_dir)
153
- if 'PYTHONPATH' in env:
154
- pythonpath = f"{pythonpath}:{env['PYTHONPATH']}"
155
- env['PYTHONPATH'] = pythonpath
156
-
157
- # Capture both stdout and stderr
158
- process = subprocess.Popen(
159
- cmd,
160
- stdout=subprocess.PIPE,
161
- stderr=subprocess.STDOUT,
162
- text=True,
163
- cwd=str(self.rvc_dir),
164
- env=env
165
- )
166
 
167
- training_output = []
168
  for line in process.stdout:
169
- training_output.append(line.strip())
170
- if "epoch" in line.lower():
171
- progress(0.2 + 0.6, desc=f"Training: {line.strip()[:50]}")
 
172
 
173
- return_code = process.wait()
174
 
175
  progress(0.9, desc="Searching for model files...")
176
 
177
- # Search for G_*.pth (generator checkpoints) and D_*.pth (discriminator)
178
- g_files = list(log_dir.glob("G_*.pth"))
179
- d_files = list(log_dir.glob("D_*.pth"))
180
- index_files = list(log_dir.rglob("*.index"))
181
 
182
- # Also check in added_* subfolders
183
- added_folders = list(log_dir.glob("added_*"))
184
- for folder in added_folders:
185
- g_files.extend(list(folder.glob("G_*.pth")))
186
- d_files.extend(list(folder.glob("D_*.pth")))
187
- index_files.extend(list(folder.glob("*.index")))
188
-
189
- if g_files or index_files:
190
  output_dir = self.workspace / model_name
191
  output_dir.mkdir(exist_ok=True)
192
 
193
  files_info = []
194
-
195
- # Get the latest G file (highest epoch number)
196
- if g_files:
197
- latest_g = max(g_files, key=lambda f: int(f.stem.split('_')[1]) if f.stem.split('_')[1].isdigit() else 0)
198
- shutil.copy2(latest_g, output_dir / f"{model_name}.pth")
199
- model_size = latest_g.stat().st_size / (1024*1024)
200
- files_info.append(f"- {model_name}.pth ({model_size:.1f}MB) [from {latest_g.name}]")
201
 
202
  if index_files:
203
  latest_index = max(index_files, key=lambda p: p.stat().st_mtime)
@@ -205,19 +189,9 @@ class RealRVCTrainer:
205
  files_info.append(f"- {latest_index.name}")
206
 
207
  progress(1.0, desc="Training complete!")
208
- 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!"
209
  else:
210
- debug_info = [f"Return code: {return_code}", ""]
211
- debug_info.append("Files in log directory:")
212
- if log_dir.exists():
213
- for item in log_dir.rglob("*"):
214
- if item.is_file():
215
- debug_info.append(f" - {item.relative_to(log_dir)} ({item.stat().st_size} bytes)")
216
-
217
- debug_info.append("\nLast 10 lines of training output:")
218
- debug_info.extend(training_output[-10:])
219
-
220
- return f"⚠️ Training completed but model files not found.\n\nπŸ“‹ Debug:\n{chr(10).join(debug_info)}\n\nπŸ’‘ Check if training actually ran or failed silently."
221
 
222
  except Exception as e:
223
  return f"❌ Training failed: {str(e)}"
@@ -227,16 +201,13 @@ class RealRVCTrainer:
227
  try:
228
  output_dir = self.workspace / model_name
229
  if not output_dir.exists():
230
- output_dir = self.rvc_dir / "logs" / model_name / "weights"
231
-
232
- if not output_dir.exists():
233
- return None, "❌ Model not found"
234
 
235
  zip_path = self.workspace / f"{model_name}_RVC.zip"
236
  with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
237
  for file in output_dir.rglob("*"):
238
- if file.is_file() and (file.suffix in ['.pth', '.index', '.json']):
239
- zipf.write(file, file.name)
240
 
241
  return str(zip_path), f"βœ… Model packaged: {zip_path.name}"
242
  except Exception as e:
@@ -244,50 +215,13 @@ class RealRVCTrainer:
244
 
245
  trainer = RealRVCTrainer()
246
 
 
247
  with gr.Blocks(title="Real RVC Training") as demo:
248
- gr.Markdown("# 🎀 Real RVC Model Training\n### Using Official RVC-Project Implementation\n\n⚠️ Uses REAL RVC training. Models work on weights.gg!")
249
-
250
- with gr.Tab("βš™οΈ Step 0: Install RVC"):
251
- gr.Markdown("Install official RVC codebase and pretrained models (~200MB)")
252
- install_btn = gr.Button("πŸ“¦ Install RVC Components", variant="primary", size="lg")
253
- install_output = gr.Textbox(label="Installation Status", lines=10)
254
- install_btn.click(fn=trainer.install_rvc, outputs=install_output)
255
-
256
- with gr.Tab("πŸ“ Step 1: Prepare Dataset"):
257
- gr.Markdown("Upload voice audio files (10-30 min recommended, WAV/MP3/FLAC)")
258
- model_name_prep = gr.Textbox(label="Model Name", value="my_voice_model")
259
- audio_files = gr.File(label="Upload Audio Files", file_count="multiple", file_types=["audio"])
260
- prep_btn = gr.Button("πŸ“ Prepare Dataset", variant="primary")
261
- prep_output = gr.Textbox(label="Status", lines=8)
262
- prep_btn.click(fn=trainer.prepare_dataset, inputs=[audio_files, model_name_prep], outputs=prep_output)
263
 
264
- with gr.Tab("πŸ”§ Step 2: Preprocess"):
265
- gr.Markdown("Preprocess audio and extract features")
266
- model_name_process = gr.Textbox(label="Model Name", value="my_voice_model")
267
- sample_rate_process = gr.Radio(choices=["40000", "48000"], value="40000", label="Sample Rate")
268
- process_btn = gr.Button("πŸ”§ Preprocess Data", variant="primary")
269
- process_output = gr.Textbox(label="Status", lines=8)
270
- process_btn.click(fn=trainer.preprocess_data, inputs=[model_name_process, sample_rate_process], outputs=process_output)
271
-
272
- with gr.Tab("πŸš€ Step 3: Train Model"):
273
- gr.Markdown("Train RVC model (⚠️ CPU training takes hours/days)")
274
- model_name_train = gr.Textbox(label="Model Name", value="my_voice_model")
275
- epochs_train = gr.Slider(minimum=10, maximum=500, value=100, step=10, label="Epochs")
276
- batch_size_train = gr.Slider(minimum=1, maximum=16, value=4, step=1, label="Batch Size")
277
- sample_rate_train = gr.Radio(choices=["40000", "48000"], value="40000", label="Sample Rate")
278
- train_btn = gr.Button("πŸŽ“ Start Real Training", variant="primary")
279
- train_output = gr.Textbox(label="Training Status", lines=15)
280
- train_btn.click(fn=trainer.train_model, inputs=[model_name_train, epochs_train, batch_size_train, sample_rate_train], outputs=train_output)
281
-
282
- with gr.Tab("πŸ“₯ Step 4: Download"):
283
- gr.Markdown("Download your trained RVC model")
284
- model_name_download = gr.Textbox(label="Model Name", value="my_voice_model")
285
- download_btn = gr.Button("πŸ“¦ Package Model", variant="primary")
286
- download_file = gr.File(label="Download")
287
- download_status = gr.Textbox(label="Status")
288
- download_btn.click(fn=trainer.package_model, inputs=model_name_download, outputs=[download_file, download_status])
289
-
290
- 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")
291
 
292
  if __name__ == "__main__":
293
  demo.launch()
 
32
  core_packages = [
33
  "torch", "torchaudio", "torchvision", "numpy", "scipy",
34
  "librosa", "soundfile", "faiss-cpu", "praat-parselmouth",
35
+ "pyworld", "scikit-learn", "numba", "resampy", "pydub",
36
+ "fairseq", "gradio", "tensorboard"
37
  ]
38
 
39
  for pkg in core_packages:
40
  try:
41
+ subprocess.run([sys.executable, "-m", "pip", "install", "-q", pkg], timeout=120)
42
  except:
43
  pass
44
 
45
  progress(0.6, desc="Downloading pretrained models...")
46
 
47
+ assets_dir = self.rvc_dir / "assets"
48
+ pretrained_dir = assets_dir / "pretrained"
49
+ pretrained_v2_dir = assets_dir / "pretrained_v2"
50
+ hubert_path = self.rvc_dir / "hubert_base.pt"
51
+
52
+ assets_dir.mkdir(exist_ok=True)
53
  pretrained_dir.mkdir(exist_ok=True)
54
+ pretrained_v2_dir.mkdir(exist_ok=True)
55
 
56
  models_to_download = [
57
+ ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/hubert_base.pt", hubert_path),
58
+ ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/pretrained/f0G40k.pth", pretrained_dir / "f0G40k.pth"),
59
+ ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/pretrained/f0D40k.pth", pretrained_dir / "f0D40k.pth"),
60
+ # Add v2 if needed
61
+ ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/pretrained_v2/f0G40k.pth", pretrained_v2_dir / "f0G40k.pth"),
62
+ ("https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/pretrained_v2/f0D40k.pth", pretrained_v2_dir / "f0D40k.pth"),
63
  ]
64
 
65
+ for idx, (url, output_path) in enumerate(models_to_download):
66
+ progress(0.6 + (idx / len(models_to_download)) * 0.3, desc=f"Downloading {output_path.name}...")
 
67
  if not output_path.exists():
68
  try:
69
  subprocess.run(["wget", "-q", "-O", str(output_path), url], timeout=300)
 
80
  self.setup_complete = True
81
  progress(1.0, desc="Setup complete!")
82
 
83
+ return "βœ… RVC Installation Complete!\n\nπŸ“¦ Installed:\n- Official RVC codebase\n- Pre-trained models in assets/\n- All dependencies\n\nπŸŽ‰ Ready to train!"
84
 
85
  except Exception as e:
86
  return f"❌ Installation failed: {str(e)}\n\nπŸ”§ Try manual installation or use Google Colab."
87
 
88
  def prepare_dataset(self, audio_files, model_name, progress=gr.Progress()):
89
+ # (unchanged - your code here is fine)
90
  if not audio_files:
91
  return "❌ Please upload audio files"
92
 
 
111
  return f"❌ Error: {str(e)}"
112
 
113
  def preprocess_data(self, model_name, sample_rate, progress=gr.Progress()):
114
+ # (mostly unchanged, but use correct preprocess script path)
115
  try:
116
  progress(0.1, desc="Starting preprocessing...")
117
  dataset_path = self.rvc_dir / "dataset" / model_name
118
  if not dataset_path.exists():
119
  return "❌ Dataset not found. Please prepare dataset first."
120
 
121
+ # Correct preprocess script for current repo
122
  preprocess_script = self.rvc_dir / "infer" / "modules" / "train" / "preprocess.py"
 
 
123
 
124
  progress(0.3, desc="Preprocessing audio...")
125
+ cmd = [sys.executable, str(preprocess_script), str(dataset_path), str(sample_rate), "2", str(self.rvc_dir / "logs" / model_name)]
126
  result = subprocess.run(cmd, capture_output=True, text=True)
127
 
128
  progress(1.0, desc="Preprocessing complete!")
129
+ return f"βœ… Preprocessing Complete!\n\n🎡 Sample Rate: {sample_rate}Hz\nπŸ” Features extracted\nπŸ“Š Ready for training!\n\nLog: {result.stdout[-500:]}"
130
 
131
  except Exception as e:
132
  return f"❌ Preprocessing failed: {str(e)}"
 
140
 
141
  progress(0.1, desc="Starting RVC training...")
142
  train_script = self.rvc_dir / "infer" / "modules" / "train" / "train.py"
143
+
144
+ # Use correct pretrained paths (v1 example; switch to pretrained_v2 for better quality)
145
+ pg_path = self.rvc_dir / "assets" / "pretrained" / "f0G40k.pth"
146
+ pd_path = self.rvc_dir / "assets" / "pretrained" / "f0D40k.pth"
147
 
148
  cmd = [
149
  sys.executable, str(train_script),
150
  "-e", model_name, "-sr", str(sample_rate),
151
  "-f0", "1", "-bs", str(batch_size),
152
  "-g", "0", "-te", str(epochs), "-se", "10",
153
+ "-pg", str(pg_path),
154
+ "-pd", str(pd_path),
155
+ "-l", "0", "-c", "0", "-sw", "1" # save weights only at end
156
  ]
157
 
158
+ process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
 
160
  for line in process.stdout:
161
+ line = line.strip()
162
+ if line:
163
+ if "epoch" in line.lower() or "loss" in line.lower():
164
+ progress(0.2 + 0.7 * (process.poll() is not None), desc=line[:100])
165
 
166
+ process.wait()
167
 
168
  progress(0.9, desc="Searching for model files...")
169
 
170
+ # FIXED: Search directly in logs/<model_name> for .pth files
171
+ model_files = list(log_dir.glob("*.pth"))
172
+ index_files = list(log_dir.glob("*.index"))
 
173
 
174
+ if model_files or index_files:
 
 
 
 
 
 
 
175
  output_dir = self.workspace / model_name
176
  output_dir.mkdir(exist_ok=True)
177
 
178
  files_info = []
179
+ if model_files:
180
+ # Pick the latest G_*.pth or the one with highest steps
181
+ latest_model = max(model_files, key=lambda p: p.stat().st_mtime)
182
+ shutil.copy2(latest_model, output_dir / f"{model_name}.pth")
183
+ model_size = latest_model.stat().st_size / (1024*1024)
184
+ files_info.append(f"- {model_name}.pth ({model_size:.1f}MB)")
 
185
 
186
  if index_files:
187
  latest_index = max(index_files, key=lambda p: p.stat().st_mtime)
 
189
  files_info.append(f"- {latest_index.name}")
190
 
191
  progress(1.0, desc="Training complete!")
192
+ return f"βœ… Training Complete!\n\nπŸŽ“ Model: {model_name}\nπŸ“Š Epochs: {epochs}\n\nπŸ’Ύ Model Files:\n{'\n'.join(files_info)}\n\nπŸ“‚ Location: {output_dir}\n\nπŸŽ‰ Ready to download!\n\n⚠️ On CPU this is very slow – use GPU if possible!"
193
  else:
194
+ return f"⚠️ Training ran but no .pth or .index files found in {log_dir}\n\nCheck logs in terminal or {log_dir} for errors."
 
 
 
 
 
 
 
 
 
 
195
 
196
  except Exception as e:
197
  return f"❌ Training failed: {str(e)}"
 
201
  try:
202
  output_dir = self.workspace / model_name
203
  if not output_dir.exists():
204
+ return None, "❌ Model not found in workspace"
 
 
 
205
 
206
  zip_path = self.workspace / f"{model_name}_RVC.zip"
207
  with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
208
  for file in output_dir.rglob("*"):
209
+ if file.is_file() and file.suffix in ['.pth', '.index']:
210
+ zipf.write(file, file.relative_to(output_dir))
211
 
212
  return str(zip_path), f"βœ… Model packaged: {zip_path.name}"
213
  except Exception as e:
 
215
 
216
  trainer = RealRVCTrainer()
217
 
218
+ # Gradio interface remains the same (only minor label updates if desired)
219
  with gr.Blocks(title="Real RVC Training") as demo:
220
+ gr.Markdown("# 🎀 Real RVC Model Training\n### Using Official RVC-Project Implementation\n\n⚠️ CPU training is EXTREMELY slow! Use GPU (e.g., Colab) for realistic times.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
+ # ... (your tabs unchanged)
223
+
224
+ gr.Markdown("---\n### ⚠️ Warning: On Hugging Face CPU Spaces, training may take days/weeks or timeout. Strongly recommend Google Colab with free GPU.\n\n### πŸ“š Resources\n- [RVC Project](https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI)\n- [Weights.gg](https://weights.gg/)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  if __name__ == "__main__":
227
  demo.launch()