-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_inference.py
More file actions
296 lines (238 loc) · 9.3 KB
/
demo_inference.py
File metadata and controls
296 lines (238 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
Interactive Demo: DistilBERT Sentiment Analysis
==============================================
Demonstrates the best-performing model (LR=1e-5) on sample reviews.
Perfect for presentations and live demonstrations.
Student: Martynas Prascevicius
Student ID: 001263199
Course: COMP1818 Artificial Intelligence Applications
"""
import torch
import torch.nn as nn
from transformers import DistilBertTokenizer, DistilBertModel
from pathlib import Path
import json
# Sample reviews for demonstration
SAMPLE_REVIEWS = [
{
"text": "This movie was absolutely fantastic! The acting was superb and the plot kept me engaged from start to finish. Highly recommended!",
"expected": "POSITIVE",
"source": "Custom (clearly positive)"
},
{
"text": "Terrible waste of time. Poor acting, nonsensical plot, and awful special effects. I want my money back!",
"expected": "NEGATIVE",
"source": "Custom (clearly negative)"
},
{
"text": "The cinematography was breathtaking and the soundtrack was amazing, but the story felt rushed and the ending was disappointing.",
"expected": "MIXED",
"source": "Custom (mixed sentiment)"
},
{
"text": "An instant classic! This film will be remembered for generations. The director's vision is truly masterful.",
"expected": "POSITIVE",
"source": "Custom (high praise)"
},
{
"text": "I've seen better films at a middle school talent show. The only good thing about this movie was the popcorn.",
"expected": "NEGATIVE",
"source": "Custom (harsh criticism)"
}
]
class DistilBERTClassifier(nn.Module):
"""DistilBERT model for binary sentiment classification."""
def __init__(self, model_name='distilbert-base-uncased', num_classes=2):
super(DistilBERTClassifier, self).__init__()
self.distilbert = DistilBertModel.from_pretrained(model_name)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(768, num_classes)
def forward(self, input_ids, attention_mask):
outputs = self.distilbert(
input_ids=input_ids,
attention_mask=attention_mask
)
# Use [CLS] token representation
pooled_output = outputs.last_hidden_state[:, 0, :]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
return logits
def load_best_model(model_dir: Path, device: str = 'mps'):
"""
Load the best-performing model (lr_1e5) for inference.
Args:
model_dir: Directory containing saved models
device: Device to load model on ('mps', 'cuda', or 'cpu')
Returns:
Tuple of (model, tokenizer, device)
"""
print("=" * 70)
print("Loading Best Model Configuration")
print("=" * 70)
# Check device availability
if device == 'mps' and not torch.backends.mps.is_available():
print("⚠️ MPS not available, falling back to CPU")
device = 'cpu'
elif device == 'cuda' and not torch.cuda.is_available():
print("⚠️ CUDA not available, falling back to CPU")
device = 'cpu'
device = torch.device(device)
# Load tokenizer
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
# Initialize model
model = DistilBERTClassifier(
model_name='distilbert-base-uncased',
num_classes=2
)
# Load best model weights (lr_1e5)
model_path = model_dir / 'best_model_lr_1e5.pt'
if not model_path.exists():
print(f"\n⚠️ Pre-trained model not found at: {model_path}")
print("Using freshly initialized DistilBERT (will give random predictions)")
print("Note: For actual demo, train the lr_1e5 experiment first!\n")
else:
checkpoint = torch.load(model_path, map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
print(f"\n✓ Loaded best model from: {model_path}")
print(f" Epoch: {checkpoint.get('epoch', 'N/A')}")
print(f" Test Accuracy: {checkpoint.get('test_accuracy', 'N/A'):.2%}")
model.to(device)
model.eval()
print(f" Device: {device}")
print(f" Parameters: {sum(p.numel() for p in model.parameters()):,}")
print()
return model, tokenizer, device
def predict_sentiment(text: str, model, tokenizer, device, max_length=256):
"""
Predict sentiment for a single text.
Args:
text: Input review text
model: Trained DistilBERT model
tokenizer: DistilBERT tokenizer
device: Device to run inference on
max_length: Maximum sequence length
Returns:
Tuple of (predicted_label, confidence)
"""
# Tokenize
encoding = tokenizer(
text,
add_special_tokens=True,
max_length=max_length,
padding='max_length',
truncation=True,
return_tensors='pt'
)
input_ids = encoding['input_ids'].to(device)
attention_mask = encoding['attention_mask'].to(device)
# Predict
with torch.no_grad():
logits = model(input_ids, attention_mask)
probs = torch.softmax(logits, dim=1)
confidence, predicted = torch.max(probs, dim=1)
label = "POSITIVE" if predicted.item() == 1 else "NEGATIVE"
confidence_pct = confidence.item() * 100
return label, confidence_pct, probs[0].cpu().numpy()
def run_demo():
"""Run interactive sentiment analysis demo."""
project_root = Path(__file__).parent
model_dir = project_root / 'models'
print("\n")
print("╔" + "=" * 68 + "╗")
print("║" + " " * 15 + "DistilBERT Sentiment Analysis Demo" + " " * 19 + "║")
print("║" + " " * 20 + "COMP1818 - CW2 Presentation" + " " * 21 + "║")
print("╚" + "=" * 68 + "╝")
print()
# Load model
model, tokenizer, device = load_best_model(model_dir, device='mps')
print("=" * 70)
print("Running Inference on Sample Reviews")
print("=" * 70)
print()
# Analyze sample reviews
results = []
for i, sample in enumerate(SAMPLE_REVIEWS, 1):
print(f"Review {i}/{len(SAMPLE_REVIEWS)}")
print("-" * 70)
print(f"Text: \"{sample['text'][:80]}...\"" if len(sample['text']) > 80 else f"Text: \"{sample['text']}\"")
print(f"Expected: {sample['expected']}")
label, confidence, probs = predict_sentiment(
sample['text'], model, tokenizer, device
)
print(f"Predicted: {label} ({confidence:.1f}% confidence)")
print(f"Probabilities: Negative={probs[0]:.1%}, Positive={probs[1]:.1%}")
# Determine correctness
correct = "✓" if (label == sample['expected'] or sample['expected'] == "MIXED") else "✗"
print(f"Status: {correct}")
print()
results.append({
'text': sample['text'],
'expected': sample['expected'],
'predicted': label,
'confidence': confidence,
'correct': correct == "✓"
})
# Summary
print("=" * 70)
print("Demo Summary")
print("=" * 70)
total = len(results)
correct = sum(1 for r in results if r['correct'])
avg_confidence = sum(r['confidence'] for r in results) / total
print(f"Total Samples: {total}")
print(f"Correct Predictions: {correct}/{total} ({correct/total:.1%})")
print(f"Average Confidence: {avg_confidence:.1f}%")
print()
print("Key Findings from Full Experiments:")
print("-" * 70)
print(" ✓ Best Configuration: LR=1e-5, Batch=16, Epochs=3")
print(" ✓ Test Accuracy: 91.04% (25,000 IMDB reviews)")
print(" ✓ Improvement: +0.27% vs. baseline (2e-5 learning rate)")
print(" ✓ Training Time: 160 minutes (Mac M4)")
print()
print("=" * 70)
print("Demo Complete!")
print("=" * 70)
print()
def interactive_mode():
"""Interactive mode for custom review input."""
project_root = Path(__file__).parent
model_dir = project_root / 'models'
print("\n")
print("╔" + "=" * 68 + "╗")
print("║" + " " * 10 + "Interactive DistilBERT Sentiment Analyzer" + " " * 17 + "║")
print("╚" + "=" * 68 + "╝")
print()
# Load model
model, tokenizer, device = load_best_model(model_dir, device='mps')
print("Enter movie reviews to analyze (or 'quit' to exit)")
print("-" * 70)
print()
while True:
try:
user_input = input("Review: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
print("\nThank you for using the demo!")
break
if not user_input:
print("Please enter a review.\n")
continue
label, confidence, probs = predict_sentiment(
user_input, model, tokenizer, device
)
print(f"\nPrediction: {label} ({confidence:.1f}% confidence)")
print(f"Probabilities: Negative={probs[0]:.1%}, Positive={probs[1]:.1%}")
print()
except KeyboardInterrupt:
print("\n\nDemo interrupted. Goodbye!")
break
except Exception as e:
print(f"\nError: {e}")
print("Please try again.\n")
if __name__ == '__main__':
import sys
if len(sys.argv) > 1 and sys.argv[1] == '--interactive':
interactive_mode()
else:
run_demo()
print("\nTip: Run 'python demo_inference.py --interactive' for custom reviews!")