Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import pickle | |
| import streamlit as st | |
| # Load the model at the start | |
| # @st.cache_resource | |
| def load_model(pkl_file): | |
| """Load the prediction model from a file.""" | |
| with open(pkl_file, 'rb') as model_file: | |
| return pickle.load(model_file) | |
| def main(): | |
| # Title and layout | |
| st.markdown("<h1 style='text-align: center; margin-top: -30px;'>Diabetes Risk Predictor 🩺</h1>", unsafe_allow_html=True) | |
| # Input fields | |
| st.subheader("Enter Your Details:") | |
| age = st.text_input("Age (years):", placeholder="e.g., 28.0") | |
| pregnancies = st.text_input("Pregnancies:", placeholder="e.g., 6") | |
| glucose = st.text_input("Glucose Level:", placeholder="e.g., 151") | |
| blood_pressure = st.text_input("Blood Pressure:", placeholder="e.g., 62.0") | |
| skin_thickness = st.text_input("Skin Thickness (mm):", placeholder="e.g., 31.0") | |
| insulin = st.text_input("Insulin Level:", placeholder="e.g., 120.0") | |
| bmi = st.text_input("BMI:", placeholder="e.g., 35.5") | |
| dpf = st.text_input("Diabetes Pedigree Function:", placeholder="e.g., 0.692") | |
| # Prediction button | |
| if st.button("Predict"): | |
| inputs_valid, validated_inputs = validate_inputs( | |
| pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age | |
| ) | |
| if inputs_valid: | |
| predict_risk(validated_inputs) | |
| def validate_inputs(pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, dpf, age): | |
| """Validate user inputs and return a flag with validated inputs.""" | |
| errors = [] | |
| def validate_input(value, field_name, is_float=True): | |
| if not value.strip(): | |
| errors.append(f"Please enter {field_name}.") | |
| return None | |
| try: | |
| return float(value) if is_float else int(value) | |
| except ValueError: | |
| errors.append(f"Please enter a valid numeric value for {field_name}.") | |
| return None | |
| # Validate each input | |
| pregnancies = validate_input(pregnancies, "Pregnancies", is_float=False) | |
| glucose = validate_input(glucose, "Glucose Level") | |
| blood_pressure = validate_input(blood_pressure, "Blood Pressure") | |
| skin_thickness = validate_input(skin_thickness, "Skin Thickness") | |
| insulin = validate_input(insulin, "Insulin Level") | |
| bmi = validate_input(bmi, "BMI") | |
| dpf = validate_input(dpf, "Diabetes Pedigree Function") | |
| age = validate_input(age, "Age", is_float=False) | |
| if errors: | |
| for error in errors: | |
| st.error(error) | |
| return False, None | |
| return True, { | |
| "Pregnancies": pregnancies, | |
| "Glucose": glucose, | |
| "BloodPressure": blood_pressure, | |
| "SkinThickness": skin_thickness, | |
| "Insulin": insulin, | |
| "BMI": bmi, | |
| "DiabetesPedigreeFunction": dpf, | |
| "Age": age | |
| } | |
| def predict_risk(inputs): | |
| """Perform the diabetes risk prediction and display the result.""" | |
| try: | |
| # Convert inputs to DataFrame | |
| input_data = pd.DataFrame([[ | |
| inputs["Pregnancies"], | |
| inputs["Glucose"], | |
| inputs["BloodPressure"], | |
| inputs["SkinThickness"], | |
| inputs["Insulin"], | |
| inputs["BMI"], | |
| inputs["DiabetesPedigreeFunction"], | |
| inputs["Age"] | |
| ]], columns=['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', | |
| 'BMI', 'DiabetesPedigreeFunction', 'Age']) | |
| # Predict and display the result | |
| result = pipe.predict(input_data)[0] | |
| if result == 1: | |
| st.success("**⚠️: HIGH RISK of Diabetes**.") | |
| else: | |
| st.success("**💪: LOW RISK of Diabetes**.") | |
| except Exception as e: | |
| st.error(f"An error occurred during prediction: {e}") | |
| if __name__ == "__main__": | |
| pipe = load_model("pipe.pkl") # Ensure this file exists | |
| main() | |
| st.markdown("<br><br><h5 style='text-align: center;'>Developed by M.Nabeel</h5>", unsafe_allow_html=True) | |