UDHOV commited on
Commit
071d54f
Β·
1 Parent(s): a48f7a5

Fix Captum memory crash in all explainability tabs

Browse files
Files changed (1) hide show
  1. 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
- captum_exp = CaptumExplainer(
574
- model, tokenizer, label_encoder, preprocessor,
575
- emoji_to_nepali_map=EMOJI_TO_NEPALI
576
- )
577
- result = captum_exp.explain_and_visualize(
578
- analysis['original_text'],
579
- target=None,
580
- n_steps=50,
581
- save_dir=None,
582
- show=False,
583
- nepali_font=nepali_font
584
- )
585
- st.subheader("Captum Integrated Gradients")
586
- col1, col2 = st.columns(2)
587
- with col1:
588
- st.markdown("**Bar Chart**")
589
- st.pyplot(result['bar_chart'])
590
- with col2:
591
- st.markdown("**Heatmap**")
592
- st.pyplot(result['heatmap'])
593
- st.markdown("---")
594
- st.markdown("**πŸ“Š Attribution Details:**")
595
- st.write(f"**Convergence Delta:** {result['explanation']['convergence_delta']:.6f}")
596
- word_attrs = result['explanation']['word_attributions']
597
- if word_attrs:
598
- df = pd.DataFrame(word_attrs, columns=['Word', 'Abs Score', 'Signed Score'])
599
- df = df.sort_values('Abs Score', ascending=False)
600
- st.dataframe(df, hide_index=True, use_container_width=True)
601
- else:
602
- st.warning("No word attributions available")
 
 
 
 
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
- captum_exp = CaptumExplainer(
1009
- model, tokenizer, label_encoder, preprocessor,
1010
- emoji_to_nepali_map=EMOJI_TO_NEPALI
1011
- )
1012
- result = captum_exp.explain_and_visualize(
1013
- analysis['original_text'],
1014
- target=None,
1015
- n_steps=n_steps,
1016
- save_dir=None,
1017
- show=False,
1018
- nepali_font=nepali_font
1019
- )
1020
- st.subheader("Captum Integrated Gradients")
1021
- col1, col2 = st.columns(2)
1022
- with col1:
1023
- st.markdown("**Bar Chart**")
1024
- st.pyplot(result['bar_chart'])
1025
- with col2:
1026
- st.markdown("**Heatmap**")
1027
- st.pyplot(result['heatmap'])
1028
- with st.expander("πŸ“Š Attribution Details"):
1029
- st.write(f"**Convergence Delta:** {result['explanation']['convergence_delta']:.6f}")
1030
- word_attrs = result['explanation']['word_attributions']
1031
- df = pd.DataFrame(word_attrs, columns=['Word', 'Abs Score', 'Signed Score'])
1032
- df = df.sort_values('Abs Score', ascending=False)
1033
- st.dataframe(df, hide_index=True, use_container_width=True)
 
 
 
 
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("Choose CSV file", type=['csv'])
 
 
 
 
1209
 
1210
  if uploaded_file:
1211
  try:
1212
- df = pd.read_csv(uploaded_file)
 
 
 
 
 
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