Spaces:
Sleeping
Sleeping
Fix Captum memory crash in all explainability tabs
Browse files- src/streamlit_app.py +79 -58
src/streamlit_app.py
CHANGED
|
@@ -68,6 +68,10 @@ try:
|
|
| 68 |
check_availability as check_captum_availability
|
| 69 |
)
|
| 70 |
CUSTOM_MODULES_AVAILABLE = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except ImportError as e:
|
| 72 |
st.error(f"β οΈ Custom modules not found: {e}")
|
| 73 |
CUSTOM_MODULES_AVAILABLE = False
|
|
@@ -570,36 +574,40 @@ def render_batch_explainability(results_df, text_column, model, tokenizer, label
|
|
| 570 |
st.warning("No word scores available")
|
| 571 |
|
| 572 |
elif explain_method == "Captum (IG)":
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
except Exception as e:
|
| 605 |
st.error(f"β Explanation failed: {str(e)}")
|
|
@@ -1005,32 +1013,36 @@ def main():
|
|
| 1005 |
st.dataframe(df, hide_index=True, use_container_width=True)
|
| 1006 |
|
| 1007 |
elif method == "Captum (IG)":
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
st.
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1034 |
|
| 1035 |
except Exception as e:
|
| 1036 |
st.error(f"β Explanation failed: {str(e)}")
|
|
@@ -1205,11 +1217,20 @@ def main():
|
|
| 1205 |
else:
|
| 1206 |
st.info("π‘ Upload CSV with a 'text' column")
|
| 1207 |
|
| 1208 |
-
uploaded_file = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1209 |
|
| 1210 |
if uploaded_file:
|
| 1211 |
try:
|
| 1212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1213 |
st.write("π **File Preview:**")
|
| 1214 |
st.dataframe(df.head(10), use_container_width=True)
|
| 1215 |
|
|
|
|
| 68 |
check_availability as check_captum_availability
|
| 69 |
)
|
| 70 |
CUSTOM_MODULES_AVAILABLE = True
|
| 71 |
+
except MemoryError:
|
| 72 |
+
st.warning("β οΈ Captum not available due to memory constraints.")
|
| 73 |
+
CUSTOM_MODULES_AVAILABLE = False
|
| 74 |
+
captum_available = False
|
| 75 |
except ImportError as e:
|
| 76 |
st.error(f"β οΈ Custom modules not found: {e}")
|
| 77 |
CUSTOM_MODULES_AVAILABLE = False
|
|
|
|
| 574 |
st.warning("No word scores available")
|
| 575 |
|
| 576 |
elif explain_method == "Captum (IG)":
|
| 577 |
+
try:
|
| 578 |
+
captum_exp = CaptumExplainer(
|
| 579 |
+
model, tokenizer, label_encoder, preprocessor,
|
| 580 |
+
emoji_to_nepali_map=EMOJI_TO_NEPALI
|
| 581 |
+
)
|
| 582 |
+
result = captum_exp.explain_and_visualize(
|
| 583 |
+
analysis['original_text'],
|
| 584 |
+
target=None,
|
| 585 |
+
n_steps=50,
|
| 586 |
+
save_dir=None,
|
| 587 |
+
show=False,
|
| 588 |
+
nepali_font=nepali_font
|
| 589 |
+
)
|
| 590 |
+
st.subheader("Captum Integrated Gradients")
|
| 591 |
+
col1, col2 = st.columns(2)
|
| 592 |
+
with col1:
|
| 593 |
+
st.markdown("**Bar Chart**")
|
| 594 |
+
st.pyplot(result['bar_chart'])
|
| 595 |
+
with col2:
|
| 596 |
+
st.markdown("**Heatmap**")
|
| 597 |
+
st.pyplot(result['heatmap'])
|
| 598 |
+
st.markdown("---")
|
| 599 |
+
st.markdown("**π Attribution Details:**")
|
| 600 |
+
st.write(f"**Convergence Delta:** {result['explanation']['convergence_delta']:.6f}")
|
| 601 |
+
word_attrs = result['explanation']['word_attributions']
|
| 602 |
+
if word_attrs:
|
| 603 |
+
df = pd.DataFrame(word_attrs, columns=['Word', 'Abs Score', 'Signed Score'])
|
| 604 |
+
df = df.sort_values('Abs Score', ascending=False)
|
| 605 |
+
st.dataframe(df, hide_index=True, use_container_width=True)
|
| 606 |
+
else:
|
| 607 |
+
st.warning("No word attributions available")
|
| 608 |
+
except (MemoryError, RuntimeError):
|
| 609 |
+
st.error("β Captum (Integrated Gradients) requires more memory than available on this server.")
|
| 610 |
+
st.info("π‘ **Tip:** Use LIME or SHAP instead β they work on cloud deployments. Captum works on local machines with more RAM/GPU.")
|
| 611 |
|
| 612 |
except Exception as e:
|
| 613 |
st.error(f"β Explanation failed: {str(e)}")
|
|
|
|
| 1013 |
st.dataframe(df, hide_index=True, use_container_width=True)
|
| 1014 |
|
| 1015 |
elif method == "Captum (IG)":
|
| 1016 |
+
try:
|
| 1017 |
+
captum_exp = CaptumExplainer(
|
| 1018 |
+
model, tokenizer, label_encoder, preprocessor,
|
| 1019 |
+
emoji_to_nepali_map=EMOJI_TO_NEPALI
|
| 1020 |
+
)
|
| 1021 |
+
result = captum_exp.explain_and_visualize(
|
| 1022 |
+
analysis['original_text'],
|
| 1023 |
+
target=None,
|
| 1024 |
+
n_steps=n_steps,
|
| 1025 |
+
save_dir=None,
|
| 1026 |
+
show=False,
|
| 1027 |
+
nepali_font=nepali_font
|
| 1028 |
+
)
|
| 1029 |
+
st.subheader("Captum Integrated Gradients")
|
| 1030 |
+
col1, col2 = st.columns(2)
|
| 1031 |
+
with col1:
|
| 1032 |
+
st.markdown("**Bar Chart**")
|
| 1033 |
+
st.pyplot(result['bar_chart'])
|
| 1034 |
+
with col2:
|
| 1035 |
+
st.markdown("**Heatmap**")
|
| 1036 |
+
st.pyplot(result['heatmap'])
|
| 1037 |
+
with st.expander("π Attribution Details"):
|
| 1038 |
+
st.write(f"**Convergence Delta:** {result['explanation']['convergence_delta']:.6f}")
|
| 1039 |
+
word_attrs = result['explanation']['word_attributions']
|
| 1040 |
+
df = pd.DataFrame(word_attrs, columns=['Word', 'Abs Score', 'Signed Score'])
|
| 1041 |
+
df = df.sort_values('Abs Score', ascending=False)
|
| 1042 |
+
st.dataframe(df, hide_index=True, use_container_width=True)
|
| 1043 |
+
except (MemoryError, RuntimeError) as mem_err:
|
| 1044 |
+
st.error("β Captum (Integrated Gradients) requires more memory than available on this server.")
|
| 1045 |
+
st.info("π‘ **Tip:** Use LIME or SHAP instead β they work great on cloud deployments. Captum works on local machines with more RAM/GPU.")
|
| 1046 |
|
| 1047 |
except Exception as e:
|
| 1048 |
st.error(f"β Explanation failed: {str(e)}")
|
|
|
|
| 1217 |
else:
|
| 1218 |
st.info("π‘ Upload CSV with a 'text' column")
|
| 1219 |
|
| 1220 |
+
uploaded_file = st.file_uploader(
|
| 1221 |
+
"Choose CSV file",
|
| 1222 |
+
type=['csv'],
|
| 1223 |
+
help="Max 200MB. Upload a CSV with a text column containing Nepali text."
|
| 1224 |
+
)
|
| 1225 |
|
| 1226 |
if uploaded_file:
|
| 1227 |
try:
|
| 1228 |
+
# Try multiple encodings for Nepali text compatibility
|
| 1229 |
+
try:
|
| 1230 |
+
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
| 1231 |
+
except UnicodeDecodeError:
|
| 1232 |
+
uploaded_file.seek(0)
|
| 1233 |
+
df = pd.read_csv(uploaded_file, encoding='latin-1')
|
| 1234 |
st.write("π **File Preview:**")
|
| 1235 |
st.dataframe(df.head(10), use_container_width=True)
|
| 1236 |
|