Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,107 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import joblib
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
model = joblib.load("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Define the prediction function
|
| 9 |
def predict_alzheimers(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
genetic_risk, social_engagement, income, stress
|
| 14 |
):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
smoking = {"Never": 0, "Former": 1, "Current": 2}.get(smoking, 1)
|
| 25 |
-
alcohol = {"Never": 0, "Occasionally": 1, "Regularly": 2}.get(alcohol, 1)
|
| 26 |
-
depression = {"Low": 0, "Medium": 1, "High": 2}.get(depression, 1)
|
| 27 |
-
sleep = {"Poor": 0, "Average": 1, "Good": 2}.get(sleep, 1)
|
| 28 |
-
dietary = {"Healthy": 0, "Average": 1, "Unhealthy": 2}.get(dietary, 1)
|
| 29 |
-
social_engagement = {"Low": 0, "Medium": 1, "High": 2}.get(social_engagement, 1)
|
| 30 |
-
income = {"Low": 0, "Medium": 1, "High": 2}.get(income, 1)
|
| 31 |
-
stress = {"Low": 0, "Medium": 1, "High": 2}.get(stress, 1)
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
family_history, cognitive_score, genetic_risk,
|
| 40 |
-
physical_activity, smoking, alcohol, depression,
|
| 41 |
-
sleep, dietary, social_engagement,
|
| 42 |
-
income, stress
|
| 43 |
-
]])
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
gr.Radio(["Never", "Occasionally", "Regularly"], label="Alcohol Consumption"),
|
| 66 |
-
gr.Radio(["No", "Yes"], label="Diabetes"),
|
| 67 |
-
gr.Radio(["No", "Yes"], label="Hypertension"),
|
| 68 |
-
gr.Radio(["Low", "Normal", "High"], label="Cholesterol Level"),
|
| 69 |
-
gr.Radio(["No", "Yes"], label="Family History of Alzheimer's"),
|
| 70 |
-
gr.Slider(0, 100, value=50, label="Cognitive Test Score"),
|
| 71 |
-
gr.Radio(["Low", "Medium", "High"], label="Depression Level"),
|
| 72 |
-
gr.Radio(["Poor", "Average", "Good"], label="Sleep Quality"),
|
| 73 |
-
gr.Radio(["Healthy", "Average", "Unhealthy"], label="Dietary Habits"),
|
| 74 |
-
gr.Radio(["No", "Yes"], label="Genetic Risk Factor (APOE-ε4 allele)"),
|
| 75 |
-
gr.Radio(["Low", "Medium", "High"], label="Social Engagement Level"),
|
| 76 |
-
gr.Radio(["Low", "Medium", "High"], label="Income Level"),
|
| 77 |
-
gr.Radio(["Low", "Medium", "High"], label="Stress Levels"),
|
| 78 |
-
],
|
| 79 |
-
outputs=gr.Textbox(label="Prediction"),
|
| 80 |
-
title="🔍 Predict Alzheimer's Risk",
|
| 81 |
-
theme=gr.themes.Soft()
|
| 82 |
-
)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
import joblib
|
|
|
|
| 4 |
|
| 5 |
+
# Load model
|
| 6 |
+
model = joblib.load("random_forest_model.pkl")
|
| 7 |
+
|
| 8 |
+
# Define the 18 features
|
| 9 |
+
input_features = [
|
| 10 |
+
"Age", "Gender", "BMI", "Physical Activity Level", "Smoking Status", "Alcohol Consumption",
|
| 11 |
+
"Diabetes", "Hypertension", "Cholesterol Level", "Family History of Alzheimer’s",
|
| 12 |
+
"Cognitive Test Score", "Depression Level", "Sleep Quality", "Dietary Habits",
|
| 13 |
+
"Genetic Risk Factor (APOE-ε4 allele)", "Social Engagement Level", "Income Level", "Stress Levels"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
def print_debug_info(input_dict):
|
| 17 |
+
print("\n🛠️ Input received:")
|
| 18 |
+
for k, v in input_dict.items():
|
| 19 |
+
print(f"{k}: {v}")
|
| 20 |
|
|
|
|
| 21 |
def predict_alzheimers(
|
| 22 |
+
Age, Gender, BMI, Physical, Smoking, Alcohol, Diabetes, Hypertension, Cholesterol,
|
| 23 |
+
FamilyHistory, CognitiveScore, Depression, Sleep, Diet, GeneticRisk, Engagement,
|
| 24 |
+
Income, Stress
|
|
|
|
| 25 |
):
|
| 26 |
+
input_dict = {
|
| 27 |
+
"Age": Age,
|
| 28 |
+
"Gender": Gender,
|
| 29 |
+
"BMI": BMI,
|
| 30 |
+
"Physical Activity Level": Physical,
|
| 31 |
+
"Smoking Status": Smoking,
|
| 32 |
+
"Alcohol Consumption": Alcohol,
|
| 33 |
+
"Diabetes": Diabetes,
|
| 34 |
+
"Hypertension": Hypertension,
|
| 35 |
+
"Cholesterol Level": Cholesterol,
|
| 36 |
+
"Family History of Alzheimer’s": FamilyHistory,
|
| 37 |
+
"Cognitive Test Score": CognitiveScore,
|
| 38 |
+
"Depression Level": Depression,
|
| 39 |
+
"Sleep Quality": Sleep,
|
| 40 |
+
"Dietary Habits": Diet,
|
| 41 |
+
"Genetic Risk Factor (APOE-ε4 allele)": GeneticRisk,
|
| 42 |
+
"Social Engagement Level": Engagement,
|
| 43 |
+
"Income Level": Income,
|
| 44 |
+
"Stress Levels": Stress
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
print_debug_info(input_dict)
|
| 48 |
+
input_df = pd.DataFrame([input_dict])
|
| 49 |
+
input_encoded = pd.get_dummies(input_df)
|
| 50 |
+
|
| 51 |
+
model_features = model.feature_names_in_
|
| 52 |
+
for col in model_features:
|
| 53 |
+
if col not in input_encoded.columns:
|
| 54 |
+
input_encoded[col] = 0
|
| 55 |
+
input_encoded = input_encoded[model_features]
|
| 56 |
|
| 57 |
+
pred = model.predict(input_encoded)[0]
|
| 58 |
+
prob = model.predict_proba(input_encoded)[0][1] * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
result = "🧠 Alzheimer's Detected" if pred == 1 else "✅ No Alzheimer's Detected"
|
| 61 |
+
return f"{result} | Risk Probability: {prob:.2f}%"
|
| 62 |
|
| 63 |
|
| 64 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 65 |
+
gr.Markdown("## 🧠 Alzheimer's Disease Predictor")
|
| 66 |
+
gr.Markdown("Enter patient details below to predict Alzheimer's risk using a Random Forest model.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
age = gr.Slider(40, 100, step=1, label="Age")
|
| 71 |
+
gender = gr.Radio(["Male", "Female"], label="Gender")
|
| 72 |
+
bmi = gr.Slider(15, 40, step=0.1, label="BMI")
|
| 73 |
+
physical = gr.Radio(["Low", "Medium", "High"], label="Physical Activity Level")
|
| 74 |
+
smoking = gr.Radio(["Never", "Former", "Current"], label="Smoking Status")
|
| 75 |
+
alcohol = gr.Radio(["Never", "Occasionally", "Regularly"], label="Alcohol Consumption")
|
| 76 |
+
diabetes = gr.Radio(["No", "Yes"], label="Diabetes")
|
| 77 |
+
hypertension = gr.Radio(["No", "Yes"], label="Hypertension")
|
| 78 |
+
cholesterol = gr.Radio(["Normal", "High", "Low"], label="Cholesterol Level")
|
| 79 |
|
| 80 |
+
with gr.Column():
|
| 81 |
+
family = gr.Radio(["No", "Yes"], label="Family History of Alzheimer’s")
|
| 82 |
+
cognitive = gr.Slider(0, 100, step=1, label="Cognitive Test Score")
|
| 83 |
+
depression = gr.Radio(["Low", "Medium", "High"], label="Depression Level")
|
| 84 |
+
sleep = gr.Radio(["Poor", "Average", "Good"], label="Sleep Quality")
|
| 85 |
+
diet = gr.Radio(["Healthy", "Average", "Unhealthy"], label="Dietary Habits")
|
| 86 |
+
genetic = gr.Radio(["No", "Yes"], label="Genetic Risk Factor (APOE-ε4 allele)")
|
| 87 |
+
engagement = gr.Radio(["Low", "Medium", "High"], label="Social Engagement Level")
|
| 88 |
+
income = gr.Radio(["Low", "Medium", "High"], label="Income Level")
|
| 89 |
+
stress = gr.Radio(["Low", "Medium", "High"], label="Stress Levels")
|
| 90 |
|
| 91 |
+
with gr.Row():
|
| 92 |
+
submit_btn = gr.Button("🔍 Predict Alzheimer's Risk", scale=2)
|
| 93 |
+
|
| 94 |
+
output = gr.Textbox(label="Prediction")
|
| 95 |
|
| 96 |
+
submit_btn.click(
|
| 97 |
+
fn=predict_alzheimers,
|
| 98 |
+
inputs=[
|
| 99 |
+
age, gender, bmi, physical, smoking, alcohol, diabetes,
|
| 100 |
+
hypertension, cholesterol, family, cognitive, depression,
|
| 101 |
+
sleep, diet, genetic, engagement, income, stress
|
| 102 |
+
],
|
| 103 |
+
outputs=output
|
| 104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch()
|