JohnJoelMota's picture
updated examples
8ce3c26 verified
raw
history blame
1.87 kB
import gradio as gr
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
# Download VADER lexicon for sentiment analysis
nltk.download('vader_lexicon', quiet=True)
def perform_sentiment_analysis(text):
"""Analyzes the sentiment of the given text using VADER."""
sia = SentimentIntensityAnalyzer()
return sia.polarity_scores(text)
def categorize_sentiment(compound_score):
"""Categorizes sentiment based on the compound score."""
if compound_score > 0.1: # Adjusted threshold for more balanced classification
return 'Positive'
elif compound_score < -0.1:
return 'Negative'
else:
return 'Neutral'
def analyze_sentiment(input_text):
"""Performs sentiment analysis and categorizes the sentiment."""
scores = perform_sentiment_analysis(input_text)
sentiment = categorize_sentiment(scores['compound'])
return {"Sentiment": sentiment, "Scores": scores}
# Improved examples for sentiment analysis
examples = [
"Absolutely thrilled about my vacation next week! Can't wait!", # Positive
"The customer service was terrible. I wouldn't recommend this place to anyone.", # Negative
"I'm not sure what to think about the new policy. It has pros and cons.", # Neutral
"This product exceeded my expectations! The quality is fantastic.", # Positive
"I'm feeling overwhelmed with all these assignments due tomorrow.", # Negative
"Did you complete your homework for the AI course?", # Neutral
]
# Create Gradio interface
demo = gr.Interface(
fn=analyze_sentiment,
inputs=gr.Textbox(label="Enter text for sentiment analysis", placeholder="Type your text here..."),
outputs="json",
title="Sentiment Analysis Tool",
description="Analyze the sentiment of any text. Enter your own text or choose an example below.",
examples=examples
)
demo.launch()