Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,11 @@ import gradio as gr
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
model = joblib.load("xgboost_model.pkl")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
def
|
| 10 |
-
# Convert inputs
|
| 11 |
gender = 1 if gender == "Male" else 0
|
| 12 |
smoking = {"Non-Smoker": 0, "Smoker": 1, "Former Smoker": 2}[smoking]
|
| 13 |
alcohol = {"None": 0, "Moderate": 1, "Heavy": 2}[alcohol]
|
|
@@ -16,9 +15,13 @@ def predict_alzheimers(age, gender, bmi, smoking, alcohol, diabetes, hypertensio
|
|
| 16 |
family_history = 1 if family_history == "Yes" else 0
|
| 17 |
apoe4 = 1 if apoe4 == "Yes" else 0
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
proba = model.predict_proba(features)[0][1]
|
| 23 |
proba_percent = round(proba * 100, 2)
|
| 24 |
|
|
@@ -27,8 +30,7 @@ def predict_alzheimers(age, gender, bmi, smoking, alcohol, diabetes, hypertensio
|
|
| 27 |
else:
|
| 28 |
return f"✅ Likely Alzheimer’s Negative ({100 - proba_percent}%)"
|
| 29 |
|
| 30 |
-
|
| 31 |
-
# Gradio UI
|
| 32 |
interface = gr.Interface(
|
| 33 |
fn=predict_alzheimers,
|
| 34 |
inputs=[
|
|
@@ -45,7 +47,7 @@ interface = gr.Interface(
|
|
| 45 |
],
|
| 46 |
outputs="text",
|
| 47 |
title="Alzheimer’s Prediction App",
|
| 48 |
-
description="Enter
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Load XGBoost model
|
| 6 |
+
model = joblib.load("xgboost_model.pkl")
|
| 7 |
|
| 8 |
+
# 🔧 Preprocessing Function
|
| 9 |
+
def preprocess(age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4):
|
|
|
|
| 10 |
gender = 1 if gender == "Male" else 0
|
| 11 |
smoking = {"Non-Smoker": 0, "Smoker": 1, "Former Smoker": 2}[smoking]
|
| 12 |
alcohol = {"None": 0, "Moderate": 1, "Heavy": 2}[alcohol]
|
|
|
|
| 15 |
family_history = 1 if family_history == "Yes" else 0
|
| 16 |
apoe4 = 1 if apoe4 == "Yes" else 0
|
| 17 |
|
| 18 |
+
return np.array([[age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4]])
|
| 19 |
|
| 20 |
+
# 🧠 Prediction Function
|
| 21 |
+
def predict_alzheimers(age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4):
|
| 22 |
+
features = preprocess(age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4)
|
| 23 |
+
|
| 24 |
+
# Predict probability of class 1 (positive)
|
| 25 |
proba = model.predict_proba(features)[0][1]
|
| 26 |
proba_percent = round(proba * 100, 2)
|
| 27 |
|
|
|
|
| 30 |
else:
|
| 31 |
return f"✅ Likely Alzheimer’s Negative ({100 - proba_percent}%)"
|
| 32 |
|
| 33 |
+
# 🎨 Gradio Interface
|
|
|
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=predict_alzheimers,
|
| 36 |
inputs=[
|
|
|
|
| 47 |
],
|
| 48 |
outputs="text",
|
| 49 |
title="Alzheimer’s Prediction App",
|
| 50 |
+
description="🔍 Enter details to predict Alzheimer’s risk using XGBoost model"
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|