File size: 1,520 Bytes
c7960cc
 
03014fb
c7960cc
 
 
03014fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7960cc
 
 
03014fb
c7960cc
 
03014fb
c7960cc
03014fb
 
 
 
c7960cc
03014fb
 
 
 
 
 
 
 
 
 
 
 
c7960cc
 
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
from flask import Flask, render_template, request
import joblib
import os

app = Flask(__name__)

# -------------------------------
# Paths
# -------------------------------
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "EmailSpamdetection.joblib")

# -------------------------------
# Load the model
# -------------------------------
# This model MUST be a pipeline:
# (vectorizer + MultinomialNB)
try:
    model = joblib.load(MODEL_PATH)
except Exception as e:
    print("Error loading model:", e)
    model = None

# -------------------------------
# Flask Routes
# -------------------------------
@app.route("/", methods=["GET", "POST"])
def index():
    prediction = None
    email_text = ""

    if request.method == "POST":
        email_text = request.form.get("email")

        if not model:
            prediction = "❌ Model not loaded"
        elif not email_text.strip():
            prediction = "❌ Please enter email text"
        else:
            # Transform & predict using pipeline
            try:
                result = model.predict([email_text])[0]
                prediction = "🚫 Spam Email" if result == 1 else "✅ Not Spam Email"
            except Exception as e:
                prediction = f"❌ Prediction Error: {e}"

    return render_template("index.html", prediction=prediction, email_text=email_text)

# -------------------------------
# Run App
# -------------------------------
if __name__ == "__main__":
    app.run(debug=True)