Flair NLP
Flair is an open-source framework for state-of-the-art Natural Language Processing (NLP) built on PyTorch. It provides a simple, unified interface for various NLP tasks like named entity recognition, sentiment analysis, part-of-speech tagging, and text classification, with robust support for multilingual models and embeddings. Currently at version 0.15.1, Flair maintains a regular release cadence, often monthly or bi-monthly, consistently adding new features and addressing bug fixes.
Common errors
-
ModuleNotFoundError: No module named 'flair'
cause The Flair library is not installed or the Python environment where it was installed is not active.fixEnsure Flair is installed in your active environment: `pip install flair`. -
Could not find a version that satisfies the requirement torch
cause PyTorch, a core dependency, often has specific installation requirements, especially for GPU support, that `pip install flair` might not fully resolve automatically in all environments.fixInstall PyTorch separately following the official instructions from [pytorch.org](https://pytorch.org/get-started/locally/) for your specific OS, CUDA version, and Python. Then, `pip install flair`. -
TypeError: ModelTrainer.__init__() got an unexpected keyword argument 'optimizer'
cause This error occurs in Flair versions 0.10.0 and later because the `optimizer` argument was moved from the `ModelTrainer` constructor to its `train()` or `fine_tune()` methods.fixRemove `optimizer` from `ModelTrainer` initialization. Pass it to the `train()` or `fine_tune()` method instead. `trainer = ModelTrainer(model, corpus)` `trainer.train('output_path', optimizer=torch.optim.AdamW, ...)` -
RuntimeError: 'target' must be of floating point type, but got Long
cause This often happens during training if your labels (targets) are integers (Long) but the loss function expects floating-point values, which can occur with certain classification setups, especially for regression or specific multi-label scenarios if not handled correctly.fixEnsure your target labels are cast to a floating-point type (e.g., `torch.float`) before passing them to the loss function during training, particularly if you are using binary cross-entropy or regression losses.
Warnings
- breaking Python 3.8 support has been deprecated and effectively dropped starting from Flair v0.15.0. Earlier versions (0.13.x) set 3.8 as a *minimum* requirement, but current versions require Python 3.9+.
- breaking The `ModelTrainer` API changed in v0.10. The `optimizer` argument is no longer passed during the `ModelTrainer`'s initialization, but instead as a parameter to the `train()` or `fine_tune()` methods.
- gotcha Older versions of Flair (prior to v0.15.1) may experience compatibility issues with newer versions of PyTorch and SciPy.
- deprecated The `flair.models.clustering` module has been completely dropped due to lack of usage and to acknowledge a CVE.
Install
-
pip install flair
Imports
- Sentence
from flair.data import Sentence
- Classifier
from flair.models import Classifier
from flair.nn import Classifier
- SequenceTagger
from flair.models import SequenceTagger
- TextClassifier
from flair.models import TextClassifier
- ModelTrainer
from flair.trainers import ModelTrainer
- WordEmbeddings
from flair.embeddings import WordEmbeddings
- TransformerDocumentEmbeddings
from flair.embeddings import TransformerDocumentEmbeddings
Quickstart
from flair.data import Sentence
from flair.nn import Classifier
# Make a sentence
sentence = Sentence('I love Berlin and New York.')
# Load the NER tagger
tagger = Classifier.load('ner')
# Run NER over sentence
tagger.predict(sentence)
# Print the sentence with all annotations
print(sentence)
# Example for sentiment analysis
sentence_sentiment = Sentence('Flair makes NLP so easy!')
sentiment_model = Classifier.load('sentiment')
sentiment_model.predict(sentence_sentiment)
print(sentence_sentiment)