Delete evaluation/QP/qp_pred.py
Browse files- evaluation/QP/qp_pred.py +0 -73
evaluation/QP/qp_pred.py
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from huggingface_hub import hf_hub_download
|
| 3 |
-
|
| 4 |
-
def ensure_checkpoint_exists(checkpoint_path, repo_id="LIUM/TTSEval", subfolder="evaluation/QP"):
|
| 5 |
-
# Check if the file exists locally
|
| 6 |
-
if not os.path.exists(checkpoint_path):
|
| 7 |
-
print(f"File {checkpoint_path} not found. Downloading from Hugging Face...")
|
| 8 |
-
|
| 9 |
-
# This downloads the file and returns the local path to the cached version
|
| 10 |
-
# Note: checkpoint_path here should be the filename within the HF repo
|
| 11 |
-
filename = os.path.basename(checkpoint_path)
|
| 12 |
-
|
| 13 |
-
downloaded_path = hf_hub_download(
|
| 14 |
-
repo_id=repo_id,
|
| 15 |
-
filename=filename,
|
| 16 |
-
subfolder=subfolder,
|
| 17 |
-
repo_type="model"
|
| 18 |
-
)
|
| 19 |
-
return downloaded_path
|
| 20 |
-
|
| 21 |
-
return checkpoint_path
|
| 22 |
-
|
| 23 |
-
def _load_sslmos_model(state):
|
| 24 |
-
"""
|
| 25 |
-
Charge le backbone fairseq + LossPredictor, puis restaure ton checkpoint finetuné.
|
| 26 |
-
Retourne (net, target_sr).
|
| 27 |
-
"""
|
| 28 |
-
device = state["device"]
|
| 29 |
-
target_sr = state.get("qp_target_sr", 24000)
|
| 30 |
-
ssl_out_dim = state.get("qp_ssl_out_dim", 9216)
|
| 31 |
-
|
| 32 |
-
base_model_ckpt = state["qp_fairseq_base_model"]
|
| 33 |
-
finetuned_ckpt = state["qp_finetuned_checkpoint"]
|
| 34 |
-
# Update paths if necessary
|
| 35 |
-
base_model_ckpt = ensure_checkpoint_exists(base_model_ckpt)
|
| 36 |
-
finetuned_ckpt = ensure_checkpoint_exists(finetuned_ckpt)
|
| 37 |
-
|
| 38 |
-
# --- patch torch.load (comme dans ton script) pour éviter weights_only=True par défaut ---
|
| 39 |
-
torch.serialization.add_safe_globals([__import__("argparse").Namespace])
|
| 40 |
-
original_torch_load = torch.load
|
| 41 |
-
|
| 42 |
-
def patched_torch_load(*args, **kwargs):
|
| 43 |
-
if "weights_only" not in kwargs:
|
| 44 |
-
kwargs["weights_only"] = False
|
| 45 |
-
return original_torch_load(*args, **kwargs)
|
| 46 |
-
|
| 47 |
-
torch.load = patched_torch_load
|
| 48 |
-
models, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task([base_model_ckpt])
|
| 49 |
-
torch.load = original_torch_load
|
| 50 |
-
|
| 51 |
-
ssl_model = models[0]
|
| 52 |
-
ssl_model.remove_pretraining_modules()
|
| 53 |
-
for p in ssl_model.parameters():
|
| 54 |
-
p.requires_grad = False
|
| 55 |
-
|
| 56 |
-
net = LossPredictor(ssl_model, ssl_out_dim).to(device)
|
| 57 |
-
net.load_state_dict(torch.load(finetuned_ckpt, map_location=device))
|
| 58 |
-
net.eval()
|
| 59 |
-
|
| 60 |
-
return net, target_sr
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def _load_and_resample_wav(wav_path: str, target_sr: int, device):
|
| 64 |
-
wav, sr = torchaudio.load(wav_path) # wav: [C, T]
|
| 65 |
-
# mono
|
| 66 |
-
if wav.size(0) > 1:
|
| 67 |
-
wav = wav.mean(dim=0, keepdim=True)
|
| 68 |
-
# resample
|
| 69 |
-
if sr != target_sr:
|
| 70 |
-
wav = torchaudio.functional.resample(wav, orig_freq=sr, new_freq=target_sr)
|
| 71 |
-
# shape attendue: [B, C, T] -> ici B=1
|
| 72 |
-
wav = wav.unsqueeze(0).to(device) # [1, 1, T]
|
| 73 |
-
return wav
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|