APM_3 / app.py
AIMLdeepanshu's picture
Update app.py
a8a9418 verified
import gradio as gr
import joblib
import numpy as np
# Load trained model
model = joblib.load("xgboost_model.pkl")
# 🔧 Preprocessing Function
def preprocess(age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4):
gender = 1 if gender == "Male" else 0
smoking = {"Non-Smoker": 0, "Smoker": 1, "Former Smoker": 2}[smoking]
alcohol = {"None": 0, "Moderate": 1, "Heavy": 2}[alcohol]
diabetes = 1 if diabetes == "Yes" else 0
hypertension = 1 if hypertension == "Yes" else 0
family_history = 1 if family_history == "Yes" else 0
apoe4 = 1 if apoe4 == "Yes" else 0
return np.array([[age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4]])
# 🧠 Prediction Function
def predict_alzheimers(age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4):
features = preprocess(age, gender, bmi, smoking, alcohol, diabetes, hypertension, family_history, cognitive_score, apoe4)
# Debug: print features being sent to model
print("🛠️ Processed input sent to model:", features)
# Predict probability of class 1 (positive)
proba = model.predict_proba(features)[0][1]
proba_percent = round(proba * 100, 2)
if proba >= 0.5:
return f"🧠 Likely Alzheimer’s Positive ({proba_percent}%)"
else:
return f"✅ Likely Alzheimer’s Negative ({100 - proba_percent}%)"
# 🎨 Gradio Interface
interface = gr.Interface(
fn=predict_alzheimers,
inputs=[
gr.Number(label="Age"),
gr.Radio(["Male", "Female"], label="Gender"),
gr.Number(label="BMI"),
gr.Radio(["Non-Smoker", "Smoker", "Former Smoker"], label="Smoking Status"),
gr.Radio(["None", "Moderate", "Heavy"], label="Alcohol Consumption"),
gr.Radio(["Yes", "No"], label="Diabetes"),
gr.Radio(["Yes", "No"], label="Hypertension"),
gr.Radio(["Yes", "No"], label="Family History of Alzheimer’s"),
gr.Number(label="Cognitive Test Score"),
gr.Radio(["Yes", "No"], label="APOE-ε4 allele Present?")
],
outputs="text",
title="Alzheimer’s Prediction App",
description="🔍 Enter details to predict Alzheimer’s risk using XGBoost model"
)
if __name__ == "__main__":
interface.launch()