Download assistant/vision-export/v2/check_tokenizer.py from T8RIN/imagetoolbox-models: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/T8RIN/imagetoolbox-models/resolve/main/assistant/vision-export/v2/check_tokenizer.py
- Command line
-
hf download hf://T8RIN/imagetoolbox-models/assistant/vision-export/v2/check_tokenizer.py
-
curl -L -o check_tokenizer.py https://huggingface.co/T8RIN/imagetoolbox-models/resolve/main/assistant/vision-export/v2/check_tokenizer.py
1.46 kB
| import json | |
| import unicodedata | |
| from pathlib import Path | |
| import onnxruntime_genai as og | |
| from tokenizers import Tokenizer | |
| root=Path(__file__).parent | |
| (root / 'reports').mkdir(exist_ok=True) | |
| (root / 'logs').mkdir(exist_ok=True) | |
| sentences=[ | |
| 'Привет, что изображено на фото?', | |
| 'Как улучшить это фото? Дай 3 конкретных совета по цвету, кадрированию и свету.', | |
| 'Describe this photograph. Do not invent details.', | |
| "It's a colourful picture: 12345, 3.14, café, йога, e\u0301, 中文, 한국어 😊", | |
| ' Первый\n\nВторой\tтретий ', | |
| '<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Что здесь?<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n', | |
| ] | |
| reference=Tokenizer.from_file(str(root/'sources/vision_max/tokenizer.json')) | |
| native=og.Tokenizer(str(root/'packages/assistant/v1/vision_max')) | |
| report=[] | |
| for text in sentences: | |
| expected=reference.encode(text,add_special_tokens=False).ids | |
| actual=native.encode(unicodedata.normalize("NFC",text)).tolist() | |
| report.append(dict(text=text,expected=expected,actual=actual,exact=expected==actual,roundtrip=native.decode(actual))) | |
| path=root/'reports';path.mkdir(exist_ok=True) | |
| (path/'tokenizer-parity.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)) | |
| print([(r['exact'],r['text'][:45]) for r in report]) | |
| assert all(r['exact'] for r in report) | |