Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
583 lines (536 loc) · 23.7 KB
/
Copy pathapp.py
File metadata and controls
583 lines (536 loc) · 23.7 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import base64
import hashlib
import pickle
import secrets
import sqlite3
from datetime import datetime
import joblib
import numpy as np
import pandas as pd
import streamlit as st
from streamlit_option_menu import option_menu
try:
from tensorflow.keras.models import load_model
_HAS_TENSORFLOW = True
except ImportError:
load_model = None
_HAS_TENSORFLOW = False
# Streamlit App
st.set_page_config(page_title="GDM Prediction", layout="wide")
# st.title("Welcome to GDM Prediction Portal")
# Cache the loading of models and scalers for performance optimization
@st.cache_resource
def load_resources():
ensemble_model = pickle.load(open('sources/ensemble_model.pkl', 'rb'))
if _HAS_TENSORFLOW:
cnn_model = load_model('sources/cnn_model.h5')
else:
cnn_model = None
scaler = joblib.load('sources/scaler.pkl')
return ensemble_model, cnn_model, scaler
ensemble_model, cnn_model, scaler = load_resources()
# Database setup with context manager for better resource handling
def init_db():
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT,
last_login TEXT,
session_token TEXT
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS history (
username TEXT,
input_data TEXT,
result TEXT,
timestamp TEXT
)
''')
conn.commit()
init_db()
def get_base64(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
# Utility Functions
def hash_password(password):
salt = secrets.token_hex(8) # Add a unique salt for each password
hashed = hashlib.sha256((salt + password).encode()).hexdigest()
return f"{salt}${hashed}" # Store salt alongside the hash
def verify_password(stored_password, provided_password):
salt, hashed = stored_password.split('$')
return hashlib.sha256((salt + provided_password).encode()).hexdigest() == hashed
def authenticate(username, password):
"""Authenticate user credentials."""
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
c.execute("SELECT password FROM users WHERE username = ?", (username,))
result = c.fetchone()
return result and verify_password(result[0], password)
def signup(username, password):
"""Sign up a new user."""
hashed_password = hash_password(password)
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
try:
c.execute(
"INSERT INTO users (username, password, last_login, session_token) VALUES (?, ?, ?, ?)",(username, hashed_password, datetime.now().isoformat(), None)
)
conn.commit()
return True
except sqlite3.IntegrityError:
return False
def generate_session_token():
"""Generate a unique session token."""
return secrets.token_hex(16)
def update_login_time(username, session_token):
"""Update login time and store session token."""
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
c.execute("UPDATE users SET last_login = ?, session_token = ? WHERE username = ?", (datetime.now().isoformat(), session_token, username))
conn.commit()
def get_user_by_session_token(session_token):
"""Retrieve user by session token."""
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
c.execute("SELECT username FROM users WHERE session_token = ?", (session_token,))
user = c.fetchone()
return user[0] if user else None
def clear_session_token(username):
"""Clear the session token for logout."""
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
c.execute("UPDATE users SET session_token = NULL WHERE username = ?", (username,))
conn.commit()
def predict_combined(input_data, ensemble_weight=0.3, cnn_weight=0.7):
"""Predict GDM using the ensemble and CNN models (CNN only if TensorFlow is available)."""
input_data_scaled = scaler.transform(input_data)
ensemble_pred_proba = ensemble_model.predict_proba(input_data_scaled)[0][1]
if cnn_model is not None:
cnn_input = input_data_scaled[..., np.newaxis]
cnn_pred_proba = cnn_model.predict(cnn_input, verbose=0)[0][0]
combined_proba = (ensemble_weight * ensemble_pred_proba) + (cnn_weight * cnn_pred_proba)
return {
"ensemble_proba": ensemble_pred_proba,
"cnn_proba": cnn_pred_proba,
"combined_proba": combined_proba,
"final_prediction": "GDM" if combined_proba > 0.5 else "Non-GDM",
}
# Fallback: ensemble only when TensorFlow is not installed
return {
"ensemble_proba": ensemble_pred_proba,
"cnn_proba": None,
"combined_proba": ensemble_pred_proba,
"final_prediction": "GDM" if ensemble_pred_proba > 0.5 else "Non-GDM",
}
def set_background(png_file):
bin_str = get_base64(png_file)
page_bg_img = '''
<style>
.stApp {
background-image: url("data:image/png;base64,%s");
background-position: center;
background-size: cover;
margin-top:10px;
margin-bottom:10px;
padding: 10px;
width: auto;
border-radius: 10px;
font-size: 20px;
}
</style>
''' % bin_str
st.markdown(page_bg_img, unsafe_allow_html=True)
set_background('image.png')
def boxed_text(text):
st.markdown(
f"""
<div style="
border: 2px solid #d3d3d3;
border-radius: 10px;
background-color: #f0f0f0;
padding: 15px;
font-size: 20px;
font-family: 'Times New Roman', serif;
color: black;">
{text}
</div>
""",
unsafe_allow_html=True
)
def home():
"""Home page."""
st.markdown("<font size='32' color='Black' font-weight='bold' face='Times New Roman'>Gestational Diabetes Detection</font>", unsafe_allow_html=True)
st.subheader("Introduction to Gestational Diabetes")
boxed_text("Gestational Diabetes Mellitus (GDM) poses severe health risks to both mother and child, making early detection crucial to preventing complications. Traditional diagnostic methods, such as the Oral Glucose Tolerance Test (OGTT), are time-consuming and resource-intensive, highlighting the need for a more efficient, AI-driven predictive system. This project leverages deep learning and ensemble machine learning models to enhance early-stage GDM prediction, facilitating timely intervention and improved maternal healthcare outcomes.")
st.subheader("Why Early Diagnosis Matters")
boxed_text("Early detection of Gestational Diabetes Mellitus (GDM) is crucial in preventing severe health risks for both the mother and baby. If left undiagnosed or unmanaged, GDM can lead to complications such as preeclampsia, high blood pressure, and an increased likelihood of developing Type 2 diabetes in the mother. It can also cause excessive fetal growth (macrosomia), which raises the risk of birth injuries and the need for a cesarean section. For the baby, uncontrolled GDM can result in neonatal hypoglycemia, respiratory distress, and a higher chance of obesity and diabetes later in life. Early diagnosis allows for timely medical intervention, including lifestyle modifications, dietary adjustments, and, if necessary, insulin therapy to maintain stable blood sugar levels. Additionally, it reduces the need for intensive medical care, lowering the risk of neonatal intensive care unit (NICU) admissions and overall healthcare costs. By implementing early screening and AI-driven predictive models, healthcare providers can enhance maternal and fetal outcomes, ensuring a healthier pregnancy and long-term well-being for both mother and child.")
st.subheader("Our Mission")
boxed_text("The goal of this project is to create a machine learning-based GDM detection system that can forecast GDM risk and offer tailored suggestions. Our solution uses historical data and machine learning algorithms to increase early detection rates, improve patient engagement, and lower problems associated with GDM. A novel ensemble-based deep learning framework for early GDM prediction is proposed in this paper. Traditional classifiers like Support Vector Machines (SVM), Random Forests (RF), and Gradient Boosting Machines (GBM) are combined with Convolutional Neural Networks (CNN) in this model. The study uses the SMOTE-ENN technique to address class imbalance and ensure that GDM situations are fairly represented. This project intends to create a reliable and scalable prediction model for early GDM identification with sophisticated machine learning techniques. This strategy can enhance maternal and newborn health outcomes, decrease problems, and ensure prompt interventions.")
def aboutus():
st.subheader("About Us")
boxed_text(" P. Kavya Sri (21501A05E5), mail: 21501a05e5@pvpsit.ac.in")
st.write("")
boxed_text("K. Venkata Nitchaya Reddy (21501A05J3), mail: 21501a05j3@pvpsit.ac.in")
st.write("")
boxed_text("R. Chaitanya Reddy (21501A05F0), mail: 21501a05f0@pvpsit.ac.in")
st.write("")
boxed_text("P. Naveen Kumar (21501A05D3), mail: 21501a05d3@pvpsit.ac.in")
st.write("")
boxed_text("R. Pavan (21501A05E6), mail: 21501a05e6@pvpsit.ac.in")
st.write("")
boxed_text("Department of Computer Science and Engineering, Prasad V. Potluri Siddhartha Institute of Technology, (Permanently affiliated to JNTU-Kakinada, Approved by AICTE) (An NBA & NAAC accredited and ISO 9001:2005 certified institute) Kanuru, Vijayawada-520 007")
def login():
"""Login function."""
st.subheader("Login")
st.markdown('''
<style>
.stApp {
color: black;
text-align: center;
}
}''',
unsafe_allow_html=True)
with st.form(key='myform', clear_on_submit=True):
css="""
<style>
[data-testid="stForm"] {
background-color: #E6B2BA;
color:black;
padding: 20px;
border-radius: 10px;
font-size: 16px;
margin:10px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
text-align: center;
transition: background-color 0.5s ease;
cursor: pointer;
width: 50%;
margin: auto;
}
</style>
"""
st.write(css, unsafe_allow_html=True)
username = st.text_input("Username")
password = st.text_input("Password", type="password")
submit_button = st.form_submit_button(label='Login')
if submit_button:
user = authenticate(username, password)
if user:
session_token = generate_session_token()
update_login_time(username, session_token)
st.session_state.username = username
st.session_state.session_token = session_token
st.query_params['session_token'] = session_token
#st.query_params[st.session_state.session_token] = session_token
st.success(f"Welcome, {username}! Redirecting to Dashboard...")
st.session_state.navigation = "Dashboard"
else:
st.error("Invalid credentials.")
def signuppage():
"""Signup function."""
st.subheader("Create an Account")
st.markdown('''
<style>
.stApp {
color: black;
text-align: center;
}
}'''
,
unsafe_allow_html=True)
with st.form(key='myform', clear_on_submit=True):
css="""
<style>
[data-testid="stForm"] {
background-color: #E6B2BA;
color:black;
padding: 20px;
border-radius: 10px;
font-size: 16px;
margin:10px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
text-align: center;
transition: background-color 0.5s ease;
cursor: pointer;
width: 50%;
margin: auto;
}
</style>
"""
st.write(css, unsafe_allow_html=True)
new_username = st.text_input("Username")
new_password = st.text_input("Password", type="password")
confirm_password = st.text_input("Confirm Password", type="password")
submit_button = st.form_submit_button(label='Create Account')
if submit_button:
if new_username and new_password:
success = signup(new_username, new_password)
if success:
session_token = generate_session_token()
update_login_time(new_username, session_token)
st.session_state.username = new_username
st.session_state.session_token = session_token
st.query_params[session_token] = session_token
st.success(f"Account created successfully! Redirecting to Dashboard...")
st.session_state.navigation = "Dashboard"
else:
st.error("Username already exists. Please choose a different username.")
else:
st.error("Please fill in all fields.")
st.markdown("""
<style>
button.step-up {display: none;}
button.step-down {display: none;}
div[data-baseweb] {border-radius: 4px;}
</style>""",
unsafe_allow_html=True)
def gdmdetector():
"""GDM detection function."""
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
st.header("GDM Detection")
st.markdown('''
<style>
.stApp {
color:black;
text-align: center;
}
}'''
,
unsafe_allow_html=True)
# Collect input data
with st.form(key='myform', clear_on_submit=True):
css="""
<style>
[data-testid="stForm"] {
background-color: #E6B2BA;
color:black;
padding: 20px;
border-radius: 10px;
font-size: 16px;
margin:10px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
text-align: center;
transition: background-color 0.5s ease;
cursor: pointer;
width: 50%;
margin: auto;
}
</style>
"""
st.write(css, unsafe_allow_html=True)
def boxed_textgdm(text):
st.markdown(
f"""
<div style="
border: 2px solid #d3d3d3;
border-radius: 10px;
background-color: #f0f0f0;
padding: 15px;
font-size: 14px;
font-family: 'Times New Roman', serif;
color: black;">
{text}
</div>
""",
unsafe_allow_html=True
)
age = st.number_input("Age", min_value=0, max_value=120, value=30)
gestation = st.number_input("Gestation in Weeks", min_value=0, max_value=50, value=0)
bmi = st.number_input("BMI", min_value=0.0, max_value=100.0, value=25.0)
hdl = st.number_input("HDL Cholesterol", min_value=0.0, max_value=100.0, value=50.0)
family_history = st.selectbox("Family History of Diabetes (Yes=1, No=0)", [0, 1])
pcos = st.selectbox("PCOS (Yes=1, No=0)", [0, 1])
dia_bp = st.number_input("Diastolic Blood Pressure", min_value=0.0, max_value=200.0, value=80.0)
ogtt = st.number_input("OGTT (mg/dL)", min_value=0.0, max_value=500.0, value=140.0)
hemoglobin = st.number_input("Hemoglobin (g/dL)", min_value=0.0, max_value=20.0, value=12.0)
prediabetes = st.selectbox("History of Prediabetes (Yes=1, No=0)", [0, 1])
submit_button = st.form_submit_button(label='Predict GDM')
if submit_button:
input_data = np.array([[age, gestation, bmi, hdl, family_history, pcos, dia_bp, ogtt, hemoglobin, prediabetes]])
prediction_result = predict_combined(input_data)
st.subheader("Prediction Results")
if not _HAS_TENSORFLOW:
st.info("Using ensemble model only (TensorFlow not installed). For full CNN+ensemble predictions, use Python 3.9–3.13 and install: pip install tensorflow>=2.16.1")
#st.write(f"Ensemble Model Probability: {prediction_result['ensemble_proba']:.2f}")
#st.write(f"CNN Model Probability: {prediction_result['cnn_proba']:.2f}")
boxed_textgdm(f"GDM Risk Probability: {prediction_result['combined_proba']:.2f}")
boxed_textgdm(f"Final Prediction: {prediction_result['final_prediction']}")
# Determine risk level
if prediction_result['combined_proba'] < 0.3:
risk_level = "Low Risk"
health_tips = [
"Maintain a balanced diet rich in whole grains, lean proteins, and healthy fats.",
"Engage in regular physical activity like walking or yoga.",
"Stay hydrated and ensure proper sleep.",
"Schedule routine check-ups for overall well-being."
]
elif 0.3 <= prediction_result['combined_proba'] <= 0.7:
risk_level = "Moderate Risk"
health_tips = [
"Monitor blood sugar levels regularly.",
"Reduce processed sugar and carbohydrate intake.",
"Increase fiber intake through fruits and vegetables.",
"Consult a healthcare provider for early intervention strategies."
]
else:
risk_level = "High Risk"
health_tips = [
"Follow a strict meal plan with portion control.",
"Engage in doctor-recommended physical activities.",
"Monitor glucose levels frequently and track changes.",
"Consult a specialist for medical intervention if required."
]
st.markdown(f"### Health Tips for {risk_level}")
for tip in health_tips:
boxed_textgdm(f"- {tip}")
# Display prediction results
st.success(f"Prediction: {prediction_result['final_prediction']}")
c.execute("INSERT INTO history (username, input_data, result, timestamp) VALUES (?, ?, ?, ?)",(st.session_state.username, str(input_data.tolist()), str(prediction_result['final_prediction']), datetime.now().isoformat()))
conn.commit()
def dashboard():
"""Dashboard function."""
with sqlite3.connect('users1.db') as conn:
c = conn.cursor()
st.header("User Dashboard")
# Apply custom CSS for styling
st.markdown(
"""
<style>
table {
width: 75%;
border-collapse: collapse;
border-radius: 10px;
overflow: hidden;
box-shadow: 0px 4px 10px rgba(0,0,0,0.1);
text-align: center;
background-color: #FAD0C4;
color:black;
}
th {
background-color: #E6B2BA;
color: white;
text-align: center;
padding: 12px;
}
td {
padding: 10px;
border-bottom: 1px solid #ddd;
text-align: center
}
tr:hover {
background-color: #FFF7F3;
}
</style>
""",
unsafe_allow_html=True
)
c.execute("SELECT input_data, result, timestamp FROM history WHERE username = ?", (st.session_state.username,))
history = c.fetchall()
df = pd.DataFrame(history, columns=["Input Data", "Result", "Timestamp"])
# Display the styled table
#st.dataframe(df, use_container_width=True)
st.table(df)
if history==[]:
st.write("No history found.")
if st.button("Clear All Data"):
c.execute("DELETE FROM history WHERE username = ?", (st.session_state.username,))
conn.commit()
st.success("All history cleared.")
#if st.button("Debug Users Table"):
# with sqlite3.connect('users1.db') as conn:
# c = conn.cursor()
# c.execute("SELECT username, password FROM users")
# users = c.fetchall()
# st.write("Users in the database:")
# st.table(users)
def logout():
clear_session_token(st.session_state.username)
st.session_state.username = None
st.session_state.session_token = None
st.query_params["session_token"] = "/" # Clear session token from query params
st.success("Logged out successfully.")
# Streamlit App
#st.set_page_config(page_title="GDM Prediction", layout="wide")
#st.title("Welcome to GDM Prediction Portal")
# Session state initialization
if 'session_token' not in st.session_state:
st.session_state.session_token = None
if 'username' not in st.session_state:
st.session_state.username = None
if 'navigation' not in st.session_state:
st.session_state.navigation = None
#params = st.query_params()
# Restore session from query params (use dict-style access; .get() not supported on all Streamlit versions)
if st.session_state.session_token is None:
session_token = None
if "session_token" in st.query_params:
val = st.query_params["session_token"]
session_token = (val[0] if isinstance(val, list) else val) if val else None
if session_token:
username = get_user_by_session_token(session_token)
if username:
st.session_state.username = username
st.session_state.session_token = session_token
st.session_state.navigation = "Dashboard" # Automatically navigate to Dashboard
st.query_params['session_token'] = session_token
st.success(f"Welcome back, {username}!")
#st.experimental_set_query_params(session_token=session_token)
# Navigation logic
if st.session_state.session_token is None:
# Login/Sign-Up Interface
option = option_menu(
menu_title=None, # required
options=["Home", "Login", "Sign Up", "About US"], # required
icons=["house", "box-arrow-in-right","person-plus","envelope"], # optional
menu_icon="cast", # optional
default_index=0, # optional
orientation="horizontal",
)
st.markdown('''<style>
.option_menu {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 20px;
border-radius: 10px;
font-size: 16px;
font-weight: bold;
margin-top: 15cm;
margin: 0 auto;
}
</style>''', unsafe_allow_html=True)
if option == "Home":
home()
elif option == "About US":
aboutus()
elif option == "Sign Up":
signuppage()
elif option == "Login":
login()
else:
# Logged-in view
option = option_menu(
menu_title=None, # required
options=["Home", "Dashboard", "GDM Detector", "Logout","About US"], # required
icons=["house", "speedometer","clipboard-pulse","box-arrow-right", "envelope"], # optional
menu_icon="cast", # optional
default_index=0, # optional
orientation="horizontal",
)
if option == "Home":
home()
elif option == "About US":
aboutus()
elif option == "GDM Detector":
gdmdetector()
elif option == "Dashboard":
dashboard()
elif option == "Logout":
logout()