Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,238 +1,332 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
NumberBlocks One Voice Cloner - RVC Inference
|
| 4 |
-
Uses the trained RVC v2 model for
|
| 5 |
"""
|
| 6 |
-
import os, json
|
| 7 |
import gradio as gr
|
| 8 |
import numpy as np
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
from huggingface_hub import hf_hub_download
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
ckpt = torch.load(model_file, map_location="cpu", weights_only=False)
|
| 37 |
cfg = ckpt['config']
|
| 38 |
sd = ckpt['model_state_dict']
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
class Encoder(nn.Module):
|
| 42 |
-
def __init__(self):
|
| 43 |
-
super().__init__()
|
| 44 |
-
self.conv1, self.bn1 = nn.Conv1d(n_mels,hd,5,padding=2), nn.BatchNorm1d(hd)
|
| 45 |
-
self.conv2, self.bn2 = nn.Conv1d(hd,hd,5,padding=2), nn.BatchNorm1d(hd)
|
| 46 |
-
self.conv3, self.bn3 = nn.Conv1d(hd,hd,5,padding=2), nn.BatchNorm1d(hd)
|
| 47 |
-
self.conv4, self.bn4 = nn.Conv1d(hd,hd*2,5,padding=2), nn.BatchNorm1d(hd*2)
|
| 48 |
-
self.conv5, self.bn5 = nn.Conv1d(hd*2,hd*2,3,padding=1), nn.BatchNorm1d(hd*2)
|
| 49 |
-
self.ln = nn.LayerNorm(hd*2)
|
| 50 |
-
def forward(self, x):
|
| 51 |
-
for c,b in [(self.conv1,self.bn1),(self.conv2,self.bn2),(self.conv3,self.bn3),(self.conv4,self.bn4),(self.conv5,self.bn5)]:
|
| 52 |
-
x = torch.relu(b(c(x)))
|
| 53 |
-
return self.ln(x.transpose(1,2)).transpose(1,2)
|
| 54 |
-
|
| 55 |
-
class Posterior(nn.Module):
|
| 56 |
-
def __init__(self):
|
| 57 |
-
super().__init__()
|
| 58 |
-
self.conv = nn.Conv1d(hd*2, 384, 1)
|
| 59 |
-
def forward(self, x):
|
| 60 |
-
stats = self.conv(x)
|
| 61 |
-
m, logs = torch.split(stats, 192, dim=1)
|
| 62 |
-
z = m + torch.randn_like(m)*torch.exp(logs)
|
| 63 |
-
return z, m, logs
|
| 64 |
-
|
| 65 |
-
class Flow(nn.Module):
|
| 66 |
-
def __init__(self):
|
| 67 |
-
super().__init__()
|
| 68 |
-
self.net = nn.Sequential(
|
| 69 |
-
nn.Conv1d(96,hd,1), nn.ReLU(),
|
| 70 |
-
nn.Conv1d(hd,hd,1), nn.ReLU(),
|
| 71 |
-
nn.Conv1d(hd,192,1),
|
| 72 |
-
)
|
| 73 |
-
def forward(self, z):
|
| 74 |
-
z1, z2 = torch.split(z, 96, dim=1)
|
| 75 |
-
return z + self.net(z1)
|
| 76 |
-
|
| 77 |
-
class Decoder(nn.Module):
|
| 78 |
-
def __init__(self):
|
| 79 |
-
super().__init__()
|
| 80 |
-
self.conv1, self.bn1 = nn.Conv1d(192,hd*2,5,padding=2), nn.BatchNorm1d(hd*2)
|
| 81 |
-
self.conv2, self.bn2 = nn.Conv1d(hd*2,hd*2,5,padding=2), nn.BatchNorm1d(hd*2)
|
| 82 |
-
self.conv3, self.bn3 = nn.Conv1d(hd*2,hd,5,padding=2), nn.BatchNorm1d(hd)
|
| 83 |
-
self.conv4, self.bn4 = nn.Conv1d(hd,hd,3,padding=1), nn.BatchNorm1d(hd)
|
| 84 |
-
self.conv5 = nn.Conv1d(hd,128,1)
|
| 85 |
-
def forward(self, z):
|
| 86 |
-
for c,b in [(self.conv1,self.bn1),(self.conv2,self.bn2),(self.conv3,self.bn3),(self.conv4,self.bn4)]:
|
| 87 |
-
z = torch.relu(b(c(z)))
|
| 88 |
-
return self.conv5(z)
|
| 89 |
-
|
| 90 |
-
class VoiceModel(nn.Module):
|
| 91 |
-
def __init__(self):
|
| 92 |
-
super().__init__()
|
| 93 |
-
self.encoder, self.posterior, self.flow, self.decoder = Encoder(), Posterior(), Flow(), Decoder()
|
| 94 |
-
def forward(self, mel):
|
| 95 |
-
h = self.encoder(mel)
|
| 96 |
-
z, m, logs = self.posterior(h)
|
| 97 |
-
z = self.flow(z)
|
| 98 |
-
return self.decoder(z), m, logs
|
| 99 |
-
|
| 100 |
-
model = VoiceModel()
|
| 101 |
model.load_state_dict(sd, strict=False)
|
| 102 |
model.eval()
|
| 103 |
return model, cfg
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
def convert_voice(audio_input, transpose=0):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
sr = config['sample_rate']
|
| 134 |
-
hop = config['hop_length']
|
| 135 |
-
|
| 136 |
if audio_input is None:
|
| 137 |
return None, "โ ่ฏทไธไผ ้ณ้ขๆไปถ"
|
| 138 |
-
|
| 139 |
sr_in, data = audio_input[0], audio_input[1]
|
| 140 |
-
|
| 141 |
-
# Resample
|
| 142 |
-
if sr_in !=
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
ratio = sr / sr_in
|
| 146 |
-
n_samples = int(len(data) * ratio)
|
| 147 |
-
indices = np.linspace(0, len(data)-1, n_samples).astype(int)
|
| 148 |
-
data = data[indices]
|
| 149 |
-
|
| 150 |
if len(data.shape) > 1:
|
| 151 |
data = data.mean(axis=1)
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Compute mel spectrogram
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
sr=sr,
|
| 158 |
-
n_mels=config['n_mels'],
|
| 159 |
-
hop_length=hop,
|
| 160 |
-
win_length=config['win_length'],
|
| 161 |
-
n_fft=config['n_fft'],
|
| 162 |
-
)
|
| 163 |
-
mel_db = librosa.power_to_db(mel, ref=np.max)
|
| 164 |
-
mel_norm = (mel_db - mel_db.mean()) / (mel_db.std() + 1e-8)
|
| 165 |
-
|
| 166 |
-
# Apply pitch shift if requested
|
| 167 |
if transpose != 0:
|
| 168 |
mel_norm = np.roll(mel_norm, transpose, axis=0)
|
| 169 |
-
|
| 170 |
-
#
|
| 171 |
with torch.no_grad():
|
| 172 |
mel_tensor = torch.FloatTensor(mel_norm).unsqueeze(0)
|
| 173 |
-
mel_out, _, _ =
|
| 174 |
-
|
| 175 |
-
mel_out_np = mel_out.squeeze().numpy()
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
def generate_sample():
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
n_frames = 400 # ~2.5s
|
| 186 |
with torch.no_grad():
|
| 187 |
z = torch.randn(1, 192, n_frames) * 0.5
|
| 188 |
-
z =
|
| 189 |
-
mel_out =
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
with gr.Blocks(title="๐๏ธ NumberBlocks One Voice", theme=gr.themes.Soft()) as demo:
|
| 198 |
gr.HTML("""
|
| 199 |
<div style="text-align:center; margin-bottom:1rem">
|
| 200 |
<h1 style="color:#ff6b6b">๐๏ธ NumberBlocks One ่ฏญ้ณๅ
้</h1>
|
| 201 |
-
<p>RVC v2 Model โ
|
| 202 |
</div>
|
| 203 |
""")
|
| 204 |
-
|
| 205 |
with gr.Tab("๐ Voice Conversion"):
|
| 206 |
-
gr.Markdown("ไธไผ ้ณ้ข๏ผๅฐๅ
ถ่ฝฌๆขไธบ One ็ๅฃฐ้ณ")
|
| 207 |
audio_in = gr.Audio(label="่พๅ
ฅ้ณ้ข", sources=["upload", "microphone"])
|
| 208 |
pitch = gr.Slider(-12, 12, value=0, step=1, label="Pitch Shift (semitones)")
|
| 209 |
convert_btn = gr.Button("๐ ่ฝฌๆข", variant="primary")
|
| 210 |
-
audio_out = gr.Audio(label="่พๅบ้ณ้ข")
|
| 211 |
status = gr.Textbox(label="็ถๆ")
|
| 212 |
convert_btn.click(convert_voice, [audio_in, pitch], [audio_out, status])
|
| 213 |
-
|
| 214 |
with gr.Tab("๐ต Sample Generation"):
|
| 215 |
-
gr.Markdown("็ๆ One ็้ๆบ่ฏญ้ณๆ ทๆฌ")
|
| 216 |
gen_btn = gr.Button("๐ต ็ๆๆ ทๆฌ", variant="primary")
|
| 217 |
-
gen_out = gr.Audio(label="็ๆ้ณ้ข")
|
| 218 |
gen_status = gr.Textbox(label="็ถๆ")
|
| 219 |
gen_btn.click(generate_sample, outputs=[gen_out, gen_status])
|
| 220 |
-
|
| 221 |
with gr.Tab("โน๏ธ About"):
|
| 222 |
gr.Markdown("""
|
| 223 |
### Model Info
|
| 224 |
-
- **
|
| 225 |
-
- **
|
| 226 |
-
- **Sample Rate**:
|
| 227 |
- **Training Data**: 100 source files, 1,334 chunks
|
| 228 |
- **Training Steps**: 500
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
### Links
|
| 232 |
- [Dataset](https://huggingface.co/datasets/ayf3/numberblocks-one-voice-dataset)
|
| 233 |
- [Training Space](https://huggingface.co/spaces/ayf3/rvc-cpu-trainer)
|
| 234 |
-
|
| 235 |
-
โ ๏ธ Note: Audio quality is limited without a neural vocoder (HiFi-GAN).
|
| 236 |
""")
|
| 237 |
|
| 238 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
NumberBlocks One Voice Cloner - RVC Inference with HiFi-GAN Vocoder
|
| 4 |
+
Uses the trained RVC v2 model + pretrained HiFi-GAN universal vocoder for high-quality synthesis.
|
| 5 |
"""
|
| 6 |
+
import os, json
|
| 7 |
import gradio as gr
|
| 8 |
import numpy as np
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.nn.functional as F
|
| 12 |
+
|
| 13 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 14 |
+
# HiFi-GAN Generator (exact match to pretrained weights)
|
| 15 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 16 |
+
|
| 17 |
+
class HiFiGANResBlock(nn.Module):
|
| 18 |
+
def __init__(self, channels, kernel_size, dilation_sizes):
|
| 19 |
+
super().__init__()
|
| 20 |
+
# Store padding values for manual padding (original HiFi-GAN doesn't use Conv1d padding)
|
| 21 |
+
self.paddings1 = []
|
| 22 |
+
self.convs1 = nn.ModuleList()
|
| 23 |
+
for d in dilation_sizes:
|
| 24 |
+
self.convs1.append(nn.utils.weight_norm(nn.Conv1d(channels, channels, kernel_size, 1, dilation=d, padding=0)))
|
| 25 |
+
self.paddings1.append((kernel_size - 1) * d // 2)
|
| 26 |
+
self.paddings2 = []
|
| 27 |
+
self.convs2 = nn.ModuleList()
|
| 28 |
+
for d in dilation_sizes:
|
| 29 |
+
self.convs2.append(nn.utils.weight_norm(nn.Conv1d(channels, channels, kernel_size, 1, dilation=d, padding=0)))
|
| 30 |
+
self.paddings2.append((kernel_size - 1) * d // 2)
|
| 31 |
+
|
| 32 |
+
def forward(self, x):
|
| 33 |
+
for c1, p1, c2, p2 in zip(self.convs1, self.paddings1, self.convs2, self.paddings2):
|
| 34 |
+
xt = F.leaky_relu(x, 0.1)
|
| 35 |
+
xt = F.pad(xt, (p1, p1))
|
| 36 |
+
xt = c1(xt)
|
| 37 |
+
xt = F.leaky_relu(xt, 0.1)
|
| 38 |
+
xt = F.pad(xt, (p2, p2))
|
| 39 |
+
xt = c2(xt)
|
| 40 |
+
x = xt + x
|
| 41 |
+
return x
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class HiFiGANGenerator(nn.Module):
|
| 45 |
+
def __init__(self, config):
|
| 46 |
+
super().__init__()
|
| 47 |
+
self.num_kernels = len(config["resblock_kernel_sizes"])
|
| 48 |
+
self.conv_pre = nn.utils.weight_norm(
|
| 49 |
+
nn.Conv1d(80, config["upsample_initial_channel"], 7, 1, padding=3)
|
| 50 |
+
)
|
| 51 |
+
self.ups = nn.ModuleList()
|
| 52 |
+
for u, k in zip(config["upsample_rates"], config["upsample_kernel_sizes"]):
|
| 53 |
+
ch_in = config["upsample_initial_channel"] // (2 ** (len(self.ups)))
|
| 54 |
+
ch_out = ch_in // 2
|
| 55 |
+
self.ups.append(
|
| 56 |
+
nn.utils.weight_norm(
|
| 57 |
+
nn.ConvTranspose1d(ch_in, ch_out, k, u, padding=(k - u) // 2)
|
| 58 |
+
)
|
| 59 |
+
)
|
| 60 |
+
self.resblocks = nn.ModuleList()
|
| 61 |
+
for i in range(len(self.ups)):
|
| 62 |
+
ch = config["upsample_initial_channel"] // (2 ** (i + 1))
|
| 63 |
+
for k, d in zip(config["resblock_kernel_sizes"], config["resblock_dilation_sizes"]):
|
| 64 |
+
self.resblocks.append(HiFiGANResBlock(ch, k, d))
|
| 65 |
+
ch_out = config["upsample_initial_channel"] // (2 ** len(self.ups))
|
| 66 |
+
self.conv_post = nn.utils.weight_norm(nn.Conv1d(ch_out, 1, 7, 1, padding=3))
|
| 67 |
+
|
| 68 |
+
def forward(self, mel):
|
| 69 |
+
x = self.conv_pre(mel)
|
| 70 |
+
for i, up in enumerate(self.ups):
|
| 71 |
+
x = F.leaky_relu(x, 0.1)
|
| 72 |
+
x = up(x)
|
| 73 |
+
xs = 0
|
| 74 |
+
for j in range(self.num_kernels):
|
| 75 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
| 76 |
+
x = xs / self.num_kernels
|
| 77 |
+
x = F.leaky_relu(x, 0.1)
|
| 78 |
+
x = self.conv_post(x)
|
| 79 |
+
x = torch.tanh(x)
|
| 80 |
+
return x
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 84 |
+
# Voice Model (VITS-like from training)
|
| 85 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 86 |
+
|
| 87 |
+
class VoiceModel(nn.Module):
|
| 88 |
+
def __init__(self, n_mels, hd):
|
| 89 |
+
super().__init__()
|
| 90 |
+
self.encoder = self._build_encoder(n_mels, hd)
|
| 91 |
+
self.posterior = self._build_posterior(hd)
|
| 92 |
+
self.flow = self._build_flow(hd)
|
| 93 |
+
self.decoder = self._build_decoder(hd)
|
| 94 |
+
|
| 95 |
+
def _build_encoder(self, n_mels, hd):
|
| 96 |
+
layers = []
|
| 97 |
+
ch_in = n_mels
|
| 98 |
+
for ch_out, ks in [(hd,5),(hd,5),(hd,5),(hd*2,5),(hd*2,3)]:
|
| 99 |
+
layers.extend([nn.utils.weight_norm(nn.Conv1d(ch_in, ch_out, ks, padding=ks//2)),
|
| 100 |
+
nn.BatchNorm1d(ch_out), nn.ReLU()])
|
| 101 |
+
ch_in = ch_out
|
| 102 |
+
layers.append(nn.LayerNorm(hd*2))
|
| 103 |
+
class Enc(nn.Module):
|
| 104 |
+
def __init__(self, seq):
|
| 105 |
+
super().__init__(); self.seq = seq
|
| 106 |
+
def forward(self, x):
|
| 107 |
+
x = self.seq[:-1](x)
|
| 108 |
+
x = self.seq[-1](x.transpose(1,2)).transpose(1,2)
|
| 109 |
+
return x
|
| 110 |
+
return Enc(nn.Sequential(*layers))
|
| 111 |
+
|
| 112 |
+
def _build_posterior(self, hd):
|
| 113 |
+
class Post(nn.Module):
|
| 114 |
+
def __init__(s):
|
| 115 |
+
super().__init__(); s.conv = nn.utils.weight_norm(nn.Conv1d(hd*2, 384, 1))
|
| 116 |
+
def forward(s, x):
|
| 117 |
+
stats = s.conv(x); m, logs = torch.split(stats, 192, dim=1)
|
| 118 |
+
z = m + torch.randn_like(m)*torch.exp(logs); return z, m, logs
|
| 119 |
+
return Post()
|
| 120 |
+
|
| 121 |
+
def _build_flow(self, hd):
|
| 122 |
+
class Flow(nn.Module):
|
| 123 |
+
def __init__(s):
|
| 124 |
+
super().__init__()
|
| 125 |
+
s.net = nn.Sequential(nn.Conv1d(96,hd,1), nn.ReLU(), nn.Conv1d(hd,hd,1), nn.ReLU(), nn.Conv1d(hd,192,1))
|
| 126 |
+
def forward(s, z):
|
| 127 |
+
z1, z2 = torch.split(z, 96, dim=1); return z + s.net(z1)
|
| 128 |
+
return Flow()
|
| 129 |
+
|
| 130 |
+
def _build_decoder(self, hd):
|
| 131 |
+
layers = []
|
| 132 |
+
ch_in = 192
|
| 133 |
+
for ch_out, ks in [(hd*2,5),(hd*2,5),(hd,5),(hd,3)]:
|
| 134 |
+
layers.extend([nn.utils.weight_norm(nn.Conv1d(ch_in, ch_out, ks, padding=ks//2)),
|
| 135 |
+
nn.BatchNorm1d(ch_out), nn.ReLU()])
|
| 136 |
+
ch_in = ch_out
|
| 137 |
+
layers.append(nn.utils.weight_norm(nn.Conv1d(hd, 80, 1)))
|
| 138 |
+
return nn.Sequential(*layers)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 142 |
+
# Model Loading
|
| 143 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 144 |
+
|
| 145 |
+
def download_file(repo_id, filename, repo_type="dataset"):
|
| 146 |
from huggingface_hub import hf_hub_download
|
| 147 |
+
return hf_hub_download(repo_id=repo_id, filename=filename, repo_type=repo_type)
|
| 148 |
+
|
| 149 |
+
def load_hifigan():
|
| 150 |
+
cfg_path = download_file("ORI-Muchim/HiFi-GAN_44100hz_universal", "config.json", repo_type="model")
|
| 151 |
+
weights_path = download_file("ORI-Muchim/HiFi-GAN_44100hz_universal", "g_02500000", repo_type="model")
|
| 152 |
+
with open(cfg_path) as f:
|
| 153 |
+
hfg_cfg = json.load(f)
|
| 154 |
+
vocoder = HiFiGANGenerator(hfg_cfg)
|
| 155 |
+
ckpt = torch.load(weights_path, map_location="cpu", weights_only=False)
|
| 156 |
+
vocoder.load_state_dict(ckpt["generator"])
|
| 157 |
+
vocoder.eval()
|
| 158 |
+
return vocoder, hfg_cfg
|
| 159 |
+
|
| 160 |
+
def load_voice_model():
|
| 161 |
+
model_file = download_file("ayf3/numberblocks-one-voice-dataset", "models/one_voice_rvc_v2.pth")
|
| 162 |
ckpt = torch.load(model_file, map_location="cpu", weights_only=False)
|
| 163 |
cfg = ckpt['config']
|
| 164 |
sd = ckpt['model_state_dict']
|
| 165 |
+
model = VoiceModel(cfg['n_mels'], cfg['hidden_dim'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
model.load_state_dict(sd, strict=False)
|
| 167 |
model.eval()
|
| 168 |
return model, cfg
|
| 169 |
|
| 170 |
+
|
| 171 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 172 |
+
# Audio Processing
|
| 173 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 174 |
+
|
| 175 |
+
def mel_spectrogram(audio, sr, n_mels=80, hop_length=256, win_length=1024, n_fft=1024):
|
| 176 |
+
import librosa
|
| 177 |
+
mel = librosa.feature.melspectrogram(
|
| 178 |
+
y=audio.astype(np.float32), sr=sr, n_mels=n_mels,
|
| 179 |
+
hop_length=hop_length, win_length=win_length, n_fft=n_fft, fmax=8000
|
| 180 |
+
)
|
| 181 |
+
mel_db = librosa.power_to_db(mel, ref=np.max)
|
| 182 |
+
return mel_db
|
| 183 |
+
|
| 184 |
+
def mel_to_audio_hifigan(vocoder, mel_tensor):
|
| 185 |
+
with torch.no_grad():
|
| 186 |
+
audio = vocoder(mel_tensor)
|
| 187 |
+
return audio.squeeze().cpu().numpy()
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 191 |
+
# Globals & Init
|
| 192 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 193 |
+
|
| 194 |
+
voice_model = None
|
| 195 |
+
voice_config = None
|
| 196 |
+
hifigan = None
|
| 197 |
+
hifigan_config = None
|
| 198 |
+
|
| 199 |
+
def init_models():
|
| 200 |
+
global voice_model, voice_config, hifigan, hifigan_config
|
| 201 |
+
if voice_model is None:
|
| 202 |
+
print("Loading voice model...")
|
| 203 |
+
voice_model, voice_config = load_voice_model()
|
| 204 |
+
print("Voice model loaded.")
|
| 205 |
+
if hifigan is None:
|
| 206 |
+
print("Loading HiFi-GAN vocoder...")
|
| 207 |
+
hifigan, hifigan_config = load_hifigan()
|
| 208 |
+
print("HiFi-GAN vocoder loaded.")
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 212 |
+
# Core Functions
|
| 213 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 214 |
|
| 215 |
def convert_voice(audio_input, transpose=0):
|
| 216 |
+
init_models()
|
| 217 |
+
import librosa
|
| 218 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
if audio_input is None:
|
| 220 |
return None, "โ ่ฏทไธไผ ้ณ้ขๆไปถ"
|
| 221 |
+
|
| 222 |
sr_in, data = audio_input[0], audio_input[1]
|
| 223 |
+
|
| 224 |
+
# Resample to 44100 for HiFi-GAN
|
| 225 |
+
if sr_in != 44100:
|
| 226 |
+
data = librosa.resample(data.astype(np.float32), orig_sr=sr_in, target_sr=44100)
|
| 227 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
if len(data.shape) > 1:
|
| 229 |
data = data.mean(axis=1)
|
| 230 |
+
|
| 231 |
+
# Trim to max 30 seconds
|
| 232 |
+
max_samples = 44100 * 30
|
| 233 |
+
if len(data) > max_samples:
|
| 234 |
+
data = data[:max_samples]
|
| 235 |
+
|
| 236 |
# Compute mel spectrogram
|
| 237 |
+
mel = mel_spectrogram(data, 44100)
|
| 238 |
+
mel_norm = (mel - mel.mean()) / (mel.std() + 1e-8)
|
| 239 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
if transpose != 0:
|
| 241 |
mel_norm = np.roll(mel_norm, transpose, axis=0)
|
| 242 |
+
|
| 243 |
+
# Voice model timbre transfer
|
| 244 |
with torch.no_grad():
|
| 245 |
mel_tensor = torch.FloatTensor(mel_norm).unsqueeze(0)
|
| 246 |
+
mel_out, _, _ = voice_model(mel_tensor)
|
| 247 |
+
|
| 248 |
+
mel_out_np = mel_out.squeeze().cpu().numpy()
|
| 249 |
+
mel_out_np = np.clip(mel_out_np, -4.0, 4.0)
|
| 250 |
+
|
| 251 |
+
# HiFi-GAN vocoding
|
| 252 |
+
with torch.no_grad():
|
| 253 |
+
audio_out = mel_to_audio_hifigan(hifigan, torch.FloatTensor(mel_out_np).unsqueeze(0))
|
| 254 |
+
|
| 255 |
+
mx = np.max(np.abs(audio_out))
|
| 256 |
+
if mx > 0:
|
| 257 |
+
audio_out = audio_out / mx * 0.85
|
| 258 |
+
|
| 259 |
+
return (44100, audio_out.astype(np.float32)), \
|
| 260 |
+
f"โ
่ฝฌๆขๅฎๆ! (HiFi-GAN vocoder)\n่พๅ
ฅ: {len(data)/44100:.1f}s โ ่พๅบ: {len(audio_out)/44100:.1f}s"
|
| 261 |
+
|
| 262 |
|
| 263 |
def generate_sample():
|
| 264 |
+
init_models()
|
| 265 |
+
|
| 266 |
+
n_frames = 400
|
|
|
|
|
|
|
| 267 |
with torch.no_grad():
|
| 268 |
z = torch.randn(1, 192, n_frames) * 0.5
|
| 269 |
+
z = voice_model.flow(z)
|
| 270 |
+
mel_out = voice_model.decoder(z)
|
| 271 |
+
|
| 272 |
+
mel_out_np = np.clip(mel_out.squeeze().cpu().numpy(), -4.0, 4.0)
|
| 273 |
+
|
| 274 |
+
with torch.no_grad():
|
| 275 |
+
audio_out = mel_to_audio_hifigan(hifigan, torch.FloatTensor(mel_out_np).unsqueeze(0))
|
| 276 |
+
|
| 277 |
+
mx = np.max(np.abs(audio_out))
|
| 278 |
+
if mx > 0:
|
| 279 |
+
audio_out = audio_out / mx * 0.85
|
| 280 |
+
|
| 281 |
+
return (44100, audio_out.astype(np.float32)), \
|
| 282 |
+
f"โ
็ๆๅฎๆ! (HiFi-GAN vocoder)\nๆถ้ฟ: {len(audio_out)/44100:.1f}s"
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 286 |
+
# Gradio UI
|
| 287 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 288 |
+
|
| 289 |
with gr.Blocks(title="๐๏ธ NumberBlocks One Voice", theme=gr.themes.Soft()) as demo:
|
| 290 |
gr.HTML("""
|
| 291 |
<div style="text-align:center; margin-bottom:1rem">
|
| 292 |
<h1 style="color:#ff6b6b">๐๏ธ NumberBlocks One ่ฏญ้ณๅ
้</h1>
|
| 293 |
+
<p>RVC v2 Model + HiFi-GAN Vocoder โ High Quality Voice Conversion</p>
|
| 294 |
</div>
|
| 295 |
""")
|
| 296 |
+
|
| 297 |
with gr.Tab("๐ Voice Conversion"):
|
| 298 |
+
gr.Markdown("ไธไผ ้ณ้ข๏ผๅฐๅ
ถ่ฝฌๆขไธบ One ็ๅฃฐ้ณ๏ผไฝฟ็จ HiFi-GAN ็ฅ็ปๅฃฐ็ ๅจๆๅ้ณ่ดจ๏ผ")
|
| 299 |
audio_in = gr.Audio(label="่พๅ
ฅ้ณ้ข", sources=["upload", "microphone"])
|
| 300 |
pitch = gr.Slider(-12, 12, value=0, step=1, label="Pitch Shift (semitones)")
|
| 301 |
convert_btn = gr.Button("๐ ่ฝฌๆข", variant="primary")
|
| 302 |
+
audio_out = gr.Audio(label="่พๅบ้ณ้ข (HiFi-GAN)")
|
| 303 |
status = gr.Textbox(label="็ถๆ")
|
| 304 |
convert_btn.click(convert_voice, [audio_in, pitch], [audio_out, status])
|
| 305 |
+
|
| 306 |
with gr.Tab("๐ต Sample Generation"):
|
| 307 |
+
gr.Markdown("็ๆ One ็้ๆบ่ฏญ้ณๆ ทๆฌ๏ผไฝฟ็จ HiFi-GAN ็ฅ็ปๅฃฐ็ ๅจ๏ผ")
|
| 308 |
gen_btn = gr.Button("๐ต ็ๆๆ ทๆฌ", variant="primary")
|
| 309 |
+
gen_out = gr.Audio(label="็ๆ้ณ้ข (HiFi-GAN)")
|
| 310 |
gen_status = gr.Textbox(label="็ถๆ")
|
| 311 |
gen_btn.click(generate_sample, outputs=[gen_out, gen_status])
|
| 312 |
+
|
| 313 |
with gr.Tab("โน๏ธ About"):
|
| 314 |
gr.Markdown("""
|
| 315 |
### Model Info
|
| 316 |
+
- **Voice Model**: VITS-like (Encoder + Posterior + Flow + Decoder) โ 5.3M params
|
| 317 |
+
- **Vocoder**: HiFi-GAN Universal (44100Hz) โ 928K params
|
| 318 |
+
- **Sample Rate**: 44100 Hz
|
| 319 |
- **Training Data**: 100 source files, 1,334 chunks
|
| 320 |
- **Training Steps**: 500
|
| 321 |
+
|
| 322 |
+
### What's New
|
| 323 |
+
- โ
Integrated HiFi-GAN neural vocoder replacing overlap-add
|
| 324 |
+
- โ
Significantly improved audio quality and naturalness
|
| 325 |
+
- โ
Proper melโaudio conversion with learned upsampling
|
| 326 |
+
|
| 327 |
### Links
|
| 328 |
- [Dataset](https://huggingface.co/datasets/ayf3/numberblocks-one-voice-dataset)
|
| 329 |
- [Training Space](https://huggingface.co/spaces/ayf3/rvc-cpu-trainer)
|
|
|
|
|
|
|
| 330 |
""")
|
| 331 |
|
| 332 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|