AIMLdeepanshu commited on
Commit
a2033ab
·
verified ·
1 Parent(s): e807a45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -70
app.py CHANGED
@@ -1,85 +1,107 @@
1
  import gradio as gr
 
2
  import joblib
3
- import numpy as np
4
 
5
- # Load the trained Random Forest model
6
- model = joblib.load("best_random_forest_model.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Define the prediction function
9
  def predict_alzheimers(
10
- age, gender, bmi, diabetes, hypertension, cholesterol,
11
- physical_activity, smoking, alcohol, family_history,
12
- cognitive_score, depression, sleep, dietary,
13
- genetic_risk, social_engagement, income, stress
14
  ):
15
- # Map categorical values to numerical ones
16
- gender = 1 if gender == "Male" else 0
17
- diabetes = 1 if diabetes == "Yes" else 0
18
- hypertension = 1 if hypertension == "Yes" else 0
19
- family_history = 1 if family_history == "Yes" else 0
20
- genetic_risk = 1 if genetic_risk == "Yes" else 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- cholesterol = {"Low": 0, "Normal": 1, "High": 2}.get(cholesterol, 1)
23
- physical_activity = {"Low": 0, "Medium": 1, "High": 2}.get(physical_activity, 1)
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
- # ... your model input code continues ...
 
34
 
35
 
36
- # Create input array
37
- features = np.array([[
38
- age, gender, bmi, diabetes, hypertension, cholesterol,
39
- family_history, cognitive_score, genetic_risk,
40
- physical_activity, smoking, alcohol, depression,
41
- sleep, dietary, social_engagement,
42
- income, stress
43
- ]])
44
 
45
- # Predict
46
- prediction = model.predict(features)[0]
47
- probability = model.predict_proba(features)[0][1] * 100
 
 
 
 
 
 
 
 
48
 
49
- if prediction == 1:
50
- result = f" Alzheimer's Detected | Risk Probability: {probability:.2f}%"
51
- else:
52
- result = f" No Alzheimer's Detected | Risk Probability: {probability:.2f}%"
 
 
 
 
 
 
53
 
54
- return result
 
 
 
55
 
56
- # Create the Gradio interface
57
- app = gr.Interface(
58
- fn=predict_alzheimers,
59
- inputs=[
60
- gr.Slider(40, 100, value=60, label="Age"),
61
- gr.Radio(["Male", "Female"], label="Gender"),
62
- gr.Slider(15, 40, value=25, label="BMI"),
63
- gr.Radio(["Low", "Medium", "High"], label="Physical Activity Level"),
64
- gr.Radio(["Never", "Former", "Current"], label="Smoking Status"),
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
- # Launch the app
85
- app.launch()
 
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()