Elffuss BraidHub

kikocis/mlx-vlm public

Open tokenizer.json within context manager (#129)

* Open tokenizer.json within context manager

* Update mlx_vlm/tokenizer_utils.py

Co-authored-by: Prince Canuma <prince.gdt@gmail.com>

* Add import statement and run pre-commit

---------

Co-authored-by: Prince Canuma <prince.gdt@gmail.com>
Changeset335fd223d5181ba3b710543702f8aff6b5f3e914f90c50530bda2acf18d84d83
AuthorNeil Mehta <neil@lmstudio.ai> · 2024-11-27 15:53 -05:00
CommitterGitHub <noreply@github.com> · 2024-11-27 21:53 +01:00
Parents89249a5c3a56

Browse the files at this changeset

1 file changed

modified mlx_vlm/tokenizer_utils.py +6 −1
@@ -1,5 +1,6 @@
11 import json
22 from functools import partial
3+from json import JSONDecodeError
34
45 from transformers import AutoTokenizer
56
@@ -314,7 +315,11 @@
314315
315316 tokenizer_file = model_path / "tokenizer.json"
316317 if tokenizer_file.exists():
317- tokenizer_content = json.load(tokenizer_file.open())
318+ with open(tokenizer_file, "r") as f:
319+ try:
320+ tokenizer_content = json.load(f)
321+ except JSONDecodeError as e:
322+ raise JSONDecodeError("Failed to parse tokenizer.json", e.doc, e.pos)
318323 if "decoder" in tokenizer_content:
319324 if _is_spm_decoder(tokenizer_content["decoder"]):
320325 detokenizer_class = SPMStreamingDetokenizer