To kick things off, what exactly XAI is. Essentially, it’s a way of making machine learning models more transparent by providing insights into how they arrive at their predictions or decisions. This can be especially useful for transformer-based language models like BERT and GPT-3, which are notoriously difficult to interpret due to the sheer complexity of their architecture.
Don’t Worry! With Streamlit, we can create interactive dashboards that allow us to visualize and explain our model’s output in a way that’s easy for humans to understand.
Here’s an example script using Streamlit and the transformers library:
# Import necessary libraries
import streamlit as st # Import Streamlit library and alias it as "st"
from transformers import pipeline, BertTokenizerFast # Import pipeline and BertTokenizerFast from the transformers library
# Load pre-trained model and tokenizer
model = pipeline('sentiment-analysis') # Load pre-trained sentiment analysis model
tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased") # Load pre-trained tokenizer for BERT model
# Define function to predict sentiment for input text
def predict(text):
# Tokenize input text using the loaded tokenizer
inputs = tokenizer(text, return_tensors="pt").to('cuda') # Tokenize input text and convert it to PyTorch tensors, then move it to the GPU for faster processing
# Run prediction through pre-trained model and get output probabilities
outputs = model(**inputs)['sentiment'] # Use the loaded model to predict the sentiment of the input text and get the output probabilities
# Convert output probabilities to sentiment labels (positive or negative) based on threshold of 0.5
if outputs > 0.5: # If the output probability is greater than 0.5, classify it as positive sentiment
return "Positive"
else: # Otherwise, classify it as negative sentiment
return "Negative"
# Define function to display input text and predicted label in Streamlit app
def main():
# Set up Streamlit app with title, header, and sidebar for input text
st.title("Sentiment Analysis Dashboard") # Set the title of the Streamlit app
st.header("Enter a sentence below:") # Set the header for the input text section
text = st.text_area('', height=150) # Create a text area for the user to input their text, with a default height of 150 pixels
# If user submits input text, run prediction function and display results in Streamlit app
if st.button('Predict'): # Create a button for the user to click to run the prediction function
result = predict(text) # Call the predict function and store the result in a variable
st.write("Sentiment: ", result) # Display the predicted sentiment label in the Streamlit app
# Run main function when script is executed
if __name__ == '__main__':
main() # Call the main function to run the Streamlit app
In this example, we’re using the transformers library to load a pre-trained sentiment analysis model and tokenizer for BERT. We then define a `predict` function that takes input text, tokenizes it using our loaded tokenizer, runs prediction through our pre-trained model, and converts output probabilities into sentiment labels based on a threshold of 0.5.
Finally, we create an interactive Streamlit app that allows users to enter input text, run the `predict` function when they click “Predict”, and displays the results in real time using the `write()` function from Streamlit’s library.
With just a few lines of code and some basic knowledge of transformers and Streamlit, you can create interactive dashboards that allow you to explain your model’s output in a way that’s easy for humans to understand.