-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster_startup.py
More file actions
354 lines (284 loc) · 12.1 KB
/
master_startup.py
File metadata and controls
354 lines (284 loc) · 12.1 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""
Master Startup Script for Email Spam Detection System
This script orchestrates the entire spam detection pipeline
"""
import os
import sys
import subprocess
import time
import threading
import signal
from pathlib import Path
class SpamDetectionSystem:
def __init__(self):
self.processes = {}
self.system_ready = False
def check_dependencies(self):
"""Check if all required dependencies are installed."""
print("🔍 Checking dependencies...")
required_packages = [
'transformers', 'torch', 'flask', 'sklearn',
'pandas', 'numpy', 'datasets', 'accelerate'
]
missing_packages = []
for package in required_packages:
try:
__import__(package)
except ImportError:
missing_packages.append(package)
if missing_packages:
print(f"❌ Missing packages: {', '.join(missing_packages)}")
print("Run: pip install -r requirements.txt")
return False
print("✅ All dependencies installed")
return True
def check_model_status(self):
"""Check if a trained model exists."""
model_path = Path('./trained_spam_model')
if model_path.exists() and any(model_path.iterdir()):
print("✅ Trained model found")
return True
else:
print("⚠️ No trained model found")
return False
def train_model(self):
"""Train the spam detection model."""
print("\n🎯 Starting model training...")
print("This may take several minutes...")
try:
result = subprocess.run([sys.executable, 'run_training.py'],
capture_output=True, text=True, timeout=3600)
if result.returncode == 0:
print("✅ Model training completed successfully")
return True
else:
print(f"❌ Training failed: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print("❌ Training timed out (1 hour limit)")
return False
except Exception as e:
print(f"❌ Training error: {str(e)}")
return False
def start_api_server(self):
"""Start the basic API server."""
print("🚀 Starting API server...")
try:
process = subprocess.Popen([sys.executable, 'run_api.py'])
self.processes['api'] = process
# Wait a bit for server to start
time.sleep(3)
# Test if server is running
import requests
try:
response = requests.get('http://localhost:5000/health', timeout=5)
if response.status_code == 200:
print("✅ API server started on http://localhost:5000")
return True
except:
pass
print("⚠️ API server may have issues - check manually")
return True
except Exception as e:
print(f"❌ Failed to start API server: {str(e)}")
return False
def start_email_processor(self):
"""Start the email processor with dashboard."""
print("📧 Starting email processor with dashboard...")
try:
process = subprocess.Popen([sys.executable, 'email_processor.py'])
self.processes['processor'] = process
# Wait a bit for server to start
time.sleep(3)
print("✅ Email processor started with dashboard on http://localhost:5001")
return True
except Exception as e:
print(f"❌ Failed to start email processor: {str(e)}")
return False
def show_system_status(self):
"""Display the current system status."""
print("\n" + "="*60)
print("📊 SPAM DETECTION SYSTEM STATUS")
print("="*60)
# Check processes
running_services = []
for service, process in self.processes.items():
if process.poll() is None: # Process is running
running_services.append(service)
print(f"🔄 Running services: {', '.join(running_services) if running_services else 'None'}")
# Show available endpoints
if 'api' in running_services:
print("\n🌐 API Endpoints:")
print(" • Health Check: http://localhost:5000/health")
print(" • Spam Detection: POST http://localhost:5000/predict")
print(" • Test Endpoint: http://localhost:5000/test")
if 'processor' in running_services:
print("\n📊 Email Processing:")
print(" • Dashboard: http://localhost:5001/dashboard")
print(" • Check Email: POST http://localhost:5001/check-email")
print(" • Results API: http://localhost:5001/results")
print("\n📧 Available Tools:")
print(" • Direct email checking: python email_integration.py")
print(" • Manual training: python run_training.py")
print("\n" + "="*60)
def run_interactive_mode(self):
"""Run interactive mode for user choices."""
while True:
print("\n🎮 INTERACTIVE MODE")
print("Choose an option:")
print("1. Check system status")
print("2. Train/retrain model")
print("3. Test API with sample emails")
print("4. Check your personal emails")
print("5. View dashboard")
print("6. Stop all services")
print("7. Exit")
choice = input("\nEnter your choice (1-7): ").strip()
if choice == '1':
self.show_system_status()
elif choice == '2':
self.train_model()
elif choice == '3':
self.test_api()
elif choice == '4':
self.run_email_checker()
elif choice == '5':
import webbrowser
webbrowser.open('http://localhost:5001/dashboard')
print("📊 Dashboard opened in your browser")
elif choice == '6':
self.stop_all_services()
print("🛑 All services stopped")
elif choice == '7':
self.stop_all_services()
print("👋 Goodbye!")
break
else:
print("❌ Invalid choice. Please try again.")
def test_api(self):
"""Test the API with sample emails."""
print("\n🧪 Testing API with sample emails...")
try:
import requests
test_emails = [
"URGENT: You have won a lottery! Claim your $1,000,000 prize now!",
"Meeting rescheduled to tomorrow at 3pm in conference room B",
"Get rich quick! Make money from home with this amazing opportunity!",
"Please review the quarterly report attached to this email"
]
response = requests.post(
'http://localhost:5000/predict',
json={'emails': test_emails},
timeout=30
)
if response.status_code == 200:
results = response.json()['predictions']
print("\n📊 Test Results:")
for result in results:
status = "🚨 SPAM" if result['is_spam'] else "✅ SAFE"
confidence = result['spam_probability'] * 100
email_preview = result['text'][:50] + "..."
print(f"{status} ({confidence:.1f}%) - {email_preview}")
print("\n✅ API test completed successfully")
else:
print(f"❌ API test failed: {response.status_code}")
except Exception as e:
print(f"❌ API test error: {str(e)}")
print("Make sure the API server is running")
def run_email_checker(self):
"""Run the direct email checker."""
print("\n📧 Starting direct email checker...")
print("This will open a new process to check your emails")
try:
subprocess.run([sys.executable, 'email_integration.py'])
except KeyboardInterrupt:
print("\n📧 Email checker stopped")
except Exception as e:
print(f"❌ Email checker error: {str(e)}")
def stop_all_services(self):
"""Stop all running services."""
print("🛑 Stopping all services...")
for service, process in self.processes.items():
try:
process.terminate()
process.wait(timeout=5)
print(f"✅ Stopped {service}")
except:
try:
process.kill()
print(f"🔥 Force stopped {service}")
except:
print(f"⚠️ Could not stop {service}")
self.processes.clear()
def signal_handler(self, signum, frame):
"""Handle system signals for graceful shutdown."""
print("\n🛑 Shutting down system...")
self.stop_all_services()
sys.exit(0)
def run_full_system(self):
"""Run the complete spam detection system."""
print("🚀 STARTING EMAIL SPAM DETECTION SYSTEM")
print("="*50)
# Set up signal handlers
signal.signal(signal.SIGINT, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
# Step 1: Check dependencies
if not self.check_dependencies():
return False
# Step 2: Check/train model
if not self.check_model_status():
print("\n🎯 No trained model found. Starting training...")
if not self.train_model():
print("❌ Cannot proceed without trained model")
return False
# Step 3: Start services
if not self.start_api_server():
print("❌ Failed to start API server")
return False
if not self.start_email_processor():
print("❌ Failed to start email processor")
return False
# Step 4: System ready
self.system_ready = True
print("\n🎉 SYSTEM READY!")
self.show_system_status()
# Step 5: Interactive mode
try:
self.run_interactive_mode()
except KeyboardInterrupt:
self.signal_handler(signal.SIGINT, None)
return True
def main():
"""Main entry point."""
# Change to script directory
script_dir = Path(__file__).parent
os.chdir(script_dir)
system = SpamDetectionSystem()
print("Welcome to the Email Spam Detection System!")
print("\nChoose startup mode:")
print("1. Full system startup (recommended)")
print("2. Training only")
print("3. API server only")
print("4. Email checker only")
choice = input("\nEnter choice (1-4): ").strip()
if choice == '1':
system.run_full_system()
elif choice == '2':
system.train_model()
elif choice == '3':
if system.check_dependencies():
system.start_api_server()
print("API server running. Press Ctrl+C to stop.")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
system.stop_all_services()
elif choice == '4':
system.run_email_checker()
else:
print("Invalid choice. Running full system...")
system.run_full_system()
if __name__ == "__main__":
main()