File size: 864 Bytes
d820bd0
 
 
 
 
 
 
 
 
 
 
 
 
2b68363
d820bd0
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import shap
from transformers import pipeline


sentiment_classifier = pipeline("text-classification", return_all_scores=True)

def interpretation_function(text):
    explainer = shap.Explainer(sentiment_classifier)
    shap_values = explainer([text])
    scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
    return {"original": text, "interpretation": scores}

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(label="Sentiment Analysis", value="Wonderfully terrible")
            with gr.Row():
                interpret = gr.Button("Interpret")
        with gr.Column():
                interpretation = gr.components.Interpretation(input_text)

    interpret.click(interpretation_function, input_text, interpretation)

if __name__ == "__main__":
    demo.launch()