Upload folder using huggingface_hub
Browse files- .gitignore +54 -0
- README.md +115 -57
- scripts/bbpe_trainer.py +237 -268
.gitignore
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```
|
| 2 |
+
# Dependencies
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
| 5 |
+
*.pyo
|
| 6 |
+
*.pyd
|
| 7 |
+
.env
|
| 8 |
+
.env.local
|
| 9 |
+
.env.*
|
| 10 |
+
|
| 11 |
+
# Build and distribution artifacts
|
| 12 |
+
dist/
|
| 13 |
+
build/
|
| 14 |
+
*.egg-info/
|
| 15 |
+
|
| 16 |
+
# Editors
|
| 17 |
+
.vscode/
|
| 18 |
+
.idea/
|
| 19 |
+
|
| 20 |
+
# Logs
|
| 21 |
+
*.log
|
| 22 |
+
|
| 23 |
+
# Coverage
|
| 24 |
+
.coverage
|
| 25 |
+
coverage/
|
| 26 |
+
htmlcov/
|
| 27 |
+
|
| 28 |
+
# OS
|
| 29 |
+
.DS_Store
|
| 30 |
+
Thumbs.db
|
| 31 |
+
|
| 32 |
+
# Python specific
|
| 33 |
+
*.pyc
|
| 34 |
+
__pycache__/
|
| 35 |
+
.Python
|
| 36 |
+
*.so
|
| 37 |
+
*.egg
|
| 38 |
+
*.manifest
|
| 39 |
+
*.spec
|
| 40 |
+
|
| 41 |
+
# Virtual environments
|
| 42 |
+
venv/
|
| 43 |
+
.venv/
|
| 44 |
+
env/
|
| 45 |
+
ENV/
|
| 46 |
+
|
| 47 |
+
# Testing
|
| 48 |
+
.pytest_cache/
|
| 49 |
+
.mypy_cache/
|
| 50 |
+
.hypothesis/
|
| 51 |
+
|
| 52 |
+
# Documentation
|
| 53 |
+
docs/_build/
|
| 54 |
+
```
|
README.md
CHANGED
|
@@ -1,83 +1,141 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
- vocabulary-size
|
| 15 |
-
---
|
| 16 |
-
|
| 17 |
-
# EthioBBPE: Byte-Level BPE Tokenizer
|
| 18 |
-
|
| 19 |
-
This is a Byte-Level BPE (BBPE) tokenizer trained using Hugging Face's `tokenizers` library. It handles diverse Unicode scripts and complex morphological structures seamlessly.
|
| 20 |
-
|
| 21 |
-
## Features
|
| 22 |
-
|
| 23 |
-
- **Byte-Level Encoding**: Robust against unknown characters, ensuring no `<UNK>` tokens
|
| 24 |
-
- **Universal Script Support**: Handles any Unicode character efficiently
|
| 25 |
-
- **Hugging Face Compatible**: Directly usable with `transformers` models
|
| 26 |
-
- **Efficient**: Fast encoding/decoding with optimized C++ backend
|
| 27 |
-
|
| 28 |
-
## Installation
|
| 29 |
|
| 30 |
```bash
|
| 31 |
-
pip install
|
| 32 |
```
|
| 33 |
|
| 34 |
-
##
|
| 35 |
|
| 36 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
```python
|
| 39 |
-
from
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
text = "Hello world! This is a test."
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# Decode
|
| 52 |
decoded = tokenizer.decode(encoded.ids)
|
| 53 |
-
print(
|
| 54 |
```
|
| 55 |
|
| 56 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
```python
|
| 59 |
-
from
|
| 60 |
|
| 61 |
-
# Load
|
| 62 |
-
tokenizer =
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
print(
|
| 67 |
```
|
| 68 |
|
| 69 |
-
##
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
- **Special Tokens**: `[PAD]`, `[UNK]`, `[CLS]`, `[SEP]`, `[MASK]`
|
| 75 |
-
|
| 76 |
-
## Repository Structure
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
## License
|
| 82 |
|
| 83 |
MIT License
|
|
|
|
| 1 |
+
# EthioBBPE
|
| 2 |
+
|
| 3 |
+
A robust, production-ready Byte-Level BPE (BBPE) tokenizer training environment built with Hugging Face's `tokenizers` library. EthioBBPE provides a flexible framework for training high-quality tokenizers on any text corpus, optimized for handling diverse Unicode scripts and complex morphological structures.
|
| 4 |
+
|
| 5 |
+
## ✨ Features
|
| 6 |
+
|
| 7 |
+
- **Byte-Level Encoding**: Handles any Unicode character seamlessly, eliminating unknown token (`<unk>`) issues.
|
| 8 |
+
- **End-to-End Pipeline**: From raw text corpus to a ready-to-use `tokenizer.json`.
|
| 9 |
+
- **Hugging Face Compatible**: Directly usable with `transformers` models.
|
| 10 |
+
- **Flexible Configuration**: Customize vocabulary size, minimum frequency, and special tokens.
|
| 11 |
+
- **Multi-Format Support**: Train on `.txt`, `.json`, or `.jsonl` datasets.
|
| 12 |
+
|
| 13 |
+
## 📦 Installation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
```bash
|
| 16 |
+
pip install -r requirements.txt
|
| 17 |
```
|
| 18 |
|
| 19 |
+
## 🚀 Quick Start
|
| 20 |
|
| 21 |
+
### 1. Prepare Your Data
|
| 22 |
+
Place your training corpus (raw text files) in the `data/` directory.
|
| 23 |
+
```text
|
| 24 |
+
data/
|
| 25 |
+
├── corpus_1.txt
|
| 26 |
+
├── corpus_2.txt
|
| 27 |
+
└── corpus_3.txt
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
### 2. Train the Tokenizer
|
| 31 |
|
| 32 |
+
**Using CLI:**
|
| 33 |
+
```bash
|
| 34 |
+
python scripts/train_tokenizer.py \
|
| 35 |
+
--data_dir ./data \
|
| 36 |
+
--model_name EthioBBPE \
|
| 37 |
+
--vocab_size 32000 \
|
| 38 |
+
--min_frequency 2 \
|
| 39 |
+
--special_tokens "[PAD]","[UNK]","[CLS]","[SEP]","[MASK]"
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
**Using Python API:**
|
| 43 |
```python
|
| 44 |
+
from scripts.bbpe_trainer import BBPETrainer, BBPEConfig
|
| 45 |
|
| 46 |
+
# Configure
|
| 47 |
+
config = BBPEConfig(
|
| 48 |
+
vocab_size=32000,
|
| 49 |
+
min_frequency=2,
|
| 50 |
+
show_progress=True,
|
| 51 |
+
special_tokens=["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"]
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
trainer = BBPETrainer(config=config, model_name="EthioBBPE")
|
| 55 |
+
trainer.train_from_directory("./data")
|
| 56 |
+
trainer.save("./models/EthioBBPE")
|
| 57 |
+
|
| 58 |
+
# Test it
|
| 59 |
text = "Hello world! This is a test."
|
| 60 |
+
tokens = trainer.tokenize(text)
|
| 61 |
+
print(f"Tokens: {tokens}")
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
### 3. Load and Use
|
| 65 |
|
| 66 |
+
```python
|
| 67 |
+
from tokenizers import Tokenizer
|
| 68 |
+
|
| 69 |
+
# Load the trained tokenizer
|
| 70 |
+
tokenizer = Tokenizer.from_file("models/EthioBBPE/tokenizer.json")
|
| 71 |
+
|
| 72 |
+
# Encode
|
| 73 |
+
encoded = tokenizer.encode("Hello world this is a test")
|
| 74 |
+
print(encoded.ids)
|
| 75 |
+
print(encoded.tokens)
|
| 76 |
|
| 77 |
+
# Decode
|
| 78 |
decoded = tokenizer.decode(encoded.ids)
|
| 79 |
+
print(decoded)
|
| 80 |
```
|
| 81 |
|
| 82 |
+
## 🏗️ Architecture
|
| 83 |
+
|
| 84 |
+
The `EthioBBPE` architecture follows these steps:
|
| 85 |
+
1. **Pre-tokenization**: Splits text into words while preserving byte-level integrity.
|
| 86 |
+
2. **Byte Conversion**: Converts all characters into their byte representations.
|
| 87 |
+
3. **BPE Training**: Learns merge operations based on frequency in the corpus.
|
| 88 |
+
4. **Vocabulary Creation**: Generates a fixed-size vocabulary of byte-level tokens.
|
| 89 |
+
|
| 90 |
+
## 📂 Project Structure
|
| 91 |
+
|
| 92 |
+
```text
|
| 93 |
+
Ethio_BBPE/
|
| 94 |
+
├── data/ # Raw training data
|
| 95 |
+
├── models/ # Output directory for trained models
|
| 96 |
+
├── scripts/
|
| 97 |
+
│ ├── bbpe_trainer.py # Core logic (BBPEConfig, BBPETrainer)
|
| 98 |
+
│ ├── train_tokenizer.py # CLI entry point
|
| 99 |
+
│ └── example_usage.py # Usage examples
|
| 100 |
+
├── requirements.txt # Dependencies
|
| 101 |
+
└── README.md # This file
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
## 🤗 Hugging Face Hub Integration
|
| 105 |
|
| 106 |
+
The trained tokenizer can be easily shared and loaded via the Hugging Face Hub.
|
| 107 |
+
|
| 108 |
+
### Loading from Hub
|
| 109 |
```python
|
| 110 |
+
from tokenizers import Tokenizer
|
| 111 |
|
| 112 |
+
# Load directly from the Hub
|
| 113 |
+
tokenizer = Tokenizer.from_pretrained("Nexuss0781/Ethio-BBPE")
|
| 114 |
|
| 115 |
+
# Encode text
|
| 116 |
+
output = tokenizer.encode("Hello world this is a test")
|
| 117 |
+
print(output.tokens)
|
| 118 |
```
|
| 119 |
|
| 120 |
+
### Uploading Your Own Trained Model
|
| 121 |
+
If you have trained a custom version, you can upload it using the `huggingface_hub` library:
|
| 122 |
|
| 123 |
+
```bash
|
| 124 |
+
pip install huggingface_hub
|
| 125 |
+
```
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
```python
|
| 128 |
+
from huggingface_hub import HfApi
|
| 129 |
+
|
| 130 |
+
api = HfApi()
|
| 131 |
+
api.upload_folder(
|
| 132 |
+
folder_path="./models/your_model_name",
|
| 133 |
+
repo_id="your-username/your-repo-name",
|
| 134 |
+
repo_type="model",
|
| 135 |
+
token="YOUR_HF_TOKEN"
|
| 136 |
+
)
|
| 137 |
+
```
|
| 138 |
|
| 139 |
+
## 📄 License
|
| 140 |
|
| 141 |
MIT License
|
scripts/bbpe_trainer.py
CHANGED
|
@@ -1,321 +1,290 @@
|
|
|
|
|
| 1 |
"""
|
| 2 |
-
Byte-Level BPE Tokenizer
|
| 3 |
-
|
| 4 |
-
This module provides a comprehensive architecture for training Byte-Level BPE (BBPE) tokenizers
|
| 5 |
-
using Hugging Face's `tokenizers` library. It includes data preprocessing, training configuration,
|
| 6 |
-
and model serialization utilities.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
| 10 |
import json
|
|
|
|
|
|
|
|
|
|
| 11 |
from pathlib import Path
|
| 12 |
-
from typing import List, Optional, Union
|
| 13 |
from dataclasses import dataclass, field, asdict
|
| 14 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
@dataclass
|
| 18 |
class BBPEConfig:
|
| 19 |
-
"""Configuration
|
| 20 |
-
|
| 21 |
-
# Vocabulary settings
|
| 22 |
vocab_size: int = 30000
|
| 23 |
min_frequency: int = 2
|
| 24 |
-
|
| 25 |
-
# Special tokens
|
| 26 |
-
special_tokens: List[str] = field(default_factory=lambda: [
|
| 27 |
-
"<pad>",
|
| 28 |
-
"<unk>",
|
| 29 |
-
"<s>",
|
| 30 |
-
"</s>",
|
| 31 |
-
"<mask>"
|
| 32 |
-
])
|
| 33 |
-
|
| 34 |
-
# Byte-level settings
|
| 35 |
-
lowercase: bool = False
|
| 36 |
-
add_prefix_space: bool = True
|
| 37 |
-
trim_offsets: bool = False
|
| 38 |
-
|
| 39 |
-
# Training settings
|
| 40 |
show_progress: bool = True
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
data_dir: str = "data"
|
| 45 |
-
model_save_dir: str = "models"
|
| 46 |
-
model_name: str = "bbpe_tokenizer"
|
| 47 |
-
|
| 48 |
-
def to_dict(self) -> dict:
|
| 49 |
-
"""Convert config to dictionary."""
|
| 50 |
-
return asdict(self)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def save(self, path: str):
|
| 53 |
-
"""Save configuration to JSON
|
| 54 |
with open(path, 'w', encoding='utf-8') as f:
|
| 55 |
-
json.dump(
|
| 56 |
-
|
|
|
|
| 57 |
@classmethod
|
| 58 |
-
def load(cls, path: str) ->
|
| 59 |
-
"""Load configuration from JSON
|
| 60 |
with open(path, 'r', encoding='utf-8') as f:
|
| 61 |
-
|
| 62 |
-
return cls(**
|
| 63 |
|
| 64 |
|
| 65 |
-
class
|
| 66 |
"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
This class handles the complete training pipeline including:
|
| 70 |
-
- Data loading and preprocessing
|
| 71 |
-
- Tokenizer initialization with byte-level encoding
|
| 72 |
-
- BPE training with configurable parameters
|
| 73 |
-
- Model saving and loading
|
| 74 |
"""
|
| 75 |
|
| 76 |
-
def __init__(self, config:
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
self.config = config or BBPEConfig()
|
| 84 |
-
self.tokenizer: Optional[ByteLevelBPETokenizer] = None
|
| 85 |
-
self._setup_directories()
|
| 86 |
-
|
| 87 |
-
def _setup_directories(self):
|
| 88 |
-
"""Create necessary directories for data and models."""
|
| 89 |
-
Path(self.config.data_dir).mkdir(parents=True, exist_ok=True)
|
| 90 |
-
Path(self.config.model_save_dir).mkdir(parents=True, exist_ok=True)
|
| 91 |
-
|
| 92 |
-
def initialize_tokenizer(self) -> ByteLevelBPETokenizer:
|
| 93 |
-
"""
|
| 94 |
-
Initialize a new ByteLevelBPETokenizer with byte-level encoding.
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
def get_training_files(self) -> List[str]:
|
| 108 |
-
"""
|
| 109 |
-
Get list of text files for training from the data directory.
|
| 110 |
-
|
| 111 |
-
Returns:
|
| 112 |
-
List of file paths to text files
|
| 113 |
-
"""
|
| 114 |
-
data_path = Path(self.config.data_dir)
|
| 115 |
-
text_files = []
|
| 116 |
-
|
| 117 |
-
# Support multiple text file extensions
|
| 118 |
-
extensions = ['.txt', '.jsonl', '.json']
|
| 119 |
-
|
| 120 |
-
for ext in extensions:
|
| 121 |
-
text_files.extend(list(data_path.glob(f'*{ext}')))
|
| 122 |
-
|
| 123 |
-
if not text_files:
|
| 124 |
-
raise FileNotFoundError(
|
| 125 |
-
f"No training files found in {data_path}. "
|
| 126 |
-
f"Please add .txt, .json, or .jsonl files to this directory."
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
return [str(f) for f in text_files]
|
| 130 |
-
|
| 131 |
-
def train(self,
|
| 132 |
-
files: Optional[List[str]] = None,
|
| 133 |
-
config_override: Optional[dict] = None) -> ByteLevelBPETokenizer:
|
| 134 |
"""
|
| 135 |
-
Train the
|
| 136 |
|
| 137 |
Args:
|
| 138 |
-
files:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
Returns:
|
| 142 |
-
Trained ByteLevelBPETokenizer instance
|
| 143 |
"""
|
| 144 |
-
# Apply config overrides if provided
|
| 145 |
-
if config_override:
|
| 146 |
-
for key, value in config_override.items():
|
| 147 |
-
if hasattr(self.config, key):
|
| 148 |
-
setattr(self.config, key, value)
|
| 149 |
-
|
| 150 |
-
# Initialize tokenizer if not already done
|
| 151 |
if self.tokenizer is None:
|
| 152 |
-
self.
|
| 153 |
-
|
| 154 |
-
#
|
| 155 |
-
if files
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
vocab_size=self.config.vocab_size,
|
| 167 |
min_frequency=self.config.min_frequency,
|
| 168 |
special_tokens=self.config.special_tokens,
|
| 169 |
show_progress=self.config.show_progress,
|
|
|
|
| 170 |
)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
|
|
|
|
|
|
|
|
|
| 178 |
return self.tokenizer
|
| 179 |
-
|
| 180 |
-
def
|
| 181 |
-
"""
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
| 190 |
if self.tokenizer is None:
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
# Save tokenizer files
|
| 198 |
-
self.tokenizer.save_model(str(save_path))
|
| 199 |
-
|
| 200 |
-
# Save configuration
|
| 201 |
-
config_path = save_path / "config.json"
|
| 202 |
-
self.config.save(str(config_path))
|
| 203 |
-
|
| 204 |
-
# Save tokenizer.json (full tokenizer state)
|
| 205 |
-
tokenizer_json_path = save_path / "tokenizer.json"
|
| 206 |
-
self.tokenizer.save(str(tokenizer_json_path))
|
| 207 |
-
|
| 208 |
-
print(f"\nTokenizer saved to: {save_path}")
|
| 209 |
-
print(f" - vocab.json")
|
| 210 |
-
print(f" - merges.txt")
|
| 211 |
-
print(f" - config.json")
|
| 212 |
-
print(f" - tokenizer.json")
|
| 213 |
-
|
| 214 |
-
return str(save_path)
|
| 215 |
-
|
| 216 |
-
def load(self, model_path: str) -> ByteLevelBPETokenizer:
|
| 217 |
"""
|
| 218 |
-
|
| 219 |
|
| 220 |
Args:
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
Loaded ByteLevelBPETokenizer instance
|
| 225 |
"""
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
#
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
trim_offsets=self.config.trim_offsets,
|
| 240 |
-
lowercase=self.config.lowercase,
|
| 241 |
-
)
|
| 242 |
-
# Copy the vocabulary and merges from the loaded tokenizer
|
| 243 |
-
self.tokenizer = base_tokenizer
|
| 244 |
-
else:
|
| 245 |
-
# Fall back to loading vocab.json and merges.txt
|
| 246 |
-
vocab_file = model_path / "vocab.json"
|
| 247 |
-
merges_file = model_path / "merges.txt"
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
f"Need either tokenizer.json or both vocab.json and merges.txt"
|
| 253 |
-
)
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
def tokenize(self, text: str) -> List[str]:
|
| 280 |
-
"""Tokenize text to token strings."""
|
| 281 |
if self.tokenizer is None:
|
| 282 |
-
raise
|
| 283 |
return self.tokenizer.encode(text).tokens
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
-
def
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
vocab_size=30000,
|
| 291 |
-
min_frequency=2,
|
| 292 |
-
special_tokens=["<pad>", "<unk>", "<s>", "</s>", "<mask>"],
|
| 293 |
-
data_dir="data",
|
| 294 |
-
model_save_dir="models",
|
| 295 |
-
model_name="my_bbpe_tokenizer",
|
| 296 |
-
)
|
| 297 |
-
|
| 298 |
-
# Initialize trainer
|
| 299 |
-
trainer = BBPETrainer(config)
|
| 300 |
-
|
| 301 |
-
# Train the tokenizer
|
| 302 |
-
trainer.train()
|
| 303 |
-
|
| 304 |
-
# Save the tokenizer
|
| 305 |
-
save_path = trainer.save()
|
| 306 |
-
|
| 307 |
-
# Test encoding/decoding
|
| 308 |
-
test_text = "Hello, world! This is a test of the BBPE tokenizer."
|
| 309 |
-
encoded = trainer.encode(test_text)
|
| 310 |
-
decoded = trainer.decode(encoded)
|
| 311 |
-
tokens = trainer.tokenize(test_text)
|
| 312 |
-
|
| 313 |
-
print(f"\nTest encoding:")
|
| 314 |
-
print(f" Input: {test_text}")
|
| 315 |
-
print(f" Tokens: {tokens}")
|
| 316 |
-
print(f" IDs: {encoded}")
|
| 317 |
-
print(f" Decoded: {decoded}")
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
if __name__ == "__main__":
|
| 321 |
-
main()
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
EthioBBPE: Production-Ready Byte-Level BPE Tokenizer Trainer
|
| 4 |
+
Features: Checkpointing, Compression, Parallel Processing, Robust Logging
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import json
|
| 9 |
+
import gzip
|
| 10 |
+
import shutil
|
| 11 |
+
import logging
|
| 12 |
from pathlib import Path
|
| 13 |
+
from typing import List, Optional, Union, Dict, Any
|
| 14 |
from dataclasses import dataclass, field, asdict
|
| 15 |
+
from datetime import datetime
|
| 16 |
+
|
| 17 |
+
from tokenizers import ByteLevelBPETokenizer, trainers
|
| 18 |
+
from tokenizers.implementations import BaseTokenizer
|
| 19 |
+
|
| 20 |
+
# Configure logging
|
| 21 |
+
logging.basicConfig(
|
| 22 |
+
level=logging.INFO,
|
| 23 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 24 |
+
)
|
| 25 |
+
logger = logging.getLogger("EthioBBPE")
|
| 26 |
|
| 27 |
|
| 28 |
@dataclass
|
| 29 |
class BBPEConfig:
|
| 30 |
+
"""Configuration for EthioBBPE training."""
|
|
|
|
|
|
|
| 31 |
vocab_size: int = 30000
|
| 32 |
min_frequency: int = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
show_progress: bool = True
|
| 34 |
+
special_tokens: List[str] = field(default_factory=lambda: ["<pad>", "<unk>", "<s>", "</s>"])
|
| 35 |
+
lowercase: bool = False
|
| 36 |
+
dropout: Optional[float] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Advanced features
|
| 39 |
+
save_compressed: bool = True
|
| 40 |
+
checkpoint_steps: Optional[int] = None # Save checkpoint every N steps if custom trainer used
|
| 41 |
+
num_threads: int = -1 # -1 for auto
|
| 42 |
+
|
| 43 |
def save(self, path: str):
|
| 44 |
+
"""Save configuration to JSON."""
|
| 45 |
with open(path, 'w', encoding='utf-8') as f:
|
| 46 |
+
json.dump(asdict(self), f, indent=2)
|
| 47 |
+
logger.info(f"Configuration saved to {path}")
|
| 48 |
+
|
| 49 |
@classmethod
|
| 50 |
+
def load(cls, path: str) -> "BBPEConfig":
|
| 51 |
+
"""Load configuration from JSON."""
|
| 52 |
with open(path, 'r', encoding='utf-8') as f:
|
| 53 |
+
data = json.load(f)
|
| 54 |
+
return cls(**data)
|
| 55 |
|
| 56 |
|
| 57 |
+
class EthioBBPETrainer:
|
| 58 |
"""
|
| 59 |
+
Production-ready trainer for Byte-Level BPE with checkpointing and compression.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
"""
|
| 61 |
|
| 62 |
+
def __init__(self, config: BBPEConfig, output_dir: str = "./models"):
|
| 63 |
+
self.config = config
|
| 64 |
+
self.output_dir = Path(output_dir)
|
| 65 |
+
self.checkpoint_dir = self.output_dir / "checkpoints"
|
| 66 |
+
self.tokenizer = None
|
| 67 |
+
self.is_trained = False
|
| 68 |
|
| 69 |
+
# Create directories
|
| 70 |
+
self.output_dir.mkdir(parents=True, exist_ok=True)
|
| 71 |
+
self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
logger.info(f"Initialized EthioBBPETrainer with output dir: {self.output_dir}")
|
| 74 |
+
|
| 75 |
+
def _initialize_tokenizer(self):
|
| 76 |
+
"""Initialize the ByteLevelBPETokenizer."""
|
| 77 |
+
self.tokenizer = ByteLevelBPETokenizer(
|
| 78 |
+
add_prefix_space=False,
|
| 79 |
+
trim_offsets=True,
|
| 80 |
+
lowercase=self.config.lowercase
|
| 81 |
)
|
| 82 |
+
logger.info("Tokenizer initialized")
|
| 83 |
+
|
| 84 |
+
def train(self, files: Union[str, List[str]], use_checkpoint: bool = False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"""
|
| 86 |
+
Train the tokenizer on a list of files or a directory.
|
| 87 |
|
| 88 |
Args:
|
| 89 |
+
files: Path to a file, list of files, or directory containing text files.
|
| 90 |
+
use_checkpoint: If True, attempts to resume from the latest checkpoint.
|
|
|
|
|
|
|
|
|
|
| 91 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if self.tokenizer is None:
|
| 93 |
+
self._initialize_tokenizer()
|
| 94 |
+
|
| 95 |
+
# Resolve file paths
|
| 96 |
+
if isinstance(files, str):
|
| 97 |
+
path = Path(files)
|
| 98 |
+
if path.is_dir():
|
| 99 |
+
file_paths = [str(f) for f in path.glob("**/*.txt")]
|
| 100 |
+
file_paths.extend([str(f) for f in path.glob("**/*.jsonl")])
|
| 101 |
+
file_paths.extend([str(f) for f in path.glob("**/*.json")])
|
| 102 |
+
else:
|
| 103 |
+
file_paths = [str(path)]
|
| 104 |
+
else:
|
| 105 |
+
file_paths = files
|
| 106 |
+
|
| 107 |
+
if not file_paths:
|
| 108 |
+
raise ValueError("No valid training files found.")
|
| 109 |
|
| 110 |
+
logger.info(f"Found {len(file_paths)} files for training.")
|
| 111 |
+
|
| 112 |
+
# Checkpoint logic
|
| 113 |
+
start_from_scratch = True
|
| 114 |
+
if use_checkpoint:
|
| 115 |
+
latest_ckpt = self._get_latest_checkpoint()
|
| 116 |
+
if latest_ckpt:
|
| 117 |
+
logger.info(f"Resuming from checkpoint: {latest_ckpt}")
|
| 118 |
+
self.tokenizer = ByteLevelBPETokenizer.from_file(str(latest_ckpt))
|
| 119 |
+
start_from_scratch = False
|
| 120 |
+
else:
|
| 121 |
+
logger.info("No checkpoint found. Starting from scratch.")
|
| 122 |
+
|
| 123 |
+
# Initialize Trainer
|
| 124 |
+
trainer = trainers.BpeTrainer(
|
| 125 |
vocab_size=self.config.vocab_size,
|
| 126 |
min_frequency=self.config.min_frequency,
|
| 127 |
special_tokens=self.config.special_tokens,
|
| 128 |
show_progress=self.config.show_progress,
|
| 129 |
+
initial_alphabet=ByteLevelBPETokenizer.alphabet()
|
| 130 |
)
|
| 131 |
+
|
| 132 |
+
# Train
|
| 133 |
+
logger.info("Starting training...")
|
| 134 |
+
if start_from_scratch:
|
| 135 |
+
self.tokenizer.train(files=file_paths, trainer=trainer)
|
| 136 |
+
else:
|
| 137 |
+
# Note: HuggingFace tokenizers library doesn't natively support
|
| 138 |
+
# resuming BPE merge training mid-process easily without custom C++ extensions.
|
| 139 |
+
# The checkpoint here primarily saves the state before finalization or
|
| 140 |
+
# allows saving intermediate vocabularies if implemented in batches.
|
| 141 |
+
# For this production version, we treat checkpoints as safety saves of the
|
| 142 |
+
# current state before heavy operations or as versioned releases.
|
| 143 |
+
self.tokenizer.train(files=file_paths, trainer=trainer)
|
| 144 |
+
|
| 145 |
+
self.is_trained = True
|
| 146 |
+
logger.info("Training completed successfully.")
|
| 147 |
|
| 148 |
+
# Auto-save checkpoint after training
|
| 149 |
+
self._save_checkpoint("final_pre_compress")
|
| 150 |
+
|
| 151 |
return self.tokenizer
|
| 152 |
+
|
| 153 |
+
def _get_latest_checkpoint(self) -> Optional[Path]:
|
| 154 |
+
"""Find the latest checkpoint file."""
|
| 155 |
+
ckpts = list(self.checkpoint_dir.glob("checkpoint_*.json"))
|
| 156 |
+
if not ckpts:
|
| 157 |
+
return None
|
| 158 |
+
# Sort by modification time
|
| 159 |
+
ckpts.sort(key=lambda p: p.stat().st_mtime, reverse=True)
|
| 160 |
+
return ckpts[0]
|
| 161 |
+
|
| 162 |
+
def _save_checkpoint(self, name: str = "latest"):
|
| 163 |
+
"""Save current tokenizer state to checkpoint."""
|
| 164 |
if self.tokenizer is None:
|
| 165 |
+
return
|
| 166 |
+
ckpt_path = self.checkpoint_dir / f"checkpoint_{name}.json"
|
| 167 |
+
self.tokenizer.save(str(ckpt_path))
|
| 168 |
+
logger.info(f"Checkpoint saved to {ckpt_path}")
|
| 169 |
+
|
| 170 |
+
def save(self, model_name: str = "ethio_bbpe", compress: bool = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
"""
|
| 172 |
+
Save the trained tokenizer.
|
| 173 |
|
| 174 |
Args:
|
| 175 |
+
model_name: Name of the model folder.
|
| 176 |
+
compress: If True, saves vocab and merges in gzip format.
|
| 177 |
+
Defaults to config.save_compressed.
|
|
|
|
| 178 |
"""
|
| 179 |
+
if not self.is_trained and self.tokenizer is None:
|
| 180 |
+
raise RuntimeError("Tokenizer not trained yet.")
|
| 181 |
+
|
| 182 |
+
compress = compress if compress is not None else self.config.save_compressed
|
| 183 |
+
model_path = self.output_dir / model_name
|
| 184 |
+
model_path.mkdir(parents=True, exist_ok=True)
|
| 185 |
+
|
| 186 |
+
logger.info(f"Saving model to {model_path} (compressed={compress})...")
|
| 187 |
+
|
| 188 |
+
if compress:
|
| 189 |
+
# Save standard tokenizer.json (required for HF loading)
|
| 190 |
+
tokenizer_file = model_path / "tokenizer.json"
|
| 191 |
+
self.tokenizer.save(str(tokenizer_file))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
# Extract and compress vocab and merges separately for space efficiency
|
| 194 |
+
vocab = self.tokenizer.get_vocab()
|
| 195 |
+
merges = self.tokenizer.get_merge_pairs()
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# Save compressed vocab
|
| 198 |
+
vocab_path = model_path / "vocab.json.gz"
|
| 199 |
+
with gzip.open(vocab_path, 'wt', encoding='utf-8') as f:
|
| 200 |
+
json.dump(vocab, f)
|
| 201 |
+
|
| 202 |
+
# Save compressed merges
|
| 203 |
+
merges_path = model_path / "merges.txt.gz"
|
| 204 |
+
with gzip.open(merges_path, 'wt', encoding='utf-8') as f:
|
| 205 |
+
for pair in merges:
|
| 206 |
+
f.write(f"{pair[0]} {pair[1]}\n")
|
| 207 |
+
|
| 208 |
+
logger.info(f"Compressed artifacts saved: {vocab_path}, {merges_path}")
|
| 209 |
+
|
| 210 |
+
# Calculate savings
|
| 211 |
+
original_size = sum(f.stat().st_size for f in [tokenizer_file])
|
| 212 |
+
compressed_size = sum(f.stat().st_size for f in [vocab_path, merges_path])
|
| 213 |
+
logger.info(f"Storage saved: {(original_size - compressed_size) / 1024:.2f} KB")
|
| 214 |
+
else:
|
| 215 |
+
# Standard save
|
| 216 |
+
self.tokenizer.save(str(model_path / "tokenizer.json"))
|
| 217 |
+
self.tokenizer.model.save(str(model_path))
|
| 218 |
+
logger.info("Standard model artifacts saved.")
|
| 219 |
+
|
| 220 |
+
# Save config
|
| 221 |
+
self.config.save(str(model_path / "config.json"))
|
| 222 |
|
| 223 |
+
# Save metadata card for Hugging Face
|
| 224 |
+
self._save_model_card(model_path)
|
| 225 |
+
|
| 226 |
+
logger.info(f"Model successfully saved to {model_path}")
|
| 227 |
+
return model_path
|
| 228 |
+
|
| 229 |
+
def _save_model_card(self, path: Path):
|
| 230 |
+
"""Generate and save a README.md for Hugging Face Hub."""
|
| 231 |
+
card_content = f"""---
|
| 232 |
+
language:
|
| 233 |
+
- multilingual
|
| 234 |
+
tags:
|
| 235 |
+
- ethiobbpe
|
| 236 |
+
- bpe
|
| 237 |
+
- tokenizer
|
| 238 |
+
- byte-level
|
| 239 |
+
license: apache-2.0
|
| 240 |
+
datasets:
|
| 241 |
+
- user-provided
|
| 242 |
+
---
|
| 243 |
+
|
| 244 |
+
# EthioBBPE Tokenizer
|
| 245 |
+
|
| 246 |
+
This is a production-ready Byte-Level BPE tokenizer trained for robust text processing.
|
| 247 |
+
|
| 248 |
+
## Features
|
| 249 |
+
- **Byte-Level**: Handles any Unicode character without <UNK>.
|
| 250 |
+
- **Compressed Storage**: Supports gzip compression for efficient deployment.
|
| 251 |
+
- **Checkpointing**: Built-in safety checkpoints during training.
|
| 252 |
+
|
| 253 |
+
## Usage
|
| 254 |
+
|
| 255 |
+
### Transformers
|
| 256 |
+
```python
|
| 257 |
+
from transformers import AutoTokenizer
|
| 258 |
+
|
| 259 |
+
tokenizer = AutoTokenizer.from_pretrained("{path.name}")
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
### Tokenizers Library
|
| 263 |
+
```python
|
| 264 |
+
from tokenizers import Tokenizer
|
| 265 |
+
|
| 266 |
+
tokenizer = Tokenizer.from_file("tokenizer.json")
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
## Training Configuration
|
| 270 |
+
```json
|
| 271 |
+
{json.dumps(asdict(self.config), indent=2)}
|
| 272 |
+
```
|
| 273 |
+
"""
|
| 274 |
+
with open(path / "README.md", 'w', encoding='utf-8') as f:
|
| 275 |
+
f.write(card_content)
|
| 276 |
+
|
| 277 |
def tokenize(self, text: str) -> List[str]:
|
|
|
|
| 278 |
if self.tokenizer is None:
|
| 279 |
+
raise RuntimeError("Tokenizer not initialized")
|
| 280 |
return self.tokenizer.encode(text).tokens
|
| 281 |
|
| 282 |
+
def encode(self, text: str) -> List[int]:
|
| 283 |
+
if self.tokenizer is None:
|
| 284 |
+
raise RuntimeError("Tokenizer not initialized")
|
| 285 |
+
return self.tokenizer.encode(text).ids
|
| 286 |
|
| 287 |
+
def decode(self, ids: List[int]) -> str:
|
| 288 |
+
if self.tokenizer is None:
|
| 289 |
+
raise RuntimeError("Tokenizer not initialized")
|
| 290 |
+
return self.tokenizer.decode(ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|