Token Classification
Transformers
ONNX
Safetensors
English
Japanese
Chinese
bert
anime
filename-parsing
Eval Results (legacy)
Run this model with HF Jobs
Run ModerRAS/AniFileBERT on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/ModerRAS/AniFileBERT. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/ModerRAS/AniFileBERT on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use ModerRAS/AniFileBERT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ModerRAS/AniFileBERT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="ModerRAS/AniFileBERT")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("ModerRAS/AniFileBERT") model = AutoModelForTokenClassification.from_pretrained("ModerRAS/AniFileBERT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add character-token DMHY training path
Browse filesFiles changed (5)
- README.md+29
- colab_train.py+11-10
- convert_to_char_dataset.pyAdded+201
- datasets/AnimeName+1-1
- train.py+13-6
59 unmodified lines606162636465666768697073 unmodified lines14414514614714814915015115215315415515615715815916016116216316416516616716816917017117233 unmodified lines20620720820921021121259 unmodified linesand individual bracket characters (`[`, `]`, `(`, `)`) are included in the newvocabulary.
For character-token training, `datasets/AnimeName/vocab.char.json` is builtfrom the full `dmhy_weak_char.jsonl` export. The full DMHY weak dataset has**6195 unique characters**, so the complete character vocab is only **6199**entries including special tokens and reaches 100% token coverage.
## Evaluation
Balanced mixed-data A/B run (`50K` synthetic + `50K` DMHY weak labels, 1 epoch, batch size 128, seed 42):73 unmodified linestrains the full model. About 96% of token occurrences are now covered (vs 90%with the old 3000-token vocabulary).
### Character-token DMHY training
```bashpython convert_to_char_dataset.py \ --input datasets/AnimeName/dmhy_weak.jsonl \ --output datasets/AnimeName/dmhy_weak_char.jsonl \ --vocab-output datasets/AnimeName/vocab.char.json \ --manifest-output datasets/AnimeName/dmhy_weak_char.manifest.json
python train.py --tokenizer char \ --data-file datasets/AnimeName/dmhy_weak_char.jsonl \ --vocab-file datasets/AnimeName/vocab.char.json \ --save-dir checkpoints_char/dmhy-weak-char \ --epochs 1 --batch-size 64 \ --learning-rate 0.0003 --warmup-steps 300 \ --max-seq-length 128 --seed 42```
The converter keeps source metadata and adds `tokenizer_variant`, source tokencount, and character token count fields to each record. The char dataset'sp99 length is 107 characters, so `--max-seq-length 128` covers almost all rowswhile leaving room for `[CLS]` and `[SEP]`.
### Regenerate datasets from source
```bash33 unmodified lines- `model.safetensors`, `config.json`, `vocab.json`: default fine-tuned model- `train.py`, `dataset.py`, `tokenizer.py`, `model.py`: training pipeline- `dmhy_dataset.py`, `mix_datasets.py`: weak-label export and dataset mixing- `convert_to_char_dataset.py`: full character-token projection for weak labels- `inference.py`: end-to-end filename parser CLI- `export_onnx.py`: ONNX export for Android integration- `exports/`: exported ONNX model and metadata12 unmodified lines131415161617181920212122232465 unmodified lines909192939394959697989910010110210010110210310410510610610710710810910911011011111211311411512 unmodified lines - Mounts Google Drive (for persistent checkpoints) - Clones AniFileBERT repo + AnimeName dataset submodule - Installs PyTorch + Transformers dependencies - Runs training: fine-tune from current checkpoint with 8000-token vocab - Runs training: train a character-token model with the full DMHY vocab - Saves final model to Drive
Output: - Checkpoints saved to: MyDrive/AniFileBERT/checkpoints/ - Final model at: MyDrive/AniFileBERT/checkpoints/dmhy-finetune/final/ - Final model at: MyDrive/AniFileBERT/checkpoints/dmhy-weak-char/final/"""
import os65 unmodified linesprint("\n" + "=" * 60)print("STEP 5: Verify vocabulary")print("=" * 60)run("python -c 'import json; v=json.load(open(\"vocab.json\")); print(f\"Vocab size: {len(v)} tokens\")'")run("python -c 'import json; v=json.load(open(\"datasets/AnimeName/vocab.char.json\", encoding=\"utf-8\")); print(f\"Character vocab size: {len(v)} tokens\")'")
# ── 6. Run training ────────────────────────────────────────────print("\n" + "=" * 60)print("STEP 6: Train model")print("=" * 60)
# The 8000-token vocab is already in datasets/AnimeName/vocab.json.# The old checkpoint (3000-token embedding) gets resized automatically.SAVE_DIR = os.path.join(DRIVE_ROOT, "checkpoints", "dmhy-finetune")# The full DMHY character vocab is only 6199 tokens and covers every character# occurrence in dmhy_weak_char.jsonl.SAVE_DIR = os.path.join(DRIVE_ROOT, "checkpoints", "dmhy-weak-char")
run( f"python train.py " f"--tokenizer char " f"--data-file datasets/AnimeName/dmhy_weak.jsonl " f"--vocab-file datasets/AnimeName/vocab.json " f"--data-file datasets/AnimeName/dmhy_weak_char.jsonl " f"--vocab-file datasets/AnimeName/vocab.char.json " f"--save-dir {SAVE_DIR} " f"--init-model-dir . " f"--epochs 10 --batch-size 128 " f"--epochs 5 --batch-size 128 " f"--learning-rate 0.0003 --warmup-steps 300 " f"--max-seq-length 128 " f"--seed 42 " f"--no-shuffle")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201"""Convert token-level anime filename JSONL datasets to character tokens.
Input records must contain parallel ``tokens`` and ``labels`` arrays. Theconverter expands each original token into Unicode code points and projects BIOlabels onto the expanded sequence:
- ``B-X`` keeps ``B-X`` on the first character and uses ``I-X`` afterwards.- ``I-X`` remains ``I-X`` on every character.- ``O`` remains ``O`` on every character.
The script streams both input and output so it can process the full DMHY weakdataset without loading hundreds of MB into memory."""
from __future__ import annotations
import argparseimport jsonfrom collections import Counterfrom datetime import datetime, timezonefrom pathlib import Pathfrom statistics import meanfrom typing import Iterable
SPECIAL_TOKENS = ("[PAD]", "[UNK]", "[CLS]", "[SEP]")
def projected_labels(token: str, label: str) -> tuple[list[str], list[str]]: """Return character tokens and projected BIO labels for one source token.""" chars = list(token) if not chars: return [], []
if label.startswith("B-"): entity = label.split("-", 1)[1] return chars, [label] + [f"I-{entity}"] * (len(chars) - 1) if label.startswith("I-"): return chars, [label] * len(chars) return chars, [label] * len(chars)
def convert_record(record: dict) -> dict: """Convert one JSONL record while preserving non-token metadata.""" tokens = record["tokens"] labels = record["labels"] if len(tokens) != len(labels): raise ValueError( f"token/label length mismatch: {len(tokens)} tokens, {len(labels)} labels" )
char_tokens: list[str] = [] char_labels: list[str] = [] for token, label in zip(tokens, labels): pieces, piece_labels = projected_labels(str(token), str(label)) char_tokens.extend(pieces) char_labels.extend(piece_labels)
converted = dict(record) converted["tokens"] = char_tokens converted["labels"] = char_labels converted["tokenizer_variant"] = "char" converted["source_token_count"] = len(tokens) converted["char_token_count"] = len(char_tokens) return converted
def iter_jsonl(path: Path) -> Iterable[dict]: with path.open("r", encoding="utf-8") as handle: for line_no, line in enumerate(handle, 1): line = line.strip() if not line: continue try: yield json.loads(line) except json.JSONDecodeError as exc: raise ValueError(f"{path}:{line_no}: invalid JSON") from exc
def build_vocab(counter: Counter[str], max_size: int | None = None) -> dict[str, int]: """Build a frequency-sorted vocab with fixed special-token IDs.""" vocab = {token: idx for idx, token in enumerate(SPECIAL_TOKENS)} limit = None if max_size is None else max(max_size - len(vocab), 0) for token, _count in counter.most_common(limit): if token not in vocab: vocab[token] = len(vocab) return vocab
def coverage(counter: Counter[str], vocab: dict[str, int]) -> float: total = sum(counter.values()) if total == 0: return 1.0 covered = sum(count for token, count in counter.items() if token in vocab) return covered / total
def percentile(values: list[int], pct: float) -> int: if not values: return 0 ordered = sorted(values) index = min(len(ordered) - 1, round((pct / 100) * (len(ordered) - 1))) return ordered[index]
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Convert JSONL token labels to character labels") parser.add_argument("--input", required=True, help="Input token-level JSONL") parser.add_argument("--output", required=True, help="Output character-level JSONL") parser.add_argument("--vocab-output", required=True, help="Output vocab JSON") parser.add_argument("--manifest-output", default=None, help="Output manifest JSON") parser.add_argument("--max-vocab-size", type=int, default=None, help="Optional vocab cap including special tokens") parser.add_argument("--limit", type=int, default=None, help="Convert only the first N records") parser.add_argument("--progress", type=int, default=50_000, help="Print progress every N records") return parser.parse_args()
def main() -> None: args = parse_args() input_path = Path(args.input) output_path = Path(args.output) vocab_path = Path(args.vocab_output) manifest_path = ( Path(args.manifest_output) if args.manifest_output else output_path.with_suffix(".manifest.json") )
output_path.parent.mkdir(parents=True, exist_ok=True) vocab_path.parent.mkdir(parents=True, exist_ok=True) manifest_path.parent.mkdir(parents=True, exist_ok=True)
char_counter: Counter[str] = Counter() label_counter: Counter[str] = Counter() row_count = 0 source_token_count = 0 char_token_count = 0 lengths: list[int] = [] examples: list[dict] = []
with output_path.open("w", encoding="utf-8", newline="\n") as out: for record in iter_jsonl(input_path): converted = convert_record(record) out.write(json.dumps(converted, ensure_ascii=False, separators=(",", ":")) + "\n")
row_count += 1 source_token_count += converted["source_token_count"] char_len = converted["char_token_count"] char_token_count += char_len lengths.append(char_len) char_counter.update(converted["tokens"]) label_counter.update(converted["labels"]) if len(examples) < 5: examples.append(converted)
if args.limit is not None and row_count >= args.limit: break if args.progress and row_count % args.progress == 0: print(f"converted {row_count:,} rows; unique chars={len(char_counter):,}")
vocab = build_vocab(char_counter, args.max_vocab_size) vocab_path.write_text(json.dumps(vocab, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
manifest = { "created_at": datetime.now(timezone.utc).isoformat(), "input": str(input_path), "output": str(output_path), "vocab_output": str(vocab_path), "tokenizer_variant": "char", "projection": { "B-X": "first char keeps B-X; remaining chars become I-X", "I-X": "all chars keep I-X", "O": "all chars keep O", }, "row_count": row_count, "source_token_count": source_token_count, "char_token_count": char_token_count, "unique_char_count": len(char_counter), "vocab_size": len(vocab), "max_vocab_size": args.max_vocab_size, "vocab_coverage": coverage(char_counter, vocab), "label_counts": dict(label_counter), "char_length": { "min": min(lengths) if lengths else 0, "mean": mean(lengths) if lengths else 0, "p50": percentile(lengths, 50), "p90": percentile(lengths, 90), "p95": percentile(lengths, 95), "p99": percentile(lengths, 99), "max": max(lengths) if lengths else 0, }, "examples": examples, } manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(json.dumps({k: v for k, v in manifest.items() if k != "examples"}, ensure_ascii=False, indent=2))
if __name__ == "__main__": main()11Subproject commit 17c478b079deae90935a0c5392ee6138ea18b02fSubproject commit 867350a1712e50cc71f5a9e81dd331ca46a7b1dd81 unmodified lines82838485868788899058 unmodified lines14915015115214915015315415515615717 unmodified lines17517617717417517817918018127 unmodified lines2092102112122132142152162172186 unmodified lines22522622722122822923023123223323422823523623723881 unmodified lines help="Use only the first N samples for quick A/B smoke runs") parser.add_argument("--rebuild-vocab", action="store_true", help="Rebuild vocab from the selected data file before training") parser.add_argument("--max-vocab-size", type=int, default=None, help="Optional vocab cap used with --rebuild-vocab") parser.add_argument("--cpu", action="store_true", help="Force CPU training") parser.add_argument("--no-shuffle", action="store_true", help="Do not shuffle before train/eval split") return parser.parse_args()
58 unmodified lines vocab_path = resolve_vocab_path(config.data_file, args.tokenizer, args.vocab_file) tokenizer = create_tokenizer(args.tokenizer) if args.rebuild_vocab or not os.path.isfile(vocab_path): max_vocab_size = args.max_vocab_size if args.max_vocab_size is not None else config.vocab_size print(f" Building {args.tokenizer} vocab: {vocab_path} (max_size={config.vocab_size})") build_vocab_from_data(all_data, tokenizer, vocab_path, max_size=config.vocab_size) print(f" Building {args.tokenizer} vocab: {vocab_path} (max_size={max_vocab_size})") build_vocab_from_data(all_data, tokenizer, vocab_path, max_size=max_vocab_size) tokenizer = create_tokenizer(args.tokenizer, vocab_file=vocab_path) print(f" Variant: {args.tokenizer}") print(f" Vocab size: {tokenizer.vocab_size}")17 unmodified lines total_params = print_model_summary(model)
if total_params >= 5_000_000: print("WARNING: Model exceeds 5M parameter limit. Consider reducing hidden_size or layers.") sys.exit(1) print("WARNING: Model exceeds the historical 5M target; continuing because vocab size is configurable.")
split_idx = int(len(all_data) * config.train_split) train_data = all_data[:split_idx]27 unmodified lines print(f" Train samples: {len(train_dataset)}") print(f" Eval samples: {len(eval_dataset)}")
use_cpu = args.cpu or not torch.cuda.is_available() use_fp16 = not use_cpu print(f" Device: {'CPU' if use_cpu else 'CUDA'}")
# Training arguments training_args = TrainingArguments( output_dir=config.save_dir,6 unmodified lines learning_rate=config.learning_rate, weight_decay=config.weight_decay, warmup_steps=config.warmup_steps, use_cpu=False, use_cpu=use_cpu, report_to="none", save_total_limit=2, load_best_model_at_end=True, metric_for_best_model="f1", greater_is_better=True, dataloader_num_workers=config.num_workers, fp16=True fp16=use_fp16, )
# Data collatorREADME.md+29
colab_train.py+11-10
convert_to_char_dataset.pyAdded+201
datasets/AnimeName+1-1
train.py+13-6