UkJin commited on
Commit
d64d5f2
ยท
verified ยท
1 Parent(s): 46c4f52

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +20 -0
model.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
+ import torch
3
+ import torch.nn.functional as F
4
+
5
+ MODEL_NAME = "beomi/KcELECTRA-small"
6
+
7
+ # ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2) # fine-tuning ํ•ด์•ผ ์ •ํ™•ํ•ด์ง
10
+
11
+ # ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
12
+ def classify_text(text):
13
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ probs = F.softmax(outputs.logits, dim=-1)
17
+ score = probs[0][1].item() # 1๋ฒˆ ๋ผ๋ฒจ์ด '์š•์„ค'์ด๋ผ๊ณ  ๊ฐ€์ •
18
+
19
+ label = "์š•์„ค" if score > 0.5 else "์ •์ƒ"
20
+ return f"๊ฒฐ๊ณผ: {label} (์š•์„ค ํ™•๋ฅ : {score:.2%})"