Repository: flairNLP/flair

Issue:
New huggingface/transformers update (4.19.0) breaks imports
**Describe the bug**

`sacremoses` was made optional in the newest `transformers` version 4.19.0 (https://github.com/huggingface/transformers/releases/tag/v4.19.0), more specifically with the PR https://github.com/huggingface/transformers/pull/17049. From the PR's description, `sacremoses` is supposed to be an "artifact of the past", which means we maybe no longer (or should not) need it. 

In the meantime, a quick solution would be to include the lib as a dependency of `flair`.

**Screenshots**

```shell
../.venv/<private-filename>
    from flair.data import Sentence
../.venv/doc_geolocation/lib/python3.9/site-packages/flair/__init__.py:20: in <module>
    from . import models
../.venv/doc_geolocation/lib/python3.9/site-packages/flair/models/__init__.py:1: in <module>
    from .sequence_tagger_model import SequenceTagger, MultiTagger
../.venv/doc_geolocation/lib/python3.9/site-packages/flair/models/sequence_tagger_model.py:20: in <module>
    from flair.embeddings import TokenEmbeddings, StackedEmbeddings, Embeddings
../.venv/doc_geolocation/lib/python3.9/site-packages/flair/embeddings/__init__.py:40: in <module>
    from .legacy import CharLMEmbeddings
../.venv/doc_geolocation/lib/python3.9/site-packages/flair/embeddings/legacy.py:16: in <module>
    from transformers import (
<frozen importlib._bootstrap>:1055: in _handle_fromlist
    ???
../.venv/doc_geolocation/lib/python3.9/site-packages/transformers/utils/import_utils.py:863: in __getattr__
    value = getattr(module, name)
../.venv/doc_geolocation/lib/python3.9/site-packages/transformers/utils/import_utils.py:862: in __getattr__
    module = self._get_module(self._class_to_module[name])
../.venv/doc_geolocation/lib/python3.9/site-packages/transformers/utils/import_utils.py:874: in _get_module
    raise RuntimeError(
E   RuntimeError: Failed to import transformers.models.transfo_xl.tokenization_transfo_xl because of the following error (look up to see its traceback):
E   No module named 'sacremoses'
```

**Environment (please complete the following information):**
 - OS : Ubuntu 20.04
 - Version : 0.11


Discussion:
quick solution use : pip install transformers==4.18.0
Thanks @vigenere23 @hmiche - could you explain a bit more? What is sacremoses used for - is it only for the transformer XL models? 

Resolving patch:
diff --git a/flair/embeddings/__init__.py b/flair/embeddings/__init__.py
--- a/flair/embeddings/__init__.py
+++ b/flair/embeddings/__init__.py
@@ -33,7 +33,6 @@
     OpenAIGPT2Embeddings,
     OpenAIGPTEmbeddings,
     RoBERTaEmbeddings,
-    TransformerXLEmbeddings,
     XLMEmbeddings,
     XLMRobertaEmbeddings,
     XLNetEmbeddings,
diff --git a/flair/embeddings/legacy.py b/flair/embeddings/legacy.py
--- a/flair/embeddings/legacy.py
+++ b/flair/embeddings/legacy.py
@@ -8,9 +8,6 @@
 from transformers import (
     AlbertModel,
     AlbertTokenizer,
-    AutoConfig,
-    AutoModel,
-    AutoTokenizer,
     BertModel,
     BertTokenizer,
     CamembertModel,
@@ -23,9 +20,6 @@
     PreTrainedTokenizer,
     RobertaModel,
     RobertaTokenizer,
-    T5Tokenizer,
-    TransfoXLModel,
-    TransfoXLTokenizer,
     XLMModel,
     XLMRobertaModel,
     XLMRobertaTokenizer,
@@ -350,67 +344,6 @@ def __str__(self):
         return self.name
 
 
-class TransformerXLEmbeddings(TokenEmbeddings):
-    @deprecated(
-        version="0.4.5",
-        reason="Use 'TransformerWordEmbeddings' for all transformer-based word embeddings",
-    )
-    def __init__(
-        self,
-        pretrained_model_name_or_path: str = "transfo-xl-wt103",
-        layers: str = "1,2,3",
-        use_scalar_mix: bool = False,
-    ):
-        """Transformer-XL embeddings, as proposed in Dai et al., 2019.
-        :param pretrained_model_name_or_path: name or path of Transformer-XL model
-        :param layers: comma-separated list of layers
-        :param use_scalar_mix: defines the usage of scalar mix for specified layer(s)
-        """
-        super().__init__()
-
-        self.tokenizer = TransfoXLTokenizer.from_pretrained(pretrained_model_name_or_path)
-        self.model = TransfoXLModel.from_pretrained(
-            pretrained_model_name_or_path=pretrained_model_name_or_path,
-            output_hidden_states=True,
-        )
-        self.name = pretrained_model_name_or_path
-        self.layers: List[int] = [int(layer) for layer in layers.split(",")]
-        self.use_scalar_mix = use_scalar_mix
-        self.static_embeddings = True
-
-        dummy_sentence: Sentence = Sentence()
-        dummy_sentence.add_token(Token("hello"))
-        embedded_dummy = self.embed(dummy_sentence)
-        self.__embedding_length: int = len(embedded_dummy[0].get_token(1).get_embedding())
-
-    @property
-    def embedding_length(self) -> int:
-        return self.__embedding_length
-
-    def _add_embeddings_internal(self, sentences: List[Sentence]) -> List[Sentence]:
-        self.model.to(flair.device)
-        self.model.eval()
-
-        sentences = _get_transformer_sentence_embeddings(
-            sentences=sentences,
-            tokenizer=self.tokenizer,
-            model=self.model,
-            name=self.name,
-            layers=self.layers,
-            pooling_operation="first",
-            use_scalar_mix=self.use_scalar_mix,
-            eos_token="<eos>",
-        )
-
-        return sentences
-
-    def extra_repr(self):
-        return "model={}".format(self.name)
-
-    def __str__(self):
-        return self.name
-
-
 class XLNetEmbeddings(TokenEmbeddings):
     @deprecated(
         version="0.4.5",


Test patch: