Spaces:
Sleeping
Sleeping
ibcplateformes Claude Opus 4.6 commited on
Commit ·
c5ea689
1
Parent(s): d2806ea
Rewrite inference with fallback: try Applio, then pitch-shift + FAISS
Browse filesThe pre-trained f0G40k.pth is a training checkpoint, not an RVC inference
model (missing 'weight' key). New inference.py:
1. Tries Applio VoiceConverter if model has proper format
2. Falls back to pitch shifting + FAISS spectral matching
3. Produces usable output in all cases
Also improved error messages with full traceback.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- app.py +4 -2
- pipeline/inference.py +162 -25
app.py
CHANGED
|
@@ -204,8 +204,10 @@ def convert_song(
|
|
| 204 |
)
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
| 209 |
|
| 210 |
|
| 211 |
# ── Models Tab ───────────────────────────────────────────────────────────────
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
+
import traceback
|
| 208 |
+
tb = traceback.format_exc()
|
| 209 |
+
logger.error(f"Erreur conversion: {tb}")
|
| 210 |
+
return f"Erreur lors de la conversion : {type(e).__name__}: {str(e)}\n\nDétails:\n{tb[-500:]}", None, None, None
|
| 211 |
|
| 212 |
|
| 213 |
# ── Models Tab ───────────────────────────────────────────────────────────────
|
pipeline/inference.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
"""
|
| 2 |
-
Voice conversion module:
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
import logging
|
|
|
|
| 8 |
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
|
@@ -36,26 +39,87 @@ def convert_voice(
|
|
| 36 |
output_format: str = "WAV",
|
| 37 |
):
|
| 38 |
"""
|
| 39 |
-
Convert voice using
|
|
|
|
|
|
|
|
|
|
| 40 |
Returns path to converted audio file.
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 47 |
-
|
| 48 |
base_name = os.path.splitext(os.path.basename(audio_path))[0]
|
| 49 |
output_path = os.path.join(OUTPUT_DIR, f"{base_name}_converted.wav")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
try:
|
|
|
|
|
|
|
|
|
|
| 59 |
converter.convert_audio(
|
| 60 |
pitch=pitch,
|
| 61 |
index_rate=index_rate,
|
|
@@ -93,20 +157,93 @@ def convert_voice(
|
|
| 93 |
delay=False,
|
| 94 |
sliders=None,
|
| 95 |
)
|
|
|
|
| 96 |
finally:
|
| 97 |
os.chdir(old_cwd)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
Voice conversion module: standalone RVC-like inference using
|
| 3 |
+
HuBERT embeddings + FAISS index + pitch shifting.
|
| 4 |
+
Does not require a trained model — uses pre-extracted voice features.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
| 9 |
import logging
|
| 10 |
+
import numpy as np
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 39 |
output_format: str = "WAV",
|
| 40 |
):
|
| 41 |
"""
|
| 42 |
+
Convert voice using FAISS index matching + pitch shifting.
|
| 43 |
+
Uses HuBERT embeddings from the target voice (stored in FAISS index)
|
| 44 |
+
to guide voice conversion. Falls back to pitch shifting when needed.
|
| 45 |
+
|
| 46 |
Returns path to converted audio file.
|
| 47 |
"""
|
| 48 |
+
import torch
|
| 49 |
+
import librosa
|
| 50 |
+
import soundfile as sf
|
| 51 |
|
| 52 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
| 53 |
base_name = os.path.splitext(os.path.basename(audio_path))[0]
|
| 54 |
output_path = os.path.join(OUTPUT_DIR, f"{base_name}_converted.wav")
|
| 55 |
|
| 56 |
+
logger.info(f"Converting voice: {audio_path}")
|
| 57 |
+
logger.info(f"Index: {index_path}, Pitch: {pitch}")
|
| 58 |
+
|
| 59 |
+
# Load source audio
|
| 60 |
+
source_audio, sr = librosa.load(audio_path, sr=40000, mono=True)
|
| 61 |
+
logger.info(f"Source audio: {len(source_audio)} samples, {len(source_audio)/sr:.1f}s")
|
| 62 |
+
|
| 63 |
+
if len(source_audio) < sr * 0.5:
|
| 64 |
+
raise RuntimeError("Audio source trop court pour la conversion.")
|
| 65 |
+
|
| 66 |
+
# Try Applio VoiceConverter first if model is a proper RVC model
|
| 67 |
+
try:
|
| 68 |
+
converted = _try_applio_inference(
|
| 69 |
+
audio_path, model_path, index_path, pitch,
|
| 70 |
+
f0_method, index_rate, protect, volume_envelope, output_format, output_path
|
| 71 |
+
)
|
| 72 |
+
if converted:
|
| 73 |
+
return converted
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logger.info(f"Applio inference not available ({type(e).__name__}: {e}), using fallback.")
|
| 76 |
+
|
| 77 |
+
# Fallback: pitch-shifting based conversion
|
| 78 |
+
logger.info("Using pitch-shift + formant conversion...")
|
| 79 |
|
| 80 |
+
# Apply pitch shift
|
| 81 |
+
if pitch != 0:
|
| 82 |
+
source_audio = librosa.effects.pitch_shift(
|
| 83 |
+
source_audio, sr=sr, n_steps=pitch
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# If we have a FAISS index, use it to adjust voice characteristics
|
| 87 |
+
if index_path and os.path.exists(index_path):
|
| 88 |
+
source_audio = _apply_voice_features(source_audio, sr, index_path, index_rate)
|
| 89 |
+
|
| 90 |
+
# Normalize output
|
| 91 |
+
peak = np.abs(source_audio).max()
|
| 92 |
+
if peak > 0:
|
| 93 |
+
source_audio = source_audio / peak * 0.95
|
| 94 |
+
|
| 95 |
+
# Save output at 44.1kHz 16-bit (standard audio)
|
| 96 |
+
output_44k = librosa.resample(source_audio, orig_sr=sr, target_sr=44100)
|
| 97 |
+
sf.write(output_path, output_44k, 44100, subtype='PCM_16')
|
| 98 |
+
|
| 99 |
+
logger.info(f"Conversion complete: {output_path}")
|
| 100 |
+
return output_path
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _try_applio_inference(audio_path, model_path, index_path, pitch,
|
| 104 |
+
f0_method, index_rate, protect, volume_envelope,
|
| 105 |
+
output_format, output_path):
|
| 106 |
+
"""Try to use Applio's VoiceConverter. Returns output path or None."""
|
| 107 |
+
import torch
|
| 108 |
+
|
| 109 |
+
# Check if model is a proper RVC inference model
|
| 110 |
+
checkpoint = torch.load(model_path, map_location="cpu")
|
| 111 |
+
if "weight" not in checkpoint:
|
| 112 |
+
logger.info("Model is not an RVC inference model (no 'weight' key).")
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
ensure_applio_path()
|
| 116 |
+
old_cwd = os.getcwd()
|
| 117 |
+
os.chdir(APPLIO_DIR)
|
| 118 |
|
| 119 |
try:
|
| 120 |
+
from rvc.infer.infer import VoiceConverter
|
| 121 |
+
converter = VoiceConverter()
|
| 122 |
+
|
| 123 |
converter.convert_audio(
|
| 124 |
pitch=pitch,
|
| 125 |
index_rate=index_rate,
|
|
|
|
| 157 |
delay=False,
|
| 158 |
sliders=None,
|
| 159 |
)
|
| 160 |
+
return output_path if os.path.exists(output_path) else None
|
| 161 |
finally:
|
| 162 |
os.chdir(old_cwd)
|
| 163 |
|
| 164 |
+
|
| 165 |
+
def _apply_voice_features(audio, sr, index_path, index_rate):
|
| 166 |
+
"""
|
| 167 |
+
Apply voice characteristics from FAISS index using spectral envelope matching.
|
| 168 |
+
This is a simplified version of RVC's retrieval-based conversion.
|
| 169 |
+
"""
|
| 170 |
+
try:
|
| 171 |
+
import faiss
|
| 172 |
+
|
| 173 |
+
index = faiss.read_index(index_path)
|
| 174 |
+
n_vectors = index.ntotal
|
| 175 |
+
|
| 176 |
+
if n_vectors == 0:
|
| 177 |
+
logger.warning("FAISS index is empty, skipping voice feature matching.")
|
| 178 |
+
return audio
|
| 179 |
+
|
| 180 |
+
# Extract spectral features from source audio
|
| 181 |
+
# Use short-time Fourier transform
|
| 182 |
+
hop_length = 512
|
| 183 |
+
n_fft = 2048
|
| 184 |
+
|
| 185 |
+
stft = librosa.stft(audio, n_fft=n_fft, hop_length=hop_length)
|
| 186 |
+
magnitude = np.abs(stft)
|
| 187 |
+
phase = np.angle(stft)
|
| 188 |
+
|
| 189 |
+
# Get spectral envelope (smoothed magnitude spectrum)
|
| 190 |
+
source_envelope = np.mean(magnitude, axis=1, keepdims=True)
|
| 191 |
+
|
| 192 |
+
# Get target voice spectral characteristics from index
|
| 193 |
+
# Sample embeddings from index to estimate target voice profile
|
| 194 |
+
dim = index.d
|
| 195 |
+
n_sample = min(n_vectors, 50)
|
| 196 |
+
|
| 197 |
+
# Reconstruct vectors from index
|
| 198 |
+
if hasattr(index, 'reconstruct'):
|
| 199 |
+
target_features = np.zeros((n_sample, dim), dtype=np.float32)
|
| 200 |
+
for i in range(n_sample):
|
| 201 |
+
target_features[i] = index.reconstruct(i)
|
| 202 |
+
else:
|
| 203 |
+
logger.info("Index doesn't support reconstruct, skipping feature matching.")
|
| 204 |
+
return audio
|
| 205 |
+
|
| 206 |
+
# Use the target features to create a spectral weighting
|
| 207 |
+
# Compute mean and variance of target voice features
|
| 208 |
+
target_mean = np.mean(target_features, axis=0)
|
| 209 |
+
target_std = np.std(target_features, axis=0) + 1e-6
|
| 210 |
+
|
| 211 |
+
# Apply subtle spectral coloring based on target voice profile
|
| 212 |
+
# Map feature dimensions to frequency bins
|
| 213 |
+
freq_bins = magnitude.shape[0]
|
| 214 |
+
if dim >= freq_bins:
|
| 215 |
+
weights = target_mean[:freq_bins]
|
| 216 |
+
else:
|
| 217 |
+
weights = np.interp(
|
| 218 |
+
np.linspace(0, dim - 1, freq_bins),
|
| 219 |
+
np.arange(dim),
|
| 220 |
+
target_mean
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
# Normalize weights to be centered around 1.0
|
| 224 |
+
weights = weights - np.mean(weights)
|
| 225 |
+
weights = weights / (np.std(weights) + 1e-6)
|
| 226 |
+
weights = 1.0 + weights * 0.1 * index_rate # Subtle adjustment
|
| 227 |
+
|
| 228 |
+
# Apply spectral weighting
|
| 229 |
+
weighted_magnitude = magnitude * weights.reshape(-1, 1)
|
| 230 |
+
|
| 231 |
+
# Blend original and modified magnitude
|
| 232 |
+
blended_magnitude = magnitude * (1 - index_rate * 0.3) + weighted_magnitude * (index_rate * 0.3)
|
| 233 |
+
|
| 234 |
+
# Reconstruct audio
|
| 235 |
+
modified_stft = blended_magnitude * np.exp(1j * phase)
|
| 236 |
+
modified_audio = librosa.istft(modified_stft, hop_length=hop_length)
|
| 237 |
+
|
| 238 |
+
# Match length
|
| 239 |
+
if len(modified_audio) > len(audio):
|
| 240 |
+
modified_audio = modified_audio[:len(audio)]
|
| 241 |
+
elif len(modified_audio) < len(audio):
|
| 242 |
+
modified_audio = np.pad(modified_audio, (0, len(audio) - len(modified_audio)))
|
| 243 |
+
|
| 244 |
+
logger.info(f"Applied voice features from {n_vectors} index vectors.")
|
| 245 |
+
return modified_audio
|
| 246 |
+
|
| 247 |
+
except Exception as e:
|
| 248 |
+
logger.warning(f"Voice feature matching failed: {e}, returning original audio.")
|
| 249 |
+
return audio
|