Update README.md
Browse files
README.md
CHANGED
|
@@ -22,3 +22,45 @@ license: apache-2.0 # เปลี่ยนได้ตามที่คุ
|
|
| 22 |
---
|
| 23 |
|
| 24 |
## โครงไฟล์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
---
|
| 23 |
|
| 24 |
## โครงไฟล์
|
| 25 |
+
common/models.py # สร้างโมเดลด้วย create_model_by_name(...)
|
| 26 |
+
baseline/config.json
|
| 27 |
+
baseline/model.safetensors
|
| 28 |
+
cnn_bilstm/config.json
|
| 29 |
+
cnn_bilstm/model.safetensors
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
## วิธีใช้งานอย่างรวดเร็ว
|
| 33 |
+
|
| 34 |
+
> ต้องมี `torch`, `transformers`, `safetensors`, `sentencepiece`
|
| 35 |
+
|
| 36 |
+
```python
|
| 37 |
+
import json, importlib.util, torch, torch.nn.functional as F
|
| 38 |
+
from transformers import AutoTokenizer
|
| 39 |
+
from huggingface_hub import hf_hub_download
|
| 40 |
+
from safetensors.torch import load_file
|
| 41 |
+
|
| 42 |
+
REPO_ID = "Dusit-P/thai-sentiment-wcb"
|
| 43 |
+
MODEL_DIR = "cnn_bilstm" # หรือ "baseline"
|
| 44 |
+
|
| 45 |
+
# โหลดสถาปัตยกรรม
|
| 46 |
+
models_py = hf_hub_download(REPO_ID, filename="common/models.py")
|
| 47 |
+
spec = importlib.util.spec_from_file_location("models", models_py)
|
| 48 |
+
mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
|
| 49 |
+
|
| 50 |
+
# โหลดคอนฟิกและน้ำหนัก
|
| 51 |
+
cfg_path = hf_hub_download(REPO_ID, filename=f"{MODEL_DIR}/config.json")
|
| 52 |
+
w_path = hf_hub_download(REPO_ID, filename=f"{MODEL_DIR}/model.safetensors")
|
| 53 |
+
cfg = json.load(open(cfg_path, "r", encoding="utf-8"))
|
| 54 |
+
|
| 55 |
+
tok = AutoTokenizer.from_pretrained(cfg["base_model"])
|
| 56 |
+
model = mod.create_model_by_name(cfg["arch"])
|
| 57 |
+
state = load_file(w_path)
|
| 58 |
+
model.load_state_dict(state, strict=True)
|
| 59 |
+
model.eval()
|
| 60 |
+
|
| 61 |
+
text = "บริการดีมาก ประทับใจ"
|
| 62 |
+
enc = tok([text], padding=True, truncation=True, max_length=cfg["max_len"], return_tensors="pt")
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
p = F.softmax(model(enc["input_ids"], enc["attention_mask"]), dim=1)[0].tolist()
|
| 65 |
+
|
| 66 |
+
print({"negative": p[0], "positive": p[1], "label": "positive" if p[1] >= p[0] else "negative"})
|