KagChi commited on
Commit
42d91ef
·
verified ·
1 Parent(s): 9749881

feat: add highlight

Browse files
Files changed (2) hide show
  1. main.py +83 -18
  2. templates/index.html +26 -0
main.py CHANGED
@@ -77,6 +77,69 @@ class PredictionResponse(BaseModel):
77
  probabilities: dict # Probabilities for each class
78
  confidence: float # Probability for the predicted class
79
  status: str # "judi", "bukan judi", or "ragu-ragu"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  @app.get("/")
82
  def read_root(request: Request):
@@ -87,18 +150,17 @@ def read_root(request: Request):
87
  @app.post("/predict", response_model=PredictionResponse)
88
  async def predict(input_data: PredictionInput):
89
  try:
90
- # Threshold values
91
  threshold_low = 0.4
92
  threshold_high = 0.7
93
-
94
- text = str(input_data.text) if input_data.text is not None else ""
95
-
96
  # Tokenize input text
97
  inputs = tokenizer(
98
- text,
99
- return_tensors="pt",
100
- padding=True,
101
- truncation=True,
102
  max_length=128
103
  )
104
  inputs = {key: val.to(device) for key, val in inputs.items()}
@@ -107,34 +169,37 @@ async def predict(input_data: PredictionInput):
107
  with torch.no_grad():
108
  outputs = model(**inputs)
109
  probs = F.softmax(outputs.logits, dim=-1)
110
- prob_0 = probs[0][0].item() # not gambling
111
- prob_1 = probs[0][1].item() # gambling
112
-
113
- # Determine prediction based on probability thresholds
114
  if prob_1 >= threshold_high:
115
- prediction = 1 # gambling
116
  prediction_text = f'judi ({prob_1:.2f})'
117
  status = "judi"
118
  confidence = prob_1
119
  elif prob_1 <= threshold_low:
120
- prediction = 0 # not gambling
121
  prediction_text = f'bukan judi ({prob_0:.2f})'
122
  status = "bukan judi"
123
  confidence = prob_0
124
  else:
125
- prediction = -1 # unknown / uncertain
126
  prediction_text = f'ragu-ragu ({prob_1:.2f})'
127
  status = "ragu-ragu"
128
  confidence = max(prob_0, prob_1)
129
-
 
 
130
  return PredictionResponse(
131
  prediction=prediction,
132
  prediction_text=prediction_text,
133
  probabilities={"prob_bukan_judi": prob_0, "prob_judi": prob_1},
134
  confidence=confidence,
135
- status=status
 
136
  )
137
-
138
  except Exception as e:
139
  raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
140
 
 
77
  probabilities: dict # Probabilities for each class
78
  confidence: float # Probability for the predicted class
79
  status: str # "judi", "bukan judi", or "ragu-ragu"
80
+ highlighted_tokens: list[str]
81
+
82
+ def get_important_tokens(text: str, tokenizer, model, target_class=1, top_k=3):
83
+ model.eval()
84
+
85
+ # Tokenize dengan offset mapping untuk kata asli
86
+ encoding = tokenizer(
87
+ text,
88
+ return_tensors="pt",
89
+ return_offsets_mapping=True,
90
+ padding=True,
91
+ truncation=True,
92
+ max_length=128
93
+ )
94
+ input_ids = encoding["input_ids"].to(model.device)
95
+ attention_mask = encoding["attention_mask"].to(model.device)
96
+ offset_mapping = encoding["offset_mapping"][0].tolist()
97
+
98
+ # Dapatkan embeddings
99
+ embedding_layer = model.get_input_embeddings()
100
+ input_embeddings = embedding_layer(input_ids)
101
+ input_embeddings.retain_grad()
102
+
103
+ # Forward pass
104
+ outputs = model(inputs_embeds=input_embeddings, attention_mask=attention_mask)
105
+ probs = F.softmax(outputs.logits, dim=-1)
106
+ target_score = probs[0][target_class]
107
+
108
+ # Backward pass
109
+ model.zero_grad()
110
+ target_score.backward()
111
+
112
+ # Importance score per token
113
+ grads = input_embeddings.grad.abs().sum(dim=-1).squeeze().tolist()
114
+
115
+ # Mapping: offset range → importance score
116
+ token_scores = []
117
+ for i, (start, end) in enumerate(offset_mapping):
118
+ if start == end: # Special tokens
119
+ continue
120
+ token_scores.append((start, end, grads[i]))
121
+
122
+ # Gabungkan berdasarkan overlap character range
123
+ from collections import defaultdict
124
+ char_score_map = defaultdict(float)
125
+ for start, end, score in token_scores:
126
+ for i in range(start, end):
127
+ char_score_map[i] += score
128
+
129
+ # Bagi teks ke kata
130
+ import re
131
+ word_spans = [(m.start(), m.end(), m.group()) for m in re.finditer(r'\S+', text)]
132
+
133
+ word_scores = []
134
+ for start, end, word in word_spans:
135
+ score = sum(char_score_map[i] for i in range(start, end))
136
+ word_scores.append((word, score))
137
+
138
+ # Ambil kata dengan skor tertinggi
139
+ word_scores.sort(key=lambda x: x[1], reverse=True)
140
+ highlighted_words = [w for w, s in word_scores[:top_k]]
141
+
142
+ return highlighted_words
143
 
144
  @app.get("/")
145
  def read_root(request: Request):
 
150
  @app.post("/predict", response_model=PredictionResponse)
151
  async def predict(input_data: PredictionInput):
152
  try:
 
153
  threshold_low = 0.4
154
  threshold_high = 0.7
155
+
156
+ text = str(input_data.text) if input_data.text else ""
157
+
158
  # Tokenize input text
159
  inputs = tokenizer(
160
+ text,
161
+ return_tensors="pt",
162
+ padding=True,
163
+ truncation=True,
164
  max_length=128
165
  )
166
  inputs = {key: val.to(device) for key, val in inputs.items()}
 
169
  with torch.no_grad():
170
  outputs = model(**inputs)
171
  probs = F.softmax(outputs.logits, dim=-1)
172
+ prob_0 = probs[0][0].item()
173
+ prob_1 = probs[0][1].item()
174
+
175
+ # Interpret prediction
176
  if prob_1 >= threshold_high:
177
+ prediction = 1
178
  prediction_text = f'judi ({prob_1:.2f})'
179
  status = "judi"
180
  confidence = prob_1
181
  elif prob_1 <= threshold_low:
182
+ prediction = 0
183
  prediction_text = f'bukan judi ({prob_0:.2f})'
184
  status = "bukan judi"
185
  confidence = prob_0
186
  else:
187
+ prediction = -1
188
  prediction_text = f'ragu-ragu ({prob_1:.2f})'
189
  status = "ragu-ragu"
190
  confidence = max(prob_0, prob_1)
191
+
192
+ highlighted_tokens = get_important_tokens(text, tokenizer, model, target_class=1)
193
+
194
  return PredictionResponse(
195
  prediction=prediction,
196
  prediction_text=prediction_text,
197
  probabilities={"prob_bukan_judi": prob_0, "prob_judi": prob_1},
198
  confidence=confidence,
199
+ status=status,
200
+ highlighted_tokens=highlighted_tokens
201
  )
202
+
203
  except Exception as e:
204
  raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
205
 
templates/index.html CHANGED
@@ -53,6 +53,15 @@
53
  .gradient-error {
54
  background: linear-gradient(135deg, #ff6b6b 0%, #ffa500 100%);
55
  }
 
 
 
 
 
 
 
 
 
56
  </style>
57
  </head>
58
 
@@ -203,6 +212,16 @@
203
  statusExplanation = 'Model tidak yakin dengan klasifikasi teks ini';
204
  }
205
 
 
 
 
 
 
 
 
 
 
 
206
  resultContent.innerHTML = `
207
  <div class="space-y-2 sm:space-y-3">
208
  <p><strong>Status:</strong> ${data.status}</p>
@@ -245,6 +264,13 @@
245
  </div>
246
  `;
247
 
 
 
 
 
 
 
 
248
  result.classList.remove('hidden');
249
  }
250
 
 
53
  .gradient-error {
54
  background: linear-gradient(135deg, #ff6b6b 0%, #ffa500 100%);
55
  }
56
+
57
+ .highlight-word {
58
+ background-color: rgba(255, 255, 255, 0.85);
59
+ color: #d6336c;
60
+ font-weight: 600;
61
+ padding: 0 4px;
62
+ border-radius: 4px;
63
+ box-shadow: 0 1px 2px rgba(0,0,0,0.1);
64
+ }
65
  </style>
66
  </head>
67
 
 
212
  statusExplanation = 'Model tidak yakin dengan klasifikasi teks ini';
213
  }
214
 
215
+ const originalText = document.getElementById('textInput').value;
216
+ const importantWords = data.prediction === 1 ? data.highlighted_tokens || [] : [];
217
+
218
+ let highlightedText = originalText;
219
+
220
+ importantWords.forEach(word => {
221
+ const regex = new RegExp(`(${word})`, 'gi');
222
+ highlightedText = highlightedText.replace(regex, '<span class="highlight-word">$1</span>');
223
+ });
224
+
225
  resultContent.innerHTML = `
226
  <div class="space-y-2 sm:space-y-3">
227
  <p><strong>Status:</strong> ${data.status}</p>
 
264
  </div>
265
  `;
266
 
267
+ resultContent.innerHTML += `
268
+ <div class="mt-4 sm:mt-6 bg-white bg-opacity-20 p-4 rounded text-xs sm:text-sm">
269
+ <strong>Highlight Kata Penting:</strong>
270
+ <p class="mt-2">${highlightedText}</p>
271
+ </div>
272
+ `;
273
+
274
  result.classList.remove('hidden');
275
  }
276