Download extract.py from chuckchen/tokenizer-vocab: direct link, hf CLI and curl.
- Browser
- Download file 478 Bytes
-
https://huggingface.co/datasets/chuckchen/tokenizer-vocab/resolve/main/extract.py
- Command line
-
hf download hf://datasets/chuckchen/tokenizer-vocab/extract.py
-
curl -L -o extract.py https://huggingface.co/datasets/chuckchen/tokenizer-vocab/resolve/main/extract.py
478 Bytes
| #!/usr/bin/env python3 | |
| import sys | |
| from transformers import AutoTokenizer | |
| import json | |
| model = sys.argv[1] if len(sys.argv) >= 2 else "nghuyong/ernie-3.0-base-zh" | |
| tokenizer = AutoTokenizer.from_pretrained(model) | |
| vocabulary = tokenizer.get_vocab() | |
| vocab_json = f"./encoder_vocab_{model.split('/')[-1]}.json" | |
| with open(vocab_json, "w", encoding='utf8') as log_file: | |
| json.dump(dict(sorted(vocabulary.items(), key=lambda x: x[1])), log_file, indent=4, ensure_ascii=False) | |