samwjya commited on
Commit ·
0ccd8ab
1
Parent(s): 9430c26
reranker
Browse files- reranker.py +83 -0
reranker.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import CrossEncoder
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
DEFAULT_MODEL = "BAAI/bge-reranker-v2-m3"
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def load_model(model_name: str = DEFAULT_MODEL) -> CrossEncoder:
|
| 8 |
+
"""Load a CrossEncoder model from HuggingFace (cached after first download)."""
|
| 9 |
+
print(f"Loading reranker model: {model_name} ...")
|
| 10 |
+
model = CrossEncoder(model_name, max_length=512)
|
| 11 |
+
print("Model loaded.\n")
|
| 12 |
+
return model
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def build_pairs(query: str, papers: list[dict]) -> list[tuple[str, str]]:
|
| 16 |
+
"""Build (query, document) pairs for CrossEncoder scoring.
|
| 17 |
+
|
| 18 |
+
The document is title + abstract concatenated — both fields carry
|
| 19 |
+
relevance signal and the CrossEncoder attends over the full pair.
|
| 20 |
+
"""
|
| 21 |
+
pairs = []
|
| 22 |
+
for paper in papers:
|
| 23 |
+
title = paper.get("title") or ""
|
| 24 |
+
abstract = paper.get("abstract") or ""
|
| 25 |
+
document = f"{title}. {abstract}".strip()
|
| 26 |
+
pairs.append((query, document))
|
| 27 |
+
return pairs
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def rerank(query: str, papers: list[dict], model: CrossEncoder, batch_size: int = 16) -> list[dict]:
|
| 31 |
+
"""Score every (query, paper) pair with the CrossEncoder and return papers sorted by score.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
query: The original search query string.
|
| 35 |
+
papers: List of paper dicts (already BM25-ranked; order does not matter here).
|
| 36 |
+
model: A loaded CrossEncoder instance.
|
| 37 |
+
batch_size: Number of pairs to score in one forward pass.
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
The same paper dicts, each annotated with `ce_score` and `ce_rank`,
|
| 41 |
+
sorted by CrossEncoder score descending.
|
| 42 |
+
"""
|
| 43 |
+
pairs = build_pairs(query, papers)
|
| 44 |
+
scores = model.predict(pairs, batch_size=batch_size, show_progress_bar=True)
|
| 45 |
+
|
| 46 |
+
ranked = sorted(
|
| 47 |
+
zip(papers, scores),
|
| 48 |
+
key=lambda x: x[1],
|
| 49 |
+
reverse=True,
|
| 50 |
+
)
|
| 51 |
+
results = []
|
| 52 |
+
for rank, (paper, score) in enumerate(ranked, start=1):
|
| 53 |
+
paper = dict(paper)
|
| 54 |
+
paper["ce_score"] = round(float(score), 4)
|
| 55 |
+
paper["ce_rank"] = rank
|
| 56 |
+
results.append(paper)
|
| 57 |
+
return results
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
import argparse
|
| 62 |
+
from retriever import retrieve
|
| 63 |
+
|
| 64 |
+
parser = argparse.ArgumentParser(description="Rerank BM25 candidates with a CrossEncoder")
|
| 65 |
+
parser.add_argument("query", type=str, help="Search query")
|
| 66 |
+
parser.add_argument("--model", type=str, default=DEFAULT_MODEL, help="HuggingFace CrossEncoder model ID")
|
| 67 |
+
parser.add_argument("--limit", type=int, default=50, help="Number of BM25 candidates to fetch")
|
| 68 |
+
parser.add_argument("--top", type=int, default=10, help="Number of reranked results to display")
|
| 69 |
+
args = parser.parse_args()
|
| 70 |
+
|
| 71 |
+
papers = retrieve(args.query, limit=args.limit)
|
| 72 |
+
model = load_model(args.model)
|
| 73 |
+
reranked = rerank(args.query, papers, model)
|
| 74 |
+
|
| 75 |
+
print(f"\nQuery: {args.query!r}")
|
| 76 |
+
print(f"Showing top {args.top} after CrossEncoder reranking:\n")
|
| 77 |
+
for paper in reranked[:args.top]:
|
| 78 |
+
authors = ", ".join(a["name"] for a in paper.get("authors", [])[:2])
|
| 79 |
+
print(f"[CE #{paper['ce_rank']}] ce_score={paper['ce_score']} (was BM25 #{paper['bm25_rank']})")
|
| 80 |
+
print(f" Title : {paper['title']}")
|
| 81 |
+
print(f" Year : {paper.get('year', 'N/A')} | Citations: {paper.get('citationCount', 'N/A')}")
|
| 82 |
+
print(f" Authors: {authors}")
|
| 83 |
+
print()
|