File size: 4,410 Bytes
a7944da
a2033ab
a7944da
 
a2033ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7944da
 
a2033ab
 
 
cc15d33
a2033ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a5d07f
a2033ab
 
1a5d07f
a2033ab
 
1a5d07f
a7944da
a2033ab
 
 
cc15d33
a2033ab
 
 
 
 
 
 
 
 
 
 
cc15d33
a2033ab
 
 
 
 
 
 
 
 
 
cc15d33
a2033ab
 
 
 
cc15d33
a2033ab
 
 
 
 
 
 
 
 
cc15d33
a2033ab
 
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
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()