# Buggy code
def __init__(
    self,
    model: str = "bert-base-uncased",
    layers: str = "all",
    subtoken_pooling: str = "first",
    layer_mean: bool = True,
    fine_tune: bool = False,
    allow_long_sentences: bool = True,
    use_context: Union[bool, int] = False,
    **kwargs,
):
    """
    Bidirectional transformer embeddings of words from various transformer architectures.
    :param model: name of transformer model (see https://huggingface.co/transformers/pretrained_models.html for
    options)
    :param layers: string indicating which layers to take for embedding (-1 is topmost layer)
    :param subtoken_pooling: how to get from token piece embeddings to token embedding. Either take the first
    subtoken ('first'), the last subtoken ('last'), both first and last ('first_last') or a mean over all ('mean')
    :param layer_mean: If True, uses a scalar mix of layers as embedding
    :param fine_tune: If True, allows transformers to be fine-tuned during training
    """
    super().__init__()
    self.instance_parameters = self.get_instance_parameters(locals=locals())

    # temporary fix to disable tokenizer parallelism warning
    # (see https://stackoverflow.com/questions/62691279/how-to-disable-tokenizers-parallelism-true-false-warning)
    import os

    os.environ["TOKENIZERS_PARALLELISM"] = "false"

    # load tokenizer and transformer model
    self.tokenizer = AutoTokenizer.from_pretrained(model, **kwargs)
    config = AutoConfig.from_pretrained(model, output_hidden_states=True, **kwargs)
    self.model = AutoModel.from_pretrained(model, config=config, **kwargs)

    self.allow_long_sentences = allow_long_sentences

    if allow_long_sentences:
        self.max_subtokens_sequence_length = self.tokenizer.model_max_length
        self.stride = self.tokenizer.model_max_length // 2
    else:
        self.max_subtokens_sequence_length = self.tokenizer.model_max_length
        self.stride = 0

    # model name
    self.name = "transformer-word-" + str(model)

    # store whether to use context (and how much)
    if type(use_context) == bool:
        self.context_length: int = 64 if use_context else 0
    if type(use_context) == int:
        self.context_length: int = use_context

    # when initializing, embeddings are in eval mode by default
    self.model.eval()
    self.model.to(flair.device)

    # embedding parameters
    if layers == "all":
        # send mini-token through to check how many layers the model has
        hidden_states = self.model(torch.tensor([1], device=flair.device).unsqueeze(0))[
            -1
        ]
        self.layer_indexes = [int(x) for x in range(len(hidden_states))]
    else:
        self.layer_indexes = [int(x) for x in layers.split(",")]

    self.pooling_operation = subtoken_pooling
    self.layer_mean = layer_mean
    self.fine_tune = fine_tune
    self.static_embeddings = not self.fine_tune

    self.special_tokens = []
    # check if special tokens exist to circumvent error message
    if self.tokenizer._bos_token:
        self.special_tokens.append(self.tokenizer.bos_token)
    if self.tokenizer._cls_token:
        self.special_tokens.append(self.tokenizer.cls_token)

    # most models have an intial BOS token, except for XLNet, T5 and GPT2
    self.begin_offset = 1
    if type(self.tokenizer) == XLNetTokenizer:
        self.begin_offset = 0
    if type(self.tokenizer) == T5Tokenizer:
        self.begin_offset = 0
    if type(self.tokenizer) == GPT2Tokenizer:
        self.begin_offset = 0
    if type(self.tokenizer) == TransfoXLTokenizer:
        self.begin_offset = 0


# Traceback (OSError)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-5-2f413379aedd> in <module>
6
7 # load the NER tagger
----> 8 tagger = SequenceTagger.load('best-model.pt')
9
10

c:\users\nicod\miniconda3\lib\site-packages\flair\nn.py in load(cls, model)
86             # see https://github.com/zalandoresearch/flair/issues/351
87             f = file_utils.load_big_file(str(model_file))
---> 88             state = torch.load(f, map_location='cpu')
89
90         model = cls._init_model_with_state_dict(state)

c:\users\nicod\miniconda3\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
527             with _open_zipfile_reader(f) as opened_zipfile:
528                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 529         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
530
531

c:\users\nicod\miniconda3\lib\site-packages\torch\serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
700     unpickler = pickle_module.Unpickler(f, **pickle_load_args)
701     unpickler.persistent_load = persistent_load
--> 702     result = unpickler.load()
703
704     deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

c:\users\nicod\miniconda3\lib\site-packages\transformers\tokenization_camembert.py in __setstate__(self, d)
259             raise
260         self.sp_model = spm.SentencePieceProcessor()
--> 261         self.sp_model.Load(self.vocab_file)
262
263     def convert_tokens_to_string(self, tokens):

c:\users\nicod\miniconda3\lib\site-packages\sentencepiece.py in Load(self, model_file, model_proto)
365       if model_proto:
366         return self.LoadFromSerializedProto(model_proto)
--> 367       return self.LoadFromFile(model_file)
368
369

c:\users\nicod\miniconda3\lib\site-packages\sentencepiece.py in LoadFromFile(self, arg)
175
176     def LoadFromFile(self, arg):
--> 177         return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
178
179     def Init(self,

OSError: Not found: "/home/ubuntu/.cache/torch/transformers/3715e3a4a2de48834619b2a6f48979e13ddff5cabfb1f3409db689f9ce3bb98f.28d30f926f545047fc59da64289371eef0fbdc0764ce9ec56f808a646fcfec59": No such file or directory Error #2

# Fixed code
def __init__(
    self,
    model: str = "bert-base-uncased",
    layers: str = "all",
    subtoken_pooling: str = "first",
    layer_mean: bool = True,
    fine_tune: bool = False,
    allow_long_sentences: bool = True,
    use_context: Union[bool, int] = False,
    memory_effective_training: bool = True,
    respect_document_boundaries: bool = True,
    **kwargs,
):
    """
    Bidirectional transformer embeddings of words from various transformer architectures.
    :param model: name of transformer model (see https://huggingface.co/transformers/pretrained_models.html for
    options)
    :param layers: string indicating which layers to take for embedding (-1 is topmost layer)
    :param subtoken_pooling: how to get from token piece embeddings to token embedding. Either take the first
    subtoken ('first'), the last subtoken ('last'), both first and last ('first_last') or a mean over all ('mean')
    :param layer_mean: If True, uses a scalar mix of layers as embedding
    :param fine_tune: If True, allows transformers to be fine-tuned during training
    """
    super().__init__()
    self.instance_parameters = self.get_instance_parameters(locals=locals())

    # temporary fix to disable tokenizer parallelism warning
    # (see https://stackoverflow.com/questions/62691279/how-to-disable-tokenizers-parallelism-true-false-warning)
    import os

    os.environ["TOKENIZERS_PARALLELISM"] = "false"

    # load tokenizer and transformer model
    self.tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(model, **kwargs)
    if not "config" in kwargs:
        config = AutoConfig.from_pretrained(model, output_hidden_states=True, **kwargs)
        self.model = AutoModel.from_pretrained(model, config=config, **kwargs)
    else:
        self.model = AutoModel.from_pretrained(None, **kwargs)

    self.allow_long_sentences = allow_long_sentences

    if allow_long_sentences:
        self.max_subtokens_sequence_length = self.tokenizer.model_max_length
        self.stride = self.tokenizer.model_max_length // 2
    else:
        self.max_subtokens_sequence_length = self.tokenizer.model_max_length
        self.stride = 0

    # model name
    self.name = "transformer-word-" + str(model)
    self.base_model = str(model)

    # whether to detach gradients on overlong sentences
    self.memory_effective_training = memory_effective_training

    # store whether to use context (and how much)
    if type(use_context) == bool:
        self.context_length: int = 64 if use_context else 0
    if type(use_context) == int:
        self.context_length: int = use_context

    # if using context, can we cross document boundaries?
    self.respect_document_boundaries = respect_document_boundaries

    # when initializing, embeddings are in eval mode by default
    self.model.eval()
    self.model.to(flair.device)

    # embedding parameters
    if layers == "all":
        # send mini-token through to check how many layers the model has
        hidden_states = self.model(torch.tensor([1], device=flair.device).unsqueeze(0))[
            -1
        ]
        self.layer_indexes = [int(x) for x in range(len(hidden_states))]
    else:
        self.layer_indexes = [int(x) for x in layers.split(",")]

    self.pooling_operation = subtoken_pooling
    self.layer_mean = layer_mean
    self.fine_tune = fine_tune
    self.static_embeddings = not self.fine_tune

    # calculate embedding length
    if not self.layer_mean:
        length = len(self.layer_indexes) * self.model.config.hidden_size
    else:
        length = self.model.config.hidden_size
    if self.pooling_operation == "first_last":
        length *= 2

    # return length
    self.embedding_length_internal = length

    self.special_tokens = []
    # check if special tokens exist to circumvent error message
    if self.tokenizer._bos_token:
        self.special_tokens.append(self.tokenizer.bos_token)
    if self.tokenizer._cls_token:
        self.special_tokens.append(self.tokenizer.cls_token)

    # most models have an intial BOS token, except for XLNet, T5 and GPT2
    self.begin_offset = 1
    if type(self.tokenizer) == XLNetTokenizer:
        self.begin_offset = 0
    if type(self.tokenizer) == T5Tokenizer:
        self.begin_offset = 0
    if type(self.tokenizer) == GPT2Tokenizer:
        self.begin_offset = 0
    if type(self.tokenizer) == TransfoXLTokenizer:
        self.begin_offset = 0