Text Ranking
sentence-transformers
ONNX
Safetensors
Transformers.js
modernbert
text-classification
reranker
cross-encoder
rerank
rag
retrieval
chinese
japanese
korean
langchain
llamaindex
haystack
text-embeddings-inference
Instructions to use CheezOne/reranker-cjk-small with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use CheezOne/reranker-cjk-small with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("CheezOne/reranker-cjk-small") query = "Which planet is known as the Red Planet?" passages = [ "Venus is often called Earth's twin because of its similar size and proximity.", "Mars, known for its reddish appearance, is often referred to as the Red Planet.", "Jupiter, the largest planet in our solar system, has a prominent red spot.", "Saturn, famous for its rings, is sometimes mistaken for the Red Planet." ] scores = model.predict([(query, passage) for passage in passages]) print(scores) - Transformers.js
How to use CheezOne/reranker-cjk-small with Transformers.js:
// npm i @huggingface/transformers import { AutoTokenizer, AutoModelForSequenceClassification } from '@huggingface/transformers'; const tokenizer = await AutoTokenizer.from_pretrained('CheezOne/reranker-cjk-small'); const model = await AutoModelForSequenceClassification.from_pretrained('CheezOne/reranker-cjk-small'); const query = 'Which planet is known as the Red Planet?'; const documents = [ 'Mars, known for its reddish appearance, is often referred to as the Red Planet.', 'Venus is often called the twin of Earth because of its similar size and proximity.', ]; const inputs = tokenizer(new Array(documents.length).fill(query), { text_pair: documents, padding: true, truncation: true }); const { logits } = await model(inputs); console.log(logits.sigmoid().tolist()); // one relevance score per document - Notebooks
- Google Colab
- Kaggle
Download scripts/rr_prep.py from CheezOne/reranker-cjk-small: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/CheezOne/reranker-cjk-small/resolve/main/scripts/rr_prep.py
- Command line
-
hf download hf://CheezOne/reranker-cjk-small/scripts/rr_prep.py
-
curl -L -o rr_prep.py https://huggingface.co/CheezOne/reranker-cjk-small/resolve/main/scripts/rr_prep.py
3.26 kB
| """Build reranker training groups {lang, query, passages[≤16], labels} from permissive sources. | |
| zh: THUIR/T2Ranking (Apache-2.0) graded qrels (0-3) + collection.tsv | |
| ja/ko/th/en: castorini/mr-tydi (Apache-2.0) train: positive + negative passages | |
| Any query whose text appears in the MIRACL reranking eval sets is dropped (no test leakage). | |
| Usage: python rr_prep.py <rr_dir> <out.jsonl> [--zh 40000] [--seed 0] | |
| """ | |
| import argparse, csv, glob, gzip, json, os, random, sys | |
| csv.field_size_limit(sys.maxsize) | |
| def main(): | |
| ap = argparse.ArgumentParser(); ap.add_argument("rr_dir"); ap.add_argument("out") | |
| ap.add_argument("--zh", type=int, default=40000); ap.add_argument("--group", type=int, default=16); ap.add_argument("--seed", type=int, default=0) | |
| a = ap.parse_args(); r = random.Random(a.seed); d = a.rr_dir | |
| import pandas as pd | |
| eval_q = set() | |
| for f in glob.glob(f"{d}/miracl_rr/*-queries/*.parquet"): | |
| eval_q |= {str(t).strip() for t in pd.read_parquet(f)["text"]} | |
| print("eval queries to exclude:", len(eval_q)) | |
| out = open(a.out, "w"); stats = {} | |
| def emit(lang, q, pos, neg): | |
| if q.strip() in eval_q or not pos: return | |
| negs = r.sample(neg, min(len(neg), a.group - min(len(pos), 2))) | |
| ps = [(p, 1) for p in pos[:2]] + [(n, 0) for n in negs] | |
| r.shuffle(ps) | |
| out.write(json.dumps({"lang": lang, "query": q, "passages": [p for p, _ in ps], "labels": [l for _, l in ps]}, ensure_ascii=False) + "\n") | |
| stats[lang] = stats.get(lang, 0) + 1 | |
| # Mr.TyDi | |
| for lang, code in [("japanese", "ja"), ("korean", "ko"), ("thai", "th"), ("english", "en")]: | |
| rows = [json.loads(l) for l in gzip.open(f"{d}/mrtydi/mrtydi-v1.1-{lang}/train.jsonl.gz", "rt")] | |
| if code == "en": rows = r.sample(rows, min(4000, len(rows))) | |
| for x in rows: | |
| fmt = lambda p: (p.get("title", "") + "\n" + p["text"]).strip() | |
| emit(code, x["query"], [fmt(p) for p in x["positive_passages"]], [fmt(p) for p in x["negative_passages"]]) | |
| # T2Ranking (zh): pick queries having >=1 passage with rel>=2 and >=3 judged passages | |
| q = {row[0]: row[1] for row in csv.reader(open(f"{d}/t2/data/queries.train.tsv"), delimiter="\t") if row[0] != "qid"} | |
| judged = {} | |
| for row in csv.reader(open(f"{d}/t2/data/qrels.train.tsv"), delimiter="\t"): | |
| if row[0] == "qid": continue | |
| judged.setdefault(row[0], []).append((row[2], int(row[3]))) | |
| good = [qid for qid, js in judged.items() if len(js) >= 3 and any(rel >= 2 for _, rel in js) and qid in q] | |
| pick = r.sample(good, min(a.zh, len(good))); need = {pid for qid in pick for pid, _ in judged[qid]} | |
| text = {} | |
| for row in csv.reader(open(f"{d}/t2/data/collection.tsv"), delimiter="\t"): | |
| if row and row[0] in need: text[row[0]] = row[1] | |
| for qid in pick: | |
| js = [(text[p], rel) for p, rel in judged[qid] if p in text] | |
| pos = [t for t, rel in js if rel >= 2]; neg = [t for t, rel in js if rel <= 1] | |
| # T2Ranking has few judged negatives per query: add random passages from other queries as easy negatives | |
| while len(neg) < 7: neg.append(text[r.choice(list(need))]) | |
| emit("zh", q[qid], pos, neg) | |
| print(stats) | |
| if __name__ == "__main__": | |
| main() | |