Views
No views yet


pip install transformers sentence-transformers torch1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4model_id = "thebajajra/RexReranker-base"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForSequenceClassification.from_pretrained(model_id)
8
9device = "cuda" if torch.cuda.is_available() else "cpu"
10model = model.to(device).eval()
11
12query = "best laptop for programming"
13title = "MacBook Pro M3"
14description = "Powerful laptop with M3 chip, 16GB RAM, perfect for developers and creative professionals"
15
16inputs = tokenizer(
17 f"Query: {query}",
18 f"Title: {title}\nDescription: {description}",
19 return_tensors="pt",
20 truncation=True,
21 max_length=min(model.config.max_position_embeddings, 7999),
22).to(device)
23
24with torch.no_grad():
25 outputs = model(**inputs)
26 score = outputs.logits.squeeze(-1) # shape: [batch]
27 print(f"Relevance Score: {score[0].item():.4f}")1from sentence_transformers import CrossEncoder
2
3# Load as CrossEncoder
4model = CrossEncoder(
5 "thebajajra/RexReranker-base",
6 trust_remote_code=True
7)
8
9# Single prediction
10query = "best laptop for programming"
11document = "MacBook Pro M3 - Powerful laptop with M3 chip for developers"
12
13score = model.predict([(query, document)])[0]
14print(f"Score: {score:.4f}")1from sentence_transformers import CrossEncoder
2
3model = CrossEncoder("thebajajra/RexReranker-base", trust_remote_code=True)
4
5query = "best laptop for programming"
6documents = [
7 "MacBook Pro M3 - Powerful laptop with M3 chip for developers",
8 "Gaming Mouse RGB - High precision gaming mouse with 16000 DPI",
9 "ThinkPad X1 Carbon - Business ultrabook with long battery life",
10 "Mechanical Keyboard - Cherry MX switches for typing comfort",
11 "Dell XPS 15 - Premium laptop with 4K OLED display",
12]
13
14# Get scores for all documents
15pairs = [(query, doc) for doc in documents]
16scores = model.predict(pairs)
17
18# Print ranked results
19print(f"Query: {query}\n")
20for doc, score in sorted(zip(documents, scores), key=lambda x: x[1], reverse=True):
21 print(f" {score:.4f} | {doc[:60]}")1from sentence_transformers import CrossEncoder
2
3model = CrossEncoder("thebajajra/RexReranker-base", trust_remote_code=True)
4
5query = "wireless headphones with noise cancellation"
6documents = [
7 "Sony WH-1000XM5 - Industry-leading noise cancellation headphones",
8 "Apple AirPods Max - Premium over-ear headphones with spatial audio",
9 "Bose QuietComfort 45 - Comfortable wireless noise cancelling headphones",
10 "JBL Tune 750BTNC - Affordable wireless headphones with ANC",
11 "Logitech Gaming Headset - Wired gaming headphones with microphone",
12]
13
14# Rank documents
15results = model.rank(query, documents, top_k=3)
16
17print(f"Query: {query}\n")
18print("Top 3 Results:")
19for result in results:
20 idx = result['corpus_id']
21 score = result['score']
22 print(f" {score:.4f} | {documents[idx][:60]}")| Field | Format |
|---|---|
| Text A (Query) | Query: {your search query} |
| Text B (Document) | Title: {document title}\nDescription: {document description} |