Skip to content

Commit

Permalink
fix: tiktoken cannot be loaded without internet (#12478)
Browse files Browse the repository at this point in the history
Signed-off-by: -LAN- <laipz8200@outlook.com>
  • Loading branch information
laipz8200 authored Jan 8, 2025
1 parent 53bb37b commit 0a49d3d
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from threading import Lock
from typing import Any

import tiktoken

_tokenizer: Any = None
_lock = Lock()

Expand Down Expand Up @@ -33,9 +31,17 @@ def get_encoder() -> Any:
if _tokenizer is None:
# Try to use tiktoken to get the tokenizer because it is faster
#
_tokenizer = tiktoken.get_encoding("gpt2")
# base_path = abspath(__file__)
# gpt2_tokenizer_path = join(dirname(base_path), "gpt2")
# _tokenizer = TransformerGPT2Tokenizer.from_pretrained(gpt2_tokenizer_path)
try:
import tiktoken

_tokenizer = tiktoken.get_encoding("gpt2")
except Exception:
from os.path import abspath, dirname, join

from transformers import GPT2Tokenizer as TransformerGPT2Tokenizer # type: ignore

base_path = abspath(__file__)
gpt2_tokenizer_path = join(dirname(base_path), "gpt2")
_tokenizer = TransformerGPT2Tokenizer.from_pretrained(gpt2_tokenizer_path)

return _tokenizer

0 comments on commit 0a49d3d

Please sign in to comment.