noranisa commited on
Commit
47e459a
Β·
verified Β·
1 Parent(s): ad0f726

Update services/sentiment.py

Browse files
Files changed (1) hide show
  1. services/sentiment.py +90 -14
services/sentiment.py CHANGED
@@ -1,26 +1,102 @@
1
  from transformers import pipeline
 
2
 
3
- classifier = pipeline(
4
- "sentiment-analysis",
5
- model="w11wo/indonesian-roberta-base-sentiment-classifier"
6
- )
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def predict(texts):
9
  results = []
10
 
 
 
 
 
11
  try:
12
- outputs = classifier(texts)
13
- for o in outputs:
14
- label = o['label'].lower()
15
 
16
- if "positive" in label:
17
- results.append("Positive")
18
- elif "negative" in label:
19
- results.append("Negative")
20
- else:
21
- results.append("Neutral")
22
 
23
- except:
 
24
  results = ["Neutral"] * len(texts)
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  return results
 
1
  from transformers import pipeline
2
+ import os
3
 
4
+ # πŸ”₯ PATH MODEL (hasil fine-tuning)
5
+ LOCAL_MODEL_PATH = "model/final_model"
 
 
6
 
7
+ # πŸ”„ fallback model (pretrained)
8
+ FALLBACK_MODEL = "w11wo/indonesian-roberta-base-sentiment-classifier"
9
+
10
+
11
+ # πŸš€ INIT MODEL
12
+ def load_model():
13
+ try:
14
+ # πŸ‘‰ cek apakah model fine-tuning ada
15
+ if os.path.exists(LOCAL_MODEL_PATH):
16
+ print("βœ… Load fine-tuned model")
17
+ return pipeline("sentiment-analysis", model=LOCAL_MODEL_PATH)
18
+
19
+ else:
20
+ print("⚠️ Load fallback model (RoBERTa)")
21
+ return pipeline("sentiment-analysis", model=FALLBACK_MODEL)
22
+
23
+ except Exception as e:
24
+ print("❌ Gagal load model:", e)
25
+ return None
26
+
27
+
28
+ # πŸ”₯ LOAD SEKALI SAJA (BIAR CEPAT)
29
+ classifier = load_model()
30
+
31
+
32
+ # 🧠 NORMALISASI LABEL
33
+ def normalize_label(label):
34
+ label = label.lower()
35
+
36
+ # πŸ‘‰ untuk model huggingface (positive/negative)
37
+ if "positive" in label:
38
+ return "Positive"
39
+ elif "negative" in label:
40
+ return "Negative"
41
+ elif "neutral" in label:
42
+ return "Neutral"
43
+
44
+ # πŸ‘‰ untuk model fine-tuned (LABEL_0,1,2)
45
+ if label == "label_0":
46
+ return "Negative"
47
+ elif label == "label_1":
48
+ return "Neutral"
49
+ elif label == "label_2":
50
+ return "Positive"
51
+
52
+ return "Neutral"
53
+
54
+
55
+ # πŸ” PREDICT UTAMA
56
  def predict(texts):
57
  results = []
58
 
59
+ if classifier is None:
60
+ print("⚠️ Model tidak tersedia")
61
+ return ["Neutral"] * len(texts)
62
+
63
  try:
64
+ # πŸ”₯ batched prediction (lebih cepat)
65
+ outputs = classifier(texts, batch_size=8, truncation=True)
 
66
 
67
+ for o in outputs:
68
+ label = normalize_label(o['label'])
69
+ results.append(label)
 
 
 
70
 
71
+ except Exception as e:
72
+ print("❌ Error saat prediksi:", e)
73
  results = ["Neutral"] * len(texts)
74
 
75
+ return results
76
+
77
+
78
+ # πŸ”Ž PREDICT SINGLE (opsional)
79
+ def predict_single(text):
80
+ return predict([text])[0]
81
+
82
+
83
+ # πŸ“Š PREDICT + SCORE (opsional untuk analisis lebih lanjut)
84
+ def predict_with_score(texts):
85
+ results = []
86
+
87
+ if classifier is None:
88
+ return [{"label": "Neutral", "score": 0}] * len(texts)
89
+
90
+ try:
91
+ outputs = classifier(texts, batch_size=8, truncation=True)
92
+
93
+ for o in outputs:
94
+ results.append({
95
+ "label": normalize_label(o['label']),
96
+ "score": round(o['score'], 4)
97
+ })
98
+
99
+ except Exception as e:
100
+ print("❌ Error:", e)
101
+
102
  return results