Spaces:
Running
Running
import gradio as gr | |
import nltk | |
from nltk.sentiment.vader import SentimentIntensityAnalyzer | |
# Download VADER lexicon on first run | |
nltk.download("vader_lexicon") | |
# Instantiate once | |
sid = SentimentIntensityAnalyzer() | |
def classify_sentiment(text: str) -> str: | |
""" | |
Returns one of: "Positive", "Neutral", "Negative" | |
based on VADER’s compound score. | |
""" | |
comp = sid.polarity_scores(text)["compound"] | |
if comp >= 0.05: | |
return "Positive 😀" | |
elif comp <= -0.05: | |
return "Negative 😞" | |
else: | |
return "Neutral 😐" | |
demo = gr.Interface( | |
fn=classify_sentiment, | |
inputs=gr.Textbox( | |
lines=2, | |
placeholder="Type an English sentence here…", | |
label="Your text" | |
), | |
outputs=gr.Radio( | |
choices=["Positive 😀", "Neutral 😐", "Negative 😞"], | |
label="Sentiment" | |
), | |
examples=[ | |
["I absolutely love this product!"], | |
["It was okay, nothing special."], | |
["This is the worst experience ever…"] | |
], | |
title="3-Way Sentiment Classifier", | |
description=( | |
"Classifies English text as **Positive**, **Neutral**, or **Negative**\n" | |
"using NLTK’s VADER (thresholds at ±0.05 on the compound score)." | |
), | |
allow_flagging="never" | |
) | |
if __name__ == "__main__": | |
demo.launch() | |