Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import nltk
|
3 |
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
4 |
+
|
5 |
+
# Download VADER lexicon on first run
|
6 |
+
nltk.download("vader_lexicon")
|
7 |
+
|
8 |
+
# Instantiate once
|
9 |
+
sid = SentimentIntensityAnalyzer()
|
10 |
+
|
11 |
+
def classify_sentiment(text: str) -> str:
|
12 |
+
"""
|
13 |
+
Returns one of: "Positive", "Neutral", "Negative"
|
14 |
+
based on VADER’s compound score.
|
15 |
+
"""
|
16 |
+
comp = sid.polarity_scores(text)["compound"]
|
17 |
+
if comp >= 0.05:
|
18 |
+
return "Positive 😀"
|
19 |
+
elif comp <= -0.05:
|
20 |
+
return "Negative 😞"
|
21 |
+
else:
|
22 |
+
return "Neutral 😐"
|
23 |
+
|
24 |
+
demo = gr.Interface(
|
25 |
+
fn=classify_sentiment,
|
26 |
+
inputs=gr.Textbox(
|
27 |
+
lines=2,
|
28 |
+
placeholder="Type an English sentence here…",
|
29 |
+
label="Your text"
|
30 |
+
),
|
31 |
+
outputs=gr.Radio(
|
32 |
+
choices=["Positive 😀", "Neutral 😐", "Negative 😞"],
|
33 |
+
label="Sentiment"
|
34 |
+
),
|
35 |
+
examples=[
|
36 |
+
["I absolutely love this product!"],
|
37 |
+
["It was okay, nothing special."],
|
38 |
+
["This is the worst experience ever…"]
|
39 |
+
],
|
40 |
+
title="3-Way Sentiment Classifier",
|
41 |
+
description=(
|
42 |
+
"Classifies English text as **Positive**, **Neutral**, or **Negative**\n"
|
43 |
+
"using NLTK’s VADER (thresholds at ±0.05 on the compound score)."
|
44 |
+
),
|
45 |
+
allow_flagging="never"
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
demo.launch()
|