Spaces:
Sleeping
Sleeping
updated examples
Browse files
app.py
CHANGED
|
@@ -2,45 +2,46 @@ import gradio as gr
|
|
| 2 |
import nltk
|
| 3 |
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 4 |
|
| 5 |
-
# Download VADER lexicon
|
| 6 |
nltk.download('vader_lexicon', quiet=True)
|
| 7 |
|
| 8 |
def perform_sentiment_analysis(text):
|
|
|
|
| 9 |
sia = SentimentIntensityAnalyzer()
|
| 10 |
return sia.polarity_scores(text)
|
| 11 |
|
| 12 |
def categorize_sentiment(compound_score):
|
| 13 |
-
|
| 14 |
-
if compound_score > 0.1: #
|
| 15 |
return 'Positive'
|
| 16 |
-
elif compound_score < -0.1:
|
| 17 |
return 'Negative'
|
| 18 |
else:
|
| 19 |
return 'Neutral'
|
| 20 |
|
| 21 |
def analyze_sentiment(input_text):
|
|
|
|
| 22 |
scores = perform_sentiment_analysis(input_text)
|
| 23 |
sentiment = categorize_sentiment(scores['compound'])
|
| 24 |
return {"Sentiment": sentiment, "Scores": scores}
|
| 25 |
|
| 26 |
-
#
|
| 27 |
examples = [
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"I
|
| 33 |
-
"
|
| 34 |
]
|
| 35 |
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=analyze_sentiment,
|
| 38 |
inputs=gr.Textbox(label="Enter text for sentiment analysis", placeholder="Type your text here..."),
|
| 39 |
outputs="json",
|
| 40 |
title="Sentiment Analysis Tool",
|
| 41 |
-
description=
|
| 42 |
-
"Enter text to see the sentiment analysis result. You can also use the examples below to test different sentiments."
|
| 43 |
-
),
|
| 44 |
examples=examples
|
| 45 |
)
|
| 46 |
|
|
|
|
| 2 |
import nltk
|
| 3 |
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 4 |
|
| 5 |
+
# Download VADER lexicon for sentiment analysis
|
| 6 |
nltk.download('vader_lexicon', quiet=True)
|
| 7 |
|
| 8 |
def perform_sentiment_analysis(text):
|
| 9 |
+
"""Analyzes the sentiment of the given text using VADER."""
|
| 10 |
sia = SentimentIntensityAnalyzer()
|
| 11 |
return sia.polarity_scores(text)
|
| 12 |
|
| 13 |
def categorize_sentiment(compound_score):
|
| 14 |
+
"""Categorizes sentiment based on the compound score."""
|
| 15 |
+
if compound_score > 0.1: # Adjusted threshold for more balanced classification
|
| 16 |
return 'Positive'
|
| 17 |
+
elif compound_score < -0.1:
|
| 18 |
return 'Negative'
|
| 19 |
else:
|
| 20 |
return 'Neutral'
|
| 21 |
|
| 22 |
def analyze_sentiment(input_text):
|
| 23 |
+
"""Performs sentiment analysis and categorizes the sentiment."""
|
| 24 |
scores = perform_sentiment_analysis(input_text)
|
| 25 |
sentiment = categorize_sentiment(scores['compound'])
|
| 26 |
return {"Sentiment": sentiment, "Scores": scores}
|
| 27 |
|
| 28 |
+
# Improved examples for sentiment analysis
|
| 29 |
examples = [
|
| 30 |
+
"Absolutely thrilled about my vacation next week! Can't wait!", # Positive
|
| 31 |
+
"The customer service was terrible. I wouldn't recommend this place to anyone.", # Negative
|
| 32 |
+
"I'm not sure what to think about the new policy. It has pros and cons.", # Neutral
|
| 33 |
+
"This product exceeded my expectations! The quality is fantastic.", # Positive
|
| 34 |
+
"I'm feeling overwhelmed with all these assignments due tomorrow.", # Negative
|
| 35 |
+
"Did you complete your homework for the AI course?", # Neutral
|
| 36 |
]
|
| 37 |
|
| 38 |
+
# Create Gradio interface
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=analyze_sentiment,
|
| 41 |
inputs=gr.Textbox(label="Enter text for sentiment analysis", placeholder="Type your text here..."),
|
| 42 |
outputs="json",
|
| 43 |
title="Sentiment Analysis Tool",
|
| 44 |
+
description="Analyze the sentiment of any text. Enter your own text or choose an example below.",
|
|
|
|
|
|
|
| 45 |
examples=examples
|
| 46 |
)
|
| 47 |
|