File size: 7,456 Bytes
92f05ef f8dc58b 6cc7090 dfd59f2 74aba07 afe1a6f f8dc58b 241cc50 e1171bf 237de53 dfd59f2 02f5e1b dfd59f2 a38d384 e46d68a dfd59f2 47f2534 2609dac f4e6b6a dfd59f2 f566e2c dfd59f2 b67d173 dfd59f2 3e0e420 f4e6b6a dfd59f2 b67d173 dfd59f2 3e0e420 f4e6b6a dfd59f2 f566e2c b67d173 dfd59f2 3e0e420 5afb196 dfd59f2 f566e2c b67d173 dfd59f2 3e0e420 f4e6b6a dfd59f2 f566e2c b67d173 dfd59f2 3e0e420 f4e6b6a dfd59f2 f566e2c f10dfc2 f566e2c b67d173 dfd59f2 3e0e420 5afb196 dfd59f2 f566e2c b67d173 dfd59f2 3e0e420 dfd59f2 f566e2c dfd59f2 f566e2c 02f5e1b f566e2c b67d173 dfd59f2 f8dc58b e1171bf 3e0e420 e1171bf 03d2590 e1171bf 03d2590 133fba6 03d2590 92f05ef a38d384 92f05ef 4cafca0 |
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 |
import os
import json
import numpy as np
import streamlit as st
import onnxruntime as rt
import streamlit.components.v1 as components
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
scalerSession = rt.InferenceSession("standard_scaler.onnx")
modelSession = rt.InferenceSession("random_forest_model.onnx")
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()
def calculate_bmi(weight, height):
# Calculate BMI
bmi = weight / (height ** 2)
return bmi
def is_number(text):
try:
# Try to convert the text to a float
float(text)
return True
except ValueError:
# If conversion fails, it's not a number
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.
"""
# Assign weights to each family member
mother_weight = 0.5
father_weight = 0.5
sibling_weight = 0.25
# Calculate the weighted contributions
family_history = (mother * mother_weight) + (father * father_weight) + (siblings) * sibling_weight
# Add a scaling factor to shift the range
scaling_factor = 1.2
bias = 0.078 # Minimum value in the desired range
# Final scaled DPF score
dpf_score = family_history * scaling_factor + bias
return round(dpf_score, 3) # Rounded for clarity
if "question_no" not in st.session_state:
st.session_state.question_no = 2
if st.session_state.question_no == 2:
progress = st.progress(0)
age = st.text_input(f"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
st.rerun()
elif st.session_state.question_no == 3:
progress = st.progress((st.session_state.question_no -2 )/ 8)
gender = st.selectbox(f"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
st.rerun()
elif st.session_state.question_no == 4:
progress = st.progress((st.session_state.question_no - 2) / 8)
pregnancies = st.text_input(f"How many times have you been pregnant?", key=st.session_state.question_no)
if st.button("Next->"):
if not is_number(pregnancies):
st.warning("Please enter a vaild Input!!!!")
else:
st.session_state.pregnancies = int(pregnancies)
st.session_state.question_no += 1
st.rerun()
elif st.session_state.question_no == 5:
progress = st.progress((st.session_state.question_no -2) / 8)
glucose = st.text_input(f"Enter your glucose level", key=st.session_state.question_no)
if st.button("Next->"):
if not is_number(glucose):
st.warning("Please enter a valid input!!!!")
else:
st.session_state.glucose = int(glucose)
st.session_state.question_no += 1
st.rerun()
elif st.session_state.question_no == 6:
progress = st.progress((st.session_state.question_no-2) / 8)
bp = st.text_input(f"Enter your blood pressure", key=st.session_state.question_no)
if st.button("Next->"):
if not is_number(bp):
st.warning("Please enter valid input!!!")
else:
st.session_state.bp = int(bp)
st.session_state.question_no += 1
st.rerun()
elif st.session_state.question_no == 7:
progress = st.progress((st.session_state.question_no-2) / 8)
height = st.text_input(f"Enter your height in cm:")
if st.button("Next->"):
if not is_number(height):
st.warning("Please enter valid input!!!!")
else:
st.session_state.height = float(height)/100
st.session_state.question_no += 1
st.rerun()
elif st.session_state.question_no == 8:
progress = st.progress((st.session_state.question_no-2) / 8)
weight = st.text_input(f"Enter your weight in KG")
if st.button("Next->"):
if not is_number(weight):
st.warning("Please enter valid input!!!")
else:
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
st.rerun()
elif st.session_state.question_no == 9:
progress = st.progress((st.session_state.question_no-2) / 8)
st.write("Select the members with diabetes in your family")
diabeticMother = st.checkbox("Mother")
diabeticFather = st.checkbox("Father")
diabeticSibling = st.text_input("Enter the number of diabetic siblings in your family", key=st.session_state.question_no)
if st.button("Next->"):
if not is_number(diabeticSibling):
st.warning("Please enter valid number for diabetic siblings!!!!")
else:
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.question_no += 1
st.rerun()
elif st.session_state.question_no == 10:
progress = st.progress((st.session_state.question_no-2) / 8)
input_name = scalerSession.get_inputs()[0].name
# Transform the new data using the ONNX scaler
transformed_data = scalerSession.run(None, {input_name: [[st.session_state.pregnancies, st.session_state.glucose, st.session_state.bp, st.session_state.bmi, st.session_state.dpf, st.session_state.age]]})[0]
# Prepare input data (convert to numpy array)
input_name = modelSession.get_inputs()[0].name
test_data = np.array(transformed_data, dtype=np.float32)
# Run the model
out = modelSession.run(None, {input_name: test_data})[0]
# out = model.predict([st.session_state.pregnancies, st.session_state.glucose, st.session_state.bp, st.session_state.bmi, st.session_state.dpf])
out = out[0]
if out == 0:
st.markdown("""
<b>CONGRATS</b> You are not in risk of diabetes type 2
""", unsafe_allow_html = True)
else:
st.markdown("""
You are in risk of type 2 diabetes. We recommend you to visit doctor as soon as possible.
""")
if st.button("Retake test"):
for key in st.session_state.keys():
del st.session_state[key]
st.rerun() # Reloads the app to reflect the changes |