Audio Classification
Transformers
PyTorch
English
liar-detection
signal-processing
anomaly-detection
numpy
audio
time-series
Instructions to use zeechimp/liar-detector with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use zeechimp/liar-detector with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("audio-classification", model="zeechimp/liar-detector")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("zeechimp/liar-detector", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Create liar_detector/feature_extractor.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198"""44-dim hand-crafted features + standardization. Pure numpy."""from __future__ import annotationsimport jsonimport mathimport osfrom typing import List, Optional, Sequence
import numpy as np
FEATURE_DIM = 44
def feature_names() -> List[str]: return [ "mean_A", "mean_B", "log_std_A", "log_std_B", "skew_A", "skew_B", "kurt_A", "kurt_B", "abs_mean_A_minus_abs_mean_B", "log_std_ratio_canonical", "mean_diff", "log_std_ratio", "skew_diff", "kurt_diff", "skew_resid", "kurt_resid", "log_std_resid", "ac_r_1", "ac_r_2", "ac_r_3", "xcorr_-2", "xcorr_-1", "xcorr_0", "xcorr_1", "xcorr_2", "resid_slope_on_A", "resid_explained_by_A", "corr_abs_r_A", "corr_abs_r_B", "quant_sig_A", "quant_sig_B", "stair_A", "stair_B", "inversion_A", "inversion_B", "smooth_A", "smooth_B", "zero_runs_A", "zero_runs_B", "log_quant_ratio", "log_smooth_ratio", "log_zero_runs_ratio", "unique_frac_A", "unique_frac_B", ]
def _safe_stats(x): m = float(np.mean(x)) s = float(np.std(x)) + 1e-12 sk = float(np.mean((x - m) ** 3) / s ** 3) ku = float(np.mean((x - m) ** 4) / s ** 4) return m, s, sk, ku
def _autocorr(x, lag): xc = x - x.mean() if lag == 0: return 1.0 v0 = float(np.mean(xc * xc)) + 1e-12 return float(np.mean(xc[lag:] * xc[:-lag])) / v0
def _crosscorr(x, y, lag): xc, yc = x - x.mean(), y - y.mean() if lag >= 0: a = xc[lag:]; b = yc[:len(yc) - lag] if lag > 0 else yc else: a = xc[:len(xc) + lag]; b = yc[-lag:] if len(a) < 2: return 0.0 denom = float(np.std(a) * np.std(b)) + 1e-12 return float(np.mean(a * b)) / denom
def _corr_abs(r, x): if len(r) < 3: return 0.0 a, b = np.abs(r), np.abs(x) if float(np.std(a)) < 1e-12 or float(np.std(b)) < 1e-12: return 0.0 c = float(np.corrcoef(a, b)[0, 1]) return c if math.isfinite(c) else 0.0
def _quant_sig(x): d = np.abs(np.diff(x)) s = float(np.std(x)) + 1e-12 return float(np.mean(d < 1e-4 * s))
def _stair(x): d = np.diff(x) return float(np.mean(np.abs(np.diff(np.sign(d))) > 0))
def _inversion_sig(x): s = float(np.std(x)) + 1e-12 small = x[np.abs(x) < 0.3 * s] if len(small) < 10: return 0.0 return float(np.mean(small) / s)
def _smooth(x): if len(x) < 7: return 0.0 d2 = x[2:] - 2 * x[1:-1] + x[:-2] s = float(np.std(x)) + 1e-12 return float(np.mean(np.abs(d2)) / s)
def _zero_runs(x): if len(x) < 3: return 0.0 same = (np.abs(np.diff(x)) < 1e-9).astype(int) runs = 0 for i in range(len(same) - 1): if same[i] and same[i + 1]: runs += 1 return float(runs) / len(x)
def extract_features(A: np.ndarray, B: np.ndarray) -> np.ndarray: r = A - B mA, sA, skA, kuA = _safe_stats(A) mB, sB, skB, kuB = _safe_stats(B) _, sR, skR, kuR = _safe_stats(r)
Am = A - mA rm = r - float(np.mean(r)) alpha = float(np.sum(Am * rm) / (float(np.sum(Am * Am)) + 1e-9)) resid_after_fit = rm - alpha * Am var_r = float(np.var(r)) + 1e-12 resid_explained = 1.0 - float(np.var(resid_after_fit)) / var_r
q_A, q_B = _quant_sig(A), _quant_sig(B) st_A, st_B = _stair(A), _stair(B) inv_A, inv_B = _inversion_sig(A), _inversion_sig(B) sm_A, sm_B = _smooth(A), _smooth(B) zr_A, zr_B = _zero_runs(A), _zero_runs(B)
return np.array([ mA, mB, math.log(sA + 1e-9), math.log(sB + 1e-9), skA, skB, kuA, kuB, abs(mA) - abs(mB), math.log(sA + 1e-9) - math.log(sB + 1e-9), mB - mA, math.log(sB + 1e-9) - math.log(sA + 1e-9), skB - skA, kuB - kuA, skR, kuR, math.log(sR + 1e-9), _autocorr(r, 1), _autocorr(r, 2), _autocorr(r, 3), _crosscorr(A, B, -2), _crosscorr(A, B, -1), _crosscorr(A, B, 0), _crosscorr(A, B, 1), _crosscorr(A, B, 2), alpha, resid_explained, _corr_abs(r, A), _corr_abs(r, B), q_A, q_B, st_A, st_B, inv_A, inv_B, sm_A, sm_B, zr_A, zr_B, math.log(q_B + 1e-9) - math.log(q_A + 1e-9), math.log(sm_B + 1e-9) - math.log(sm_A + 1e-9), math.log(zr_B + 1e-9) - math.log(zr_A + 1e-9), float(len(np.unique(np.round(A, 3))) / len(A)), float(len(np.unique(np.round(B, 3))) / len(B)), ], dtype=np.float32)
class LiarDetectorFeatureExtractor: """Holds normalization stats. Save/load next to the model."""
def __init__(self, mean: Optional[Sequence[float]] = None, std: Optional[Sequence[float]] = None): self.mean = (np.zeros(FEATURE_DIM, dtype=np.float32) if mean is None else np.asarray(mean, dtype=np.float32)) self.std = (np.ones(FEATURE_DIM, dtype=np.float32) if std is None else np.asarray(std, dtype=np.float32)) self.std = np.where(self.std < 1e-9, 1.0, self.std)
def fit(self, features: np.ndarray) -> "LiarDetectorFeatureExtractor": self.mean = features.mean(axis=0).astype(np.float32) std = features.std(axis=0).astype(np.float32) self.std = np.where(std < 1e-9, 1.0, std) return self
def transform(self, features: np.ndarray) -> np.ndarray: return ((features - self.mean) / self.std).astype(np.float32)
def __call__(self, A: np.ndarray, B: np.ndarray) -> np.ndarray: return self.transform(extract_features(A, B))
def save_pretrained(self, save_directory: str) -> str: os.makedirs(save_directory, exist_ok=True) path = os.path.join(save_directory, "feature_extractor.json") with open(path, "w") as f: json.dump({ "mean": self.mean.tolist(), "std": self.std.tolist(), "feature_names": feature_names(), }, f, indent=2) return save_directory
@classmethod def from_pretrained(cls, load_directory: str) -> "LiarDetectorFeatureExtractor": path = os.path.join(load_directory, "feature_extractor.json") with open(path) as f: d = json.load(f) return cls(mean=d["mean"], std=d["std"])liar_detector/feature_extractor.pyAdded+198