Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
|
| 2 |
---
|
| 3 |
+
title: blocks_interpretation_3-x
|
| 4 |
+
emoji: 🔥
|
| 5 |
+
colorFrom: indigo
|
| 6 |
+
colorTo: indigo
|
| 7 |
sdk: gradio
|
| 8 |
+
sdk_version: 3.50.1
|
| 9 |
+
app_file: run.py
|
| 10 |
pinned: false
|
| 11 |
+
hf_oauth: true
|
| 12 |
---
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
shap
|
| 2 |
+
matplotlib
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
run.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_interpretation"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio shap matplotlib transformers torch"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import shap\n", "from transformers import pipeline\n", "import matplotlib.pyplot as plt\n", "\n", "\n", "sentiment_classifier = pipeline(\"text-classification\", return_all_scores=True)\n", "\n", "\n", "def classifier(text):\n", " pred = sentiment_classifier(text)\n", " return {p[\"label\"]: p[\"score\"] for p in pred[0]}\n", "\n", "\n", "def interpretation_function(text):\n", " explainer = shap.Explainer(sentiment_classifier)\n", " shap_values = explainer([text])\n", " # Dimensions are (batch size, text size, number of classes)\n", " # Since we care about positive sentiment, use index 1\n", " scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))\n", "\n", " scores_desc = sorted(scores, key=lambda t: t[1])[::-1]\n", "\n", " # Filter out empty string added by shap\n", " scores_desc = [t for t in scores_desc if t[0] != \"\"]\n", "\n", " fig_m = plt.figure()\n", " plt.bar(x=[s[0] for s in scores_desc[:5]],\n", " height=[s[1] for s in scores_desc[:5]])\n", " plt.title(\"Top words contributing to positive sentiment\")\n", " plt.ylabel(\"Shap Value\")\n", " plt.xlabel(\"Word\")\n", " return {\"original\": text, \"interpretation\": scores}, fig_m\n", "\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " with gr.Column():\n", " input_text = gr.Textbox(label=\"Input Text\")\n", " with gr.Row():\n", " classify = gr.Button(\"Classify Sentiment\")\n", " interpret = gr.Button(\"Interpret\")\n", " with gr.Column():\n", " label = gr.Label(label=\"Predicted Sentiment\")\n", " with gr.Column():\n", " with gr.Tab(\"Display interpretation with built-in component\"):\n", " interpretation = gr.components.Interpretation(input_text)\n", " with gr.Tab(\"Display interpretation with plot\"):\n", " interpretation_plot = gr.Plot()\n", "\n", " classify.click(classifier, input_text, label)\n", " interpret.click(interpretation_function, input_text, [interpretation, interpretation_plot])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import shap
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
sentiment_classifier = pipeline("text-classification", return_all_scores=True)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def classifier(text):
|
| 11 |
+
pred = sentiment_classifier(text)
|
| 12 |
+
return {p["label"]: p["score"] for p in pred[0]}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def interpretation_function(text):
|
| 16 |
+
explainer = shap.Explainer(sentiment_classifier)
|
| 17 |
+
shap_values = explainer([text])
|
| 18 |
+
# Dimensions are (batch size, text size, number of classes)
|
| 19 |
+
# Since we care about positive sentiment, use index 1
|
| 20 |
+
scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
|
| 21 |
+
|
| 22 |
+
scores_desc = sorted(scores, key=lambda t: t[1])[::-1]
|
| 23 |
+
|
| 24 |
+
# Filter out empty string added by shap
|
| 25 |
+
scores_desc = [t for t in scores_desc if t[0] != ""]
|
| 26 |
+
|
| 27 |
+
fig_m = plt.figure()
|
| 28 |
+
plt.bar(x=[s[0] for s in scores_desc[:5]],
|
| 29 |
+
height=[s[1] for s in scores_desc[:5]])
|
| 30 |
+
plt.title("Top words contributing to positive sentiment")
|
| 31 |
+
plt.ylabel("Shap Value")
|
| 32 |
+
plt.xlabel("Word")
|
| 33 |
+
return {"original": text, "interpretation": scores}, fig_m
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
input_text = gr.Textbox(label="Input Text")
|
| 40 |
+
with gr.Row():
|
| 41 |
+
classify = gr.Button("Classify Sentiment")
|
| 42 |
+
interpret = gr.Button("Interpret")
|
| 43 |
+
with gr.Column():
|
| 44 |
+
label = gr.Label(label="Predicted Sentiment")
|
| 45 |
+
with gr.Column():
|
| 46 |
+
with gr.Tab("Display interpretation with built-in component"):
|
| 47 |
+
interpretation = gr.components.Interpretation(input_text)
|
| 48 |
+
with gr.Tab("Display interpretation with plot"):
|
| 49 |
+
interpretation_plot = gr.Plot()
|
| 50 |
+
|
| 51 |
+
classify.click(classifier, input_text, label)
|
| 52 |
+
interpret.click(interpretation_function, input_text, [interpretation, interpretation_plot])
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|