MaxKB/apps/common/config/tokenizer_manage_config.py
2025-11-07 14:57:04 +08:00

31 lines
827 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
"""
@project: maxkb
@Author
@file tokenizer_manage_config.py
@date2024/4/28 10:17
@desc:
"""
import os
class MKTokenizer:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def encode(self, text):
return self.tokenizer.encode(text).ids
class TokenizerManage:
tokenizer = None
@staticmethod
def get_tokenizer():
from tokenizers import Tokenizer
# 创建Tokenizer
model_path = os.path.join("/opt/maxkb-app", "model", "tokenizer", "models--bert-base-cased")
with open(f"{model_path}/refs/main", encoding="utf-8") as f: snapshot = f.read()
TokenizerManage.tokenizer = Tokenizer.from_file(f"{model_path}/snapshots/{snapshot}/tokenizer.json")
return MKTokenizer(TokenizerManage.tokenizer)