Spaces:
Runtime error
Runtime error
File size: 2,170 Bytes
1683bca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import streamlit as st
import streamlit.components.v1 as components
from transformers import (AutoModelForSequenceClassification, AutoTokenizer,
pipeline)
import shap
from PIL import Image
st.set_option('deprecation.showPyplotGlobalUse', False)
output_width = 800
output_height = 300
rescale_logits = False
st.set_page_config(page_title='Text Classification with Shap')
logo = Image.open('Intel-logo.png')
st.sidebar.image(logo)
st.title('Interpreting HF Pipeline Text Classification with Shap')
form = st.sidebar.form("Model Selection")
form.header('Model Selection')
model_name = form.text_input("Enter the name of the text classification LLM (note: model must be fine-tuned on a text classification task)", value = "Hate-speech-CNERG/bert-base-uncased-hatexplain")
form.form_submit_button("Submit")
@st.cache_data()
def load_model(model_name):
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
return tokenizer, model
tokenizer, model = load_model(model_name)
pred = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None)
explainer = shap.Explainer(pred, rescale_to_logits = rescale_logits)
col1, col2 = st.columns(2)
text = col1.text_area("Enter text input", value = "Classify me.")
result = pred(text)
top_pred = result[0][0]['label']
col2.write('')
for label in result[0]:
col2.write(f'**{label["label"]}**: {label["score"]: .2f}')
shap_values = explainer([text])
force_plot = shap.plots.text(shap_values, display=False)
bar_plot = shap.plots.bar(shap_values[0, :, top_pred], order=shap.Explanation.argsort.flip, show=False)
st.markdown("""
<style>
.big-font {
font-size:35px !important;
}
</style>
""", unsafe_allow_html=True)
st.markdown(f'<center><p class="big-font">Shap Bar Plot for <i>{top_pred}</i> Prediction</p></center>', unsafe_allow_html=True)
st.pyplot(bar_plot, clear_figure=True)
st.markdown('<center><p class="big-font">Shap Interactive Force Plot</p></center>', unsafe_allow_html=True)
components.html(force_plot, height=output_height, width=output_width, scrolling=True)
|