Text Ranking
sentence-transformers
Safetensors
Transformers
xlm-roberta
text-classification
Generated from Trainer
text-embeddings-inference
Instructions to use netandreus/bge-reranker-v2-m3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use netandreus/bge-reranker-v2-m3 with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("netandreus/bge-reranker-v2-m3") 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
How to use netandreus/bge-reranker-v2-m3 with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("netandreus/bge-reranker-v2-m3") model = AutoModelForSequenceClassification.from_pretrained("netandreus/bge-reranker-v2-m3", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from netandreus/bge-reranker-v2-m3: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/netandreus/bge-reranker-v2-m3/resolve/main/handler.py
- Command line
-
hf download hf://netandreus/bge-reranker-v2-m3/handler.py
-
curl -L -o handler.py https://huggingface.co/netandreus/bge-reranker-v2-m3/resolve/main/handler.py
2.23 kB
| from typing import Dict, List, Any | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch.nn.functional as F | |
| class EndpointHandler: | |
| def __init__(self, path: str = "netandreus/bge-reranker-v2-m3"): | |
| # Load tokenizer and model | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| self.model = AutoModelForSequenceClassification.from_pretrained(path) | |
| self.model.eval() | |
| # Determine the computation device | |
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| self.model.to(self.device) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| """ | |
| Expected input format: | |
| { | |
| "inputs": { | |
| "source_sentence": "Your query here", | |
| "sentences": ["Document 1", "Document 2", ...] | |
| }, | |
| "normalize": true # Optional; defaults to False | |
| } | |
| """ | |
| inputs = data.get("inputs", {}) | |
| source_sentence = inputs.get("source_sentence") | |
| sentences = inputs.get("sentences", []) | |
| normalize = data.get("normalize", False) | |
| if not source_sentence or not sentences: | |
| return [{"error": "Both 'source_sentence' and 'sentences' fields are required inside 'inputs'."}] | |
| # Prepare input pairs | |
| pairs = [[source_sentence, text] for text in sentences] | |
| # Tokenize input pairs | |
| tokenizer_inputs = self.tokenizer( | |
| pairs, | |
| padding=True, | |
| truncation=True, | |
| return_tensors="pt", | |
| max_length=512 | |
| ).to(self.device) | |
| with torch.no_grad(): | |
| # Get model logits | |
| outputs = self.model(**tokenizer_inputs) | |
| scores = outputs.logits.view(-1) | |
| # Apply sigmoid normalization if requested | |
| if normalize: | |
| scores = torch.sigmoid(scores) | |
| # Prepare the response | |
| results = [ | |
| {"index": idx, "score": score.item()} | |
| for idx, score in enumerate(scores) | |
| ] | |
| # Sort results by descending score | |
| results.sort(key=lambda x: x["score"], reverse=True) | |
| return results | |