|
import json |
|
import streamlit as st |
|
from sklearn.preprocessing import StandardScaler |
|
from sklearn.ensemble import RandomForestClassifier |
|
|
|
with open("random_forest_model.json", "r") as f: |
|
params = json.load(f) |
|
model = RandomForestClassifier(**params) |
|
del(params) |
|
|
|
with open("scaler.json", "r") as f: |
|
params = json.load(f) |
|
scaler = StandardScaler(**params) |
|
del(params) |
|
|
|
def calculate_bmi(weight, height): |
|
|
|
bmi = weight / (height ** 2) |
|
|
|
return bmi |
|
|
|
def is_number(text): |
|
try: |
|
|
|
float(text) |
|
return True |
|
except ValueError: |
|
|
|
return False |
|
|
|
def diabetic_pedigree_function(mother, father, siblings): |
|
""" |
|
Calculate a scaled Diabetic Pedigree Function (DPF) for an individual, |
|
aiming for an output range of approximately (0.078, 2.42). |
|
|
|
Parameters: |
|
mother (int): 1 if the mother has diabetes, 0 otherwise. |
|
father (int): 1 if the father has diabetes, 0 otherwise. |
|
siblings (list): A list of 0s and 1s representing siblings' diabetes status. |
|
|
|
Returns: |
|
float: The scaled diabetic pedigree function score. |
|
""" |
|
|
|
mother_weight = 0.5 |
|
father_weight = 0.5 |
|
sibling_weight = 0.25 |
|
|
|
|
|
family_history = (mother * mother_weight) + (father * father_weight) + (sum(siblings) * sibling_weight) |
|
|
|
|
|
scaling_factor = 1.2 |
|
bias = 0.078 |
|
|
|
|
|
dpf_score = family_history * scaling_factor + bias |
|
|
|
return round(dpf_score, 3) |
|
|
|
|
|
|
|
if "question_no" not in st.session_state: |
|
st.session_state.question_no = 1 |
|
|
|
st.title(f"{st.session_state.question_no}Know your diabetic status") |
|
if st.session_state.question_no == 1: |
|
name = st.text_input(f"{st.session_state.question_no}What is your name?", key=st.session_state.question_no,placeholder="Type your answer here") |
|
|
|
if st.button("Next->"): |
|
if name: |
|
st.session_state.name = name |
|
else: |
|
st.session_state.name = "Anonymous user" |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 2: |
|
st.write(f""" |
|
Name = {st.session_state.name} |
|
""") |
|
age = st.number_input(f"{st.session_state.question_no}How old are you?", key=st.session_state.question_no, placeholder="Type your answer") |
|
if st.button("Next->"): |
|
if not is_number(age): |
|
st.warning("Please enter a valid age") |
|
else: |
|
st.session_state.age = int(age) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 3: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age} |
|
""") |
|
gender = st.selectbox(f"{st.session_state.question_no}What is your gender?", ["Male", "Female", "Other"], key=st.session_state.question_no) |
|
if st.button("Next->"): |
|
st.session_state.gender = gender |
|
if gender == "Male": |
|
st.session_state.pregnancies = 0 |
|
st.session_state.question_no += 2 |
|
else: |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 4: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender} |
|
""") |
|
pregnancies = st.number_input(f"{st.session_state.question_no}How many times have you been pregnant?", key=st.session_state.question_no) |
|
if st.button("Next->"): |
|
st.session_state.pregnancies = int(pregnancies) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 5: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender}\n |
|
No of pregnancies = {st.session_state.pregnancies} |
|
""") |
|
glucose = st.number_input(f"{st.session_state.question_no}Enter your glucose level", key=st.session_state.question_no) |
|
if st.button("Next->"): |
|
st.session_state.glucose = int(glucose) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 6: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender}\n |
|
No of pregnancies = {st.session_state.pregnancies}\n |
|
Glucose Level = {st.session_state.glucose} |
|
""") |
|
bp = st.number_input(f"{st.session_state.question_no}Enter your blood pressure", key=st.session_state.question_no) |
|
if st.button("Next->"): |
|
st.session_state.bp = int(bp) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 7: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender}\n |
|
No of pregnancies = {st.session_state.pregnancies}\n |
|
Glucose Level = {st.session_state.glucose}\n |
|
BT Level = {st.session_state.bp} |
|
""") |
|
height = st.number_input(f"{st.session_state.question_no}Enter your height in cm:") |
|
if st.button("Next->"): |
|
st.session_state.height = float(height/100) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 8: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender}\n |
|
No of pregnancies = {st.session_state.pregnancies}\n |
|
Glucose Level = {st.session_state.glucose}\n |
|
BT Level = {st.session_state.bp}\n |
|
Height = {st.session_state.height} |
|
""") |
|
weight = st.number_input(f"{st.session_state.question_no}Enter your weight in KG") |
|
if st.button("Next->"): |
|
st.session_state.weight = float(weight) |
|
st.session_state.bmi = calculate_bmi(st.session_state.weight, st.session_state.height) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 9: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender}\n |
|
No of pregnancies = {st.session_state.pregnancies}\n |
|
Glucose Level = {st.session_state.glucose}\n |
|
BT Level = {st.session_state.bp}\n |
|
Height = {st.session_state.height}\n |
|
Weight = {st.session_state.weight}\n |
|
BMI = {st.session_state.bmi} |
|
""") |
|
st.write("Select the members with diabetes in your family") |
|
diabeticMother = st.checkbox("Mother") |
|
diabeticFather = st.checkbox("Father") |
|
diabeticSibling = st.number_input("Enter the number of diabetic siblings in your family", key=st.session_state.question_no) |
|
if st.button("Next->"): |
|
st.session_state.diabeticMother = 1 if diabeticMother else 0 |
|
st.session_state.diabeticFather = 1 if diabeticFather else 0 |
|
st.session_state.diabeticSibling = int(diabeticSibling) |
|
st.session_state.dpf = diabetic_pedigree_function(st.session_state.diabeticMother, st.session_state.diabeticFather, st.session_state.diabeticSibling, st.session_state.diabeticSibling) |
|
st.session_state.question_no += 1 |
|
|
|
elif st.session_state.question_no == 10: |
|
st.write(f""" |
|
Name = {st.session_state.name}\n |
|
Age = {st.session_state.age}\n |
|
Gender = {st.session_state.gender}\n |
|
No of pregnancies = {st.session_state.pregnancies}\n |
|
Glucose Level = {st.session_state.glucose}\n |
|
BT Level = {st.session_state.bp}\n |
|
Height = {st.session_state.height}\n |
|
Weight = {st.session_state.weight}\n |
|
BMI = {st.session_state.bmi}\n |
|
Diabetic Mother = {st.session_state.diabeticMother}\n |
|
Diabetic Father = {st.session_state.diabeticFather}\n |
|
Diabetic Sibling Number = {st.session_state.diabeticSibling}\n |
|
Diabetes Pedigree Function = {st.session_state.dpf} |
|
""") |
|
out = model.predict([st.session_state.pregnancies, st.session_state.glucose, st.session.state.bp, st.bmi, st.dpf]) |
|
st.write(out) |
|
|
|
|
|
|
|
|
|
|
|
|