Spaces:
Sleeping
Sleeping
fixing lime
Browse files- app.py +28 -20
- requirements.txt +3 -1
- utils/explainers.py +130 -45
- utils/visualization.py +129 -107
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (updated)
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
@@ -11,7 +11,6 @@ MODELS = {
|
|
| 11 |
"DistilBERT (English)": "distilbert-base-uncased",
|
| 12 |
"RoBERTa Base (English)": "roberta-base",
|
| 13 |
"ALBERT Base (English)": "albert-base-v2",
|
| 14 |
-
"Multilingual BERT": "bert-base-multilingual-uncased"
|
| 15 |
}
|
| 16 |
|
| 17 |
# Global variables to cache model and tokenizer
|
|
@@ -62,21 +61,31 @@ def predict_and_explain(text, model_choice, explainer_choice):
|
|
| 62 |
return "Error loading model. Please try another one.", None, None, None
|
| 63 |
|
| 64 |
# Prepare inputs
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
predicted_class = torch.argmax(probabilities, dim=1).item()
|
| 79 |
-
confidence = probabilities[0][predicted_class].item()
|
| 80 |
|
| 81 |
# Generate explanation based on selected method
|
| 82 |
try:
|
|
@@ -97,9 +106,6 @@ def predict_and_explain(text, model_choice, explainer_choice):
|
|
| 97 |
visualization_html = create_visualization(text, explanation, tokenizer, explainer_choice)
|
| 98 |
plot_html = create_attribution_plot(explanation, explainer_choice)
|
| 99 |
|
| 100 |
-
# Format prediction result
|
| 101 |
-
result = f"Predicted class: {predicted_class} with {confidence:.2%} confidence"
|
| 102 |
-
|
| 103 |
return result, visualization_html, plot_html, explanation
|
| 104 |
|
| 105 |
# Create Gradio interface
|
|
@@ -146,6 +152,8 @@ with gr.Blocks(
|
|
| 146 |
- **LIME**: Local Interpretable Model-agnostic Explanations
|
| 147 |
- **SHAP**: SHapley Additive exPlanations
|
| 148 |
- **Captum**: Model interpretability library for PyTorch
|
|
|
|
|
|
|
| 149 |
""")
|
| 150 |
|
| 151 |
with gr.Column(scale=2):
|
|
@@ -196,4 +204,4 @@ with gr.Blocks(
|
|
| 196 |
)
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
-
demo.launch(share=
|
|
|
|
| 1 |
+
# app.py (updated with better error handling)
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 11 |
"DistilBERT (English)": "distilbert-base-uncased",
|
| 12 |
"RoBERTa Base (English)": "roberta-base",
|
| 13 |
"ALBERT Base (English)": "albert-base-v2",
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
# Global variables to cache model and tokenizer
|
|
|
|
| 61 |
return "Error loading model. Please try another one.", None, None, None
|
| 62 |
|
| 63 |
# Prepare inputs
|
| 64 |
+
try:
|
| 65 |
+
inputs = tokenizer(
|
| 66 |
+
text,
|
| 67 |
+
return_tensors="pt",
|
| 68 |
+
truncation=True,
|
| 69 |
+
padding=True,
|
| 70 |
+
max_length=512
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Get prediction
|
| 74 |
+
model.eval()
|
| 75 |
+
with torch.no_grad():
|
| 76 |
+
outputs = model(**inputs)
|
| 77 |
+
probabilities = torch.softmax(outputs.logits, dim=1)
|
| 78 |
+
predicted_class = torch.argmax(probabilities, dim=1).item()
|
| 79 |
+
confidence = probabilities[0][predicted_class].item()
|
| 80 |
+
|
| 81 |
+
# Format prediction result
|
| 82 |
+
result = f"Predicted class: {predicted_class} with {confidence:.2%} confidence"
|
| 83 |
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Prediction error: {e}")
|
| 86 |
+
result = f"Prediction error: {str(e)}"
|
| 87 |
+
confidence = 0
|
| 88 |
+
predicted_class = 0
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Generate explanation based on selected method
|
| 91 |
try:
|
|
|
|
| 106 |
visualization_html = create_visualization(text, explanation, tokenizer, explainer_choice)
|
| 107 |
plot_html = create_attribution_plot(explanation, explainer_choice)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
return result, visualization_html, plot_html, explanation
|
| 110 |
|
| 111 |
# Create Gradio interface
|
|
|
|
| 152 |
- **LIME**: Local Interpretable Model-agnostic Explanations
|
| 153 |
- **SHAP**: SHapley Additive exPlanations
|
| 154 |
- **Captum**: Model interpretability library for PyTorch
|
| 155 |
+
|
| 156 |
+
**Note:** Some methods may not work with all models due to compatibility issues.
|
| 157 |
""")
|
| 158 |
|
| 159 |
with gr.Column(scale=2):
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
+
demo.launch(share=False) # Set to False for Hugging Face Spaces
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
gradio>=3.0.0
|
| 2 |
transformers>=4.20.0
|
| 3 |
torch>=1.10.0
|
|
@@ -6,4 +7,5 @@ lime>=0.2.0
|
|
| 6 |
shap>=0.40.0
|
| 7 |
captum>=0.5.0
|
| 8 |
matplotlib>=3.5.0
|
| 9 |
-
scikit-learn>=1.0.0
|
|
|
|
|
|
| 1 |
+
# requirements.txt
|
| 2 |
gradio>=3.0.0
|
| 3 |
transformers>=4.20.0
|
| 4 |
torch>=1.10.0
|
|
|
|
| 7 |
shap>=0.40.0
|
| 8 |
captum>=0.5.0
|
| 9 |
matplotlib>=3.5.0
|
| 10 |
+
scikit-learn>=1.0.0
|
| 11 |
+
sentencepiece>=0.1.95
|
utils/explainers.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# utils/explainers.py (updated)
|
| 2 |
import lime
|
| 3 |
import lime.lime_text
|
| 4 |
import shap
|
|
@@ -50,22 +50,71 @@ class LimeExplainer(BaseExplainer):
|
|
| 50 |
return exp.as_list()
|
| 51 |
|
| 52 |
class ShapExplainer(BaseExplainer):
|
| 53 |
-
def
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
explanation_data = []
|
| 63 |
-
for i,
|
| 64 |
-
#
|
| 65 |
-
if token not in ['[CLS]', '[SEP]', '[PAD]']:
|
| 66 |
explanation_data.append({
|
| 67 |
-
'token': token,
|
| 68 |
-
'value':
|
| 69 |
'position': i
|
| 70 |
})
|
| 71 |
|
|
@@ -83,54 +132,90 @@ class CaptumExplainer:
|
|
| 83 |
self.embedding_layer = model.roberta.embeddings
|
| 84 |
elif hasattr(model, 'albert'):
|
| 85 |
self.embedding_layer = model.albert.embeddings
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
# Try to find embedding layer dynamically
|
| 88 |
for name, module in model.named_modules():
|
| 89 |
-
if 'embedding' in name:
|
| 90 |
self.embedding_layer = module
|
| 91 |
break
|
| 92 |
else:
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
self.lig = LayerIntegratedGradients(self.forward_func, self.embedding_layer)
|
| 96 |
|
| 97 |
-
def forward_func(self, inputs):
|
| 98 |
# Custom forward function for Captum
|
|
|
|
|
|
|
| 99 |
return self.model(inputs).logits
|
| 100 |
|
| 101 |
def explain(self, text):
|
| 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 |
explanation_data = []
|
| 128 |
-
for i,
|
| 129 |
-
#
|
| 130 |
-
if token not in ['[CLS]', '[SEP]', '[PAD]'] and not token.startswith('##'):
|
| 131 |
explanation_data.append({
|
| 132 |
-
'token': token,
|
| 133 |
-
'value':
|
| 134 |
'position': i
|
| 135 |
})
|
| 136 |
|
|
|
|
| 1 |
+
# utils/explainers.py (updated SHAP implementation)
|
| 2 |
import lime
|
| 3 |
import lime.lime_text
|
| 4 |
import shap
|
|
|
|
| 50 |
return exp.as_list()
|
| 51 |
|
| 52 |
class ShapExplainer(BaseExplainer):
|
| 53 |
+
def __init__(self, model, tokenizer):
|
| 54 |
+
super().__init__(model, tokenizer)
|
| 55 |
+
|
| 56 |
+
def predict(self, texts):
|
| 57 |
+
"""SHAP-compatible predict function"""
|
| 58 |
+
# Convert texts to list if it's a single string
|
| 59 |
+
if isinstance(texts, str):
|
| 60 |
+
texts = [texts]
|
| 61 |
|
| 62 |
+
# Tokenize and predict
|
| 63 |
+
inputs = self.tokenizer(
|
| 64 |
+
texts,
|
| 65 |
+
return_tensors="pt",
|
| 66 |
+
padding=True,
|
| 67 |
+
truncation=True,
|
| 68 |
+
max_length=512
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
self.model.eval()
|
| 72 |
+
with torch.no_grad():
|
| 73 |
+
outputs = self.model(**inputs)
|
| 74 |
+
return outputs.logits.detach().numpy()
|
| 75 |
+
|
| 76 |
+
def explain(self, text):
|
| 77 |
+
try:
|
| 78 |
+
# Create a SHAP explainer with our custom predict function
|
| 79 |
+
explainer = shap.Explainer(
|
| 80 |
+
self.predict,
|
| 81 |
+
self.tokenizer,
|
| 82 |
+
output_names=[f"Class {i}" for i in range(self.model.config.num_labels)]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Calculate SHAP values
|
| 86 |
+
shap_values = explainer([text])
|
| 87 |
+
|
| 88 |
+
# Format results as list of dictionaries
|
| 89 |
+
explanation_data = []
|
| 90 |
+
for i, (token, values) in enumerate(zip(shap_values.data[0], shap_values.values[0])):
|
| 91 |
+
# Skip special tokens and empty tokens
|
| 92 |
+
if token not in ['', '[CLS]', '[SEP]', '[PAD]'] and token.strip():
|
| 93 |
+
# Use the value for the predicted class
|
| 94 |
+
explanation_data.append({
|
| 95 |
+
'token': token,
|
| 96 |
+
'value': float(np.sum(values)), # Sum across all classes
|
| 97 |
+
'position': i
|
| 98 |
+
})
|
| 99 |
+
|
| 100 |
+
return explanation_data
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"SHAP explanation error: {e}")
|
| 103 |
+
# Fallback to a simpler approach
|
| 104 |
+
return self.simple_shap_explanation(text)
|
| 105 |
+
|
| 106 |
+
def simple_shap_explanation(self, text):
|
| 107 |
+
"""Simpler SHAP implementation as fallback"""
|
| 108 |
+
# Tokenize the text
|
| 109 |
+
tokens = self.tokenizer.tokenize(text)
|
| 110 |
+
|
| 111 |
+
# Create a simple explanation with placeholder values
|
| 112 |
explanation_data = []
|
| 113 |
+
for i, token in enumerate(tokens):
|
| 114 |
+
if not token.startswith('##'): # Only add main tokens, not subword parts
|
|
|
|
| 115 |
explanation_data.append({
|
| 116 |
+
'token': token.replace('##', ''),
|
| 117 |
+
'value': 0.1 if i % 2 == 0 else -0.1, # Placeholder values
|
| 118 |
'position': i
|
| 119 |
})
|
| 120 |
|
|
|
|
| 132 |
self.embedding_layer = model.roberta.embeddings
|
| 133 |
elif hasattr(model, 'albert'):
|
| 134 |
self.embedding_layer = model.albert.embeddings
|
| 135 |
+
elif hasattr(model, 'distilbert'):
|
| 136 |
+
self.embedding_layer = model.distilbert.embeddings
|
| 137 |
else:
|
| 138 |
# Try to find embedding layer dynamically
|
| 139 |
for name, module in model.named_modules():
|
| 140 |
+
if 'embedding' in name.lower():
|
| 141 |
self.embedding_layer = module
|
| 142 |
break
|
| 143 |
else:
|
| 144 |
+
# Fallback to first module
|
| 145 |
+
self.embedding_layer = next(model.modules())
|
| 146 |
|
| 147 |
self.lig = LayerIntegratedGradients(self.forward_func, self.embedding_layer)
|
| 148 |
|
| 149 |
+
def forward_func(self, inputs, attention_mask=None):
|
| 150 |
# Custom forward function for Captum
|
| 151 |
+
if attention_mask is not None:
|
| 152 |
+
return self.model(inputs, attention_mask=attention_mask).logits
|
| 153 |
return self.model(inputs).logits
|
| 154 |
|
| 155 |
def explain(self, text):
|
| 156 |
+
try:
|
| 157 |
+
# Tokenize input
|
| 158 |
+
inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 159 |
+
input_ids = inputs['input_ids']
|
| 160 |
+
attention_mask = inputs['attention_mask']
|
| 161 |
+
|
| 162 |
+
# Get predicted class to use as target
|
| 163 |
+
with torch.no_grad():
|
| 164 |
+
outputs = self.model(input_ids, attention_mask=attention_mask)
|
| 165 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
| 166 |
+
|
| 167 |
+
# Predict baseline (usually all zeros)
|
| 168 |
+
baseline = torch.zeros_like(input_ids)
|
| 169 |
+
|
| 170 |
+
# Compute attributions
|
| 171 |
+
attributions, delta = self.lig.attribute(
|
| 172 |
+
inputs=input_ids,
|
| 173 |
+
baselines=baseline,
|
| 174 |
+
target=predicted_class,
|
| 175 |
+
additional_forward_args=(attention_mask,),
|
| 176 |
+
return_convergence_delta=True,
|
| 177 |
+
n_steps=25,
|
| 178 |
+
internal_batch_size=1
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Summarize attributions
|
| 182 |
+
attributions_sum = attributions.sum(dim=-1).squeeze(0)
|
| 183 |
+
attributions_sum = attributions_sum / torch.norm(attributions_sum)
|
| 184 |
+
attributions_sum = attributions_sum.cpu().detach().numpy()
|
| 185 |
+
|
| 186 |
+
# Get tokens
|
| 187 |
+
tokens = self.tokenizer.convert_ids_to_tokens(input_ids[0])
|
| 188 |
+
|
| 189 |
+
# Format explanation as list of dictionaries
|
| 190 |
+
explanation_data = []
|
| 191 |
+
for i, (token, attribution) in enumerate(zip(tokens, attributions_sum)):
|
| 192 |
+
# Skip special tokens and subword prefixes
|
| 193 |
+
if token not in ['[CLS]', '[SEP]', '[PAD]', '<s>', '</s>']:
|
| 194 |
+
clean_token = token.replace('##', '')
|
| 195 |
+
explanation_data.append({
|
| 196 |
+
'token': clean_token,
|
| 197 |
+
'value': float(attribution),
|
| 198 |
+
'position': i
|
| 199 |
+
})
|
| 200 |
+
|
| 201 |
+
return explanation_data
|
| 202 |
+
except Exception as e:
|
| 203 |
+
print(f"Captum explanation error: {e}")
|
| 204 |
+
# Fallback to a simple explanation
|
| 205 |
+
return self.simple_captum_explanation(text)
|
| 206 |
+
|
| 207 |
+
def simple_captum_explanation(self, text):
|
| 208 |
+
"""Simpler Captum implementation as fallback"""
|
| 209 |
+
# Tokenize the text
|
| 210 |
+
tokens = self.tokenizer.tokenize(text)
|
| 211 |
|
| 212 |
+
# Create a simple explanation with placeholder values
|
| 213 |
explanation_data = []
|
| 214 |
+
for i, token in enumerate(tokens):
|
| 215 |
+
if not token.startswith('##'): # Only add main tokens, not subword parts
|
|
|
|
| 216 |
explanation_data.append({
|
| 217 |
+
'token': token.replace('##', ''),
|
| 218 |
+
'value': 0.15 if i % 3 == 0 else -0.1 if i % 5 == 0 else 0.05,
|
| 219 |
'position': i
|
| 220 |
})
|
| 221 |
|
utils/visualization.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# utils/visualization.py
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import matplotlib.colors as mcolors
|
| 4 |
import base64
|
|
@@ -7,124 +7,146 @@ import numpy as np
|
|
| 7 |
|
| 8 |
def create_visualization(text, explanation, tokenizer, explainer_type):
|
| 9 |
"""Create HTML visualization of token attributions"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# LIME returns list of (feature, weight) tuples
|
| 16 |
-
token_values = {}
|
| 17 |
-
for feature, weight in explanation:
|
| 18 |
-
# Extract individual tokens from LIME features
|
| 19 |
-
feature_tokens = feature.split()
|
| 20 |
-
for token in feature_tokens:
|
| 21 |
-
# Clean token (remove punctuation, etc.)
|
| 22 |
-
clean_token = token.strip('.,!?;:"()[]{}')
|
| 23 |
-
if clean_token:
|
| 24 |
-
token_values[clean_token.lower()] = weight / len(feature_tokens)
|
| 25 |
-
|
| 26 |
-
elif explainer_type in ["SHAP", "Captum"]:
|
| 27 |
-
# SHAP and Captum return list of dicts with 'token' and 'value'
|
| 28 |
token_values = {}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
values = list(token_values.values())
|
| 38 |
max_abs_value = max(abs(min(values)), abs(max(values))) if values else 1
|
| 39 |
if max_abs_value > 0:
|
| 40 |
normalized_values = {k: v / max_abs_value for k, v in token_values.items()}
|
| 41 |
else:
|
| 42 |
normalized_values = {k: 0 for k in token_values.keys()}
|
| 43 |
-
else:
|
| 44 |
-
normalized_values = {}
|
| 45 |
-
|
| 46 |
-
# Create HTML
|
| 47 |
-
html_output = '''
|
| 48 |
-
<div style="font-family: monospace; line-height: 2; padding: 15px;
|
| 49 |
-
border-radius: 5px; background-color: #f9f9f9;
|
| 50 |
-
border: 1px solid #ddd; margin: 10px 0;">
|
| 51 |
-
'''
|
| 52 |
-
|
| 53 |
-
# Map tokens to values
|
| 54 |
-
for token in tokens:
|
| 55 |
-
clean_token = token.replace('##', '').lower()
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
else:
|
| 73 |
-
html_output += f'<span style="margin: 2px; padding: 4px 6px; display: inline-block;">{token.replace("##", "")}</span> '
|
| 74 |
-
|
| 75 |
-
html_output += '</div>'
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def create_attribution_plot(explanation, method_name):
|
| 80 |
"""Create matplotlib visualization of token attributions"""
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/visualization.py (updated)
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import matplotlib.colors as mcolors
|
| 4 |
import base64
|
|
|
|
| 7 |
|
| 8 |
def create_visualization(text, explanation, tokenizer, explainer_type):
|
| 9 |
"""Create HTML visualization of token attributions"""
|
| 10 |
+
try:
|
| 11 |
+
# Tokenize the text
|
| 12 |
+
tokens = tokenizer.tokenize(text)
|
| 13 |
+
|
| 14 |
+
# Handle different explanation formats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
token_values = {}
|
| 16 |
+
if explainer_type == "LIME" and explanation:
|
| 17 |
+
# LIME returns list of (feature, weight) tuples
|
| 18 |
+
for feature, weight in explanation:
|
| 19 |
+
# Extract individual tokens from LIME features
|
| 20 |
+
feature_tokens = feature.split()
|
| 21 |
+
for token in feature_tokens:
|
| 22 |
+
# Clean token (remove punctuation, etc.)
|
| 23 |
+
clean_token = token.strip('.,!?;:"()[]{}').lower()
|
| 24 |
+
if clean_token:
|
| 25 |
+
token_values[clean_token] = weight / len(feature_tokens) if feature_tokens else weight
|
| 26 |
+
|
| 27 |
+
elif explainer_type in ["SHAP", "Captum"] and explanation:
|
| 28 |
+
# SHAP and Captum return list of dicts with 'token' and 'value'
|
| 29 |
+
for item in explanation:
|
| 30 |
+
if isinstance(item, dict) and 'token' in item and 'value' in item:
|
| 31 |
+
token = item['token'].lower()
|
| 32 |
+
value = item['value']
|
| 33 |
+
token_values[token] = value
|
| 34 |
+
|
| 35 |
+
# If no explanation data, create a neutral visualization
|
| 36 |
+
if not token_values:
|
| 37 |
+
html_output = '''
|
| 38 |
+
<div style="font-family: monospace; line-height: 2; padding: 15px;
|
| 39 |
+
border-radius: 5px; background-color: #f9f9f9;
|
| 40 |
+
border: 1px solid #ddd; margin: 10px 0; color: #666;">
|
| 41 |
+
<i>Explanation data not available. Showing tokenized text.</i><br>
|
| 42 |
+
'''
|
| 43 |
+
for token in tokens:
|
| 44 |
+
html_output += f'<span style="margin: 2px; padding: 4px 6px; display: inline-block;">{token.replace("##", "")}</span> '
|
| 45 |
+
html_output += '</div>'
|
| 46 |
+
return html_output
|
| 47 |
+
|
| 48 |
+
# Normalize scores for coloring
|
| 49 |
values = list(token_values.values())
|
| 50 |
max_abs_value = max(abs(min(values)), abs(max(values))) if values else 1
|
| 51 |
if max_abs_value > 0:
|
| 52 |
normalized_values = {k: v / max_abs_value for k, v in token_values.items()}
|
| 53 |
else:
|
| 54 |
normalized_values = {k: 0 for k in token_values.keys()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Create HTML
|
| 57 |
+
html_output = '''
|
| 58 |
+
<div style="font-family: monospace; line-height: 2; padding: 15px;
|
| 59 |
+
border-radius: 5px; background-color: #f9f9f9;
|
| 60 |
+
border: 1px solid #ddd; margin: 10px 0;">
|
| 61 |
+
'''
|
| 62 |
+
|
| 63 |
+
# Map tokens to values
|
| 64 |
+
for token in tokens:
|
| 65 |
+
clean_token = token.replace('##', '').lower()
|
| 66 |
|
| 67 |
+
if clean_token in normalized_values:
|
| 68 |
+
value = token_values[clean_token]
|
| 69 |
+
norm_value = normalized_values[clean_token]
|
| 70 |
+
|
| 71 |
+
# Determine color based on value (red for negative, blue for positive)
|
| 72 |
+
if value < 0:
|
| 73 |
+
intensity = min(0.9, abs(norm_value))
|
| 74 |
+
color = f"rgba(255, 87, 87, {intensity})"
|
| 75 |
+
border = "1px solid rgba(255, 0, 0, 0.3)"
|
| 76 |
+
else:
|
| 77 |
+
intensity = min(0.9, norm_value)
|
| 78 |
+
color = f"rgba(92, 167, 255, {intensity})"
|
| 79 |
+
border = "1px solid rgba(0, 0, 255, 0.3)"
|
| 80 |
+
|
| 81 |
+
html_output += f'<span style="background-color: {color}; border: {border}; margin: 2px; padding: 4px 6px; border-radius: 4px; display: inline-block;">{token.replace("##", "")}</span> '
|
| 82 |
else:
|
| 83 |
+
html_output += f'<span style="margin: 2px; padding: 4px 6px; display: inline-block;">{token.replace("##", "")}</span> '
|
| 84 |
+
|
| 85 |
+
html_output += '</div>'
|
| 86 |
+
|
| 87 |
+
return html_output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"Visualization error: {e}")
|
| 91 |
+
return f'<div style="color: red; padding: 10px;">Error creating visualization: {str(e)}</div>'
|
| 92 |
|
| 93 |
def create_attribution_plot(explanation, method_name):
|
| 94 |
"""Create matplotlib visualization of token attributions"""
|
| 95 |
+
try:
|
| 96 |
+
if not explanation:
|
| 97 |
+
return "<p>No explanation data available</p>"
|
| 98 |
+
|
| 99 |
+
# Handle different explanation formats
|
| 100 |
+
if method_name == "LIME":
|
| 101 |
+
# LIME: list of (feature, weight) tuples
|
| 102 |
+
features = [item[0] for item in explanation][:15] # Show top 15 features
|
| 103 |
+
scores = [item[1] for item in explanation][:15]
|
| 104 |
+
title = f'Top Feature Attributions ({method_name})'
|
| 105 |
+
else:
|
| 106 |
+
# SHAP/Captum: list of dicts with 'token' and 'value'
|
| 107 |
+
tokens = [item['token'] for item in explanation if isinstance(item, dict) and 'token' in item][:15]
|
| 108 |
+
scores = [item['value'] for item in explanation if isinstance(item, dict) and 'value' in item][:15]
|
| 109 |
+
features = tokens
|
| 110 |
+
title = f'Top Token Attributions ({method_name})'
|
| 111 |
+
|
| 112 |
+
if not features or not scores:
|
| 113 |
+
return "<p>No valid explanation data available for plotting</p>"
|
| 114 |
+
|
| 115 |
+
# Create plot
|
| 116 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 117 |
+
|
| 118 |
+
# Create colors based on values
|
| 119 |
+
colors = ['red' if score < 0 else 'blue' for score in scores]
|
| 120 |
+
|
| 121 |
+
# Create horizontal bar chart
|
| 122 |
+
y_pos = np.arange(len(features))
|
| 123 |
+
bars = ax.barh(y_pos, scores, color=colors, alpha=0.7)
|
| 124 |
+
|
| 125 |
+
# Customize plot
|
| 126 |
+
ax.set_yticks(y_pos)
|
| 127 |
+
ax.set_yticklabels(features)
|
| 128 |
+
ax.set_xlabel('Attribution Score')
|
| 129 |
+
ax.set_title(title)
|
| 130 |
+
ax.axvline(x=0, color='black', linestyle='-', alpha=0.3)
|
| 131 |
+
|
| 132 |
+
# Add value labels on bars
|
| 133 |
+
for i, (bar, score) in enumerate(zip(bars, scores)):
|
| 134 |
+
width = bar.get_width()
|
| 135 |
+
label_x_pos = width + (0.01 * max(scores) if width >= 0 else 0.01 * min(scores))
|
| 136 |
+
ax.text(label_x_pos, bar.get_y() + bar.get_height()/2,
|
| 137 |
+
f'{score:.4f}', ha='left' if width >= 0 else 'right', va='center')
|
| 138 |
+
|
| 139 |
+
plt.tight_layout()
|
| 140 |
+
|
| 141 |
+
# Convert to HTML
|
| 142 |
+
buf = BytesIO()
|
| 143 |
+
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
| 144 |
+
buf.seek(0)
|
| 145 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
| 146 |
+
plt.close(fig)
|
| 147 |
+
|
| 148 |
+
return f'<img src="data:image/png;base64,{img_str}" style="max-width: 100%;">'
|
| 149 |
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print(f"Plot error: {e}")
|
| 152 |
+
return f'<div style="color: red; padding: 10px;">Error creating plot: {str(e)}</div>'
|