Rahul23232 commited on
Commit
03014fb
·
verified ·
1 Parent(s): 4aa09c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -13
app.py CHANGED
@@ -1,27 +1,53 @@
1
  from flask import Flask, render_template, request
2
  import joblib
 
3
 
4
  app = Flask(__name__)
5
 
6
- # Load trained model
7
- model = joblib.load("EmailSpamdetection.joblib")
8
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  @app.route("/", methods=["GET", "POST"])
10
  def index():
11
  prediction = None
 
12
 
13
  if request.method == "POST":
14
- email_text = request.form["email"]
15
-
16
- # Model expects list-like input
17
- result = model.predict([email_text])[0]
18
 
19
- if result == 1:
20
- prediction = "🚫 Spam Email"
 
 
21
  else:
22
- prediction = "✅ Not Spam Email"
23
-
24
- return render_template("index.html", prediction=prediction)
25
-
 
 
 
 
 
 
 
 
26
  if __name__ == "__main__":
27
  app.run(debug=True)
 
1
  from flask import Flask, render_template, request
2
  import joblib
3
+ import os
4
 
5
  app = Flask(__name__)
6
 
7
+ # -------------------------------
8
+ # Paths
9
+ # -------------------------------
10
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
11
+ MODEL_PATH = os.path.join(BASE_DIR, "EmailSpamdetection.joblib")
12
+
13
+ # -------------------------------
14
+ # Load the model
15
+ # -------------------------------
16
+ # This model MUST be a pipeline:
17
+ # (vectorizer + MultinomialNB)
18
+ try:
19
+ model = joblib.load(MODEL_PATH)
20
+ except Exception as e:
21
+ print("Error loading model:", e)
22
+ model = None
23
+
24
+ # -------------------------------
25
+ # Flask Routes
26
+ # -------------------------------
27
  @app.route("/", methods=["GET", "POST"])
28
  def index():
29
  prediction = None
30
+ email_text = ""
31
 
32
  if request.method == "POST":
33
+ email_text = request.form.get("email")
 
 
 
34
 
35
+ if not model:
36
+ prediction = " Model not loaded"
37
+ elif not email_text.strip():
38
+ prediction = "❌ Please enter email text"
39
  else:
40
+ # Transform & predict using pipeline
41
+ try:
42
+ result = model.predict([email_text])[0]
43
+ prediction = "🚫 Spam Email" if result == 1 else "✅ Not Spam Email"
44
+ except Exception as e:
45
+ prediction = f"❌ Prediction Error: {e}"
46
+
47
+ return render_template("index.html", prediction=prediction, email_text=email_text)
48
+
49
+ # -------------------------------
50
+ # Run App
51
+ # -------------------------------
52
  if __name__ == "__main__":
53
  app.run(debug=True)