Spaces:
Sleeping
Sleeping
Update src/webApp.py
Browse files- src/webApp.py +11 -32
src/webApp.py
CHANGED
|
@@ -139,11 +139,19 @@ def get_word_importance(review, tokenizer, model, predicted_label):
|
|
| 139 |
max_length=128,
|
| 140 |
return_attention_mask=True
|
| 141 |
)
|
|
|
|
| 142 |
with torch.no_grad():
|
| 143 |
-
outputs = model(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
tokens
|
| 146 |
-
attentions
|
| 147 |
cls_attention = attentions.mean(dim=0)[0].mean(dim=0)[0]
|
| 148 |
|
| 149 |
word_importance = []
|
|
@@ -157,35 +165,6 @@ def get_word_importance(review, tokenizer, model, predicted_label):
|
|
| 157 |
return word_importance[:15]
|
| 158 |
|
| 159 |
|
| 160 |
-
def word_importance_chart(word_importance, predicted_label):
|
| 161 |
-
if not word_importance:
|
| 162 |
-
return None
|
| 163 |
-
|
| 164 |
-
words, scores = zip(*word_importance)
|
| 165 |
-
colors = ["#e63946" if s > 0 else "#2a9d8f" for s in scores]
|
| 166 |
-
sentiment_word = "Positive" if predicted_label == "LABEL_1" else "Negative"
|
| 167 |
-
|
| 168 |
-
fig = go.Figure(go.Bar(
|
| 169 |
-
x=list(scores),
|
| 170 |
-
y=list(words),
|
| 171 |
-
orientation="h",
|
| 172 |
-
marker_color=colors,
|
| 173 |
-
text=[f"{v:+.4f}" for v in scores],
|
| 174 |
-
textposition="outside"
|
| 175 |
-
))
|
| 176 |
-
fig.update_layout(
|
| 177 |
-
title=f"Word Importance for {sentiment_word} Sentiment",
|
| 178 |
-
xaxis_title=f"Importance Score (positive = pushes toward {sentiment_word})",
|
| 179 |
-
height=450,
|
| 180 |
-
margin=dict(t=50, b=40, l=10, r=80),
|
| 181 |
-
plot_bgcolor="rgba(0,0,0,0)",
|
| 182 |
-
paper_bgcolor="rgba(0,0,0,0)",
|
| 183 |
-
font=dict(color="white"),
|
| 184 |
-
yaxis=dict(autorange="reversed")
|
| 185 |
-
)
|
| 186 |
-
return fig
|
| 187 |
-
|
| 188 |
-
|
| 189 |
def shap_bar_chart(shap_values, feature_names):
|
| 190 |
"""
|
| 191 |
Renders a horizontal bar chart of SHAP values for the suspicious review model.
|
|
|
|
| 139 |
max_length=128,
|
| 140 |
return_attention_mask=True
|
| 141 |
)
|
| 142 |
+
|
| 143 |
with torch.no_grad():
|
| 144 |
+
outputs = model(
|
| 145 |
+
**inputs,
|
| 146 |
+
output_attentions=True # explicitly request attentions
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Check attentions were returned
|
| 150 |
+
if not outputs.attentions:
|
| 151 |
+
return []
|
| 152 |
|
| 153 |
+
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 154 |
+
attentions = torch.stack(list(outputs.attentions))
|
| 155 |
cls_attention = attentions.mean(dim=0)[0].mean(dim=0)[0]
|
| 156 |
|
| 157 |
word_importance = []
|
|
|
|
| 165 |
return word_importance[:15]
|
| 166 |
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
def shap_bar_chart(shap_values, feature_names):
|
| 169 |
"""
|
| 170 |
Renders a horizontal bar chart of SHAP values for the suspicious review model.
|