-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_enhanced_model.py
More file actions
295 lines (254 loc) Β· 10.8 KB
/
test_enhanced_model.py
File metadata and controls
295 lines (254 loc) Β· 10.8 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
#!/usr/bin/env python3
"""
Enhanced SMS Spam Detection Test Script
Tests the trained machine learning model with various SMS examples
"""
import requests
import json
import time
# Test SMS messages with expected labels
test_cases = [
# Obvious spam messages
{
"text": "WINNER!! As a valued network customer you have been selected to receivea Β£900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only.",
"expected": "spam",
"description": "Classic lottery scam"
},
{
"text": "Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's",
"expected": "spam",
"description": "Competition scam"
},
{
"text": "SIX chances to win CASH! From 100 to 20,000 pounds txt> CSH11 and send to 87575. Cost 150p/day, 6days, 16+ TsandCs apply Reply HL 4 info",
"expected": "spam",
"description": "Cash prize scam"
},
{
"text": "URGENT! You have won a 1 week FREE membership in our Β£100,000 Prize Jackpot! Txt the word: CLAIM to No: 81010 T&C www.dbuk.net LCCLTD POBOX 4403LDNW1A7RW18",
"expected": "spam",
"description": "Prize jackpot scam"
},
{
"text": "XXXMobileMovieClub: To use your credit, click the WAP link in the next txt message or click here>> http://wap. xxxmobilemovieclub.com?n=QJKGIGHJJGCBL",
"expected": "spam",
"description": "Mobile service scam"
},
# Legitimate messages
{
"text": "Hey, how are you doing? Want to grab coffee later?",
"expected": "not spam",
"description": "Casual conversation"
},
{
"text": "I'm gonna be home soon and i don't want to talk about this stuff anymore tonight, k? I've cried enough today.",
"expected": "not spam",
"description": "Personal message"
},
{
"text": "I've been searching for the right words to thank you for this breather. I promise i wont take your help for granted and will fulfil my promise. You have been wonderful and a blessing at all times.",
"expected": "not spam",
"description": "Thank you message"
},
{
"text": "I HAVE A DATE ON SUNDAY WITH WILL!!",
"expected": "not spam",
"description": "Excited personal message"
},
{
"text": "Can you pick up some milk on your way home?",
"expected": "not spam",
"description": "Simple request"
},
# Edge cases
{
"text": "Congratulations! You've won $1000! Click here to claim now!",
"expected": "spam",
"description": "Generic spam template"
},
{
"text": "URGENT: Your account will be closed in 24 hours. Call now to prevent this!",
"expected": "spam",
"description": "Urgency-based spam"
},
{
"text": "Thanks for the meeting today. Let me know if you need anything else.",
"expected": "not spam",
"description": "Professional message"
},
{
"text": "FREE! Win a new iPhone! Text STOP to opt out. Limited time offer!",
"expected": "spam",
"description": "Free offer spam"
},
{
"text": "See you at the gym at 6 PM?",
"expected": "not spam",
"description": "Simple question"
}
]
def test_prediction_api():
"""Test the prediction API endpoint with comprehensive test cases"""
print("π§ͺ Testing Enhanced SMS Spam Detection API")
print("=" * 60)
base_url = "http://localhost:5001"
correct_predictions = 0
total_predictions = len(test_cases)
for i, test_case in enumerate(test_cases, 1):
text = test_case['text']
expected = test_case['expected']
description = test_case['description']
print(f"\nπ± Test {i}: {description}")
print(f" Text: {text[:60]}...")
try:
response = requests.post(
f"{base_url}/predict",
json={"sms_text": text},
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
data = response.json()
predicted = data['label']
confidence = data['confidence']
# Check if prediction is correct
is_correct = predicted == expected
if is_correct:
correct_predictions += 1
# Color coding for output
status_icon = "β
" if is_correct else "β"
result_color = "π’" if predicted == 'not spam' else "π΄"
print(f" {status_icon} Expected: {expected.upper()}")
print(f" {result_color} Predicted: {predicted.upper()} (Confidence: {confidence:.2f})")
if not is_correct:
print(f" β οΈ MISCLASSIFICATION!")
else:
print(f" β Error: {response.status_code}")
except requests.exceptions.ConnectionError:
print(" β Error: Could not connect to server. Make sure the Flask app is running.")
return False
except Exception as e:
print(f" β Error: {e}")
# Calculate accuracy
accuracy = (correct_predictions / total_predictions) * 100
print(f"\nπ RESULTS SUMMARY")
print("=" * 60)
print(f"β
Correct Predictions: {correct_predictions}/{total_predictions}")
print(f"π Accuracy: {accuracy:.1f}%")
if accuracy >= 90:
print("π Excellent performance!")
elif accuracy >= 80:
print("π Good performance!")
elif accuracy >= 70:
print("β οΈ Moderate performance - consider retraining")
else:
print("β Poor performance - model needs improvement")
return True
def test_model_performance():
"""Test model performance with detailed analysis"""
print("\n\nπ DETAILED MODEL ANALYSIS")
print("=" * 60)
base_url = "http://localhost:5001"
# Test different types of messages
spam_examples = [
"Free entry in 2 a wkly comp to win FA Cup final tkts",
"WINNER!! You have been selected to receive Β£900 prize",
"URGENT! Your account will be closed in 24 hours",
"SIX chances to win CASH! From 100 to 20,000 pounds",
"XXXMobileMovieClub: To use your credit, click the WAP link"
]
ham_examples = [
"Hey, how are you doing? Want to grab coffee later?",
"I'm gonna be home soon and i don't want to talk about this",
"Can you pick up some milk on your way home?",
"Thanks for the meeting today. Let me know if you need anything",
"See you at the gym at 6 PM?"
]
print("π΄ SPAM DETECTION TEST:")
spam_correct = 0
for i, text in enumerate(spam_examples, 1):
try:
response = requests.post(f"{base_url}/predict",
json={"sms_text": text},
headers={"Content-Type": "application/json"})
if response.status_code == 200:
data = response.json()
predicted = data['label']
confidence = data['confidence']
is_correct = predicted == 'spam'
if is_correct:
spam_correct += 1
status = "β
" if is_correct else "β"
print(f" {status} Test {i}: {predicted.upper()} (Confidence: {confidence:.2f})")
except:
print(f" β Test {i}: Error")
print(f"\nπ’ HAM DETECTION TEST:")
ham_correct = 0
for i, text in enumerate(ham_examples, 1):
try:
response = requests.post(f"{base_url}/predict",
json={"sms_text": text},
headers={"Content-Type": "application/json"})
if response.status_code == 200:
data = response.json()
predicted = data['label']
confidence = data['confidence']
is_correct = predicted == 'not spam'
if is_correct:
ham_correct += 1
status = "β
" if is_correct else "β"
print(f" {status} Test {i}: {predicted.upper()} (Confidence: {confidence:.2f})")
except:
print(f" β Test {i}: Error")
spam_accuracy = (spam_correct / len(spam_examples)) * 100
ham_accuracy = (ham_correct / len(ham_examples)) * 100
overall_accuracy = ((spam_correct + ham_correct) / (len(spam_examples) + len(ham_examples))) * 100
print(f"\nπ DETAILED ACCURACY:")
print(f" π΄ Spam Detection: {spam_correct}/{len(spam_examples)} ({spam_accuracy:.1f}%)")
print(f" π’ Ham Detection: {ham_correct}/{len(ham_examples)} ({ham_accuracy:.1f}%)")
print(f" π Overall Accuracy: {overall_accuracy:.1f}%")
def test_api_endpoints():
"""Test all API endpoints"""
print("\n\nπ API ENDPOINTS TEST")
print("=" * 60)
base_url = "http://localhost:5001"
# Test messages endpoint
try:
response = requests.get(f"{base_url}/messages")
if response.status_code == 200:
data = response.json()
print(f"β
Messages API: {data['total_count']} messages loaded")
else:
print(f"β Messages API: Error {response.status_code}")
except Exception as e:
print(f"β Messages API: {e}")
# Test stats endpoint
try:
response = requests.get(f"{base_url}/stats")
if response.status_code == 200:
data = response.json()
print(f"β
Stats API: {data['spam_percentage']:.1f}% spam rate")
else:
print(f"β Stats API: Error {response.status_code}")
except Exception as e:
print(f"β Stats API: {e}")
def main():
"""Main test function"""
print("π Enhanced SMS Spam Detection - Comprehensive Test Suite")
print("=" * 70)
print("Testing the trained Random Forest model with 98.92% accuracy")
print("Make sure the Flask application is running on http://localhost:5001")
print("=" * 70)
# Wait a moment for user to start the server
time.sleep(2)
# Run comprehensive tests
if test_prediction_api():
test_model_performance()
test_api_endpoints()
print("\n\nπ All tests completed!")
print("Visit http://localhost:5001 to see the web interface")
print("\nπ‘ The model has been trained on 5,574 real SMS messages")
print(" with 747 spam and 4,827 ham examples for high accuracy!")
else:
print("\nβ Tests failed. Please start the Flask application first.")
if __name__ == "__main__":
main()