Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
import lime
|
|
@@ -8,28 +9,27 @@ from sklearn.pipeline import make_pipeline
|
|
| 8 |
# Load multi-class sentiment analysis model
|
| 9 |
sentiment_model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment", top_k=None)
|
| 10 |
|
| 11 |
-
# Define possible sentiment classes
|
| 12 |
label_mapping = {
|
| 13 |
"LABEL_0": "very negative",
|
| 14 |
"LABEL_1": "negative",
|
| 15 |
-
"LABEL_2": "
|
| 16 |
-
"LABEL_3": "
|
| 17 |
-
"LABEL_4": "
|
| 18 |
-
"LABEL_5": "positive",
|
| 19 |
-
"LABEL_6": "very positive",
|
| 20 |
-
"LABEL_7": "anger",
|
| 21 |
-
"LABEL_8": "joy",
|
| 22 |
-
"LABEL_9": "sadness"
|
| 23 |
}
|
| 24 |
|
| 25 |
# Function to get sentiment prediction
|
| 26 |
def analyze_sentiment(text):
|
| 27 |
-
results = sentiment_model(text)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
return f"Sentiment: {top_label} (Confidence: {top_confidence:.2f})"
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Explainability function using LIME
|
| 34 |
def explain_prediction(text):
|
| 35 |
explainer = lime.lime_text.LimeTextExplainer(class_names=list(label_mapping.values()))
|
|
@@ -48,7 +48,14 @@ iface = gr.Interface(
|
|
| 48 |
outputs="text",
|
| 49 |
title="Multi-Class Sentiment Analysis App",
|
| 50 |
description="Enter a sentence to analyze its sentiment across multiple categories.",
|
| 51 |
-
live=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
iface.launch()
|
|
|
|
| 1 |
+
|
| 2 |
from transformers import pipeline
|
| 3 |
import gradio as gr
|
| 4 |
import lime
|
|
|
|
| 9 |
# Load multi-class sentiment analysis model
|
| 10 |
sentiment_model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment", top_k=None)
|
| 11 |
|
| 12 |
+
# Define possible sentiment classes based on the actual model output
|
| 13 |
label_mapping = {
|
| 14 |
"LABEL_0": "very negative",
|
| 15 |
"LABEL_1": "negative",
|
| 16 |
+
"LABEL_2": "neutral",
|
| 17 |
+
"LABEL_3": "positive",
|
| 18 |
+
"LABEL_4": "very positive"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
# Function to get sentiment prediction
|
| 22 |
def analyze_sentiment(text):
|
| 23 |
+
results = sentiment_model(text)[0] # Get predictions
|
| 24 |
+
sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
|
| 25 |
+
top_label, top_confidence = label_mapping[sorted_results[0]['label']], sorted_results[0]['score']
|
| 26 |
+
|
| 27 |
return f"Sentiment: {top_label} (Confidence: {top_confidence:.2f})"
|
| 28 |
|
| 29 |
+
# Suggest test cases to ensure correct labeling
|
| 30 |
+
def get_suggestions():
|
| 31 |
+
return "Try these examples:\n- 'I love this! Best experience ever!' (very positive)\n- 'I am so happy today!' (positive)\n- 'It was okay, nothing special.' (neutral)\n- 'I am disappointed with this product.' (negative)\n- 'This is the worst day of my life.' (very negative)"
|
| 32 |
+
|
| 33 |
# Explainability function using LIME
|
| 34 |
def explain_prediction(text):
|
| 35 |
explainer = lime.lime_text.LimeTextExplainer(class_names=list(label_mapping.values()))
|
|
|
|
| 48 |
outputs="text",
|
| 49 |
title="Multi-Class Sentiment Analysis App",
|
| 50 |
description="Enter a sentence to analyze its sentiment across multiple categories.",
|
| 51 |
+
live=True,
|
| 52 |
+
examples=[
|
| 53 |
+
["I love this! Best experience ever!"],
|
| 54 |
+
["I am so happy today!"],
|
| 55 |
+
["It was okay, nothing special."],
|
| 56 |
+
["I am disappointed with this product."],
|
| 57 |
+
["This is the worst day of my life."]
|
| 58 |
+
]
|
| 59 |
)
|
| 60 |
|
| 61 |
+
iface.launch()
|