JoyceTan's picture
Update app.py
4176e7f verified
import gradio as gr
import numpy as np
import joblib
import pandas as pd
# ------------------------------------------------------------
# AAC Participation Predictor (Gradio)
# - Loads sklearn preprocessor + model artifacts
# - Outputs Yes/No for the senior's likelihood to attend
# ------------------------------------------------------------
PREPROCESS_PATH = "aac_preprocess.joblib"
MODEL_PATH = "aac_mlp.joblib"
preprocess = joblib.load(PREPROCESS_PATH)
model = joblib.load(MODEL_PATH)
ACTIVITIES = [
"Elastic Band Workout",
"Chair Pilates",
"Memory Game",
"Digital Skills Workshop",
"Chinese New Year Celebration",
]
def predict_participation(
age: int,
gender: str,
single_household: str,
health_score: float,
mobility: str,
last_cny_attendance: str,
activity_type: str
):
"""Return Yes/No + probability."""
# Build one-row dataframe matching training columns
x = pd.DataFrame([{
"Age": age,
"Gender": "M" if gender == "Male" else "F",
"Single Household (Y/N)": "Y" if single_household == "Yes" else "N",
"Health Score (0-100)": float(health_score),
"Mobility (Low/Medium/High)": mobility,
"Last Chinese New Year Event Attendance": "Y" if last_cny_attendance == "Yes" else "N",
"Activity Type": activity_type,
}])
X_t = preprocess.transform(x)
prob = float(model.predict_proba(X_t)[:, 1][0])
THRESHOLD = 0.5
decision = "Yes" if prob >= THRESHOLD else "No"
return decision, round(prob, 4)
demo = gr.Interface(
fn=predict_participation,
inputs=[
gr.Slider(60, 100, step=1, label="Age", value=70),
gr.Dropdown(["Male", "Female"], label="Gender"),
gr.Radio(["Yes", "No"], label="Single Household?"),
gr.Slider(0, 100, step=0.1, label="Health Score (0-100)", value=75.0),
gr.Dropdown(["Low", "Medium", "High"], label="Mobility Level"),
gr.Radio(["Yes", "No"], label="Attended last Chinese New Year event?"),
gr.Dropdown(ACTIVITIES, label="Type of Activity")
],
outputs=[
gr.Textbox(label="Likely to Attend? (Yes/No)"),
gr.Number(label="Predicted attendance probability (0–1)"),
],
title="Senior Activity Participation Predictor (AAC)",
description=(
"Enter senior details + activity type to predict attendance likelihood. "
"This demo loads a trained model and returns a Yes/No recommendation."
)
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
ssr_mode=False
)