Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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 |
-
**
|
75 |
-
|
76 |
-
|
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 |
-
#
|
86 |
-
|
87 |
-
|
88 |
-
file.write(shap.plots.text(shap_values[:, :, i], display=False))
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
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")
|