import gradio as gr import pandas as pd import joblib # Load model model = joblib.load("random_forest_model.pkl") # Define the 18 features input_features = [ "Age", "Gender", "BMI", "Physical Activity Level", "Smoking Status", "Alcohol Consumption", "Diabetes", "Hypertension", "Cholesterol Level", "Family History of Alzheimer’s", "Cognitive Test Score", "Depression Level", "Sleep Quality", "Dietary Habits", "Genetic Risk Factor (APOE-ε4 allele)", "Social Engagement Level", "Income Level", "Stress Levels" ] def print_debug_info(input_dict): print("\n🛠️ Input received:") for k, v in input_dict.items(): print(f"{k}: {v}") def predict_alzheimers( Age, Gender, BMI, Physical, Smoking, Alcohol, Diabetes, Hypertension, Cholesterol, FamilyHistory, CognitiveScore, Depression, Sleep, Diet, GeneticRisk, Engagement, Income, Stress ): input_dict = { "Age": Age, "Gender": Gender, "BMI": BMI, "Physical Activity Level": Physical, "Smoking Status": Smoking, "Alcohol Consumption": Alcohol, "Diabetes": Diabetes, "Hypertension": Hypertension, "Cholesterol Level": Cholesterol, "Family History of Alzheimer’s": FamilyHistory, "Cognitive Test Score": CognitiveScore, "Depression Level": Depression, "Sleep Quality": Sleep, "Dietary Habits": Diet, "Genetic Risk Factor (APOE-ε4 allele)": GeneticRisk, "Social Engagement Level": Engagement, "Income Level": Income, "Stress Levels": Stress } print_debug_info(input_dict) input_df = pd.DataFrame([input_dict]) input_encoded = pd.get_dummies(input_df) model_features = model.feature_names_in_ for col in model_features: if col not in input_encoded.columns: input_encoded[col] = 0 input_encoded = input_encoded[model_features] pred = model.predict(input_encoded)[0] prob = model.predict_proba(input_encoded)[0][1] * 100 result = "🧠 Alzheimer's Detected" if pred == 1 else "✅ No Alzheimer's Detected" return f"{result} | Risk Probability: {prob:.2f}%" with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("## 🧠 Alzheimer's Disease Predictor") gr.Markdown("Enter patient details below to predict Alzheimer's risk using a Random Forest model.") with gr.Row(): with gr.Column(): age = gr.Slider(40, 100, step=1, label="Age") gender = gr.Radio(["Male", "Female"], label="Gender") bmi = gr.Slider(15, 40, step=0.1, label="BMI") physical = gr.Radio(["Low", "Medium", "High"], label="Physical Activity Level") smoking = gr.Radio(["Never", "Former", "Current"], label="Smoking Status") alcohol = gr.Radio(["Never", "Occasionally", "Regularly"], label="Alcohol Consumption") diabetes = gr.Radio(["No", "Yes"], label="Diabetes") hypertension = gr.Radio(["No", "Yes"], label="Hypertension") cholesterol = gr.Radio(["Normal", "High", "Low"], label="Cholesterol Level") with gr.Column(): family = gr.Radio(["No", "Yes"], label="Family History of Alzheimer’s") cognitive = gr.Slider(0, 100, step=1, label="Cognitive Test Score") depression = gr.Radio(["Low", "Medium", "High"], label="Depression Level") sleep = gr.Radio(["Poor", "Average", "Good"], label="Sleep Quality") diet = gr.Radio(["Healthy", "Average", "Unhealthy"], label="Dietary Habits") genetic = gr.Radio(["No", "Yes"], label="Genetic Risk Factor (APOE-ε4 allele)") engagement = gr.Radio(["Low", "Medium", "High"], label="Social Engagement Level") income = gr.Radio(["Low", "Medium", "High"], label="Income Level") stress = gr.Radio(["Low", "Medium", "High"], label="Stress Levels") with gr.Row(): submit_btn = gr.Button("🔍 Predict Alzheimer's Risk", scale=2) output = gr.Textbox(label="Prediction") submit_btn.click( fn=predict_alzheimers, inputs=[ age, gender, bmi, physical, smoking, alcohol, diabetes, hypertension, cholesterol, family, cognitive, depression, sleep, diet, genetic, engagement, income, stress ], outputs=output ) if __name__ == "__main__": demo.launch()