sifujohn commited on
Commit
3c7449e
·
verified ·
1 Parent(s): 4328c84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -1,39 +1,34 @@
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]}...")
 
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()