-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_api.py
More file actions
78 lines (61 loc) · 2.24 KB
/
Copy pathrun_api.py
File metadata and controls
78 lines (61 loc) · 2.24 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
from flask import Flask, request, jsonify
import os
from spam_detec import EmailSpamDetector
# Initialize Flask app
app = Flask(__name__)
# Global detector instance
detector = None
def initialize_model():
"""Initialize the spam detector model."""
global detector
print("Initializing spam detector model...")
detector = EmailSpamDetector()
# Check if we have a fine-tuned model
model_path = './trained_spam_model'
if os.path.exists(model_path):
print(f"Loading fine-tuned model from {model_path}")
detector.load_model(model_path)
else:
print("No fine-tuned model found. Loading pre-trained model...")
detector.load_model()
print("Model initialization complete!")
@app.route('/health', methods=['GET'])
def health_check():
"""Endpoint to check if the service is running."""
return jsonify({"status": "healthy", "model_loaded": detector is not None})
@app.route('/predict', methods=['POST'])
def predict_spam():
"""Endpoint to predict if emails are spam."""
# Check if model is loaded
global detector
if detector is None:
return jsonify({"error": "Model not loaded"}), 500
# Get data from request
data = request.json
if not data or 'emails' not in data:
return jsonify({"error": "Missing 'emails' field in request"}), 400
emails = data['emails']
# Make predictions
try:
results = detector.predict(emails)
return jsonify({"predictions": results})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/test', methods=['GET'])
def test_endpoint():
"""Endpoint with example predictions for testing."""
global detector
if detector is None:
return jsonify({"error": "Model not loaded"}), 500
test_emails = [
"URGENT: You have won a free iPhone, claim now!",
"Meeting rescheduled to 3pm tomorrow"
]
results = detector.predict(test_emails)
return jsonify({"test_predictions": results})
if __name__ == '__main__':
# Initialize the model before starting the server
initialize_model()
# Run the Flask app
print("Starting API server on port 5000...")
app.run(host='0.0.0.0', port=5000, debug=False)