Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -11,7 +11,12 @@ CRITICAL FIX: Model classes now match the actual checkpoint architecture.
|
|
| 11 |
import os
|
| 12 |
import random
|
| 13 |
import tempfile
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
import soundfile as sf
|
| 16 |
import torch
|
| 17 |
import torch.nn as nn
|
|
@@ -231,7 +236,7 @@ def mel_to_audio_griffinlim(mel, sr=SAMPLE_RATE, n_iter=60):
|
|
| 231 |
spec = inverse_mel(mel_power)
|
| 232 |
gl = torchaudio.transforms.GriffinLim(n_fft=1024, hop_length=256, n_iter=n_iter)
|
| 233 |
audio = gl(spec)
|
| 234 |
-
return audio.numpy()
|
| 235 |
|
| 236 |
|
| 237 |
# ============================================================
|
|
@@ -277,7 +282,7 @@ class VoiceCloner:
|
|
| 277 |
print("[LAZY] Loading HiFi-GAN vocoder...")
|
| 278 |
try:
|
| 279 |
hifigan_path = hf_hub_download(
|
| 280 |
-
repo_id="
|
| 281 |
)
|
| 282 |
ckpt = torch.load(hifigan_path, map_location="cpu", weights_only=False)
|
| 283 |
state_dict = ckpt.get("generator", ckpt.get("state_dict", ckpt))
|
|
@@ -316,7 +321,7 @@ class VoiceCloner:
|
|
| 316 |
try:
|
| 317 |
# Step 1: Griffin-Lim to get rough audio at 40kHz
|
| 318 |
audio_gl = mel_to_audio_griffinlim(mel_out, sr=SAMPLE_RATE)
|
| 319 |
-
audio_tensor = torch.from_numpy(audio_gl).float()
|
| 320 |
|
| 321 |
# Step 2: Resample 40kHz → 22.05kHz
|
| 322 |
resampler = torchaudio.transforms.Resample(SAMPLE_RATE, 22050)
|
|
@@ -333,7 +338,7 @@ class VoiceCloner:
|
|
| 333 |
# Step 4: HiFi-GAN
|
| 334 |
with torch.no_grad():
|
| 335 |
audio_out = self.hifigan(mel_80.unsqueeze(0))
|
| 336 |
-
audio_out = audio_out.squeeze(0).squeeze(0).cpu().numpy()
|
| 337 |
return audio_out, 22050, "HiFi-GAN+GL"
|
| 338 |
except Exception as e:
|
| 339 |
print(f"HiFi-GAN pipeline failed, falling back to Griffin-Lim: {e}")
|
|
@@ -354,7 +359,7 @@ class VoiceCloner:
|
|
| 354 |
audio_data, sr = sf.read(input_audio, dtype="float32")
|
| 355 |
if audio_data.ndim > 1:
|
| 356 |
audio_data = audio_data.mean(axis=1)
|
| 357 |
-
y = torch.from_numpy(audio_data)
|
| 358 |
if sr != SAMPLE_RATE:
|
| 359 |
y = torchaudio.transforms.Resample(sr, SAMPLE_RATE)(y)
|
| 360 |
sr = SAMPLE_RATE
|
|
@@ -388,7 +393,7 @@ class VoiceCloner:
|
|
| 388 |
mel_out = mel_out.squeeze(0)
|
| 389 |
|
| 390 |
audio_out, out_sr, vocoder_name = self._mel_to_audio(mel_out)
|
| 391 |
-
audio_out = audio_out / (
|
| 392 |
output_path = tempfile.mktemp(suffix=".wav")
|
| 393 |
sf.write(output_path, audio_out, out_sr)
|
| 394 |
return output_path, f"✅ {vocoder_name} | {len(y)/SAMPLE_RATE:.1f}s → {len(audio_out)/out_sr:.1f}s | Model: strict=True, 128-mel"
|
|
|
|
| 11 |
import os
|
| 12 |
import random
|
| 13 |
import tempfile
|
| 14 |
+
try:
|
| 15 |
+
import numpy as np
|
| 16 |
+
except ImportError:
|
| 17 |
+
# Fallback: use torch operations instead
|
| 18 |
+
np = None
|
| 19 |
+
print("[WARN] numpy not available, using torch fallback")
|
| 20 |
import soundfile as sf
|
| 21 |
import torch
|
| 22 |
import torch.nn as nn
|
|
|
|
| 236 |
spec = inverse_mel(mel_power)
|
| 237 |
gl = torchaudio.transforms.GriffinLim(n_fft=1024, hop_length=256, n_iter=n_iter)
|
| 238 |
audio = gl(spec)
|
| 239 |
+
return audio.detach().cpu().numpy() if np is not None else audio.detach().cpu().tolist()
|
| 240 |
|
| 241 |
|
| 242 |
# ============================================================
|
|
|
|
| 282 |
print("[LAZY] Loading HiFi-GAN vocoder...")
|
| 283 |
try:
|
| 284 |
hifigan_path = hf_hub_download(
|
| 285 |
+
repo_id="csdc-atl/hifigan-universal_v1", filename="g_02500000"
|
| 286 |
)
|
| 287 |
ckpt = torch.load(hifigan_path, map_location="cpu", weights_only=False)
|
| 288 |
state_dict = ckpt.get("generator", ckpt.get("state_dict", ckpt))
|
|
|
|
| 321 |
try:
|
| 322 |
# Step 1: Griffin-Lim to get rough audio at 40kHz
|
| 323 |
audio_gl = mel_to_audio_griffinlim(mel_out, sr=SAMPLE_RATE)
|
| 324 |
+
audio_tensor = torch.as_tensor(audio_gl, dtype=torch.float32) if isinstance(audio_gl, torch.Tensor) else torch.from_numpy(audio_gl).float() if np is not None else torch.tensor(audio_gl, dtype=torch.float32)
|
| 325 |
|
| 326 |
# Step 2: Resample 40kHz → 22.05kHz
|
| 327 |
resampler = torchaudio.transforms.Resample(SAMPLE_RATE, 22050)
|
|
|
|
| 338 |
# Step 4: HiFi-GAN
|
| 339 |
with torch.no_grad():
|
| 340 |
audio_out = self.hifigan(mel_80.unsqueeze(0))
|
| 341 |
+
audio_out = audio_out.squeeze(0).squeeze(0).detach().cpu().numpy() if np is not None else audio_out.squeeze(0).squeeze(0).detach().cpu().tolist()
|
| 342 |
return audio_out, 22050, "HiFi-GAN+GL"
|
| 343 |
except Exception as e:
|
| 344 |
print(f"HiFi-GAN pipeline failed, falling back to Griffin-Lim: {e}")
|
|
|
|
| 359 |
audio_data, sr = sf.read(input_audio, dtype="float32")
|
| 360 |
if audio_data.ndim > 1:
|
| 361 |
audio_data = audio_data.mean(axis=1)
|
| 362 |
+
y = torch.from_numpy(audio_data) if np is not None else torch.tensor(audio_data, dtype=torch.float32)
|
| 363 |
if sr != SAMPLE_RATE:
|
| 364 |
y = torchaudio.transforms.Resample(sr, SAMPLE_RATE)(y)
|
| 365 |
sr = SAMPLE_RATE
|
|
|
|
| 393 |
mel_out = mel_out.squeeze(0)
|
| 394 |
|
| 395 |
audio_out, out_sr, vocoder_name = self._mel_to_audio(mel_out)
|
| 396 |
+
audio_out = audio_out / (torch.max(torch.abs(torch.tensor(audio_out) if not isinstance(audio_out, torch.Tensor) else audio_out)) + 1e-7).item() * 0.95
|
| 397 |
output_path = tempfile.mktemp(suffix=".wav")
|
| 398 |
sf.write(output_path, audio_out, out_sr)
|
| 399 |
return output_path, f"✅ {vocoder_name} | {len(y)/SAMPLE_RATE:.1f}s → {len(audio_out)/out_sr:.1f}s | Model: strict=True, 128-mel"
|