-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.py
More file actions
45 lines (34 loc) · 1.3 KB
/
App.py
File metadata and controls
45 lines (34 loc) · 1.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
from flask import Flask, request, render_template
import joblib
# Load model and vectorizers
model = joblib.load('model.pkl')
vectorizer = joblib.load('vector.pkl')
tfidf = joblib.load('tf.pkl')
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
message = request.form['message']
data = [message]
# Transform the input text
vect = vectorizer.transform(data)
tfidf_data = tfidf.transform(vect)
# Get prediction and confidence
prediction = model.predict(tfidf_data)[0]
#probabilities = model.predict_proba(tfidf_data)[0]
#confidence = round(max(probabilities) * 100, 2)
# Identify important words
feature_names = vectorizer.get_feature_names_out()
word_weights = tfidf_data.toarray()[0]
top_indices = word_weights.argsort()[-5:][::-1] # top 5 words
top_words = [(feature_names[i], round(word_weights[i], 4)) for i in top_indices if word_weights[i] > 0]
# Format output
if prediction == 1:
result = f"🚫 Spam Email "
else:
result = f"✅ Legitimate (Ham) "
return render_template('index.html', prediction=result, top_words=top_words)
if __name__ == '__main__':
app.run(debug=True)