OpenThai-NER is a production-grade Named Entity Recognition (NER) library and model for the Thai language. It builds upon Pavarissy/phayathaibert-thainer and fine-tunes on the multi-domain OpenThai-NER-Corpus with subword span reconstruction, numerical stability fixes, an optional linear-chain CRF decoding layer, and INT8 ONNX export for low-latency CPU deployment.
1OpenThai/
2├── openthai_ner/ # Core Python package
3│ ├── __init__.py # Package entrypoint
4│ ├── pipeline.py # Inference pipeline & span reconstruction
5│ ├── crf.py # Linear-chain CRF with Viterbi decoding
6│ ├── losses.py # Focal Loss & class weighting
7│ ├── model_crf.py # Combined Transformer + CRF model class
8│ └── utils.py # Offset alignment & HTML rendering
9├── scripts/
10│ ├── clean_dataset.py # Dataset cleaner & stratified split
11│ ├── export_onnx.py # ONNX export & INT8 quantization
12│ ├── evaluate_benchmark.py # Evaluation script (seqeval)
13│ ├── benchmark_sota.py # Multi-model comparative benchmark
14│ └── build_package.py # Packaging script for PyPI release
15├── notebooks/
16│ └── OpenThai_NER_FineTuning_Final.ipynb # Google Colab GPU training notebook
17├── space_deploy/ # Standalone Hugging Face Space application
18│ ├── app.py
19│ ├── README.md
20│ └── requirements.txt
21├── data/ # Processed data splits
22│ ├── train.jsonl # 6,792 training sequences
23│ ├── val.jsonl # 717 validation sequences
24│ ├── test.jsonl # 1,092 test sequences
25│ └── label_map.json # Canonical 128-tag label mapping
26├── train_ner.py # Self-contained training script
27├── app.py # Local Gradio web demo
28├── pyproject.toml # Build configuration
29└── README.md
1git clone https://github.com/JonusNattapong/OpenThai.git
2cd OpenThai
3pip install -r requirements.txt
1pip install .
2# Or directly via Git:
3pip install git+https://github.com/JonusNattapong/OpenThai.git
1from openthai_ner import OpenThaiNER
2
3ner = OpenThaiNER("JonusNattapong/OpenThai-NER")
4
5text = "นายสมชาย เข็มกลัด เดินทางไปประชุมที่กระทรวงการคลัง ถนนพระราม 6 ในวันที่ 15 มกราคม"
6entities = ner.predict(text, threshold=0.5)
7
8for ent in entities:
9 print(f"[{ent['entity']}] '{ent['word']}' (Span: {ent['start']}:{ent['end']}, Score: {ent['score']:.4f})")
1[PERSON] 'นายสมชาย เข็มกลัด' (Span: 0:17, Score: 0.9812)
2[ORGANIZATION] 'กระทรวงการคลัง' (Span: 36:50, Score: 0.9924)
3[LOCATION] 'ถนนพระราม 6' (Span: 51:62, Score: 0.9540)
4[DATE] 'วันที่ 15 มกราคม' (Span: 66:82, Score: 0.9715)
1html_output = ner.render_html(text)
2# In Jupyter Notebook:
3# from IPython.display import HTML; display(HTML(html_output))
1from openthai_ner import OpenThaiNER
2
3ner_onnx = OpenThaiNER(
4 "JonusNattapong/OpenThai-NER",
5 onnx_path="models/onnx/openthai_ner_quantized.onnx"
6)
7results = ner_onnx.predict("ธนาคารแห่งประเทศไทย ประกาศปรับลดอัตราดอกเบี้ย")
The training script is self-contained and handles dataset downloading, subword alignment, and metric logging automatically.
1python train_ner.py \
2 --model_name Pavarissy/phayathaibert-thainer \
3 --data_dir data \
4 --output_dir models/openthai-ner-final \
5 --epochs 3 \
6 --batch_size 16 \
7 --learning_rate 2e-5
1# Train with Linear-Chain CRF Layer
2python train_ner.py --use_crf --output_dir models/openthai-ner-crf
3
4# Train with Focal Loss for class imbalance
5python train_ner.py --loss_type focal --focal_gamma 2.0
1python scripts/export_onnx.py \
2 --model JonusNattapong/OpenThai-NER \
3 --output_dir models/onnx
To deploy directly to Hugging Face Spaces, push the contents of
space_deploy/ to your Space repository.
1@software{openthai_ner2026,
2 author = {Nattapong Tapachoom},
3 title = {OpenThai-NER: Production-Ready Thai Named Entity Recognition},
4 url = {https://github.com/JonusNattapong/OpenThai},
5 version = {0.1.0},
6 year = {2026}
7}