"""Self-contained inference for the AIAMD GRU: is this call answered by a human or a machine? pip install tensorflow tensorflow-hub "setuptools<81" soundfile scipy numpy huggingface_hub python inference.py call.wav Pipeline (identical to training): mono audio -> fill runs of exact zeros (>= 10 ms) with -70 dBFS noise -> causal 3.6 kHz low-pass -> resample to 16 kHz -> first 3 s -> YAMNet (frozen) embeddings, one per 0.96 s window every 0.48 s -> GRU -> p(machine) after every window -> temperature calibration -> threshold """ import json import sys from math import gcd from pathlib import Path import numpy as np import soundfile as sf from scipy.signal import butter, resample_poly, sosfilt HERE = Path(__file__).resolve().parent YAMNET_HANDLE = "https://tfhub.dev/google/yamnet/1" CLIP_SECONDS = 3.0 WINDOW_SAMPLES, HOP_SAMPLES = 15600, 7680 # YAMNet window and hop at 16 kHz def preprocess(x: np.ndarray, sr: int, seed: int = 0) -> np.ndarray: """Zero fill + low-pass at the original sample rate, then resample to 16 kHz.""" x = x.astype(np.float32).copy() edges = np.diff(np.concatenate([[0], (x == 0).astype(np.int8), [0]])) starts, ends = np.where(edges == 1)[0], np.where(edges == -1)[0] rng = np.random.default_rng(seed) for s, e in zip(starts, ends, strict=True): if e - s >= int(0.010 * sr): x[s:e] = rng.normal(0, 10 ** (-70 / 20), e - s) x = sosfilt(butter(8, 3600, btype="low", fs=sr, output="sos"), x).astype(np.float32) g = gcd(sr, 16000) return resample_poly(x, 16000 // g, sr // g).astype(np.float32) if sr != 16000 else x def window_dbfs(x16k: np.ndarray, n: int) -> np.ndarray: padded = np.zeros((n - 1) * HOP_SAMPLES + WINDOW_SAMPLES, np.float32) padded[: len(x16k)] = x16k frames = np.lib.stride_tricks.sliding_window_view(padded, WINDOW_SAMPLES)[::HOP_SAMPLES] return 20 * np.log10(np.sqrt(np.mean(np.square(frames, dtype=np.float64), axis=1)) + 1e-10) class AmdDetector: def __init__(self, folder: Path = HERE): import keras import tensorflow_hub as hub self.yamnet = hub.load(YAMNET_HANDLE) self.model = keras.models.load_model(folder / "amd_gru.keras") calibration = json.loads((folder / "amd_gru_calibration.json").read_text()) self.temperature, self.threshold = calibration["temperature"], calibration["threshold"] def predict(self, x: np.ndarray, sr: int) -> dict: x16k = preprocess(x, sr)[: int(CLIP_SECONDS * 16000)] _, embeddings, _ = self.yamnet(x16k) p = self.model.predict(embeddings.numpy()[None], verbose=0)[0, :, 0] logit = np.log(np.clip(p, 1e-6, 1 - 1e-6) / np.clip(1 - p, 1e-6, 1)) p = 1 / (1 + np.exp(-logit / self.temperature)) # calibrated p(machine) per window return { "decision": "machine" if p[-1] >= self.threshold else "human", "p_machine": float(p[-1]), "threshold": self.threshold, "p_machine_per_window": [round(float(v), 4) for v in p], "window_dbfs": [round(float(v), 1) for v in window_dbfs(x16k, len(p))], } def main() -> None: if len(sys.argv) != 2: raise SystemExit("usage: python inference.py call.wav") x, sr = sf.read(sys.argv[1], dtype="float32", always_2d=True) print(json.dumps(AmdDetector().predict(x.mean(axis=1), sr), indent=2)) if __name__ == "__main__": main()