Jatin112002's picture
Update app.py
11a6111 verified
from transformers import pipeline
import gradio as gr
import lime
import lime.lime_text
import numpy as np
from sklearn.pipeline import make_pipeline
# Load multi-class sentiment analysis model
sentiment_model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment", top_k=None)
# Define possible sentiment classes with a reduced, logical set
label_mapping = {
"LABEL_0": "negative",
"LABEL_1": "neutral",
"LABEL_2": "positive",
"LABEL_3": "anger",
"LABEL_4": "chill"
}
# Function to get sentiment prediction
def analyze_sentiment(text):
results = sentiment_model(text)[0] # Get predictions
sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
top_label, top_confidence = label_mapping[sorted_results[0]['label']], sorted_results[0]['score']
return f"Sentiment: {top_label} (Confidence: {top_confidence:.2f})"
# Suggest test cases to ensure correct labeling
def get_suggestions():
return "Try these examples:\n- 'I love this! Best experience ever!' (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.' (negative)\n- 'I am furious right now!' (anger)\n- 'I am extremely relaxed and enjoying the moment.' (chill)"
# Explainability function using LIME
def explain_prediction(text):
explainer = lime.lime_text.LimeTextExplainer(class_names=list(label_mapping.values()))
def predictor(texts):
predictions = [sentiment_model(text)[0] for text in texts]
return np.array([[pred[label] if label in pred else 0 for label in label_mapping.values()] for pred in predictions])
exp = explainer.explain_instance(text, predictor, num_features=6)
return exp.as_list()
# Create Gradio interface
iface = gr.Interface(
fn=analyze_sentiment,
inputs="text",
outputs="text",
title="Multi-Class Sentiment Analysis App",
description="Enter a sentence to analyze its sentiment across multiple categories (Negative, Neutral, Positive, Anger, Chill).",
live=True,
examples=[
["I love this! Best experience ever!"],
["I am so happy today!"],
["It was okay, nothing special."],
["I am disappointed with this product."],
["This is the worst day of my life."],
["The movie was fantastic, I really enjoyed it!"],
["I am so angry, I can't believe this happened!"],
["I feel completely at peace right now."],
["The service was terrible, I wouldn’t recommend this place."],
["I feel great today, everything is going well!"],
["It’s just another day, nothing special to report."],
["This food is awful, I can’t even eat it!"],
["The book was so engaging, I couldn’t put it down!"],
["I don’t really have an opinion on this matter."],
["My day has been okay, not good but not bad either."],
["I regret buying this product, it’s a waste of money."],
["The customer support was helpful and solved my issue quickly."],
["This experience has been quite frustrating, honestly."],
["I had fun at the party, it was a great time!"],
["There was too much traffic today, it was so annoying."],
["I appreciate your help, it really made a difference."],
["The test was hard, but I think I did okay."],
["I wouldn’t buy this again, it didn’t meet my expectations."],
["This new update has improved the app significantly!"],
["I’m not sure how I feel about this decision."],
["Everything went smoothly today, no issues at all."],
["The weather is nice today, not too hot or too cold."],
["I had a terrible time at the event, it was poorly organized."],
["My experience was neutral, I don’t have strong feelings either way."],
["I highly recommend this to everyone, it’s fantastic!"],
["This place is so relaxing, I could stay here forever."],
["I had a bad day, but I’ll get through it."],
["The lecture was informative, I learned a lot."],
["It’s neither good nor bad, just okay overall."],
["The store was crowded and the staff was rude, not a good experience."],
["I’m satisfied with my purchase, it met my expectations."],
["This situation is frustrating, I don’t know what to do."],
["I’m feeling optimistic about the future!"],
["It was a boring day, nothing interesting happened."],
["I love spending time with my friends, they make me happy."],
["The flight was delayed, but at least I got home safely."],
["This dessert is absolutely delicious, I need more!"],
["I wish things had gone differently, but it’s okay."],
["The staff was unfriendly, I didn’t feel welcome at all."],
["I had a productive day, I got a lot of work done."],
["This movie was neither exciting nor dull, just in between."],
["I’m really grateful for your kindness, it means a lot."],
["I have no strong opinion about this, it’s just okay."],
["The food was decent, but I’ve had better."],
["Everything was perfect, I couldn’t have asked for more!"],
["The trip was stressful, nothing went according to plan."],
["I’m hopeful that things will get better soon."],
["The presentation was well done, I was impressed."],
["I feel indifferent about this, it doesn’t affect me much."],
["The concert was amazing, I had a blast!"]
]
)
iface.launch()