forked from deepchem/deepchem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added huggingface vocabulary builder
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from deepchem.feat.vocabulary_builders.vocabulary_builder import VocabularyBuilder | ||
from tokenizers import Tokenizer | ||
from typing import List | ||
|
||
|
||
class HuggingFaceVocabularyBuilder(VocabularyBuilder): | ||
|
||
def __init__(self, model, trainer): | ||
# This tokenizer is from transformers | ||
self.model = model | ||
self.trainer = trainer | ||
self.tokenizer = Tokenizer(model) | ||
|
||
# superclass accepts a DeepChem dataset while huggingface vocabulary builders | ||
# reads data from file | ||
def build(self, paths: List[str]): # type: ignore | ||
|
||
self.tokenizer.train(paths, self.trainer) | ||
|
||
@classmethod | ||
def load(cls, fname: str): | ||
from transformers import PreTrainedTokenizerFast | ||
tokenizer = PreTrainedTokenizerFast(tokenizer_file=fname) | ||
return tokenizer | ||
|
||
def save(self, fname: str): | ||
""" | ||
Parameters | ||
---------- | ||
fname: str | ||
A json file path | ||
""" | ||
self.tokenizer.save(fname) |