Update README.md
Browse files
README.md
CHANGED
|
@@ -52,23 +52,68 @@ KoELECTRA ๊ธฐ๋ฐ์ ํ๊ตญ์ด(ํนํ ์ผ๊ธฐ/์ฌ๋ฆฌ ๊ธฐ๋ก) ๊ฐ์ ๋ถ๋ฅ ๋ชจ๋ธ
|
|
| 52 |
## How to Get Started with the Model
|
| 53 |
```python
|
| 54 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 55 |
-
import torch.nn.functional as F
|
| 56 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
model_name = "LimYeri/HowRU-KoELECTRA-Emotion-Classifier"
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
inputs = tokenizer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with torch.no_grad():
|
| 66 |
logits = model(**inputs).logits
|
| 67 |
probs = F.softmax(logits, dim=-1)[0]
|
| 68 |
-
label_id = probs.argmax().item()
|
| 69 |
-
return label_id, probs.tolist()
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
```
|
| 73 |
|
| 74 |
---
|
|
|
|
| 52 |
## How to Get Started with the Model
|
| 53 |
```python
|
| 54 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 55 |
import torch
|
| 56 |
+
import torch.nn.functional as F
|
| 57 |
+
|
| 58 |
+
# 1) Load Model & Tokenizer
|
| 59 |
+
MODEL_NAME = "LimYeri/HowRU-KoELECTRA-Emotion-Classifier"
|
| 60 |
+
|
| 61 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 62 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 63 |
+
|
| 64 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ ์๋ ์ ํ
|
| 65 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 66 |
+
model.to(device)
|
| 67 |
+
model.eval()
|
| 68 |
+
|
| 69 |
+
# ๊ฐ์ ๋ผ๋ฒจ ๋งคํ (id2label)
|
| 70 |
+
id2label = model.config.id2label
|
| 71 |
|
|
|
|
| 72 |
|
| 73 |
+
# 2) Inference Function
|
| 74 |
+
def predict_emotion(text: str):
|
| 75 |
+
"""
|
| 76 |
+
Returns:
|
| 77 |
+
- top1_pred: ์์ธก๋ ๊ฐ์ ๋ผ๋ฒจ
|
| 78 |
+
- probs_sorted: ๊ฐ์ ๋ณ ํ๋ฅ (๋ด๋ฆผ์ฐจ์)
|
| 79 |
+
- top2_pred: ์์ ๋ ๊ฐ์ ๊ฐ์
|
| 80 |
+
"""
|
| 81 |
|
| 82 |
+
# ํ ํฌ๋์ด์ง
|
| 83 |
+
inputs = tokenizer(
|
| 84 |
+
text,
|
| 85 |
+
return_tensors="pt",
|
| 86 |
+
truncation=True,
|
| 87 |
+
padding=True,
|
| 88 |
+
max_length=512
|
| 89 |
+
).to(device)
|
| 90 |
+
|
| 91 |
+
# ์ถ๋ก
|
| 92 |
with torch.no_grad():
|
| 93 |
logits = model(**inputs).logits
|
| 94 |
probs = F.softmax(logits, dim=-1)[0]
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
# ์ ๋ ฌ๋ ํ๋ฅ
|
| 97 |
+
probs_sorted = sorted(
|
| 98 |
+
[(id2label[i], float(probs[i])) for i in range(len(probs))],
|
| 99 |
+
key=lambda x: x[1],
|
| 100 |
+
reverse=True
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
top1_pred = probs_sorted[0]
|
| 104 |
+
top2_pred = probs_sorted[:2]
|
| 105 |
+
|
| 106 |
+
return {
|
| 107 |
+
"text": text,
|
| 108 |
+
"top1_emotion": top1_pred,
|
| 109 |
+
"top2_emotions": top2_pred,
|
| 110 |
+
"all_probabilities": probs_sorted,
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# 3) Example
|
| 115 |
+
result = predict_emotion("์ค๋ ์ ๋ง ๊ธฐ๋ถ์ด ์ข๊ณ ํ๋ณตํ ํ๋ฃจ์์ด!")
|
| 116 |
+
print(result)
|
| 117 |
```
|
| 118 |
|
| 119 |
---
|