Update app.py
Browse files
app.py
CHANGED
|
@@ -7,40 +7,42 @@ import numpy as np
|
|
| 7 |
import pickle
|
| 8 |
import streamlit as st
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
# changing the input_data to numpy array
|
| 21 |
input_data_as_numpy_array = np.asarray(input_data)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)
|
| 25 |
-
|
| 26 |
prediction = loaded_model.predict(input_data_reshaped)
|
| 27 |
-
print(prediction)
|
| 28 |
|
| 29 |
-
if
|
| 30 |
-
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
def main():
|
| 37 |
-
|
| 38 |
-
# giving a title
|
| 39 |
st.title('Diabetes Prediction Web App')
|
| 40 |
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
Pregnancies = st.text_input('Number of Pregnancies')
|
| 45 |
Glucose = st.text_input('Glucose Level')
|
| 46 |
BloodPressure = st.text_input('Blood Pressure Value')
|
|
@@ -50,15 +52,11 @@ def main():
|
|
| 50 |
DiabetesPedigreeFunction = st.text_input('Diabetes Pedigree Function Value')
|
| 51 |
Age = st.text_input('Age of the Person')
|
| 52 |
|
| 53 |
-
|
| 54 |
-
# code for Prediction
|
| 55 |
diagnosis = ''
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
if st.button('Diabetes Test Result'):
|
| 60 |
try:
|
| 61 |
-
# Convert input data to floating-point numbers with error handling
|
| 62 |
input_data = [
|
| 63 |
float(Pregnancies),
|
| 64 |
float(Glucose),
|
|
@@ -69,11 +67,11 @@ def main():
|
|
| 69 |
float(DiabetesPedigreeFunction),
|
| 70 |
float(Age)
|
| 71 |
]
|
| 72 |
-
diagnosis = diabetes_prediction(input_data)
|
| 73 |
except ValueError as e:
|
| 74 |
diagnosis = "Invalid input. Please enter numeric values for all fields."
|
| 75 |
|
| 76 |
st.success(diagnosis)
|
| 77 |
|
| 78 |
if __name__ == '__main__':
|
| 79 |
-
main()
|
|
|
|
| 7 |
import pickle
|
| 8 |
import streamlit as st
|
| 9 |
|
| 10 |
+
# Dictionary to hold different model names and their corresponding file paths
|
| 11 |
+
models = {
|
| 12 |
+
"Logistic Regression": "LogisticRegression_model.pkl",
|
| 13 |
+
"Decision Tree Classifier": "DecisionTreeClassifier_model.pkl",
|
| 14 |
+
"Random Forest Classifier": "RandomForestClassifier_model.pkl",
|
| 15 |
+
"SVC": "SVC_model.pkl"
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
# Load the default model (Logistic Regression in this case)
|
| 19 |
+
selected_model = "Logistic Regression"
|
| 20 |
+
loaded_model = pickle.load(open(models[selected_model], 'rb'))
|
| 21 |
|
| 22 |
+
# Function for making predictions
|
| 23 |
+
def diabetes_prediction(input_data, model):
|
| 24 |
+
# Load the selected model
|
| 25 |
+
loaded_model = pickle.load(open(models[model], 'rb'))
|
| 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 |
+
# Dropdown for model selection
|
| 43 |
+
selected_model = st.selectbox("Select Model", list(models.keys()))
|
| 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')
|
|
|
|
| 52 |
DiabetesPedigreeFunction = st.text_input('Diabetes Pedigree Function Value')
|
| 53 |
Age = st.text_input('Age of the Person')
|
| 54 |
|
|
|
|
|
|
|
| 55 |
diagnosis = ''
|
| 56 |
|
| 57 |
+
# Prediction button
|
|
|
|
| 58 |
if st.button('Diabetes Test Result'):
|
| 59 |
try:
|
|
|
|
| 60 |
input_data = [
|
| 61 |
float(Pregnancies),
|
| 62 |
float(Glucose),
|
|
|
|
| 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 |
if __name__ == '__main__':
|
| 77 |
+
main()
|