Spaces:
Sleeping
Sleeping
| 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 | |
| # ------------------------------- | |
| 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) | |