Ashed00 commited on
Commit
aff6f72
Β·
verified Β·
1 Parent(s): d2f811f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import shap
4
  import torch
5
  import numpy as np
6
- import os
7
 
8
  # Load model and tokenizer with caching
9
  @st.cache_resource
@@ -71,9 +71,9 @@ if st.button("Analyze Sentiment"):
71
  # Generate SHAP explanations
72
  st.subheader("πŸ” Explanation")
73
  st.markdown("""
74
- **Word impacts**
75
- Red β†’ Increases score | Blue β†’ Decreases score
76
- Intensity shows magnitude of impact
77
  """)
78
 
79
  shap_values = explainer([text_input])
@@ -82,19 +82,13 @@ if st.button("Analyze Sentiment"):
82
  tabs = st.tabs(output_names)
83
  for i, tab in enumerate(tabs):
84
  with tab:
85
- # Save SHAP text explanation as an HTML file
86
- html_path = f"shap_explanation_{i}.html"
87
- with open(html_path, "w", encoding="utf-8") as file:
88
- file.write(shap.plots.text(shap_values[:, :, i], display=False))
89
 
90
- # Read and display the saved HTML file in Streamlit
91
- with open(html_path, "r", encoding="utf-8") as file:
92
- shap_html = file.read()
93
-
94
- st.components.v1.html(shap_html, height=400, scrolling=True)
95
-
96
- # Clean up temporary HTML files (optional)
97
- os.remove(html_path)
98
 
99
  else:
100
  st.warning("Please enter some text to analyze")
 
3
  import shap
4
  import torch
5
  import numpy as np
6
+ import matplotlib.pyplot as plt
7
 
8
  # Load model and tokenizer with caching
9
  @st.cache_resource
 
71
  # Generate SHAP explanations
72
  st.subheader("πŸ” Explanation")
73
  st.markdown("""
74
+ **Feature importance (word-level impacts)**
75
+ πŸ”΄ Higher positive values β†’ Increases sentiment
76
+ πŸ”΅ Lower negative values β†’ Decreases sentiment
77
  """)
78
 
79
  shap_values = explainer([text_input])
 
82
  tabs = st.tabs(output_names)
83
  for i, tab in enumerate(tabs):
84
  with tab:
85
+ # Create a bar plot of SHAP values
86
+ fig, ax = plt.subplots(figsize=(8, 4))
87
+ shap.plots.bar(shap_values[:, :, i], show=False)
 
88
 
89
+ # Display the plot in Streamlit
90
+ st.pyplot(fig)
91
+ plt.close(fig) # Free memory after rendering
 
 
 
 
 
92
 
93
  else:
94
  st.warning("Please enter some text to analyze")