Download inference.py from thelamapi/Next-Echo: direct link, hf CLI and curl.
- Browser
- Download file 2.3 kB
-
https://huggingface.co/thelamapi/Next-Echo/resolve/main/inference.py
- Command line
-
hf download hf://thelamapi/Next-Echo/inference.py
-
curl -L -o inference.py https://huggingface.co/thelamapi/Next-Echo/resolve/main/inference.py
2.3 kB
| """Next Echo inference (NumPy only): find occurrences of an example sound in a recording. | |
| Usage: | |
| python inference.py --query clap.wav --recording street.wav | |
| python inference.py --selftest | |
| Audio must be 16 kHz mono 16-bit WAV for the CLI (resample elsewhere if needed). | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import wave | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, HERE) | |
| import numpy as np # noqa: E402 | |
| import echo_core as ec # noqa: E402 | |
| from nlrt import load_model as _load # noqa: E402 | |
| def load(path=os.path.join(HERE, "model.json")): | |
| return ec.load_model(_load(path)) | |
| def read_wav(path): | |
| with wave.open(path, "rb") as w: | |
| assert w.getframerate() == 16000 and w.getnchannels() == 1 and w.getsampwidth() == 2, "need 16 kHz mono 16-bit" | |
| return np.frombuffer(w.readframes(w.getnframes()), dtype="<i2") / 32768.0 | |
| def find(model, query, recording, method="learned"): | |
| times, learned, base = ec.similarity_curves(model, query, recording) | |
| nxt, bl = ec.final_scores(learned, base, model["score"]) | |
| sims = nxt if method == "learned" else bl | |
| th = model["thresholds"]["learned" if method == "learned" else "baseline"] | |
| return [{"start": round(a, 3), "end": round(b, 3), "score": round(s, 4)} for a, b, s in ec.detect(times, sims, th)] | |
| def _selftest(): | |
| model = load() | |
| with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f: | |
| t = json.load(f) | |
| q, r = np.asarray(t["query"]) / 32768.0, np.asarray(t["recording"]) / 32768.0 | |
| _, learned, base = ec.similarity_curves(model, q, r) | |
| diff = max(float(np.abs(learned - np.asarray(t["learned"])).max()), float(np.abs(base - np.asarray(t["base"])).max())) | |
| ok = diff < 1e-6 | |
| print(json.dumps({"passed": ok, "max_abs_diff": diff, "windows": len(learned)})) | |
| return ok | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--selftest", action="store_true") | |
| ap.add_argument("--query") | |
| ap.add_argument("--recording") | |
| ap.add_argument("--method", default="learned", choices=["learned", "logmel"]) | |
| a = ap.parse_args() | |
| if a.selftest: | |
| sys.exit(0 if _selftest() else 1) | |
| print(json.dumps(find(load(), read_wav(a.query), read_wav(a.recording), a.method), indent=1)) | |