Update README.md
Browse filesupdate readme file
README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
# SIMPLE EXAMPLE: How to Use Your Trained Model
|
| 3 |
# ============================================================
|
| 4 |
|
|
|
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
import torch
|
| 7 |
|
|
@@ -14,69 +15,77 @@ tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
| 14 |
# Step 2: Put model in evaluation mode
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
-
# Step 3: Test on a
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Example 1: Two sentences that ARE paraphrases
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
inputs = tokenizer(sentence1, sentence2, return_tensors="pt",
|
| 27 |
-
truncation=True, padding=True, max_length=128)
|
| 28 |
-
|
| 29 |
-
# Step 4: Make prediction
|
| 30 |
-
with torch.no_grad():
|
| 31 |
-
outputs = model(**inputs)
|
| 32 |
-
logits = outputs.logits
|
| 33 |
-
prediction = torch.argmax(logits, dim=1).item()
|
| 34 |
-
|
| 35 |
-
print("="*60)
|
| 36 |
-
print("EXAMPLE 1 - Are these paraphrases?")
|
| 37 |
-
print("="*60)
|
| 38 |
-
print(f"Sentence 1: {sentence1}")
|
| 39 |
-
print(f"Sentence 2: {sentence2}")
|
| 40 |
-
print(f"Prediction: {'YES (paraphrases)' if prediction == 1 else 'NO (not paraphrases)'}")
|
| 41 |
-
print(f"Confidence: {torch.softmax(logits, dim=1)[0].max().item():.4f}")
|
| 42 |
-
print()
|
| 43 |
|
| 44 |
# Example 2: Two sentences that are NOT paraphrases
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
truncation=True, padding=True, max_length=128)
|
| 50 |
|
| 51 |
-
with torch.no_grad():
|
| 52 |
-
outputs = model(**inputs)
|
| 53 |
-
logits = outputs.logits
|
| 54 |
-
prediction = torch.argmax(logits, dim=1).item()
|
| 55 |
-
|
| 56 |
-
print("="*60)
|
| 57 |
-
print("EXAMPLE 2 - Are these paraphrases?")
|
| 58 |
-
print("="*60)
|
| 59 |
-
print(f"Sentence 1: {sentence1}")
|
| 60 |
-
print(f"Sentence 2: {sentence2}")
|
| 61 |
-
print(f"Prediction: {'YES (paraphrases)' if prediction == 1 else 'NO (not paraphrases)'}")
|
| 62 |
-
print(f"Confidence: {torch.softmax(logits, dim=1)[0].max().item():.4f}")
|
| 63 |
print("="*60)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
# SIMPLE EXAMPLE: How to Use Your Trained Model
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
+
```python
|
| 6 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 7 |
import torch
|
| 8 |
|
|
|
|
| 15 |
# Step 2: Put model in evaluation mode
|
| 16 |
model.eval()
|
| 17 |
|
| 18 |
+
# Step 3: Test on simple examples using a helper function
|
| 19 |
+
|
| 20 |
+
def predict_paraphrase(sentence1, sentence2):
|
| 21 |
+
"""
|
| 22 |
+
Predicts whether two sentences are paraphrases and returns prediction and confidence.
|
| 23 |
+
"""
|
| 24 |
+
inputs = tokenizer(sentence1, sentence2, return_tensors="pt",
|
| 25 |
+
truncation=True, padding=True, max_length=128)
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
outputs = model(**inputs)
|
| 28 |
+
logits = outputs.logits
|
| 29 |
+
prediction = torch.argmax(logits, dim=1).item()
|
| 30 |
+
confidence = torch.softmax(logits, dim=1)[0].max().item()
|
| 31 |
+
return prediction, confidence
|
| 32 |
+
|
| 33 |
+
def display_result(example_idx, sentence1, sentence2):
|
| 34 |
+
prediction, confidence = predict_paraphrase(sentence1, sentence2)
|
| 35 |
+
print("="*60)
|
| 36 |
+
print(f"EXAMPLE {example_idx} - Are these paraphrases?")
|
| 37 |
+
print("="*60)
|
| 38 |
+
print(f"Sentence 1: {sentence1}")
|
| 39 |
+
print(f"Sentence 2: {sentence2}")
|
| 40 |
+
print(f"Prediction: {'YES (paraphrases)' if prediction == 1 else 'NO (not paraphrases)'}")
|
| 41 |
+
print(f"Confidence: {confidence:.4f}")
|
| 42 |
+
print()
|
| 43 |
|
| 44 |
# Example 1: Two sentences that ARE paraphrases
|
| 45 |
+
sentence1_1 = "The cat is sleeping on the mat"
|
| 46 |
+
sentence2_1 = "The cat is napping on the mat"
|
| 47 |
|
| 48 |
+
display_result(1, sentence1_1, sentence2_1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Example 2: Two sentences that are NOT paraphrases
|
| 51 |
+
sentence1_2 = "The dog is barking loudly"
|
| 52 |
+
sentence2_2 = "I love eating pizza"
|
| 53 |
|
| 54 |
+
display_result(2, sentence1_2, sentence2_2)
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
print("="*60)
|
| 57 |
|
| 58 |
+
# -----------------------
|
| 59 |
+
# Try your own examples!
|
| 60 |
+
# -----------------------
|
| 61 |
+
# Uncomment and edit the sentences below to test your own custom examples:
|
| 62 |
+
# user_sentence1 = "Your first sentence here."
|
| 63 |
+
# user_sentence2 = "Your second sentence here."
|
| 64 |
+
# display_result("USER", user_sentence1, user_sentence2)
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
# ------------------------------------------------------------
|
| 68 |
+
# How to call/use this model:
|
| 69 |
+
# ------------------------------------------------------------
|
| 70 |
+
# 1. Make sure you have the saved model files in the directory 'optimized-bert-model'
|
| 71 |
+
# 2. Run this script in your Python environment (with 'transformers' and 'torch' installed)
|
| 72 |
+
# 3. Change the example sentences inside the code block above to your own inputs to test paraphrase detection
|
| 73 |
+
# 4. The script prints whether the sentences are paraphrases and gives a confidence score
|
| 74 |
+
|
| 75 |
+
# Sample Output:
|
| 76 |
+
# ============================================================
|
| 77 |
+
# EXAMPLE 1 - Are these paraphrases?
|
| 78 |
+
# ============================================================
|
| 79 |
+
# Sentence 1: The cat is sleeping on the mat
|
| 80 |
+
# Sentence 2: The cat is napping on the mat
|
| 81 |
+
# Prediction: YES (paraphrases)
|
| 82 |
+
# Confidence: 0.9998
|
| 83 |
+
#
|
| 84 |
+
# ============================================================
|
| 85 |
+
# EXAMPLE 2 - Are these paraphrases?
|
| 86 |
+
# ============================================================
|
| 87 |
+
# Sentence 1: The dog is barking loudly
|
| 88 |
+
# Sentence 2: I love eating pizza
|
| 89 |
+
# Prediction: NO (not paraphrases)
|
| 90 |
+
# Confidence: 0.9584
|
| 91 |
+
# ============================================================
|