Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Load sentiment pipeline locally (downloads model once)
|
| 6 |
MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 7 |
-
classifier = pipeline("sentiment-analysis", model=MODEL)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"text": "Makes decent coffee. Nothing special for the price."},
|
| 21 |
-
{"id": 6, "product": "Coffee Maker",
|
| 22 |
-
"text": "Best purchase this year! Perfect espresso every morning."},
|
| 23 |
-
{"id": 7, "product": "Laptop Stand",
|
| 24 |
-
"text": "It does the job. Average quality."},
|
| 25 |
-
{"id": 8, "product": "Laptop Stand",
|
| 26 |
-
"text": "Wobbly and unstable. Returned it immediately."},
|
| 27 |
-
]
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
f"{result['label']:<10} "
|
| 38 |
-
f"{result['score']:>5.1%} "
|
| 39 |
-
f"{review['text'][:40]}...")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
| 4 |
MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
|
|
|
| 5 |
|
| 6 |
+
# Load once at startup
|
| 7 |
+
classifier = pipeline(
|
| 8 |
+
"sentiment-analysis",
|
| 9 |
+
model=MODEL
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
label_map = {
|
| 13 |
+
"LABEL_0": "Negative",
|
| 14 |
+
"LABEL_1": "Neutral",
|
| 15 |
+
"LABEL_2": "Positive"
|
| 16 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def analyze(text):
|
| 19 |
+
result = classifier(text)[0]
|
| 20 |
+
return {
|
| 21 |
+
"Sentiment": label_map[result["label"]],
|
| 22 |
+
"Confidence": f"{result['score']:.2%}"
|
| 23 |
+
}
|
| 24 |
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=analyze,
|
| 27 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter review text here..."),
|
| 28 |
+
outputs="json",
|
| 29 |
+
title="Product Sentiment Analyzer",
|
| 30 |
+
description="Runs locally using Transformers inside HF Spaces"
|
| 31 |
+
)
|
| 32 |
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch()
|
|
|
|
|
|
|
|
|