============================================================
SIMPLE EXAMPLE: How to Use Your Trained Model
============================================================
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model_path = "optimized-bert-model"
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
model.eval()
def predict_paraphrase(sentence1, sentence2):
"""
Predicts whether two sentences are paraphrases and returns prediction and confidence.
"""
inputs = tokenizer(sentence1, sentence2, return_tensors="pt",
truncation=True, padding=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
prediction = torch.argmax(logits, dim=1).item()
confidence = torch.softmax(logits, dim=1)[0].max().item()
return prediction, confidence
def display_result(example_idx, sentence1, sentence2):
prediction, confidence = predict_paraphrase(sentence1, sentence2)
print("="*60)
print(f"EXAMPLE {example_idx} - Are these paraphrases?")
print("="*60)
print(f"Sentence 1: {sentence1}")
print(f"Sentence 2: {sentence2}")
print(f"Prediction: {'YES (paraphrases)' if prediction == 1 else 'NO (not paraphrases)'}")
print(f"Confidence: {confidence:.4f}")
print()
sentence1_1 = "The cat is sleeping on the mat"
sentence2_1 = "The cat is napping on the mat"
display_result(1, sentence1_1, sentence2_1)
sentence1_2 = "The dog is barking loudly"
sentence2_2 = "I love eating pizza"
display_result(2, sentence1_2, sentence2_2)
print("="*60)
------------------------------------------------------------
How to call/use this model:
------------------------------------------------------------
1. Make sure you have the saved model files in the directory 'optimized-bert-model'
2. Run this script in your Python environment (with 'transformers' and 'torch' installed)
3. Change the example sentences inside the code block above to your own inputs to test paraphrase detection
4. The script prints whether the sentences are paraphrases and gives a confidence score
Sample Output:
============================================================
EXAMPLE 1 - Are these paraphrases?
============================================================
Sentence 1: The cat is sleeping on the mat
Sentence 2: The cat is napping on the mat
Prediction: YES (paraphrases)
Confidence: 0.9998
============================================================
EXAMPLE 2 - Are these paraphrases?
============================================================
Sentence 1: The dog is barking loudly
Sentence 2: I love eating pizza
Prediction: NO (not paraphrases)
Confidence: 0.9584
============================================================