Update README.md
Browse files
README.md
CHANGED
|
@@ -84,102 +84,51 @@ base_model:
|
|
| 84 |
---
|
| 85 |
## How to use the model
|
| 86 |
```
|
| 87 |
-
import torch
|
| 88 |
-
import torch.nn as nn
|
| 89 |
-
import numpy as np
|
| 90 |
from transformers import AutoTokenizer, AutoModel
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
self.bert = base_model
|
| 110 |
-
self.head_senti = nn.Linear(hidden, 5)
|
| 111 |
-
self.head_act = nn.Linear(hidden, 6)
|
| 112 |
-
self.head_emo = nn.Linear(hidden, 7)
|
| 113 |
-
self.head_reg = nn.Linear(hidden, 3)
|
| 114 |
-
self.has_token_type = getattr(self.bert.embeddings, "token_type_embeddings", None) is not None
|
| 115 |
-
|
| 116 |
-
def forward(self, input_ids, attention_mask, token_type_ids=None):
|
| 117 |
-
kwargs = dict(input_ids=input_ids, attention_mask=attention_mask)
|
| 118 |
-
if self.has_token_type and token_type_ids is not None:
|
| 119 |
-
kwargs["token_type_ids"] = token_type_ids
|
| 120 |
-
out = self.bert(**kwargs)
|
| 121 |
-
h = out.last_hidden_state[:, 0] # [CLS]
|
| 122 |
-
return {
|
| 123 |
-
"logits_senti": self.head_senti(h),
|
| 124 |
-
"logits_act": self.head_act(h),
|
| 125 |
-
"logits_emo": self.head_emo(h),
|
| 126 |
-
"pred_reg": self.head_reg(h)
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
# ---- ์ฒดํฌํฌ์ธํธ ๋ก๋ ----
|
| 130 |
-
def load_ckpt_from_hub():
|
| 131 |
-
path = hf_hub_download(repo_id=REPO_ID, filename=CKPT_RELPATH)
|
| 132 |
-
obj = torch.load(path, map_location="cpu")
|
| 133 |
-
return obj
|
| 134 |
-
|
| 135 |
-
# ---- ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๊ตฌ์ฑ ----
|
| 136 |
-
def build_model_and_tokenizer(ckpt_obj, hidden=768):
|
| 137 |
-
model_name = ckpt_obj.get("model_name", "klue/bert-base")
|
| 138 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 139 |
-
base = AutoModel.from_pretrained(model_name)
|
| 140 |
-
model = KbertMTL(base_model=base, hidden=hidden)
|
| 141 |
-
state_dict = ckpt_obj["state_dict"] if "state_dict" in ckpt_obj else ckpt_obj
|
| 142 |
-
model.load_state_dict(state_dict, strict=False)
|
| 143 |
-
emo_thr = float(ckpt_obj.get("emo_threshold", 0.5))
|
| 144 |
-
return model, tokenizer, emo_thr
|
| 145 |
-
|
| 146 |
-
# ---- ์ถ๋ก ----
|
| 147 |
-
@torch.no_grad()
|
| 148 |
-
def predict(text, model, tokenizer, device="cpu", max_len=200, emo_threshold=0.5):
|
| 149 |
-
model.to(device).eval()
|
| 150 |
-
enc = tokenizer([text], padding=True, truncation=True, max_length=max_len, return_tensors="pt").to(device)
|
| 151 |
out = model(**enc)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
"text":
|
| 162 |
"pred_sentiment_strength": IDX2SENTI[senti],
|
| 163 |
-
"pred_action_signal":
|
| 164 |
-
"pred_emotions":
|
| 165 |
-
"pred_certainty": float(
|
| 166 |
-
"pred_relevance": float(
|
| 167 |
-
"pred_toxicity": float(
|
| 168 |
}
|
|
|
|
| 169 |
|
| 170 |
-
# ---- ๋ฉ์ธ ----
|
| 171 |
-
if __name__ == "__main__":
|
| 172 |
-
text = input("๋ถ์ํ ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์: ").strip()
|
| 173 |
-
print("[๋ชจ๋ธ ๋ก๋ ์ค...]")
|
| 174 |
-
ckpt = load_ckpt_from_hub()
|
| 175 |
-
model, tokenizer, emo_thr = build_model_and_tokenizer(ckpt)
|
| 176 |
-
|
| 177 |
-
print("[์ถ๋ก ์ค...]")
|
| 178 |
-
result = predict(text, model, tokenizer, device="cuda" if torch.cuda.is_available() else "cpu", emo_threshold=emo_thr)
|
| 179 |
-
|
| 180 |
-
print("\n=== ๊ฒฐ๊ณผ ===")
|
| 181 |
-
for k,v in result.items():
|
| 182 |
-
print(f"{k}: {v}")
|
| 183 |
```
|
| 184 |
---
|
| 185 |
|
|
|
|
| 84 |
---
|
| 85 |
## How to use the model
|
| 86 |
```
|
| 87 |
+
import torch, json
|
|
|
|
|
|
|
| 88 |
from transformers import AutoTokenizer, AutoModel
|
| 89 |
+
|
| 90 |
+
repo_or_dir = "LangQuant/LQ-Kbert-base"
|
| 91 |
+
texts = [
|
| 92 |
+
"๋นํธ์ฝ์ธ ์กฐ์ ํ ๋ฐ๋ฑ, ํฌ์์ฌ๋ฆฌ ๊ฐ์ ",
|
| 93 |
+
"ํ์จ ๊ธ๋ฑ์ ์ฆ์ ๋ณ๋์ฑ ํ๋",
|
| 94 |
+
"๋นํธ ๊ทธ๋ง ์ข ๋ด๋ ค๋ผ ์ง์ง..",
|
| 95 |
+
"ํญ๋ฝใ
ใ
ใ
ใ
ใ
๋ค ํ์์ผํ ๊น์?"
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_or_dir, local_files_only=True)
|
| 100 |
+
model = AutoModel.from_pretrained(repo_or_dir, trust_remote_code=True, local_files_only=True)
|
| 101 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 102 |
+
model.to(device).eval()
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
enc = tokenizer(texts, return_tensors="pt", padding=True, truncation=True, max_length=200).to(device)
|
| 106 |
+
with torch.inference_mode():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
out = model(**enc)
|
| 108 |
|
| 109 |
+
IDX2SENTI = {0:"strong_pos",1:"weak_pos",2:"neutral",3:"weak_neg",4:"strong_neg"}
|
| 110 |
+
IDX2ACT = {0:"buy",1:"hold",2:"sell",3:"avoid",4:"info_only",5:"ask_info"}
|
| 111 |
+
EMO_LIST = ["greed","fear","confidence","doubt","anger","hope","sarcasm"]
|
| 112 |
+
|
| 113 |
|
| 114 |
+
for i, t in enumerate(texts):
|
| 115 |
+
senti = int(out["logits_senti"][i].argmax().item())
|
| 116 |
+
act = int(out["logits_act"][i].argmax().item())
|
| 117 |
+
emo_p = torch.sigmoid(out["logits_emo"][i]).tolist()
|
| 118 |
+
reg = torch.clamp(out["pred_reg"][i], 0, 1).tolist()
|
| 119 |
+
emos = [EMO_LIST[j] for j,p in enumerate(emo_p) if p >= 0.5]
|
| 120 |
|
| 121 |
+
result = {
|
| 122 |
+
"text": t,
|
| 123 |
"pred_sentiment_strength": IDX2SENTI[senti],
|
| 124 |
+
"pred_action_signal": IDX2ACT[act],
|
| 125 |
+
"pred_emotions": emos,
|
| 126 |
+
"pred_certainty": float(reg[0]),
|
| 127 |
+
"pred_relevance": float(reg[1]),
|
| 128 |
+
"pred_toxicity": float(reg[2]),
|
| 129 |
}
|
| 130 |
+
print(json.dumps(result, ensure_ascii=False))
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
```
|
| 133 |
---
|
| 134 |
|