To use a Transformer model, follow these three steps:
- Download/Load the dictionary metadata: Use
download_or_load with the model directory and language to retrieve the necessary paths. - Initialize the model: Use
TransformerModel.from_pretrained passing the paths retrieved from the metadata. - Initialize the tokenizer: If the model requires a tokenizer, use
CustomTokenizer.from_file pointing to the vocab.json and merges.txt files within the tokenizer directory.
# 1. Pass the model name to download, and then get the path
load_dict = download_or_load(f"transformer/{self._n_model}", self._lang)
# 2. Use the path information to load the model
model = TransformerModel.from_pretrained(
model_name_or_path=load_dict.path,
checkpoint_file=f"{self._n_model}.pt",
data_name_or_path=load_dict.dict_path,
source_lang = load_dict.src_dict,
target_lang = load_dict.tgt_dict,
)
# 3. Load the tokenizer, if necessary
tokenizer = CustomTokenizer.from_file(
vocab_filename=f"{load_dict.src_tok}/vocab.json",
merges_filename=f"{load_dict.src_tok}/merges.txt",
)