Rahul23232's picture
Update app.py
03014fb verified
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)