-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
280 lines (216 loc) · 8.43 KB
/
app.py
File metadata and controls
280 lines (216 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import json
from pathlib import Path
import joblib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import shap
import streamlit as st
import sys
PROJECT_ROOT = Path(__file__).resolve().parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.append(str(PROJECT_ROOT))
from src.config import PROCESSED_DATA_DIR, TARGET_COL, TIME_COL # type: ignore
@st.cache_resource
def load_model():
model_path = PROJECT_ROOT / "models" / "satisfaction_pipeline.joblib"
if not model_path.exists():
st.error(
f"Model not found at {model_path}. Run `python -m src.train_model` first."
)
st.stop()
model = joblib.load(model_path)
return model
@st.cache_data
def load_sample_data():
test_path = PROCESSED_DATA_DIR / "sessions_test.csv"
if not test_path.exists():
st.error(
f"Processed test data not found at {test_path}. "
"Run `python -m src.data_prep` first."
)
st.stop()
df = pd.read_csv(test_path)
return df
def engineer_time_features(df: pd.DataFrame) -> pd.DataFrame:
if TIME_COL not in df.columns:
return df
df = df.copy()
df[TIME_COL] = pd.to_datetime(df[TIME_COL])
df["hour_of_day"] = df[TIME_COL].dt.hour
df["day_of_week"] = df[TIME_COL].dt.dayofweek
df["is_weekend"] = df["day_of_week"].isin([5, 6]).astype(int)
return df
def score_sessions(model, df: pd.DataFrame) -> pd.DataFrame:
# Ensure time features exist
df_feat = engineer_time_features(df)
df_features = df_feat.drop(columns=[TARGET_COL], errors="ignore")
preds = model.predict(df_features)
proba = model.predict_proba(df_features)
df_scored = df_feat.copy()
df_scored["pred_satisfaction"] = preds
classes = model.classes_
for i, c in enumerate(classes):
df_scored[f"p_rating_{c}"] = proba[:, i]
return df_scored
@st.cache_resource
def get_shap_explainer(_model):
preprocessor = _model.named_steps["preprocess"]
clf = _model.named_steps["clf"]
explainer = shap.TreeExplainer(clf)
feature_names = preprocessor.get_feature_names_out()
return explainer, preprocessor, feature_names
def plot_single_shap_bar(shap_values, feature_names, max_features: int = 10):
shap_values = np.asarray(shap_values).reshape(-1)
feature_names = np.asarray(feature_names)
n_features = min(len(shap_values), len(feature_names))
shap_values = shap_values[:n_features]
feature_names = feature_names[:n_features]
abs_vals = np.abs(shap_values)
idx_sorted = np.argsort(abs_vals)[::-1][:max_features]
selected_shap = shap_values[idx_sorted]
selected_names = feature_names[idx_sorted]
fig, ax = plt.subplots(figsize=(6, 4))
y_pos = np.arange(len(selected_names))
ax.barh(y_pos, selected_shap)
ax.set_yticks(y_pos)
ax.set_yticklabels(selected_names)
ax.invert_yaxis()
ax.set_xlabel("SHAP value (impact on satisfaction prediction)")
ax.set_title("Top feature contributions for this session")
plt.tight_layout()
return fig
def explain_single_session(model, df_scored: pd.DataFrame, row_idx: int):
explainer, preprocessor, feature_names = get_shap_explainer(model)
cols_to_drop = [TARGET_COL, "pred_satisfaction"]
cols_to_drop.extend([c for c in df_scored.columns if c.startswith("p_rating_")])
features_df = df_scored.drop(columns=[c for c in cols_to_drop if c in df_scored.columns])
x_row = features_df.iloc[[row_idx]]
x_transformed = preprocessor.transform(x_row)
try:
import scipy.sparse as sp
if sp.issparse(x_transformed):
x_for_shap = x_transformed.toarray()
else:
x_for_shap = x_transformed
except ImportError:
x_for_shap = x_transformed
shap_vals = explainer.shap_values(x_for_shap)
# Multiclass: shap_vals is list [class0,...]
if isinstance(shap_vals, list):
# choose class predicted for this row
pred_class = df_scored.iloc[row_idx]["pred_satisfaction"]
classes = model.classes_.tolist()
class_index = classes.index(pred_class)
shap_for_class = shap_vals[class_index][0]
else:
shap_for_class = shap_vals[0]
fig = plot_single_shap_bar(shap_for_class, feature_names)
return fig
def main():
st.set_page_config(
page_title="AI Assistant Satisfaction Prediction",
layout="wide",
)
st.title("AI Assistant Satisfaction Prediction Engine")
st.markdown(
"""
This dashboard uses a trained machine learning model to predict **user satisfaction**
with an AI assistant based on usage behavior, and explains which features drive each prediction.
- Upload a CSV of sessions or use the sample test set
- See **satisfaction distribution** across devices, usage categories, and models
- Explore **per-session predictions** and **SHAP explanations**
"""
)
model = load_model()
st.sidebar.header("Data Source")
uploaded_file = st.sidebar.file_uploader("Upload sessions CSV", type=["csv"])
if uploaded_file is not None:
df_raw = pd.read_csv(uploaded_file)
st.sidebar.success("Using uploaded data.")
else:
df_raw = load_sample_data()
st.sidebar.info("Using sample test data from the project.")
df_scored = score_sessions(model, df_raw)
st.subheader("Overview")
total_sessions = len(df_scored)
true_available = TARGET_COL in df_scored.columns
pred_distribution = df_scored["pred_satisfaction"].value_counts(normalize=True).sort_index()
col1, col2, col3 = st.columns(3)
col1.metric("Total sessions", f"{total_sessions:,}")
if true_available:
true_distribution = (
df_scored[TARGET_COL].value_counts(normalize=True).sort_index()
)
avg_true = df_scored[TARGET_COL].mean()
col2.metric("Avg true satisfaction", f"{avg_true:.2f}/5")
else:
col2.metric("Avg true satisfaction", "Unknown")
avg_pred = df_scored["pred_satisfaction"].mean()
col3.metric("Avg predicted satisfaction", f"{avg_pred:.2f}/5")
st.subheader("Satisfaction Distribution")
c1, c2 = st.columns([2, 3])
with c1:
fig, ax = plt.subplots()
idx = sorted(pred_distribution.index.tolist())
pred_vals = [pred_distribution.get(i, 0) for i in idx]
if true_available:
true_distribution = (
df_scored[TARGET_COL].value_counts(normalize=True).sort_index()
)
true_vals = [true_distribution.get(i, 0) for i in idx]
else:
true_vals = None
width = 0.35
x = np.arange(len(idx))
if true_vals is not None:
ax.bar(x - width / 2, true_vals, width, label="True")
ax.bar(x + width / 2, pred_vals, width, label="Predicted")
else:
ax.bar(x, pred_vals, width, label="Predicted")
ax.set_xticks(x)
ax.set_xticklabels(idx)
ax.set_ylabel("Proportion")
ax.set_xlabel("Satisfaction rating")
ax.set_title("Satisfaction Distribution")
ax.legend()
st.pyplot(fig)
with c2:
st.markdown("**Scored sessions (top 30)**")
st.dataframe(df_scored.head(30))
st.subheader("Segmented View")
seg_col = st.selectbox(
"Group by feature",
options=["device", "usage_category", "assistant_model"],
)
if seg_col in df_scored.columns:
grouped = df_scored.groupby(seg_col)["pred_satisfaction"].mean().sort_values(
ascending=False
)
fig, ax = plt.subplots()
ax.bar(grouped.index.astype(str), grouped.values)
ax.set_ylabel("Avg predicted satisfaction")
ax.set_title(f"Average predicted satisfaction by {seg_col}")
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
st.pyplot(fig)
st.subheader("Explain a Single Session")
if len(df_scored) == 0:
st.warning("No data available.")
return
row_idx = st.number_input(
"Row index to explain (0-based)", min_value=0, max_value=len(df_scored) - 1, value=0
)
row = df_scored.iloc[row_idx]
st.markdown("**Selected session**")
st.write(row.to_frame().T)
st.markdown("**Model prediction**")
st.write(
f"Predicted satisfaction: **{row['pred_satisfaction']}**"
)
st.markdown("**Feature contribution (SHAP)**")
with st.spinner("Computing SHAP values..."):
fig_shap = explain_single_session(model, df_scored, row_idx)
st.pyplot(fig_shap)
if __name__ == "__main__":
main()