Download pellav2.py from SpeechAntiSpoofingBenchmarks/Pellav2: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/SpeechAntiSpoofingBenchmarks/Pellav2/resolve/main/pellav2.py
- Command line
-
hf download hf://SpeechAntiSpoofingBenchmarks/Pellav2/pellav2.py
-
curl -L -o pellav2.py https://huggingface.co/SpeechAntiSpoofingBenchmarks/Pellav2/resolve/main/pellav2.py
2.28 kB
| from __future__ import annotations | |
| import os | |
| import numpy as np | |
| import torch | |
| from safetensors.torch import load_file | |
| from speech_spoof_bench.model import AntiSpoofingModel | |
| from _net import Detector, preprocess | |
| _DIR = os.path.dirname(os.path.abspath(__file__)) | |
| # HF Sadanie/pellav2-audio-deepfake-detector @ b668eb4dfe4719665692c73c1d70383680afff6b pellav2_detector.pt | |
| # (sha256 d21aedfb52e2f87706c2753715a29665482b0f4a034800c31579df5d458559ac) -> convert_ckpt.py -> safetensors | |
| # (sha256 050234557deabc95d9697a2ede42a5152ed06ba500a4488a42542ab793af428f, identical keys/values) | |
| _CKPT = os.path.join(_DIR, "pellav2_detector.safetensors") | |
| N_TENSORS = 776 | |
| # Loaded (they are Wav2Vec2BertModel parameters) but unused at inference: SpecAugment's mask token. | |
| UNUSED_AT_INFERENCE = frozenset({"backbone.masked_spec_embed"}) | |
| def load_state(path=_CKPT): | |
| sd = load_file(path) | |
| assert len(sd) == N_TENSORS, len(sd) | |
| return sd | |
| def build_net(path=_CKPT): | |
| net = Detector() | |
| r = net.load_state_dict(load_state(path), strict=True) # every checkpoint tensor consumed, none missing | |
| assert not r.missing_keys and not r.unexpected_keys | |
| return net | |
| class Pellav2(AntiSpoofingModel): | |
| name = "Pellav2" | |
| expected_sample_rate = 16000 | |
| batch_size = 24 # sweep 2026-11-10 (64000-sample view): RTX 4070 Ti SUPER 56.9 utt/s @24 (plateau 16-64); RTX 5060 Ti -> 4 (see load) | |
| def load(self) -> None: | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| torch.backends.cudnn.allow_tf32 = False | |
| torch.backends.cuda.matmul.allow_tf32 = False | |
| self.net = build_net().to(self.device).eval() | |
| if self.device == "cuda" and "5060" in torch.cuda.get_device_name(): | |
| self.batch_size = 4 # sweep 2026-11-10: RTX 5060 Ti 25.1 utt/s @4, ~18 at bs >= 8 | |
| def score_batch(self, audios, srs): | |
| x = np.stack([preprocess(a) for a in audios]) | |
| logit_fake = self.net(torch.from_numpy(x).to(self.device)).float() | |
| # pellav2_infer.py: p_fake = sigmoid(logit) -> score = -logit(fake), higher = bona fide | |
| return (-logit_fake).cpu().tolist() | |
| def unload(self) -> None: | |
| self.net = None | |
| if torch.cuda.is_available(): | |
| torch.cuda.empty_cache() | |