-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
275 lines (229 loc) · 8.38 KB
/
app.py
File metadata and controls
275 lines (229 loc) · 8.38 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# app.py
import os
import pickle
import joblib
import numpy.random._pickle as np_pickle
from datetime import timedelta
from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify
from models import db, User, Prediction
from ml_utils import prepare_features, get_tips
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(
__name__,
template_folder=os.path.join(BASE_DIR, "Flask", "templates"),
static_folder=os.path.join(BASE_DIR, "Flask", "static")
)
database_url = os.environ.get("DATABASE_URL")
if database_url:
database_url = database_url.replace("postgres://", "postgresql://", 1)
else:
database_url = "sqlite:///" + os.path.join(BASE_DIR, "hemosense.db")
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.secret_key = os.environ.get("HEMO_SECRET", "change_this_secret_please")
db.init_app(app)
app.permanent_session_lifetime = timedelta(minutes=60)
# load model
MODEL_CANDIDATES = [
os.path.join(BASE_DIR, "model.pkl"),
os.path.join(BASE_DIR, "trained_model.pkl"),
]
MODEL_PATH = next((p for p in MODEL_CANDIDATES if os.path.exists(p)), None)
if not MODEL_PATH:
raise FileNotFoundError(
f"Model file not found. Expected one of: {', '.join(MODEL_CANDIDATES)}"
)
try:
# Compatibility for legacy pickles that store a BitGenerator class object.
_orig_bitgen_ctor = np_pickle.__bit_generator_ctor
def _patched_bitgen_ctor(bit_generator_name='MT19937'):
if isinstance(bit_generator_name, type):
bit_generator_name = bit_generator_name.__name__
return _orig_bitgen_ctor(bit_generator_name)
np_pickle.__bit_generator_ctor = _patched_bitgen_ctor
with open(MODEL_PATH, "rb") as f:
ml_model = pickle.load(f)
except Exception:
ml_model = joblib.load(MODEL_PATH)
print(f"✅ Loaded model from {os.path.basename(MODEL_PATH)}")
# HOME
@app.route("/")
def index():
return render_template("index.html")
# AUTH (combined login/register)
@app.route("/auth")
def auth():
return render_template("auth.html")
@app.route("/register", methods=["POST"])
def register():
name = request.form.get("name", "").strip()
email = request.form.get("email", "").strip().lower()
password = request.form.get("password", "")
if not name or not email or not password:
flash("Please fill all fields.", "error")
return redirect(url_for("auth"))
existing = User.query.filter_by(email=email).first()
if existing:
flash("Email already registered. Please login.", "error")
return redirect(url_for("auth"))
user = User(name=name, email=email, password=password)
db.session.add(user)
db.session.commit()
flash("Registration successful. Please login.", "success")
return redirect(url_for("auth"))
@app.route("/login", methods=["POST"])
def login():
email = request.form.get("email", "").strip().lower()
password = request.form.get("password", "")
user = User.query.filter_by(email=email).first()
if user and user.password == password:
session.permanent = True
session["user"] = {"id": user.id, "name": user.name, "email": user.email}
flash(f"Welcome back, {user.name}!", "success")
return redirect(url_for("dashboard"))
else:
flash("Invalid email/password.", "error")
return redirect(url_for("auth"))
@app.route("/logout")
def logout():
session.pop("user", None)
flash("Logged out.", "info")
return redirect(url_for("index"))
# PROFILE
@app.route("/profile")
def profile():
if "user" not in session:
flash("Please login to continue.", "error")
return redirect(url_for("auth"))
user = User.query.filter_by(email=session["user"]["email"]).first()
return render_template("profile.html", user=user)
# CHANGE PASSWORD
@app.route("/change_password", methods=["GET", "POST"])
def change_password():
if "user" not in session:
flash("Please login to continue.", "error")
return redirect(url_for("auth"))
user = User.query.filter_by(email=session["user"]["email"]).first()
if request.method == "POST":
old = request.form.get("old_password")
new = request.form.get("new_password")
confirm = request.form.get("confirm_new")
if old != user.password:
flash("Old password incorrect.", "error")
elif new != confirm:
flash("New passwords do not match.", "error")
else:
user.password = new
db.session.commit()
flash("Password updated.", "success")
return redirect(url_for("profile"))
return render_template("change_password.html", user=user)
# DASHBOARD
@app.route("/dashboard")
def dashboard():
if "user" not in session:
flash("Please login to continue.", "error")
return redirect(url_for("auth"))
email = session["user"]["email"]
user = User.query.filter_by(email=email).first()
preds = Prediction.query.filter_by(user_email=email).order_by(Prediction.timestamp).all()
total_predictions = len(preds)
recent_predictions = preds[-6:] if preds else []
category_counts = {"Normal": 0, "Anemia": 0}
hb_ranges = {"<7": 0, "7-9.9": 0, "10-12": 0, ">12": 0}
for p in preds:
category_counts[p.category] = category_counts.get(p.category, 0) + 1
hb = p.hb
if hb < 7:
hb_ranges["<7"] += 1
elif hb < 10:
hb_ranges["7-9.9"] += 1
elif hb <= 12:
hb_ranges["10-12"] += 1
else:
hb_ranges[">12"] += 1
return render_template(
"dashboard.html",
user_name=user.name,
total_predictions=total_predictions,
recent_predictions=recent_predictions,
category_counts=category_counts,
hb_ranges=hb_ranges
)
# PREDICT
@app.route("/predict", methods=["GET", "POST"])
def predict():
if "user" not in session:
flash("Please login to continue.", "error")
return redirect(url_for("auth"))
result_category = None
confidence = None
tips = []
form_data = {}
if request.method == "POST":
form_data = request.form.to_dict()
try:
age = int(request.form.get("age", 0))
except:
age = 0
gender = request.form.get("gender", "")
try:
hb = float(request.form.get("hb", 0))
except:
hb = 0.0
mch = request.form.get("mch", "")
mchc = request.form.get("mchc", "")
mcv = request.form.get("mcv", "")
if age <= 0 or not gender or hb <= 0:
flash("Please enter valid Age, Gender and Hemoglobin.", "error")
else:
X = prepare_features(age, gender, hb, mch, mchc, mcv)
try:
pred = ml_model.predict(X)[0]
except Exception as e:
flash(f"Model prediction error: {e}", "error")
return redirect(url_for("predict"))
# Try to get probabilities if available
try:
prob = ml_model.predict_proba(X)[0]
confidence = round(float(max(prob) * 100), 2)
except:
confidence = None
label_map = {0: "Normal", 1: "Anemia"}
result_category = label_map.get(pred, str(pred))
tips = get_tips(result_category)
# save
saved = Prediction(
user_email=session["user"]["email"],
age=age,
gender=gender,
hb=hb,
mch=float(mch) if mch else None,
mchc=float(mchc) if mchc else None,
mcv=float(mcv) if mcv else None,
category=result_category,
confidence=confidence
)
db.session.add(saved)
db.session.commit()
flash("Prediction completed.", "success")
return render_template(
"predict.html",
result_category=result_category,
confidence=confidence,
tips=tips,
form_data=form_data
)
# Simple API to return tip categories (optional)
@app.route("/api/tips/<category>")
def api_tips(category):
return jsonify(get_tips(category))
# Create DB tables if needed
with app.app_context():
db.create_all()
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=int(os.environ.get("PORT", 5000)),
debug=os.environ.get("FLASK_DEBUG", "1") == "1",
)