noranisa commited on
Commit
280a8f1
·
verified ·
1 Parent(s): c5dd0aa

Update services/sentiment.py

Browse files
Files changed (1) hide show
  1. services/sentiment.py +18 -13
services/sentiment.py CHANGED
@@ -1,20 +1,25 @@
1
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
- import torch
3
- from config import MODEL_NAME
4
 
5
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
6
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
7
-
8
- labels = ["Negative", "Neutral", "Positive"]
 
9
 
10
  def predict(texts):
11
  results = []
12
 
13
- for text in texts:
14
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
15
- outputs = model(**inputs)
16
- probs = torch.nn.functional.softmax(outputs.logits, dim=1)
17
- label = labels[torch.argmax(probs).item()]
18
- results.append(label)
 
 
 
 
 
 
19
 
20
  return results
 
1
+ from transformers import pipeline
 
 
2
 
3
+ # 🔥 lebih ringan daripada load manual
4
+ classifier = pipeline(
5
+ "sentiment-analysis",
6
+ model="indobenchmark/indobert-base-p1"
7
+ )
8
 
9
  def predict(texts):
10
  results = []
11
 
12
+ try:
13
+ outputs = classifier(texts)
14
+ for o in outputs:
15
+ label = o['label']
16
+ if label == 'LABEL_0':
17
+ results.append("Negative")
18
+ elif label == 'LABEL_1':
19
+ results.append("Neutral")
20
+ else:
21
+ results.append("Positive")
22
+ except:
23
+ results = ["Neutral"] * len(texts)
24
 
25
  return results