Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# 3-class model (negative / neutral / positive)
|
5 |
+
MODEL_ID = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
6 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model=MODEL_ID)
|
7 |
+
|
8 |
+
LABEL_MAP = {
|
9 |
+
"LABEL_0": "Negative",
|
10 |
+
"LABEL_1": "Neutral",
|
11 |
+
"LABEL_2": "Positive",
|
12 |
+
"NEGATIVE": "Negative",
|
13 |
+
"NEUTRAL": "Neutral",
|
14 |
+
"POSITIVE": "Positive",
|
15 |
+
}
|
16 |
+
|
17 |
+
def analyze_sentiment(text):
|
18 |
+
text = (text or "").strip()
|
19 |
+
if not text:
|
20 |
+
return "⚠️ Please enter some text."
|
21 |
+
result = sentiment_pipeline(text, truncation=True)[0]
|
22 |
+
label = LABEL_MAP.get(result["label"], result["label"].title())
|
23 |
+
score = round(float(result["score"]), 3)
|
24 |
+
return f"{label} (confidence: {score})"
|
25 |
+
|
26 |
+
demo = gr.Interface(
|
27 |
+
fn=analyze_sentiment,
|
28 |
+
inputs=gr.Textbox(lines=3, placeholder="Type a sentence here..."),
|
29 |
+
outputs="text",
|
30 |
+
title="Sentiment Analyzer",
|
31 |
+
description="Classifies text as Negative, Neutral, or Positive using a Hugging Face transformer.",
|
32 |
+
examples=[["I love this!"], ["This is okay, I guess."], ["I hate it."]],
|
33 |
+
)
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
demo.launch()
|