Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Paraphrase Detection with Roberta-base
|
| 2 |
+
|
| 3 |
+
## π Overview
|
| 4 |
+
|
| 5 |
+
This repository hosts the quantized version of the Roberta-base model for Paraphrase Detection. The model is designed to determine whether two sentences convey the same meaning. If they are similar, the model outputs "duplicate" with a confidence score; otherwise, it outputs "not duplicate" with a confidence score. The model has been optimized for efficient deployment while maintaining reasonable accuracy, making it suitable for real-time applications.
|
| 6 |
+
|
| 7 |
+
## π Model Details
|
| 8 |
+
|
| 9 |
+
- **Model Architecture:** Roberta-base
|
| 10 |
+
- **Task:** Paraphrase Detection
|
| 11 |
+
- **Dataset:** Hugging Face's `quora-question-pairs`
|
| 12 |
+
- **Quantization:** Float16 (FP16) for optimized inference
|
| 13 |
+
- **Fine-tuning Framework:** Hugging Face Transformers
|
| 14 |
+
|
| 15 |
+
## π Usage
|
| 16 |
+
|
| 17 |
+
### Installation
|
| 18 |
+
|
| 19 |
+
```bash
|
| 20 |
+
pip install transformers torch
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
### Loading the Model
|
| 24 |
+
|
| 25 |
+
```python
|
| 26 |
+
from transformers import RobertaTokenizer, RobertaForSequenceClassification
|
| 27 |
+
import torch
|
| 28 |
+
|
| 29 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
+
|
| 31 |
+
model_name = "AventIQ-AI/roberta-paraphrase-detection"
|
| 32 |
+
tokenizer = RobertaTokenizer.from_pretrained(model_name)
|
| 33 |
+
model = RobertaForSequenceClassification.from_pretrained(model_name).to(device)
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
### Paraphrase Detection Inference
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
def predict_paraphrase(sentence1, sentence2, threshold=0.96):
|
| 40 |
+
inputs = tokenizer(sentence1, sentence2, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model(**inputs)
|
| 44 |
+
|
| 45 |
+
logits = outputs.logits
|
| 46 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
| 47 |
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
| 48 |
+
confidence = probabilities[0][predicted_class].item()
|
| 49 |
+
|
| 50 |
+
label_map = {0: "Not Duplicate", 1: "Duplicate"}
|
| 51 |
+
|
| 52 |
+
# Apply a slightly less strict threshold
|
| 53 |
+
if predicted_class == 1 and confidence < threshold:
|
| 54 |
+
return {"sentence1": sentence1, "sentence2": sentence2, "predicted_label": "Not Duplicate", "confidence": confidence}
|
| 55 |
+
else:
|
| 56 |
+
return {"sentence1": sentence1, "sentence2": sentence2, "predicted_label": label_map[predicted_class], "confidence": confidence}
|
| 57 |
+
|
| 58 |
+
# π Test Example
|
| 59 |
+
test_cases = [
|
| 60 |
+
("The sun rises in the east.", "The east is where the sun rises."), # Duplicate
|
| 61 |
+
("She enjoys playing the piano.", "She loves playing musical instruments."), # Duplicate
|
| 62 |
+
("I had a great time at the party.", "The event was really fun."), # Duplicate
|
| 63 |
+
|
| 64 |
+
("The sky is blue.", "Bananas are yellow."), # Not Duplicate
|
| 65 |
+
("The capital of France is Paris.", "Berlin is the capital of Germany."), # Not Duplicate
|
| 66 |
+
("I like reading books.", "She is going for a run."), # Not Duplicate
|
| 67 |
+
]
|
| 68 |
+
for sent1, sent2 in test_cases:
|
| 69 |
+
result = predict_paraphrase(sent1, sent2)
|
| 70 |
+
print(result)
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## π Quantized Model Evaluation Results
|
| 74 |
+
|
| 75 |
+
### π₯ Evaluation Metrics π₯
|
| 76 |
+
|
| 77 |
+
- β
**Accuracy:** 0.7515
|
| 78 |
+
- β
**Precision:** 0.6697
|
| 79 |
+
- β
**Recall:** 0.5840
|
| 80 |
+
- β
**F1-score:** 0.6022
|
| 81 |
+
|
| 82 |
+
## β‘ Quantization Details
|
| 83 |
+
|
| 84 |
+
Post-training quantization was applied using PyTorch's built-in quantization framework. The model was quantized to Float16 (FP16) to reduce model size and improve inference efficiency while balancing accuracy.
|
| 85 |
+
|
| 86 |
+
## π Repository Structure
|
| 87 |
+
|
| 88 |
+
```
|
| 89 |
+
.
|
| 90 |
+
βββ model/ # Contains the quantized model files
|
| 91 |
+
βββ tokenizer_config/ # Tokenizer configuration and vocabulary files
|
| 92 |
+
βββ model.safetensors/ # Quantized Model
|
| 93 |
+
βββ README.md # Model documentation
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
## β οΈ Limitations
|
| 97 |
+
|
| 98 |
+
- The model may struggle with highly nuanced paraphrases.
|
| 99 |
+
- Quantization may lead to slight degradation in accuracy compared to full-precision models.
|
| 100 |
+
- Performance may vary across different domains and sentence structures.
|
| 101 |
+
|
| 102 |
+
## π€ Contributing
|
| 103 |
+
|
| 104 |
+
Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.
|