hy commited on
Commit
61d0a1d
ยท
1 Parent(s): 1ae484c
Files changed (1) hide show
  1. aggro_model.py +82 -19
aggro_model.py CHANGED
@@ -152,48 +152,111 @@ except Exception as e:
152
  # 3. ๋ฉ”์ธ ํ•จ์ˆ˜
153
  # =============================================================================
154
  def get_aggro_score(title: str) -> dict:
155
-
 
156
  # 1. ๊ทœ์น™ ๊ธฐ๋ฐ˜ ์ ์ˆ˜
157
  rule_score = 0.0
158
  rule_pattern = "๋ถ„์„ ๋ถˆ๊ฐ€"
159
 
160
- if rule_scorer:
161
- try:
162
- res = rule_scorer.get_score(title)
163
- rule_score = res['score']
164
- rule_pattern = res.get('pattern_name', '์•Œ ์ˆ˜ ์—†์Œ')
165
- except Exception as e:
166
- print(f"๊ทœ์น™ ๊ณ„์‚ฐ ์—๋Ÿฌ: {e}")
167
- rule_score = 50.0
 
168
 
169
  # 2. KoBERT ์ ์ˆ˜
170
  bert_score = 0.0
171
  if aggro_model and tokenizer:
172
  try:
173
  inputs = tokenizer(
174
- title, return_tensors='pt', padding="max_length", truncation=True, max_length=64
 
 
 
 
175
  )
176
  input_ids = inputs['input_ids'].to(device)
177
  mask = inputs['attention_mask'].to(device)
 
 
 
178
 
179
  with torch.no_grad():
180
  outputs = aggro_model(input_ids, mask)
181
- probs = F.softmax(outputs / 2.0, dim=1)
 
 
 
 
182
  bert_score = probs[0][1].item() * 100
183
- except:
 
 
 
 
184
  bert_score = 50.0
185
 
186
- # Safety Net ์ ์šฉ (๊ทœ์น™ ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด AI ์ ์ˆ˜๋„ ๊นŽ์Œ)
187
  if rule_score < 5:
188
- bert_score *= 0.3 # ๊ทœ์น™ ์ ์ˆ˜๊ฐ€ ๊ฑฐ์˜ ์—†์œผ๋ฉด AI ์ ์ˆ˜ 70% ์‚ญ๊ฐ
 
189
  elif rule_score < 20:
190
- bert_score *= 0.8 # ๊ทœ์น™ ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด AI ์ ์ˆ˜ 20% ์‚ญ๊ฐ
191
-
192
- #3. ํ•ฉ์‚ฐ
193
- w_rule = 0.0
194
- w_bert = 1.0
195
 
 
 
 
 
196
  final_score = (rule_score * w_rule) + (bert_score * w_bert)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  # 4. ๊ฒฐ๊ณผ
199
  normalized_score = min(final_score / 100.0, 1.0)
 
152
  # 3. ๋ฉ”์ธ ํ•จ์ˆ˜
153
  # =============================================================================
154
  def get_aggro_score(title: str) -> dict:
155
+ print(f"\n[DEBUG] ๋ถ„์„ํ•  ์ œ๋ชฉ: {title}") # 1. ์ œ๋ชฉ์ด ์ž˜ ๋“ค์–ด์™”๋‚˜ ํ™•์ธ
156
+
157
  # 1. ๊ทœ์น™ ๊ธฐ๋ฐ˜ ์ ์ˆ˜
158
  rule_score = 0.0
159
  rule_pattern = "๋ถ„์„ ๋ถˆ๊ฐ€"
160
 
161
+ try:
162
+ res = rule_scorer.get_score(title)
163
+ rule_score = res['score'] # 0~100์ 
164
+ rule_pattern = res.get('pattern_name', '์•Œ ์ˆ˜ ์—†์Œ')
165
+ except Exception as e:
166
+ print(f"[DEBUG] ๊ทœ์น™ ๊ณ„์‚ฐ ์—๋Ÿฌ: {e}")
167
+ rule_score = 0.0
168
+
169
+ print(f"[DEBUG] 1. ๊ทœ์น™ ์ ์ˆ˜: {rule_score}") # 2. ๊ทœ์น™ ์ ์ˆ˜ ํ™•์ธ
170
 
171
  # 2. KoBERT ์ ์ˆ˜
172
  bert_score = 0.0
173
  if aggro_model and tokenizer:
174
  try:
175
  inputs = tokenizer(
176
+ title,
177
+ return_tensors='pt',
178
+ padding="max_length",
179
+ truncation=True,
180
+ max_length=64
181
  )
182
  input_ids = inputs['input_ids'].to(device)
183
  mask = inputs['attention_mask'].to(device)
184
+
185
+ # ํ† ํฐํ™” ๊ฒฐ๊ณผ ํ™•์ธ (์ œ๋Œ€๋กœ ์ž˜๋ ธ๋Š”์ง€)
186
+ # print(f"[DEBUG] ํ† ํฐํ™” ๊ฒฐ๊ณผ: {inputs['input_ids'][0][:10]}")
187
 
188
  with torch.no_grad():
189
  outputs = aggro_model(input_ids, mask)
190
+ # ๐Ÿšจ ์›๋ณธ ๋กœ์ง (Logits ๊ฐ’ ํ™•์ธ)
191
+ print(f"[DEBUG] ๋ชจ๋ธ ์ถœ๋ ฅ๊ฐ’(Logits): {outputs}")
192
+
193
+ # Temperature Scaling ์ ์šฉ ์ „/ํ›„ ๋น„๊ต
194
+ probs = F.softmax(outputs / 2.0, dim=1)
195
  bert_score = probs[0][1].item() * 100
196
+
197
+ print(f"[DEBUG] 2. BERT ์›๋ณธ ์ ์ˆ˜: {bert_score}") # 3. AI ์ ์ˆ˜ ํ™•์ธ
198
+
199
+ except Exception as e:
200
+ print(f"[Aggro] KoBERT ์˜ˆ์ธก ์˜ค๋ฅ˜: {e}")
201
  bert_score = 50.0
202
 
203
+ # 3. Safety Net (์ ์ˆ˜ ๊นŽ๊ธฐ)
204
  if rule_score < 5:
205
+ print("[DEBUG] Safety Net ๋ฐœ๋™! (๊ทœ์น™ ์ ์ˆ˜ ๋ฏธ๋‹ฌ -> AI ์ ์ˆ˜ 70% ์‚ญ๊ฐ)")
206
+ bert_score *= 0.3
207
  elif rule_score < 20:
208
+ print("[DEBUG] Safety Net ๋ฐœ๋™! (๊ทœ์น™ ์ ์ˆ˜ ๋‚ฎ์Œ -> AI ์ ์ˆ˜ 20% ์‚ญ๊ฐ)")
209
+ bert_score *= 0.8
210
+
211
+ print(f"[DEBUG] 3. ๋ณด์ •๋œ BERT ์ ์ˆ˜: {bert_score}") # 4. ๊นŽ์ธ ์ ์ˆ˜ ํ™•์ธ
 
212
 
213
+ # 4. ์ตœ์ข… ํ•ฉ์‚ฐ
214
+ w_rule = 0.4
215
+ w_bert = 0.6
216
+
217
  final_score = (rule_score * w_rule) + (bert_score * w_bert)
218
+ print(f"[DEBUG] 4. ์ตœ์ข… ํ•ฉ์‚ฐ ์ ์ˆ˜: {final_score}")
219
+ # # 1. ๊ทœ์น™ ๊ธฐ๋ฐ˜ ์ ์ˆ˜
220
+ # rule_score = 0.0
221
+ # rule_pattern = "๋ถ„์„ ๋ถˆ๊ฐ€"
222
+
223
+ # if rule_scorer:
224
+ # try:
225
+ # res = rule_scorer.get_score(title)
226
+ # rule_score = res['score']
227
+ # rule_pattern = res.get('pattern_name', '์•Œ ์ˆ˜ ์—†์Œ')
228
+ # except Exception as e:
229
+ # print(f"๊ทœ์น™ ๊ณ„์‚ฐ ์—๋Ÿฌ: {e}")
230
+ # rule_score = 50.0
231
+
232
+ # # 2. KoBERT ์ ์ˆ˜
233
+ # bert_score = 0.0
234
+ # if aggro_model and tokenizer:
235
+ # try:
236
+ # inputs = tokenizer(
237
+ # title, return_tensors='pt', padding="max_length", truncation=True, max_length=64
238
+ # )
239
+ # input_ids = inputs['input_ids'].to(device)
240
+ # mask = inputs['attention_mask'].to(device)
241
+
242
+ # with torch.no_grad():
243
+ # outputs = aggro_model(input_ids, mask)
244
+ # probs = F.softmax(outputs / 2.0, dim=1)
245
+ # bert_score = probs[0][1].item() * 100
246
+ # except:
247
+ # bert_score = 50.0
248
+
249
+ # # Safety Net ์ ์šฉ (๊ทœ์น™ ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด AI ์ ์ˆ˜๋„ ๊นŽ์Œ)
250
+ # if rule_score < 5:
251
+ # bert_score *= 0.3 # ๊ทœ์น™ ์ ์ˆ˜๊ฐ€ ๊ฑฐ์˜ ์—†์œผ๋ฉด AI ์ ์ˆ˜ 70% ์‚ญ๊ฐ
252
+ # elif rule_score < 20:
253
+ # bert_score *= 0.8 # ๊ทœ์น™ ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด AI ์ ์ˆ˜ 20% ์‚ญ๊ฐ
254
+
255
+ # #3. ํ•ฉ์‚ฐ
256
+ # w_rule = 0.0
257
+ # w_bert = 1.0
258
+
259
+ # final_score = (rule_score * w_rule) + (bert_score * w_bert)
260
 
261
  # 4. ๊ฒฐ๊ณผ
262
  normalized_score = min(final_score / 100.0, 1.0)