Skip to main content

HugTokenCraft is a user-friendly Python library that simplifies the process of modifying the vocabulary of a PreTrainedTokenizer from HuggingFace Transformers, making it accessible without additional training.

Project description

HugTokenCraft

HugTokenCraft is a user-friendly Python library that simplifies the process of modifying the vocabulary of a PreTrainedTokenizer from HuggingFace Transformers, making it accessible without additional training. As of now, this was validated for BertTokenizer, which is word-piece-based vocabulary.

Installation

Install from PyPI

You can install HugTokenCraft using pip:

pip install hugtokencraft

Install from source

git clone git@github.com/MDFahimAnjum/HugTokenCraft.git
cd HugTokenCraft
python setup.py install

Usage

1. Reduce vocabulary

Let's take a pre-trained BertTokenizer which has 30,000 tokens and modify it to only keep 20 tokens

#import library
from hugtokencraft import editor
from transformers import BertTokenizer
import os

#load BertTokenizer
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

#check
initial_vocab_size=len(tokenizer)
print(f"initial vocab size: {initial_vocab_size}")

#Target vocabulary
target_vocab_size=20
selected_words=editor.get_top_tokens(tokenizer,target_vocab_size)

#parameters
current_directory = os.getcwd()
# Define the path where you want to save the tokenizer
tokenizer_path = os.path.join(current_directory,"ModifiedTokenizer")
model_max_length=128

#reduce vocabulary
modified_tokenizer=editor.reduce_vocabulary(tokenizer,selected_words)
tokenizer_path=editor.save_tokenizer(modified_tokenizer,tokenizer_path,model_max_length)
modified_tokenizer=editor.load_tokenizer(type(tokenizer),tokenizer_path)

#check
new_vocab_size=len(modified_tokenizer)
print(f"new vocab size: {new_vocab_size} words")

2. Expand vocabulary

Let's take a pre-trained BertTokenizer and add two new tokens

#import library
from hugtokencraft import editor
from transformers import BertTokenizer
import os

#load BertTokenizer
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

#check
initial_vocab_size=len(tokenizer)
print(f"initial vocab size: {initial_vocab_size}")

#Target vocabulary
selected_words_add={'hugtoken','hugtokencraft'}

#parameters
current_directory = os.getcwd()
# Define the path where you want to save the tokenizer
tokenizer_path = os.path.join(current_directory,"ModifiedTokenizer")


#expand vocabulary
modified_tokenizer=editor.expand_vocabulary(tokenizer,selected_words_add)
tokenizer_path=editor.save_tokenizer(modified_tokenizer,tokenizer_path,model_max_length=None,isreduced=False)
modified_tokenizer=editor.load_tokenizer(type(tokenizer),tokenizer_path)

#check
new_vocab_size=len(modified_tokenizer)
print(f"new vocab size: {new_vocab_size}")

Notebook Example

You can also run the Python jupyter notebook examples directly by running example_notebook.ipynb

Documentation

get_top_tokens()

Obtains the k most frequently used tokens from tokenizer vocabulary.

Syntex

token_set=get_top_tokens(tokenizer,k)

Parameters

  • tokenizer: BertTokenizer
    • Pre-trained Bert Tokenizer
  • k: int
    • Desired number of tokens

Returns

  • token_list: set
    • Set of k most frequent tokens

expand_vocabulary()

Adds a set of new tokens to the vocabulary

Syntex

modified_tokenizer=expand_vocabulary(tokenizer,tokens_to_add)

Parameters

  • tokenizer: BertTokenizer
    • Pre-trained Bert Tokenizer
  • tokens_to_add: set
    • Set of tokens to add

Returns

  • modified_tokenizer: BertTokenizer
    • Modified Bert Tokenizer

reduce_vocabulary()

Removes all tokens execpt the given set of tokens from vocabulary

Syntex

modified_tokenizer=reduce_vocabulary(tokenizer,tokens_to_keep)

Parameters

  • tokenizer: BertTokenizer
    • Pre-trained Bert Tokenizer
  • tokens_to_keep: set
    • Set of tokens to keep

Returns

  • modified_tokenizer: BertTokenizer
    • Modified Bert Tokenizer

save_tokenizer()

Saves the modified tokenizer for use

Syntex

tokenizer_path=save_tokenizer(tokenizer,tokenizer_path,model_max_length=None,isreduced=True)

Parameters

  • tokenizer: BertTokenizer
    • Pre-trained Bert Tokenizer
  • tokenizer_path: str
    • Location path to save the tokenizer
  • model_max_length: int
    • New value of maximum token length
    • Defaults to None which means no change
  • isreduced: bool
    • Whether the modified tokenizer was reduced
    • True if vocabulary was reduced (Default)
    • False if vocabulary was expanded

Returns

  • tokenizer_path: str
    • Location path to save the tokenizer

load_tokenizer()

Loads a tokenizer from a given path

Syntex

tokenizer=load_tokenizer(tokenizer_class,tokenizer_path)

Parameters

  • tokenizer_class: type
    • Class type of Tokenizer
  • tokenizer_path: str
    • Location path to save the tokenizer

Returns

  • tokenizer: tokenizer_class
    • Tokenizer

validate_tokenizer()

Simple sanity check for tokenizer

Syntex

is_pass=validate_tokenizer(tokenizer)

Parameters

  • tokenizer: BertTokenizer
    • Pre-trained Bert Tokenizer

Returns

  • is_pass: bool
    • Valication result
    • True: validation passed
    • False: Validation failed

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

We welcome contributions!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hugtokencraft-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hugtokencraft-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file hugtokencraft-0.1.0.tar.gz.

File metadata

  • Download URL: hugtokencraft-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.18

File hashes

Hashes for hugtokencraft-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bbc6b170593077e77de36ed8e7a2d61dd2fe432c181e738bf07306abb24af9b0
MD5 8962b75a98cc15beed3a2d5926fbc493
BLAKE2b-256 6954fa9502cdb00835b62bd89dffef6c52f38469d83e806de82e4488ea1112cf

See more details on using hashes here.

File details

Details for the file hugtokencraft-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hugtokencraft-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.18

File hashes

Hashes for hugtokencraft-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f9ed18f7640343a4b49224467531787874578a074ad6e72639e859d0368c996
MD5 cdd02a77f671a69206140a856ea2da9c
BLAKE2b-256 e40b8a44d0a16f1b10bdbc5e57ffc44b04a0c12d8ee85f525f8aed242fc1da00

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page