tiny-hash-intent / infer.py
WideChaos's picture
Publish Tiny Hash Intent with model and experiment records
a2bd61d verified
Raw History Blame Contribute Delete
1.78 kB
"""Run the bundled CPU model locally without server or model API calls."""
import argparse, json
from pathlib import Path
import numpy as np
from model import (
PARAMETERS,
decode_portable,
features,
logits_for,
probabilities,
file_sha,
)
r = Path(__file__).resolve().parent
if __name__ == "__main__":
a = argparse.ArgumentParser()
a.add_argument("text")
a.add_argument("--run", type=Path, default=r / "run-001")
a.add_argument("--representation", choices=["float64", "int8"], default="int8")
a.add_argument("--raw", action="store_true")
x = a.parse_args()
m = json.loads((x.run / "model-int8.json").read_text())
T = 1 if x.raw else m["temperature"]
if x.representation == "int8":
p = decode_portable(m)
artifact = x.run / "model-int8.json"
else:
artifact = x.run / "model-float64.npz"
with np.load(artifact, allow_pickle=False) as z:
p = {k: z[k] for k in PARAMETERS}
z = logits_for([features(x.text)], p)[0]
prob = probabilities(z, T)
order = np.argsort(-z, kind="stable")[:3]
print(
json.dumps(
{
"input": x.text,
"representation": x.representation,
"temperature": T,
"model_file_sha256": file_sha(artifact),
"top3": [
{
"label_id": int(i),
"label": m["labels"][int(i)],
"score": float(prob[int(i)]),
}
for i in order
],
"scope": "closed-set English intent labels, no OOD detector or banking advice",
},
ensure_ascii=False,
indent=2,
)
)