Download inference.py from thelamapi/Next-Match: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/thelamapi/Next-Match/resolve/main/inference.py
- Command line
-
hf download hf://thelamapi/Next-Match/inference.py
-
curl -L -o inference.py https://huggingface.co/thelamapi/Next-Match/resolve/main/inference.py
1.89 kB
| """Next Match inference (NumPy only): rank catalog items for a reference image plus a text edit. | |
| Usage: | |
| python inference.py --selftest | |
| from inference import load, search | |
| ids = search(spec, ref_image_uint8_hwc, "bunun kırmızı olanı") | |
| """ | |
| import json | |
| import os | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, HERE) | |
| import numpy as np # noqa: E402 | |
| import match_core as mc # noqa: E402 | |
| from nlrt import load_model # noqa: E402 | |
| def load(path=os.path.join(HERE, "model.json")): | |
| return load_model(path) | |
| def to_chw(img_uint8_hwc): | |
| return np.asarray(img_uint8_hwc, dtype=np.float64).transpose(2, 0, 1) / 255.0 | |
| def search(spec, ref_image, text, mode="combined", top=10, keys=None): | |
| """ref_image: (48, 48, 3) uint8. Returns catalog indices (into the released catalog) best first.""" | |
| q = mc.encode_query(spec, to_chw(ref_image), text, mode) | |
| name = {"combined": "gallery_keys", "image": "gallery_keys_img_only", "text": "gallery_keys_txt_only"}[mode] | |
| K = spec["tensors"][name] if keys is None else keys | |
| s = K @ q | |
| return sorted(range(len(s)), key=lambda i: (-s[i], i))[:top] | |
| def _selftest(): | |
| spec = load() | |
| with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f: | |
| cases = json.load(f)["cases"] | |
| bad = [] | |
| for c in cases: | |
| img = np.asarray(c["pixels"], dtype=np.uint8).reshape(3, 48, 48).transpose(1, 2, 0) | |
| got = {m: search(spec, img, c["text"], m, 5) for m in ("combined", "image", "text")} | |
| if got != c["expected"]: | |
| bad.append({"text": c["text"], "got": got, "expected": c["expected"]}) | |
| print(json.dumps({"passed": not bad, "cases": len(cases), "mismatches": bad[:2]}, ensure_ascii=False)) | |
| return not bad | |
| if __name__ == "__main__": | |
| if "--selftest" in sys.argv: | |
| sys.exit(0 if _selftest() else 1) | |
| print(__doc__) | |