Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# lab1_sentiment_local.py
|
| 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 |
+
# Our test reviews (in production, load from CSV or database)
|
| 10 |
+
reviews = [
|
| 11 |
+
{"id": 1, "product": "Wireless Headphones",
|
| 12 |
+
"text": "Incredible sound quality and the noise cancellation is superb."},
|
| 13 |
+
{"id": 2, "product": "Wireless Headphones",
|
| 14 |
+
"text": "Broke after 2 weeks. Cheap plastic. Do not buy."},
|
| 15 |
+
{"id": 3, "product": "Standing Desk",
|
| 16 |
+
"text": "Easy to assemble. Sturdy build. Motor is a bit noisy."},
|
| 17 |
+
{"id": 4, "product": "Standing Desk",
|
| 18 |
+
"text": "Arrived with a huge scratch on the surface. Disappointed."},
|
| 19 |
+
{"id": 5, "product": "Coffee Maker",
|
| 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 |
+
print(f"{'ID':<4} {'Product':<20} {'Sentiment':<10} {'Conf':>6} Text")
|
| 30 |
+
print("-" * 90)
|
| 31 |
+
|
| 32 |
+
for review in reviews:
|
| 33 |
+
result = classifier(review["text"])[0]
|
| 34 |
+
|
| 35 |
+
print(f"{review['id']:<4} "
|
| 36 |
+
f"{review['product']:<20} "
|
| 37 |
+
f"{result['label']:<10} "
|
| 38 |
+
f"{result['score']:>5.1%} "
|
| 39 |
+
f"{review['text'][:40]}...")
|