Datasets:
Download lab/split_check.py from HuggingFaceM4/reachy-gestures-data: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/datasets/HuggingFaceM4/reachy-gestures-data/resolve/main/lab/split_check.py
- Command line
-
hf download hf://datasets/HuggingFaceM4/reachy-gestures-data/lab/split_check.py
-
curl -L -o split_check.py https://huggingface.co/datasets/HuggingFaceM4/reachy-gestures-data/resolve/main/lab/split_check.py
3.03 kB
| """Whole text vs last 64 tokens vs sentence-by-sentence on the test sets, with the deployed model and threshold.""" | |
| import re, json | |
| import numpy as np, onnxruntime as ort | |
| from transformers import AutoTokenizer | |
| from huggingface_hub import snapshot_download | |
| d = snapshot_download("HuggingFaceM4/reachy-gestures", allow_patterns=["*.json", "*.npz", "model_qint8.onnx"]) | |
| tok = AutoTokenizer.from_pretrained("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2") | |
| sess = ort.InferenceSession(f"{d}/model_qint8.onnx", providers=["CPUExecutionProvider"]) | |
| heads, labels = np.load(f"{d}/gesture_heads.npz"), json.load(open(f"{d}/gesture_labels.json")) | |
| POS = {"success1","success2","laughing2","grateful1","loving1","amazed1","surprised1","welcoming2","helpful1","relief1","yes1","understanding2"} | |
| NEG = {"downcast1","fear1","disgusted1","irritated1","rage1","resigned1","no1"} | |
| val = lambda m: "+" if m in POS else "-" if m in NEG else "0" | |
| SPLIT = re.compile(r"(?<=[.!?…])\s+|(?<=[。!?])") | |
| def probs(ids, kind): | |
| ids = np.array([ids], dtype=np.int64) | |
| h = sess.run(None, {"input_ids": ids, "attention_mask": np.ones_like(ids), "token_type_ids": np.zeros_like(ids)})[0][0].mean(0) | |
| v = h / np.linalg.norm(h) * 10 | |
| l = heads[f"{kind}_weight"] @ v + heads[f"{kind}_bias"]; p = np.exp(l - l.max()); return p / p.sum() | |
| def decide(p, kind): | |
| j = int(p.argmax()); m = labels[kind][j] | |
| return (m, p[j]) if m != "none" and p[j] >= 0.3 else ("none", p[j]) | |
| def ids_of(text, tail=False): | |
| ids = tok(text, add_special_tokens=False)["input_ids"] | |
| ids = ids[-62:] if tail else ids[:62] | |
| return [tok.cls_token_id, *ids, tok.sep_token_id] | |
| def strategies(text, kind): | |
| out = {"whole (first 64)": decide(probs(ids_of(text), kind), kind)[0], | |
| "last 64": decide(probs(ids_of(text, tail=True), kind), kind)[0]} | |
| parts = [s for s in SPLIT.split(text) if s.strip()] or [text] | |
| best = ("none", 0.0) | |
| for s in parts: | |
| m, p = decide(probs(ids_of(s), kind), kind) | |
| if m != "none" and p > best[1]: best = (m, p) | |
| out["per sentence, most confident"] = best[0] | |
| return out, len(parts), len(tok(text)["input_ids"]) | |
| for name in ("test", "test_real", "hard"): | |
| rows = [json.loads(l) for l in open(f"data/{name}.jsonl")] | |
| stats = {} | |
| for r in rows: | |
| res, n_sent, n_tok = strategies(r["text"], r["kind"]) | |
| for sname, got in res.items(): | |
| for subset, inc in (("all", True), ("multi-sentence", n_sent > 1)): | |
| if not inc: continue | |
| s = stats.setdefault((sname, subset), [0, 0, 0]) | |
| s[0] += 1; s[1] += got in r["ok"] | |
| wanted = {val(m) for m in r["ok"] if m != "none"}; opp = {"+": "-", "-": "+"}.get(val(got)) | |
| s[2] += got not in r["ok"] and opp in wanted and val(got) not in wanted | |
| print(f"== {name}") | |
| for (sname, subset), (n, ok, sev) in sorted(stats.items(), key=lambda x: (x[0][1], x[0][0])): | |
| print(f" {subset:15} {sname:30} {ok/n:6.1%} severe {sev:2} (n={n})") | |