Spaces:
Runtime error
Runtime error
import gradio as gr | |
import nltk | |
from nltk.sentiment.vader import SentimentIntensityAnalyzer | |
# Download required NLTK resources | |
nltk.download('vader_lexicon') | |
# Load the pre-trained sentiment intensity analyzer | |
sia = SentimentIntensityAnalyzer() | |
def get_sentiment(tweet): | |
# Preprocess the tweet | |
processed_tweet = preprocess([tweet]) | |
# Get the sentiment score using VADER sentiment analyzer | |
sentiment_score = sia.polarity_scores(processed_tweet[0]) | |
# Determine the sentiment label based on the compound score | |
compound_score = sentiment_score['compound'] | |
if compound_score >= 0.05: | |
sentiment = 'Positive' | |
elif compound_score <= -0.05: | |
sentiment = 'Negative' | |
else: | |
sentiment = 'Neutral' | |
return sentiment | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=get_sentiment, | |
inputs='text', | |
outputs='text', | |
title='Tweet Sentiment Analyzer', | |
description='Enter a tweet with text or emoticon or both, and get the sentiment prediction.', | |
examples=[['I love this movie!', 'This weather is terrible.']], | |
theme='Soft' | |
) | |
# Launch the interface | |
iface.launch(share = True) |