Spaces:
Sleeping
Sleeping
File size: 1,869 Bytes
cb3c4b3 e4e3102 cb3c4b3 8ce3c26 cb3c4b3 8ce3c26 cb3c4b3 8ce3c26 cb3c4b3 8ce3c26 cb3c4b3 8ce3c26 cb3c4b3 8ce3c26 e4e3102 8ce3c26 e4e3102 8ce3c26 cb3c4b3 e4e3102 cb3c4b3 f5f358c 8ce3c26 f5f358c cb3c4b3 | 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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()
|