File size: 1,325 Bytes
3f9083b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
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()