CoolFace
Apppublic

daniel-de-leon/streamlit-intel-docker

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py75 linesDownload Raw Back to root
1# Code modified from https://huggingface.co/spaces/Hellisotherpeople/HF-SHAP2 3 4import streamlit as st5import streamlit.components.v1 as components6from transformers import (AutoModelForSequenceClassification, AutoTokenizer,7                          pipeline)8import shap9from PIL import Image10import time11 12st.set_option('deprecation.showPyplotGlobalUse', False)13output_width = 80014output_height = 30015rescale_logits = False16 17 18 19st.set_page_config(page_title='Text Classification with Shap')20st.title('Interpreting HF Pipeline Text Classification with Shap')21 22form = st.sidebar.form("Model Selection")23form.header('Model Selection')24 25model_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")26form.form_submit_button("Submit")27 28 29@st.cache_data()30def load_model(model_name):31    tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)32    model = AutoModelForSequenceClassification.from_pretrained(model_name)33 34    return tokenizer, model35 36tokenizer, model = load_model(model_name)37pred = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None)38explainer = shap.Explainer(pred, rescale_to_logits = rescale_logits)39 40col1, col2, col3 = st.columns(3)41text = col1.text_area("Enter text input", value = "Classify me.")42 43start_time = time.time()44result = pred(text)45inference_time = time.time() - start_time46 47col3.write('')48col3.write(f'**Inference Time:** {inference_time: .4f}')49 50top_pred = result[0][0]['label']51col2.write('')52for label in result[0]:53    col2.write(f'**{label["label"]}**: {label["score"]: .2f}')54 55shap_values = explainer([text])56explanation_time = shap_values.compute_time57 58col3.write('')59col3.write(f'**Explanation Time:** {explanation_time: .4f}')60 61force_plot = shap.plots.text(shap_values, display=False)62bar_plot = shap.plots.bar(shap_values[0, :, top_pred], order=shap.Explanation.argsort.flip, show=False)63 64st.markdown("""65<style>66.big-font {67    font-size:35px !important;68}69</style>70""", unsafe_allow_html=True)71st.markdown(f'<center><p class="big-font">Shap Bar Plot for <i>{top_pred}</i> Prediction</p></center>', unsafe_allow_html=True)72st.pyplot(bar_plot, clear_figure=True)73 74st.markdown('<center><p class="big-font">Shap Interactive Force Plot</p></center>', unsafe_allow_html=True)75components.html(force_plot, height=output_height, width=output_width, scrolling=True)