LimYeri commited on
Commit
b67b029
ยท
verified ยท
1 Parent(s): d227671

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +54 -9
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
- tokenizer = AutoTokenizer.from_pretrained(model_name)
61
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
 
 
 
 
 
62
 
63
- def predict(text):
64
- inputs = tokenizer(text, return_tensors="pt", truncation=True)
 
 
 
 
 
 
 
 
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
- print(predict("์˜ค๋Š˜ ์ •๋ง ๊ธฐ๋ถ„์ด ์ข‹๊ณ  ํ–‰๋ณตํ•œ ํ•˜๋ฃจ์˜€์–ด!"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  ---