Spaces:
Running on Zero
Running on Zero
calibrated confidence gate; indexed title lookup
Browse filesFiles changed (3)
78 unmodified lines79808182838485868778 unmodified lines# hits 0.67 against a bone-densitometry paper) while the mean stays at 0.19.# Scale is [0,1] because CrossEncoder.predict applies a sigmoid. The ONNX# backend returns raw logits instead - rescale if you switch.# Measured: in-corpus mean5 0.67-0.996, out-of-corpus 0.001-0.19.confidence_top_n: 5min_mean_score: 0.35# Below this (but above min_mean_score) the corpus has *something* but not# much, so live arXiv results are added alongside it and the answer draws on# both. Above it, a strong corpus hit answers without touching the network.78 unmodified lines7980818283848586878889909192939478 unmodified lines# hits 0.67 against a bone-densitometry paper) while the mean stays at 0.19.# Scale is [0,1] because CrossEncoder.predict applies a sigmoid. The ONNX# backend returns raw logits instead - rescale if you switch.# Measured, not eyeballed: scripts/calibrate_gate.py (results/gate_calibration_*.json).# The old 0.35 came from a few hand-picked queries ("in-corpus 0.67-0.996") and# refused 49% of specific questions whose answer paper WAS retrieved# (recall@10 = 100%). Pass rates, specific / vague / off-topic:# 0.35 -> 51% / 31% / 2.5% 0.20 -> 94% / 59% / 20%# A wrongly refused good question costs more than an off-topic one let# through: the answer prompt still says plainly when passages do not# cover the question. Recalibrate whenever the corpus changes size.confidence_top_n: 5min_mean_score: 0.20# Below this (but above min_mean_score) the corpus has *something* but not# much, so live arXiv results are added alongside it and the answer draws on# both. Above it, a strong corpus hit answers without touching the network.
68 unmodified lines69707172737475767768 unmodified lines# hits 0.67 against a bone-densitometry paper) while the mean stays at 0.19.# Scale is [0,1] because CrossEncoder.predict applies a sigmoid. The ONNX# backend returns raw logits instead - rescale if you switch.# Measured: in-corpus mean5 0.67-0.996, out-of-corpus 0.001-0.19.confidence_top_n: 5min_mean_score: 0.35# Below this (but above min_mean_score) the corpus has *something* but not# much, so live arXiv results are added alongside it and the answer draws on# both. Above it, a strong corpus hit answers without touching the network.68 unmodified lines6970717273747576777879808182838468 unmodified lines# hits 0.67 against a bone-densitometry paper) while the mean stays at 0.19.# Scale is [0,1] because CrossEncoder.predict applies a sigmoid. The ONNX# backend returns raw logits instead - rescale if you switch.# Measured, not eyeballed: scripts/calibrate_gate.py (results/gate_calibration_*.json).# The old 0.35 came from a few hand-picked queries ("in-corpus 0.67-0.996") and# refused 49% of specific questions whose answer paper WAS retrieved# (recall@10 = 100%). Pass rates, specific / vague / off-topic:# 0.35 -> 51% / 31% / 2.5% 0.20 -> 94% / 59% / 20%# A wrongly refused good question costs more than an off-topic one let# through: the answer prompt still says plainly when passages do not# cover the question. Recalibrate whenever the corpus changes size.confidence_top_n: 5min_mean_score: 0.20# Below this (but above min_mean_score) the corpus has *something* but not# much, so live arXiv results are added alongside it and the answer draws on# both. Above it, a strong corpus hit answers without touching the network.
251 unmodified lines252253254255256257258259260261262263264265266267268251 unmodified linesreturn (matched / total) * length_penaltydef find_in_corpus(query: str, papers_by_id: dict[str, dict[str, Any]], threshold: float = 0.72) -> tuple[dict[str, Any] | None, list[dict[str, str]]]:"""Best local title match, plus runners-up for disambiguation."""idf = _build_idf(papers_by_id)default_idf = max(idf.values(), default=1.0)scored = [(_weighted_coverage(query, meta.get("title", ""), idf, default_idf), meta)for meta in papers_by_id.values()]scored.sort(key=lambda kv: kv[0], reverse=True)candidates = [{"paper_id": m.get("paper_id", ""), "title": m.get("title", ""), "score": f"{s:.2f}"}for s, m in scored[:5]251 unmodified lines252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329251 unmodified linesreturn (matched / total) * length_penaltyclass TitleIndex:"""Inverted index over corpus titles, built once per corpus.find_in_corpus used to rebuild the IDF table and score every title onevery lookup: fine at 8k papers, seconds per lookup at 300k. This keepsthe scoring identical and only changes which titles get scored.Pruning is exact, not heuristic. A title that contains none of a set S ofquery tokens can match at most (total - w(S)) of the query's IDF weight,so if w(S) >= (1 - floor) * total it cannot score above `floor`. TakingS as the rarest query tokens until that holds, the postings of S containevery title that could clear the floor - the same results as a full scan."""def __init__(self, papers_by_id: dict[str, dict[str, Any]]) -> None:import mathself.papers = papers_by_idself.tokens: dict[str, set[str]] = {}postings: dict[str, list[str]] = {}for pid, meta in papers_by_id.items():toks = _tokens(meta.get("title", ""))self.tokens[pid] = toksfor t in toks:postings.setdefault(t, []).append(pid)self.postings = postingsn = max(1, len(papers_by_id))self.idf = {t: math.log(n / (1 + len(ids))) for t, ids in postings.items()}self.default_idf = max(self.idf.values(), default=1.0)def search(self, query: str, floor: float = 0.2) -> list[tuple[float, dict[str, Any]]]:qt = _tokens(query)if not qt:return []weight = {t: self.idf.get(t, self.default_idf) for t in qt}total = sum(weight.values())needed, covered, pivots = (1.0 - floor) * total, 0.0, []for t in sorted(qt, key=weight.get, reverse=True):pivots.append(t)covered += weight[t]if covered >= needed:breakcandidates = {pid for t in pivots for pid in self.postings.get(t, ())}scored = []for pid in candidates:tt = self.tokens[pid]matched = sum(weight[t] for t in qt & tt)score = (matched / total) * min(1.0, len(qt) / max(1, len(tt))) ** 0.5scored.append((score, self.papers[pid]))scored.sort(key=lambda kv: kv[0], reverse=True)return scored_TITLE_INDEX: dict[int, tuple[int, TitleIndex]] = {}def _title_index(papers_by_id: dict[str, dict[str, Any]]) -> TitleIndex:"""One index per corpus dict, rebuilt only if the corpus changes size."""key = id(papers_by_id)cached = _TITLE_INDEX.get(key)if cached is None or cached[0] != len(papers_by_id):_TITLE_INDEX.clear() # one live corpus per processcached = (len(papers_by_id), TitleIndex(papers_by_id))_TITLE_INDEX[key] = cachedreturn cached[1]def find_in_corpus(query: str, papers_by_id: dict[str, dict[str, Any]], threshold: float = 0.72) -> tuple[dict[str, Any] | None, list[dict[str, str]]]:"""Best local title match, plus runners-up for disambiguation."""scored = _title_index(papers_by_id).search(query, floor=0.2)candidates = [{"paper_id": m.get("paper_id", ""), "title": m.get("title", ""), "score": f"{s:.2f}"}for s, m in scored[:5]
configs/ml.cpu.yaml+9-2
configs/ml.yaml+9-2
src/data/citation.py+68-7