Views
No views yet
โมเดลวิเคราะห์อารมณ์ ภาษาไทย แบบ 2 คลาส (negative/positive) อิง WangchanBERTa และปล่อยหลายสถาปัตยกรรม (heads) เพื่อความยืดหยุ่นของงานจริง
cnn_bilstm — WangchanBERTa → Conv1d → BiLSTM (โมเดลหลัก: ผลดีที่สุดบนชุดทดสอบแยก)baseline — WangchanBERTa → BiLSTM (รุ่นเบา/พื้นฐาน)last4weighted_bilstm — รวม last 4 hidden layers แบบถ่วงน้ำหนัก + (Bi)LSTM (คะแนนเฉลี่ย CV สูงสุด)Demo (Space): https://huggingface.co/spaces/Dusit-P/thai-sentiment-api>
ฐานโมเดล:airesearch/wangchanberta-base-att-spm-uncased
Labels:0 → negative,1 → positive(ตัดสินใจด้วยargmaxหรือpositive ≥ negative)
ใช้ชุด Wisesight Sentiment (ภาษาไทย) และ คัดเลือกเฉพาะ 2 คลาส (positive, negative) — ไม่ใช้ neutral และ question
max_len = 128โปรดตรวจสอบสัญญาอนุญาตของ Wisesight ต้นทางก่อนใช้งานเชิงพาณิชย์/แจกจ่ายซ้ำ
| โมเดล | CV Accuracy | CV F1 | CV ROC-AUC | Test Accuracy | Test F1 | Test ROC-AUC |
|---|---|---|---|---|---|---|
| Model1_Baseline | 90.36 ± 1.07 | 89.99 ± 1.10 | 95.67 ± 0.59 | 90.15 | 89.71 | 95.69 |
| Model2_CNN_BiLSTM | 90.32 ± 0.56 | 89.95 ± 0.56 | 95.92 ± 0.28 | 90.29 | 89.88 | 95.76 |
| Model3_Last4Weighted (Pure/BiLSTM) | 90.80 ± 0.70 | 90.42 ± 0.75 | 96.19 ± 0.27 | 90.11 | 89.68 | 95.78 |
| Model4_Middle4Mean | 90.51 ± 0.67 | 90.11 ± 0.68 | 95.78 ± 0.43 | 90.20 | 89.76 | 95.55 |
สรุป: ผลแต่ละสถาปัตยกรรม แตกต่างกันเล็กน้อย (~<1%)
ใช้cnn_bilstmเป็น โมเดลหลักในการใช้งานจริง และเปิดlast4weighted_bilstmให้เลือกสำหรับเคสเฉพาะ/เทียบผล
review จะใช้ทันที (ไม่พบจะเดาคอลัมน์ object ตัวแรก)shop จะสรุปผลต่อร้าน + แสดงกราฟสรุปโปรดพิจารณาใช้ร่วมกับกฎ/กระบวนการทวนโดยมนุษย์
1common/models.py
2baseline/
3├─ config.json
4└─ model.safetensors
5cnn_bilstm/
6├─ config.json
7└─ model.safetensors
8last4weighted_bilstm/
9├─ config.json
10└─ model.safetensors
11requirements.txt
12LICENSEต้องมี:torch,transformers,safetensors,sentencepiece,huggingface_hub
1pip install -U torch transformers safetensors sentencepiece huggingface_hub
2
3import json, importlib.util, torch, torch.nn.functional as F
4from huggingface_hub import hf_hub_download
5from transformers import AutoTokenizer
6from safetensors.torch import load_file
7
8REPO_ID = "Dusit-P/thai-sentiment-wcb"
9# เลือกหนึ่ง: "cnn_bilstm" | "baseline" | "last4weighted_bilstm"
10MODEL_DIR = "cnn_bilstm"
11
12# โหลดสถาปัตยกรรม (factory)
13models_py = hf_hub_download(REPO_ID, filename="common/models.py")
14spec = importlib.util.spec_from_file_location("models", models_py)
15mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
16
17# โหลดคอนฟิก/น้ำหนัก
18cfg_path = hf_hub_download(REPO_ID, filename=f"{MODEL_DIR}/config.json")
19w_path = hf_hub_download(REPO_ID, filename=f"{MODEL_DIR}/model.safetensors")
20cfg = json.load(open(cfg_path, "r", encoding="utf-8"))
21
22tok = AutoTokenizer.from_pretrained(cfg["base_model"])
23model = mod.create_model_by_name(cfg["arch"])
24state = load_file(w_path); model.load_state_dict(state, strict=True)
25model.eval()
26
27def classify(text: str):
28 enc = tok([text], padding=True, truncation=True, max_length=cfg["max_len"], return_tensors="pt")
29 with torch.no_grad():
30 p = F.softmax(model(enc["input_ids"], enc["attention_mask"]), dim=1)[0].tolist()
31 probs = {"negative": float(p[0]), "positive": float(p[1])}
32 label = "positive" if probs["positive"] >= probs["negative"] else "negative"
33 return probs, label
34
35print(classify("บริการดีมาก ประทับใจ"))
36
37---
38
39Space / REST API
40Base URL: https://<YOUR_SPACE_URL>
41API ด้านล่างอ้างอิงฟังก์ชันใน app.py ของ Space: predict_one, predict_many, predict_csv
42หากคุณเปลี่ยนชื่อฟังก์ชัน/เส้นทาง ให้ปรับ URL ให้สอดคล้อง
431) Predict ข้อความเดียว
44POST /run/predict_one
45Body (JSON):
46{
47 "data": ["อาหารอร่อยมาก บริการดี", "cnn_bilstm"]
48}
49
50-Response (ตัวอย่าง):
51{
52 "data": [
53 {"negative": 0.12, "positive": 0.88},
54 "positive"
55 ]
56}
57
58>-curl ตัวอย่าง:
59curl -X POST "https://<YOUR_SPACE_URL>/run/predict_one" \
60 -H "content-type: application/json" \
61 -d '{"data":["อาหารอร่อยมาก บริการดี","cnn_bilstm"]}'
62
632) Predict หลายข้อความ (ทีละบรรทัด)
64POST /run/predict_many
65>Body (JSON):
66{
67 "data": ["แย่มาก รอนานมาก\nอร่อย บริการไว", "cnn_bilstm"]
68}
69
70>-curl ตัวอย่าง:
71curl -X POST "https://<YOUR_SPACE_URL>/run/predict_many" \
72 -H "content-type: application/json" \
73 -d '{"data":["แย่มาก รอนานมาก\nอร่อย บริการไว","cnn_bilstm"]}'
74
753) อัปโหลด CSV
76POST /run/predict_csv (multipart/form-data)
77Fields
78file: ไฟล์ CSV (ต้องมีคอลัมน์ review; ถ้ามี shop จะแสดงสรุปต่อร้าน)
79model_choice: cnn_bilstm | baseline | last4weighted_bilstm
80>-curl ตัวอย่าง:
81
82curl -X POST "https://<YOUR_SPACE_URL>/run/predict_csv" \
83 -F "file=@/path/to/reviews.csv" \
84 -F "model_choice=cnn_bilstm"
85
86>บางเวอร์ชันของ Gradio มีปุ่ม “View API” บนหน้า Space เพื่อตรวจ schema/endpoint ล่าสุดอัตโนมัติ
87---
88#Reproducibility (ย่อ)
89-Base: airesearch/wangchanberta-base-att-spm-uncased
90-max_len=128, Batch size=16, Optimizer: AdamW (lr_bert=2e-5, lr_others=1e-3), Early stopping
91-5-Fold Stratified, Seed=42
92-ไลบรารีหลัก: torch, transformers, safetensors, sentencepiece
93---
94#License & Attribution
95>Model license: MIT (ปรับได้ตามต้องการ)
96>Dataset: Wisesight Sentiment — โปรดอ้างอิงและปฏิบัติตามสัญญาอนุญาตของชุดข้อมูลต้นทาง
97
98---
99#Citation
100>Dusit P. (2025). Thai Sentiment WCB (WangchanBERTa + LSTM/CNN/Last4 heads).
101>Hugging Face: Dusit-P/thai-sentiment-wcb.
102>Demo: <https://<YOUR_SPACE_URL>>.
103---
104#Changelog
105-v1.0.0 — ปล่อย cnn_bilstm, baseline, last4weighted_bilstm; เพิ่ม Space (UI/REST)