Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- ko
|
| 4 |
+
base_model:
|
| 5 |
+
- beomi/KcELECTRA-base
|
| 6 |
+
pipeline_tag: text-classification
|
| 7 |
+
tags:
|
| 8 |
+
- emotion
|
| 9 |
+
- sentiment
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
## Text์ ๊ฐ์ ์ ๋ถ์ํ๊ธฐ ์ํ ๋ชจ๋ธ์
๋๋ค.
|
| 13 |
+
|
| 14 |
+
### transformers๋ก ๋ชจ๋ธ์ ๋ฐ์ ํ ์๋์ ์ฝ๋๋ฅผ ์คํ, sample_text์ ๋ฌธ์ฅ์ ๋ฃ์ผ๋ฉด ๋ถ์ํด์ค๋๋ค.
|
| 15 |
+
|
| 16 |
+
```
|
| 17 |
+
import torch
|
| 18 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 19 |
+
import torch.nn.functional as F
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
# ๋ชจ๋ธ์ด๋ ํ ํฌ๋์ด์ ๋ถ๋ฌ์ค๊ธฐ.
|
| 23 |
+
MODEL_DIR = os.path.join(OUTPUT_DIR, "best_model")
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 25 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
|
| 26 |
+
model.eval().to("cuda" if torch.cuda.is_available() else "cpu")
|
| 27 |
+
|
| 28 |
+
# ๊ฐ์ ์นดํ
๊ณ ๋ฆฌ - ๋งจ ์์ ์ ์ฅํด๋์ ๊ทธ๊ฑฐ
|
| 29 |
+
CATEGORIES = ["happy", "embarrass", "anger", "unrest", "damaged", "sadness"]
|
| 30 |
+
|
| 31 |
+
# ์ถ๋ก ํจ์. ์ฌ๊ธฐ์ txt๋ฅผ ๋ฃ์ผ๋ฉด ๊ฒฐ๊ณผ๊ฐ ๋์ด.
|
| 32 |
+
def predict_emotion(texts):
|
| 33 |
+
if isinstance(texts, str):
|
| 34 |
+
texts = [texts]
|
| 35 |
+
|
| 36 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
+
inputs = tokenizer(
|
| 38 |
+
texts,
|
| 39 |
+
padding=True,
|
| 40 |
+
truncation=True,
|
| 41 |
+
max_length=64,
|
| 42 |
+
return_tensors="pt"
|
| 43 |
+
).to(device)
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
outputs = model(**inputs)
|
| 47 |
+
probs = F.softmax(outputs.logits, dim=-1)
|
| 48 |
+
preds = probs.argmax(dim=-1)
|
| 49 |
+
|
| 50 |
+
results = []
|
| 51 |
+
for i, text in enumerate(texts):
|
| 52 |
+
results.append({
|
| 53 |
+
"text": text,
|
| 54 |
+
"pred_label": CATEGORIES[preds[i].item()],
|
| 55 |
+
"probabilities": {CATEGORIES[j]: round(probs[i][j].item(), 4) for j in range(len(CATEGORIES))}
|
| 56 |
+
})
|
| 57 |
+
return results
|
| 58 |
+
|
| 59 |
+
# ๋ฃ์ ๋ฌธ์ฅ ์์. ๋์ค์๋ ์ฌ์ฉ์์ ๋ค์ด์ด๋ฆฌ ๋ด์ฉ์ด ๋ค์ด๊ฐ ๊ฒ.
|
| 60 |
+
sample_texts = [
|
| 61 |
+
"๋ ์ค๋ ๊ธฐ๋ป์ ์ธ์์ด"
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
for r in predict_emotion(sample_texts):
|
| 65 |
+
print(f"๋ฌธ์ฅ: {r['text']}")
|
| 66 |
+
print(f"์์ธก ๊ฐ์ : {r['pred_label']}")
|
| 67 |
+
print("ํ๋ฅ ๋ถํฌ:", r["probabilities"])
|
| 68 |
+
print("-" * 60)
|
| 69 |
+
```
|