首次运行 Classifier.load('ner') 等会从 Hugging Face 下载预训练模型,需联网;README 说明需 Python 3.9+,版本过低会装不上;书中示例针对旧版本 Flair,部分代码可能已失效。
flair
flairNLP活跃维护基于 PyTorch 的轻量级 NLP 框架,几行代码就能完成命名实体识别、词性标注、语义角色标注等序列标注任务的训练与微调。
A very simple framework for state-of-the-art Natural Language Processing (NLP)
pip install flairREADME 未说明显存、CPU 或 GPU 型号要求,仅说明需要 Python 3.9+。
基于 PyTorch 的简单 NLP 框架,一行代码加载预训练模型做 NER、情感分析等任务
基于 PyTorch 的轻量级 NLP 框架,几行代码就能完成命名实体识别、词性标注、语义角色标注等序列标注任务的训练与微调。
核心能力
- 用 Classifier.load 一行加载预训练模型并预测
- 支持 NER、情感分析、词性标注、语义消歧与分类
- 可组合使用 Flair embeddings 与多种 transformer 词/文档向量
- 直接构建在 PyTorch 上,便于训练和实验自定义模型
- 提供生物医学文本的专门支持与模型
容易踩的坑
- v0.15.1Release 0.15.1最新正式版2025-02-05 · 2025-02-05 14:46 UTC
This release fixes compatibility bugs with the newest PyTorch and SciPy versions, and adds a number of small improvements and new features.
Improvements and new features
SegtokTokenizer: Add option to customize SegtokTokenizer, by @alanakbik in https://github.com/flairNLP/flair/pull/3592RegexpTagger: Add option to define matching groups to RegexpTagger, by @alanakbik in https://github.com/flairNLP/flair/pull/3598RelationClassifier: Optimize RelationClassifier by adding the option to filter long sentences and truncate context, by @alanakbik in https://github.com/flairNLP/flair/pull/3593RelationClassifier: Modify printouts in RelationClassifier evaluation to remove clutter by @alanakbik in https://github.com/flairNLP/flair/pull/3591- Add sentence labeler, by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3570
- Adding a Deep Nearest Class Means Classifier model to Flair, by @sheldon-roberts in https://github.com/flairNLP/flair/pull/3532
- Add per-task metrics by @ntravis22 in https://github.com/flairNLP/flair/pull/3605
- Add options to load full documents as Sentence objects, by @alanakbik in https://github.com/flairNLP/flair/pull/3595
New Model: Deep Nearest Class Means Classifier (#3532)
Adds a new Nearest Class Mean classification approach to Flair that classifies data points to the class with the closest class data mean. This approach can be used as an alternative to fitting a Softmax Classifier. It is now available for any class in Flair that implements DefaultClassifier. For instance, to train a TextClassifier with DeepNCMs you can use the following code:
from flair.data import Corpus from flair.datasets import TREC_50 from flair.embeddings import TransformerDocumentEmbeddings from flair.models import TextClassifier from flair.nn import DeepNCMDecoder from flair.trainers import ModelTrainer from flair.trainers.plugins import DeepNCMPlugin # load the TREC dataset corpus: Corpus = TREC_50() label_type = "class" # make a transformer document embedding document_embeddings = TransformerDocumentEmbeddings("distilbert-base-uncased", fine_tune=True) # create the label_dictionary label_dictionary = corpus.make_label_dictionary(label_type=label_type) # create a text classifier with a special DeepNCM decoder classifier = TextClassifier( document_embeddings, label_type=label_type, label_dictionary=label_dictionary, decoder=DeepNCMDecoder( mean_update_method="condensation", embeddings_size=document_embeddings.embedding_length, label_dictionary=label_dictionary, ), ) # initialize the trainer trainer = ModelTrainer(classifier, corpus) # train the model using the DeepNCM plugin trainer.fine_tune( "resources/taggers/deepncm_baseline", plugins=[DeepNCMPlugin()], )Contributed by @sheldon-roberts in https://github.com/flairNLP/flair/pull/3532
Datasets
- Add BarNER Dataset by @stefan-it in https://github.com/flairNLP/flair/pull/3604
Bug Fixes
- Fix model loading for compatibility with PyTorch 2.6, by @helpmefindaname in https://github.com/flairNLP/flair/pull/3608
- Fix SciPy compatibility by updating scipy .A to toarray(), by @sg-wbi in https://github.com/flairNLP/flair/pull/3606
- Fix: use proper eval default main eval metrics for text regression model by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3602
- Fix: cast indices tensor to int to fix bug by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3601
New Contributors
- @sg-wbi made their first contribution in https://github.com/flairNLP/flair/pull/3606
- @ntravis22 made their first contribution in https://github.com/flairNLP/flair/pull/3605
Full Changelog: https://github.com/flairNLP/flair/compare/v0.15.0...v0.15.1
- v0.15.0Release 0.15.02024-12-20 · 2024-12-20 08:57 UTC
查看更新说明
This release adds multi-GPU support, an improved documentation page with API docs and finally deprecates Python 3.8!
Improved Documentation and API Docs
Thanks to @konstantin-lukas we have a completely new design of our documentation page, which now includes API docs.
You can check it out here!
- Check out our tutorials
- Check out the new Python API docs
Future releases will improve docstring coverage and further improve upon the documentation!
PRs:
- Fix doc build by @helpmefindaname in https://github.com/flairNLP/flair/pull/3528
- Rework Doc page by @konstantin-lukas in https://github.com/flairNLP/flair/pull/3563
- Test new docstrings and apidocs deployment by @alanakbik in https://github.com/flairNLP/flair/pull/3573
Multi-GPU Support
Flair now offers support for training models on multiple GPUs! Big thanks to @jeffpicard!
PRs:
- Add multi-GPU support by @jeffpicard in https://github.com/flairNLP/flair/pull/3548
- Fix gradient accumulation and learning rate aggregation by @jeffpicard in #3583
Deprecations
Since Python3.8 is no longer supported, we are also dropping support for it, in favor of features added in python 3.9. To acknowledge CVE-2024-10073, we decided to drop support for the
flair.models.clusteringmodule, since we aren't aware of any usage of it, we decided to do a hard drop instead of a deprecation.- Drop python 3.8 by @helpmefindaname in https://github.com/flairNLP/flair/pull/3560
- Remove clustering support by @helpmefindaname in https://github.com/flairNLP/flair/pull/3567
Other Improvements
New Datasets
- Add CleanCoNLL object by @susannaruecker in https://github.com/flairNLP/flair/pull/3557
- Add NoiseBench object by @elenamer in #3512
Performance Improvements
- perf: optimize dictionary items check by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3569
- Refactor
fill_mean_token_embeddingsfor performance optimization on GPU by @sheldon-roberts in https://github.com/flairNLP/flair/pull/3525
New Features and Improvements
- Add proxies information to requests.head by @diego-morientez in https://github.com/flairNLP/flair/pull/3535
- Allow specifying proxy information in TransformerEmbeddings by @diego-morientez in https://github.com/flairNLP/flair/pull/3539
- Add
use_tokenizertoJsonlDatasetby @david-waterworth in https://github.com/flairNLP/flair/pull/3486 - Use built-in version parsing from packaging by @adrianeboyd in https://github.com/flairNLP/flair/pull/3502
Bugfixes
TransformerDocumentEmbeddings: Fix error whencls_pooling="mean"orcls_pooling="max"by @fkdosilovic in https://github.com/flairNLP/flair/pull/3558SequenceTagger: Fix the incorrect token prediction distribution from_all_scores_for_token()by @mdmotaharmahtab in https://github.com/flairNLP/flair/pull/3449TransformerEmbeddings: Fix T5 tokenizer loading by @helpmefindaname in https://github.com/flairNLP/flair/pull/3544TextPairRegressor: Fix: use proper eval default main eval metrics by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3538TextPairRegressor: Fix state dict key mismatch for embeddings by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3537- Make onnx export work again by @helpmefindaname in https://github.com/flairNLP/flair/pull/3530
- Fix support metric by @MattGPT-ai in https://github.com/flairNLP/flair/pull/3510
Operations/Development
- Invalidate tars classifier and tars ner tests to save disk space by @helpmefindaname in https://github.com/flairNLP/flair/pull/3527
- Ignore FutureWarning by @alanakbik in https://github.com/flairNLP/flair/pull/3526
- Update SECURITY.md with current contact by @alanakbik in
- v0.14.0Release 0.14.02024-07-25 · 2024-07-25 12:21 UTC
查看更新说明
This release adds major new support for biomedical text analytics! It adds improved biomedical NER and a state-of-the-art model for biomedical entity linking. Other new features include (1) support for parameter-efficient fine-tuning and (2) various new datasets, bug fixes and enhancements! We also removed a few dependencies, so Flair should install faster and take up less space!
Biomedical NER and Entity Linking
With Flair 0.14.0, you can now detect and normalize biomedical entities in text.
For example, to analyze the sentence "
We correlate genetic variants in IFNAR2 and POLG with long-COVID syndrome", use this code snippet:from flair.models import EntityMentionLinker from flair.nn import Classifier from flair.data import Sentence # A sentence from biomedical literature sentence = Sentence("We correlate genetic variants in IFNAR2 and POLG with long-COVID syndrome.") # Tag named entities in the text ner_tagger = Classifier.load("hunflair2") ner_tagger.predict(sentence) # Normalize disease names disease_linker = EntityMentionLinker.load("gene-linker") disease_linker.predict(sentence) # Normalize gene names gene_linker = EntityMentionLinker.load("disease-linker") gene_linker.predict(sentence) # Iterate over predicted entities and print for label in sentence.get_labels(): print(label)This should print out:
Span[5:6]: "IFNAR2" → Gene (1.0) Span[5:6]: "IFNAR2" → 3455/name=IFNAR2 Span[7:8]: "POLG" → Gene (1.0) Span[7:8]: "POLG" → 5428/name=POLG Span[9:11]: "long-COVID syndrome" → Disease (1.0) Span[9:11]: "long-COVID syndrome" → MESH:D000094024/name=Post-Acute COVID-19 SyndromeThe printout shows that:
-
"IFNAR2" is a gene. Further, it is recognized as gene 3455 ("interferon alpha and beta receptor subunit 2") in the NCBI database.
-
"POLG" is a gene. Further, it is recognized as gene 5428 ("DNA polymerase gamma, catalytic subunit") in the NCBI database.
-
"long-COVID syndrome" is a disease. Further, it is uniquely linked to "Post-Acute COVID-19 Syndrome" in the MESH database.
Big thanks to @sg-wbi @WangXII @mariosaenger @helpmefindaname for all their work:
- Entity Mention Linker by @helpmefindaname in https://github.com/flairNLP/flair/pull/3388
- Support for biomedical datasets with multiple entity types by @WangXII in https://github.com/flairNLP/flair/pull/3387
- Update documentation for Hunflair2 release by @mariosaenger in https://github.com/flairNLP/flair/pull/3410
- Improve nel tutorial by @helpmefindaname in https://github.com/flairNLP/flair/pull/3369
- Incorporate hunflair2 docs to docpage by @helpmefindaname in https://github.com/flairNLP/flair/pull/3442
Parameter-Efficient Fine-Tuning
Flair 0.14.0 also adds support for PEFT.
For instance, to fine-tune a BERT model on the TREC question classification task using LoRA, use the following snippet:
from flair.data import Corpus from flair.datasets import TREC_6 from flair.embeddings import TransformerDocumentEmbeddings from flair.models import TextClassifier from flair.trainers import ModelTrainer # Note: you need to install peft to use this feature! from peft import LoraConfig, TaskType # Get corpus and make label dictionary corpus: Corpus = TREC_6() label_type = "question_class" label_dict = corpus.make_label_dictionary(label_type=label_type) # Define embeddings with LoRA fine-tuning document_embeddings = TransformerDocumentEmbeddings( "bert-base-uncased", fine_tune=True, # set LoRA config peft_config=LoraConfig( task_type=TaskType.FEATURE_EXTRACTION, inference_mode=False, ), ) # define model classifier = TextClassifier(document_embeddings, label_dictionary=label_dict, label_type=label_type) # trai -
- v0.13.1Release 0.13.12023-12-18 · 2023-12-18 09:29 UTC
查看更新说明
This releases adds some bugfixes on top of the 0.13.0 Release, and adds a new dataset.
Bug fixes
- fix doc redirect by @helpmefindaname in https://github.com/flairNLP/flair/pull/3366
- fix awaiting response check by @helpmefindaname in https://github.com/flairNLP/flair/pull/3371
- fix has unknown label is not always initialized by @helpmefindaname in https://github.com/flairNLP/flair/pull/3372
- Fix classification report if dataset has no labels by @alanakbik in https://github.com/flairNLP/flair/pull/3375
- fix flert hidden context breaks reduced vocab by @helpmefindaname in https://github.com/flairNLP/flair/pull/3370
- update HF cache env variable by @helpmefindaname in https://github.com/flairNLP/flair/pull/3386
Enhancements
- use batch count instead of total training samples for logging metrics by @helpmefindaname in https://github.com/flairNLP/flair/pull/3374
New Datasets
- Add AGNews corpus by @elenamer in https://github.com/flairNLP/flair/pull/3385
New Contributors
- @elenamer made their first contribution in https://github.com/flairNLP/flair/pull/3385
Full Changelog: https://github.com/flairNLP/flair/compare/v0.13.0...v0.13.1
- v0.13.0Release 0.13.02023-10-28 · 2023-10-28 12:19 UTC
查看更新说明
This release adds several major new features such as (1) faster and more memory-efficient transformer training, (2) a new plugin system for custom logging and training, (3) new API docs for better documentation - still in beta, and (4) various new models, datasets, bug fixes and enhancements. This release also increases the minimum requirement to Python 3.8!
New Feature: Faster and more memory-efficient transformer training
This release integrates @helpmefindaname's transformer-smaller-training-vocab into the ModelTrainer. This temporarily reduces a transformer's vocabulary to only the tokens in the training dataset, and after training restores the full vocabulary. Depending on the dataset, this may effect huge savings in GPU memory and tuning speeds.
To use this feature, simply add the flag
reduce_transformer_vocab=Trueto thefine_tunemethod. For example, to fine-tune a distilbert model on TREC_6, run this code (step 7 has the flag to reduce the vocabulary):# 1. get the corpus corpus: Corpus = TREC_6() # 2. what label do we want to predict? label_type = "question_class" # 3. create the label dictionary label_dict = corpus.make_label_dictionary(label_type=label_type) # 4. initialize transformer document embeddings (many models are available) document_embeddings = TransformerDocumentEmbeddings("distilbert-base-uncased", fine_tune=True) # 5. create the text classifier classifier = TextClassifier(document_embeddings, label_dictionary=label_dict, label_type=label_type) # 6. initialize trainer trainer = ModelTrainer(classifier, corpus) # 7. fine-tune the model, but **reduce the vocabulary** for faster training trainer.fine_tune( "resources/taggers/question-classification-with-transformer", reduce_transformer_vocab=True, # set this to False for slow version )Involved PR: add reduce transformer vocab plugin by @helpmefindaname in https://github.com/flairNLP/flair/pull/3217
New Feature: Trainer Plugins
A new "Plugin" system was added to the
ModelTrainer, allowing far greater options to customize the training cycle (and slimming down the code of the ModelTrainer somewhat). For instance, it is now possible to customize logging to a far greater degree and integrate third-party logging tools.For instance, if you want to integrate ClearML logging into the above script, simply instantiate the plugin and attach it to the trainer:
[...] # 6. initialize trainer trainer = ModelTrainer(classifier, corpus) # NEW: instantiate a special logger and attach it to the trainer before the training run ClearmlLoggerPlugin(clearml.Task.init(project_name="test", task_name="test")).attach_to(trainer) # 7. fine-tune the model, but **reduce the vocabulary** for faster training trainer.fine_tune( "resources/taggers/question-classification-with-transformer", reduce_transformer_vocab=True, # set this to False for slow version )Involved PRs:
- Proposal: Pluggable
ModelTrainertrain function by @plonerma in https://github.com/flairNLP/flair/pull/3084 - Major refactoring of ModelTrainer by @alanakbik in https://github.com/flairNLP/flair/pull/3182
- Allow users to use no scheduler and use a custom scheduling plugin by @plonerma in https://github.com/flairNLP/flair/pull/3200
- Don't pickle classes & plugins in modelcard by @helpmefindaname in https://github.com/flairNLP/flair/pull/3325
- Clearml logger by @helpmefindaname in https://github.com/flairNLP/flair/pull/3259
- Add a convenience conversion for flair.device by @alanakbik in https://github.com/flairNLP/flair/pull/3350
API Docs and other documentation
We are working towards improving our documentation. A first step was the release of our tutorial page. Now, we are adding (in beta) online API docs to make navigating the code and options offered by Flair easier. To enable it, we chan
- Proposal: Pluggable
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。

A very simple framework for state-of-the-art NLP. Developed by Humboldt University of Berlin and friends.
Flair is:
-
A powerful NLP library. Flair allows you to apply our state-of-the-art natural language processing (NLP) models to your text, such as named entity recognition (NER), sentiment analysis, part-of-speech tagging (PoS), special support for biomedical texts, sense disambiguation and classification, with support for a rapidly growing number of languages.
-
A text embedding library. Flair has simple interfaces that allow you to use and combine different word and document embeddings, including our proposed Flair embeddings and various transformers.
-
A PyTorch NLP framework. Our framework builds directly on PyTorch, making it easy to train your own models and experiment with new approaches using Flair embeddings and classes.
Now at version 0.15.1!
State-of-the-Art Models
Flair ships with state-of-the-art models for a range of NLP tasks. For instance, check out our latest NER models:
| Language | Dataset | Flair | Best published | Model card & demo |
|---|---|---|---|---|
| English | Conll-03 (4-class) | 94.09 | 94.3 (Yamada et al., 2020) | Flair English 4-class NER demo |
| English | Ontonotes (18-class) | 90.93 | 91.3 (Yu et al., 2020) | Flair English 18-class NER demo |
| German | Conll-03 (4-class) | 92.31 | 90.3 (Yu et al., 2020) | Flair German 4-class NER demo |
| Dutch | Conll-03 (4-class) | 95.25 | 93.7 (Yu et al., 2020) | Flair Dutch 4-class NER demo |
| Spanish | Conll-03 (4-class) | 90.54 | 90.3 (Yu et al., 2020) | Flair Spanish 4-class NER demo |
Many Flair sequence tagging models (named entity recognition, part-of-speech tagging etc.) are also hosted on the 🤗 Hugging Face model hub! You can browse models, check detailed information on how they were trained, and even try each model out online!
Quick Start
Requirements and Installation
In your favorite virtual environment, simply do:
pip install flair
Flair requires Python 3.9+.
Example 1: Tag Entities in Text
Let's run named entity recognition (NER) over an example sentence. All you need to do is make a Sentence, load
a pre-trained model and use it to predict tags for the sentence:
from flair.data import Sentence
from flair.nn import Classifier
# make a sentence
sentence = Sentence('I love Berlin .')
# load the NER tagger
tagger = Classifier.load('ner')
# run NER over sentence
tagger.predict(sentence)
# print the sentence with all annotations
print(sentence)
This should print:
Sentence: "I love Berlin ." → ["Berlin"/LOC]
This means that "Berlin" was tagged as a location entity in this sentence.
- to learn more about NER tagging in Flair, check out our NER tutorial!
Example 2: Detect Sentiment
Let's run sentiment analysis over an example sentence to determine whether it is POSITIVE or NEGATIVE. Same code as above, just a different model:
from flair.data import Sentence
from flair.nn import Classifier
# make a sentence
sentence = Sentence('I love Berlin .')
# load the NER tagger
tagger = Classifier.load('sentiment')
# run NER over sentence
tagger.predict(sentence)
# print the sentence with all annotations
print(sentence)
This should print:
Sentence[4]: "I love Berlin ." → POSITIVE (0.9983)
This means that the sentence "I love Berlin" was tagged as having POSITIVE sentiment.
- to learn more about sentiment analysis in Flair, check out our sentiment analysis tutorial!
Tutorials
On our new :fire: Flair documentation page you will find many tutorials to get you started!
In particular:
- Tutorial 1: Basic tagging → how to tag your text
- Tutorial 2: Training models → how to train your own state-of-the-art NLP models
- Tutorial 3: Embeddings → how to produce embeddings for words and documents
- Tutorial 4: Biomedical text → how to analyse biomedical text data
There is also a dedicated landing page for our biomedical NER and datasets with installation instructions and tutorials.
More Documentation
Another great place to start is the book Natural Language Processing with Flair and its accompanying code repository, though it was written for an older version of Flair and some examples may no longer work.
There are also good third-party articles and posts that illustrate how to use Flair:
- Training an NER model with Flair
- Training a text classifier with Flair
- Zero and few-shot learning
- Visualisation tool for highlighting the extracted entities
- Flair functionality and how to use in Colab
- Benchmarking NER algorithms
- Clinical NLP
- How to build a microservice with Flair and Flask
- A docker image for Flair
- Practical approach of State-of-the-Art Flair in Named Entity Recognition
- Training a Flair text classifier on Google Cloud Platform (GCP) and serving predictions on GCP
- Model Interpretability for transformer-based Flair models
Citing Flair
Please cite the following paper when using Flair embeddings:
@inproceedings{akbik2018coling,
title={Contextual String Embeddings for Sequence Labeling},
author={Akbik, Alan and Blythe, Duncan and Vollgraf, Roland},
booktitle = {{COLING} 2018, 27th International Conference on Computational Linguistics},
pages = {1638--1649},
year = {2018}
}
If you use the Flair framework for your experiments, please cite this paper:
@inproceedings{akbik2019flair,
title={{FLAIR}: An easy-to-use framework for state-of-the-art {NLP}},
author={Akbik, Alan and Bergmann, Tanja and Blythe, Duncan and Rasul, Kashif and Schweter, Stefan and Vollgraf, Roland},
booktitle={{NAACL} 2019, 2019 Annual Conference of the North American Chapter of the Association for Computational Linguistics (Demonstrations)},
pages={54--59},
year={2019}
}
If you use our new "FLERT" models or approach, please cite this paper:
@misc{schweter2020flert,
title={{FLERT}: Document-Level Features for Named Entity Recognition},
author={Stefan Schweter and Alan Akbik},
year={2020},
eprint={2011.06993},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
If you use our TARS approach for few-shot and zero-shot learning, please cite this paper:
@inproceedings{halder2020coling,
title={Task Aware Representation of Sentences for Generic Text Classification},
author={Halder, Kishaloy and Akbik, Alan and Krapac, Josip and Vollgraf, Roland},
booktitle = {{COLING} 2020, 28th International Conference on Computational Linguistics},
year = {2020}
}
Contact
Please email your questions or comments to Alan Akbik.
Contributing
Thanks for your interest in contributing! There are many ways to get involved; start with our contributor guidelines and then check these open issues for specific tasks.
License
The MIT License (MIT)
Flair is licensed under the following MIT license: The MIT License (MIT) Copyright © 2018 Zalando SE, https://tech.zalando.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.