Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Step 1: Load the model and tokenizer from the local directory
5# (This assumes you ran Cell 18 earlier to save the model)
6model_path = "optimized-bert-model"
7model = AutoModelForSequenceClassification.from_pretrained(model_path)
8tokenizer = AutoTokenizer.from_pretrained(model_path)
9
10# Step 2: Put model in evaluation mode
11model.eval()
12
13# Step 3: Test on simple examples using a helper function
14
15def predict_paraphrase(sentence1, sentence2):
16 """
17 Predicts whether two sentences are paraphrases and returns prediction and confidence.
18 """
19 inputs = tokenizer(sentence1, sentence2, return_tensors="pt",
20 truncation=True, padding=True, max_length=128)
21 with torch.no_grad():
22 outputs = model(**inputs)
23 logits = outputs.logits
24 prediction = torch.argmax(logits, dim=1).item()
25 confidence = torch.softmax(logits, dim=1)[0].max().item()
26 return prediction, confidence
27
28def display_result(example_idx, sentence1, sentence2):
29 prediction, confidence = predict_paraphrase(sentence1, sentence2)
30 print("="*60)
31 print(f"EXAMPLE {example_idx} - Are these paraphrases?")
32 print("="*60)
33 print(f"Sentence 1: {sentence1}")
34 print(f"Sentence 2: {sentence2}")
35 print(f"Prediction: {'YES (paraphrases)' if prediction == 1 else 'NO (not paraphrases)'}")
36 print(f"Confidence: {confidence:.4f}")
37 print()
38
39# Example 1: Two sentences that ARE paraphrases
40sentence1_1 = "The cat is sleeping on the mat"
41sentence2_1 = "The cat is napping on the mat"
42
43display_result(1, sentence1_1, sentence2_1)
44
45# Example 2: Two sentences that are NOT paraphrases
46sentence1_2 = "The dog is barking loudly"
47sentence2_2 = "I love eating pizza"
48
49display_result(2, sentence1_2, sentence2_2)
50
51print("="*60)
52
53# -----------------------
54# Try your own examples!
55# -----------------------
56# Uncomment and edit the sentences below to test your own custom examples:
57# user_sentence1 = "Your first sentence here."
58# user_sentence2 = "Your second sentence here."
59# display_result("USER", user_sentence1, user_sentence2)