Instructions to use lookevink/scribeski-classifier-r3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use lookevink/scribeski-classifier-r3 with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download lookevink/scribeski-classifier-r3 --local-dir scribeski-classifier-r3
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Download classify.py from lookevink/scribeski-classifier-r3: direct link, hf CLI and curl.
- Browser
- Download file 4.41 kB
-
https://huggingface.co/lookevink/scribeski-classifier-r3/resolve/main/classify.py
- Command line
-
hf download hf://lookevink/scribeski-classifier-r3/classify.py
-
curl -L -o classify.py https://huggingface.co/lookevink/scribeski-classifier-r3/resolve/main/classify.py
4.41 kB
| """Scribeski intake classifier (r3): answer every choice field of a session transcript in one pass. | |
| python classify.py transcript.txt [--json] | |
| Depends only on this folder (classifier.json + model/) plus `mlx`, `mlx-lm` and `transformers`. | |
| Output: one line per field, `value` (or blank) with the model's confidence. **A blank means | |
| "needs review": the social worker fills it, as today.** Never treat a blank as "no". | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import math | |
| import re | |
| import sys | |
| from pathlib import Path | |
| HERE = Path(__file__).resolve().parent | |
| SPEC = json.loads((HERE / "classifier.json").read_text()) | |
| LINE = re.compile(r"^\[(\d+):(\d+)\]\s*([A-Z_ ]+?):\s*(.*)$") | |
| def read_lines(path: Path) -> list[str]: | |
| out = [] | |
| for raw in path.read_text().splitlines(): | |
| if raw.startswith("#") or not raw.strip(): | |
| continue | |
| m = LINE.match(raw.strip()) | |
| out.append(f"{m.group(3).strip()}: {m.group(4).strip()}" if m else raw.strip()) | |
| return out | |
| def build(lines, tok): | |
| p = SPEC["prompt"] | |
| head = tok.apply_chat_template( | |
| [{"role": "system", "content": p["rules"]}, {"role": "user", "content": "TRANSCRIPT:\n" + "\n".join(lines)}], | |
| tokenize=False, add_generation_prompt=True, enable_thinking=False) | |
| qs = SPEC["questions"] | |
| q_ids = [tok(("\n" if i else "") + f"Q{i + 1}. {q['text']}\nAnswer:", add_special_tokens=False)["input_ids"] for i, q in enumerate(qs)] | |
| head_ids = tok(head, add_special_tokens=False)["input_ids"] | |
| budget = p["max_len"] - sum(len(x) for x in q_ids) | |
| if len(head_ids) > budget: | |
| keep = budget - 16 | |
| cut = tok("\n[…]\n", add_special_tokens=False)["input_ids"] | |
| head_ids = head_ids[: keep // 2] + cut + head_ids[-(keep - keep // 2):] | |
| ids, pos = list(head_ids), [] | |
| for x in q_ids: | |
| ids += x | |
| pos.append(len(ids) - 1) | |
| return ids, pos | |
| def classify(lines, model, tok): | |
| import mlx.core as mx | |
| ids, pos = build(lines, tok) | |
| letters = SPEC["letters"] | |
| order = sorted(letters) | |
| rows = mx.array([letters[L] for L in order]) | |
| col = {L: i for i, L in enumerate(order)} | |
| h = model.model(mx.array([ids]))[0] | |
| logits = (h[mx.array(pos)] @ model.model.embed_tokens(rows).T).astype(mx.float32) | |
| mx.eval(logits) | |
| import numpy as np | |
| L = np.array(logits) | |
| dec = SPEC["decoding"] | |
| T, th = dec["temperature"], dec["threshold"] | |
| probs: dict = {} | |
| for i, q in enumerate(SPEC["questions"]): | |
| z = [L[i, col[c]] / T for c in q["letters"]] | |
| m = max(z) | |
| e = [math.exp(v - m) for v in z] | |
| s = sum(e) | |
| p = [v / s for v in e] | |
| if q["field"] in SPEC["checkbox_fields"]: | |
| probs.setdefault(q["field"], {})[q["qid"].split(":", 1)[1]] = p[0] | |
| else: | |
| probs[q["field"]] = {("" if v is None else v): pi for v, pi in zip(q["values"], p)} | |
| out = {} | |
| for f in SPEC["scope"]: | |
| d = probs[f] | |
| if f in SPEC["checkbox_fields"]: | |
| chosen = [o for o, pi in d.items() if pi >= max(0.5, th)] | |
| if len(chosen) > 1: | |
| chosen = [o for o in chosen if o not in SPEC["exclusive_options"]] | |
| conf = min((d[o] for o in chosen), default=max(1 - pi for pi in d.values())) | |
| out[f] = {"value": sorted(chosen) or None, "confidence": round(conf, 3)} | |
| else: | |
| v, pi = max(d.items(), key=lambda kv: kv[1]) | |
| out[f] = {"value": None if v == "" or pi < th else v, "confidence": round(pi, 3)} | |
| out[f]["status"] = "filled" if out[f]["value"] else "needs_review" | |
| return out | |
| def load(): | |
| from mlx_lm import load as mlx_load | |
| from transformers import AutoTokenizer | |
| model, _ = mlx_load(str(HERE / "model")) | |
| return model, AutoTokenizer.from_pretrained(str(HERE / "model")) | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("transcript") | |
| ap.add_argument("--json", action="store_true") | |
| a = ap.parse_args() | |
| model, tok = load() | |
| res = classify(read_lines(Path(a.transcript)), model, tok) | |
| if a.json: | |
| json.dump(res, sys.stdout, indent=1) | |
| else: | |
| for f, r in res.items(): | |
| if r["value"]: | |
| print(f"{f:28s} {r['value']} ({r['confidence']:.0%})") | |
| print(f"\n{sum(r['status'] == 'needs_review' for r in res.values())} of {len(res)} fields left for review.") | |