Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""
|
| 3 |
-
Diabetes Prediction Web App
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import numpy as np
|
| 7 |
import pickle
|
| 8 |
import streamlit as st
|
|
@@ -15,34 +10,27 @@ models = {
|
|
| 15 |
"SVC": "SVC_model.pkl"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Function for making predictions
|
| 23 |
def diabetes_prediction(input_data, model):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Convert input_data to numpy array
|
| 28 |
-
input_data_as_numpy_array = np.asarray(input_data)
|
| 29 |
-
input_data_reshaped = input_data_as_numpy_array.reshape(1, -1)
|
| 30 |
-
|
| 31 |
-
prediction = loaded_model.predict(input_data_reshaped)
|
| 32 |
|
| 33 |
-
if prediction[0] == 0:
|
| 34 |
-
return 'The person is not diabetic'
|
| 35 |
-
else:
|
| 36 |
-
return 'The person is diabetic'
|
| 37 |
-
|
| 38 |
-
# Main function for the Streamlit app
|
| 39 |
def main():
|
| 40 |
st.title('Diabetes Prediction Web App')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
# Input fields for user data
|
| 46 |
Pregnancies = st.text_input('Number of Pregnancies')
|
| 47 |
Glucose = st.text_input('Glucose Level')
|
| 48 |
BloodPressure = st.text_input('Blood Pressure Value')
|
|
@@ -51,32 +39,23 @@ def main():
|
|
| 51 |
BMI = st.text_input('BMI Value')
|
| 52 |
DiabetesPedigreeFunction = st.text_input('Diabetes Pedigree Function Value')
|
| 53 |
Age = st.text_input('Age of the Person')
|
| 54 |
-
|
| 55 |
-
diagnosis = ''
|
| 56 |
|
| 57 |
-
|
| 58 |
if st.button('Diabetes Test Result'):
|
| 59 |
try:
|
| 60 |
-
input_data = [
|
| 61 |
-
float(Pregnancies),
|
| 62 |
-
float(
|
| 63 |
-
float(
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
float(DiabetesPedigreeFunction),
|
| 68 |
-
float(Age)
|
| 69 |
-
]
|
| 70 |
-
diagnosis = diabetes_prediction(input_data, selected_model)
|
| 71 |
-
except ValueError as e:
|
| 72 |
diagnosis = "Invalid input. Please enter numeric values for all fields."
|
| 73 |
|
| 74 |
st.success(diagnosis)
|
| 75 |
|
| 76 |
-
# Dummy Entries or Demo for showcasing
|
| 77 |
st.markdown('## Demo - Test with Dummy Entries')
|
| 78 |
|
| 79 |
-
# You can set default values for the inputs to showcase the prediction functionality
|
| 80 |
default_values = {
|
| 81 |
"Number of Pregnancies": 5,
|
| 82 |
"Glucose Level": 130,
|
|
@@ -88,20 +67,14 @@ def main():
|
|
| 88 |
"Age of the Person": 40
|
| 89 |
}
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
default_values["
|
| 96 |
-
default_values["
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
default_values["Insulin Level"],
|
| 100 |
-
default_values["BMI Value"],
|
| 101 |
-
default_values["Diabetes Pedigree Function Value"],
|
| 102 |
-
default_values["Age of the Person"]
|
| 103 |
-
]
|
| 104 |
-
demo_diagnosis = diabetes_prediction(demo_input_data, selected_model)
|
| 105 |
st.success(f'Demo Result: {demo_diagnosis}')
|
| 106 |
|
| 107 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import pickle
|
| 3 |
import streamlit as st
|
|
|
|
| 10 |
"SVC": "SVC_model.pkl"
|
| 11 |
}
|
| 12 |
|
| 13 |
+
def load_model(model_name):
|
| 14 |
+
model_path = models.get(model_name)
|
| 15 |
+
if model_path:
|
| 16 |
+
return pickle.load(open(model_path, 'rb'))
|
| 17 |
+
return None
|
| 18 |
|
|
|
|
| 19 |
def diabetes_prediction(input_data, model):
|
| 20 |
+
prediction = model.predict([input_data])
|
| 21 |
+
return 'The person is diabetic' if prediction[0] else 'The person is not diabetic'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def main():
|
| 24 |
st.title('Diabetes Prediction Web App')
|
| 25 |
+
|
| 26 |
+
selected_model_name = st.selectbox("Choose Prediction Method (Default: Logistic Regression)", list(models.keys()))
|
| 27 |
+
|
| 28 |
+
loaded_model = load_model(selected_model_name)
|
| 29 |
|
| 30 |
+
if loaded_model is None:
|
| 31 |
+
st.error("Model not found!")
|
| 32 |
+
return
|
| 33 |
|
|
|
|
| 34 |
Pregnancies = st.text_input('Number of Pregnancies')
|
| 35 |
Glucose = st.text_input('Glucose Level')
|
| 36 |
BloodPressure = st.text_input('Blood Pressure Value')
|
|
|
|
| 39 |
BMI = st.text_input('BMI Value')
|
| 40 |
DiabetesPedigreeFunction = st.text_input('Diabetes Pedigree Function Value')
|
| 41 |
Age = st.text_input('Age of the Person')
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
diagnosis = ''
|
| 44 |
if st.button('Diabetes Test Result'):
|
| 45 |
try:
|
| 46 |
+
input_data = np.array([
|
| 47 |
+
float(Pregnancies), float(Glucose), float(BloodPressure),
|
| 48 |
+
float(SkinThickness), float(Insulin), float(BMI),
|
| 49 |
+
float(DiabetesPedigreeFunction), float(Age)
|
| 50 |
+
])
|
| 51 |
+
diagnosis = diabetes_prediction(input_data, loaded_model)
|
| 52 |
+
except ValueError:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
diagnosis = "Invalid input. Please enter numeric values for all fields."
|
| 54 |
|
| 55 |
st.success(diagnosis)
|
| 56 |
|
|
|
|
| 57 |
st.markdown('## Demo - Test with Dummy Entries')
|
| 58 |
|
|
|
|
| 59 |
default_values = {
|
| 60 |
"Number of Pregnancies": 5,
|
| 61 |
"Glucose Level": 130,
|
|
|
|
| 67 |
"Age of the Person": 40
|
| 68 |
}
|
| 69 |
|
| 70 |
+
if st.button('Run Demo'):
|
| 71 |
+
demo_input_data = np.array([
|
| 72 |
+
default_values["Number of Pregnancies"], default_values["Glucose Level"],
|
| 73 |
+
default_values["Blood Pressure Value"], default_values["Skin Thickness Value"],
|
| 74 |
+
default_values["Insulin Level"], default_values["BMI Value"],
|
| 75 |
+
default_values["Diabetes Pedigree Function Value"], default_values["Age of the Person"]
|
| 76 |
+
])
|
| 77 |
+
demo_diagnosis = diabetes_prediction(demo_input_data, loaded_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
st.success(f'Demo Result: {demo_diagnosis}')
|
| 79 |
|
| 80 |
if __name__ == '__main__':
|