Lyndon Carlson commited on
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,130 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- es
|
| 5 |
+
base_model:
|
| 6 |
+
- dccuchile/bert-base-spanish-wwm-cased
|
| 7 |
+
---
|
| 8 |
+
# **Model Card: Spanish Text Reranker using BETO**
|
| 9 |
+
|
| 10 |
+
This model is a **reranker** for Spanish text passages, built on top of [**BETO**](https://huggingface.co/dccuchile/bert-base-spanish-wwm-cased) (a BERT-based model pre-trained on Spanish). It was trained to **score the relevance** of text passages given a user prompt, enabling you to reorder search results or candidate answers by how closely they match the user’s query.
|
| 11 |
+
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
## **Model Details**
|
| 15 |
+
|
| 16 |
+
- **Model Name**: `reranker_beto_pytorch_optimized`
|
| 17 |
+
- **Architecture**: [BETO (BERT-base Spanish WWM)](https://huggingface.co/dccuchile/bert-base-spanish-wwm-cased)
|
| 18 |
+
- **Language**: Spanish
|
| 19 |
+
- **Task**: Regression-based Reranking
|
| 20 |
+
- Given a `(prompt, content)` pair, the model outputs a **single numerical score** indicating predicted relevance.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## **Intended Use and Applications**
|
| 25 |
+
|
| 26 |
+
1. **Passage Reranking**: Use the model to rerank search results, QA passages, or any candidate text snippet according to how well they answer a Spanish query.
|
| 27 |
+
2. **Information Retrieval Pipelines**: Integrate the model as a final step after retrieving multiple candidate passages from a search engine. The model will reorder candidates by relevance.
|
| 28 |
+
3. **Question-Answering Systems**: Filter or sort passages that might contain the best answer to a user’s Spanish question.
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## **How It Was Trained**
|
| 33 |
+
|
| 34 |
+
1. **Data Source**:
|
| 35 |
+
- Model training data came from an internal system that stores:
|
| 36 |
+
- **Prompts** (user queries or questions)
|
| 37 |
+
- **Content** (text chunks from documents)
|
| 38 |
+
- **Rank** (a manual or heuristic-based 1–5 relevance score)
|
| 39 |
+
- Additional generation steps (HyDE / T5) were used to create synthetic queries, but **this reranker model** specifically used the `(prompt, content, rank)` tuples from the database.
|
| 40 |
+
|
| 41 |
+
2. **Preprocessing**:
|
| 42 |
+
- The textual pairs (`prompt`, `content`) were tokenized using the BETO tokenizer (cased) with:
|
| 43 |
+
- `max_length = 512`
|
| 44 |
+
- `doc_stride = 256` (for lengthy passages)
|
| 45 |
+
- The `rank` field was normalized and mapped to a continuous value (`relevance`) for regression.
|
| 46 |
+
|
| 47 |
+
3. **Training Setup**:
|
| 48 |
+
- **Base model**: `dccuchile/bert-base-spanish-wwm-cased`
|
| 49 |
+
- **Loss**: Mean Squared Error (MSE) to predict the `relevance` score
|
| 50 |
+
- **Optimizer**: `AdamW` with a learning rate of `3e-5`
|
| 51 |
+
- **Epochs**: 3
|
| 52 |
+
- **Batch Size**: 8
|
| 53 |
+
- **Hardware**: CPU/GPU (CUDA if available)
|
| 54 |
+
|
| 55 |
+
4. **Splits**:
|
| 56 |
+
- Data was split into **train (80%)**, **validation (10%)**, and **test (10%)** sets using `sklearn.model_selection.train_test_split`.
|
| 57 |
+
|
| 58 |
+
---
|
| 59 |
+
|
| 60 |
+
## **Model Performance**
|
| 61 |
+
|
| 62 |
+
- The code logs training and validation **loss** (MSE).
|
| 63 |
+
- Final test set MSE is logged as `test_loss`.
|
| 64 |
+
- Specific numerical results depend on your data distribution and training logs.
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
## **Usage Example**
|
| 69 |
+
|
| 70 |
+
Below is a quick example in Python using [Hugging Face Transformers](https://github.com/huggingface/transformers). After you’ve downloaded the model and tokenizer to `./reranker_beto_pytorch_optimized`, you can do:
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
import torch
|
| 74 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 75 |
+
|
| 76 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 77 |
+
|
| 78 |
+
# Load the fine-tuned model and tokenizer
|
| 79 |
+
model_dir = "./reranker_beto_pytorch_optimized"
|
| 80 |
+
tokenizer = BertTokenizer.from_pretrained(model_dir)
|
| 81 |
+
model = BertForSequenceClassification.from_pretrained(model_dir).to(device)
|
| 82 |
+
model.eval()
|
| 83 |
+
|
| 84 |
+
prompt = "¿Cómo implementar un sistema solar en una escuela primaria?"
|
| 85 |
+
passage = "Este documento describe las partes del sistema solar ..."
|
| 86 |
+
|
| 87 |
+
inputs = tokenizer(
|
| 88 |
+
prompt,
|
| 89 |
+
passage,
|
| 90 |
+
max_length=512,
|
| 91 |
+
truncation='only_second',
|
| 92 |
+
padding='max_length',
|
| 93 |
+
return_tensors='pt'
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Forward pass
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
outputs = model(
|
| 99 |
+
input_ids=inputs['input_ids'].to(device),
|
| 100 |
+
attention_mask=inputs['attention_mask'].to(device)
|
| 101 |
+
)
|
| 102 |
+
score = outputs.logits.squeeze().item()
|
| 103 |
+
|
| 104 |
+
print(f"Predicted relevance score: {score:.4f}")
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
You would compare **scores** across multiple passages for a single prompt, then **rank** or **sort** them from **highest** to **lowest** predicted relevance.
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## **Limitations and Ethical Considerations**
|
| 112 |
+
|
| 113 |
+
1. **Bias and Fairness**:
|
| 114 |
+
- Model performance is influenced by training data’s content and labels. If the data distribution is skewed, the model might reflect those biases (e.g., domain-specific content, reading level bias).
|
| 115 |
+
2. **Domain Generalization**:
|
| 116 |
+
- Trained primarily on text from a specific database of Spanish prompts and passages. Performance may degrade in **highly specialized** or **different** domains, or with non-standard Spanish dialects.
|
| 117 |
+
3. **Possible Misinformation**:
|
| 118 |
+
- Reranking aims to find the “most relevant” snippet, not necessarily the “most correct” or “fact-checked.” Always verify final results for correctness or harmful misinformation.
|
| 119 |
+
4. **Data Confidentiality**:
|
| 120 |
+
- If your data contains personal or sensitive info, ensure you comply with relevant privacy and data handling regulations before using or distributing the model.
|
| 121 |
+
|
| 122 |
+
---
|
| 123 |
+
|
| 124 |
+
## **Intended Users**
|
| 125 |
+
|
| 126 |
+
- **Developers** building Spanish-based search and question-answering systems.
|
| 127 |
+
- **Researchers** experimenting with Spanish language reranking or IR tasks.
|
| 128 |
+
- **Content Managers** wanting to reorder Spanish text snippets by relevance.
|
| 129 |
+
|
| 130 |
+
---
|