flair

flairNLP活跃维护

基于 PyTorch 的轻量级 NLP 框架,几行代码就能完成命名实体识别、词性标注、语义角色标注等序列标注任务的训练与微调。

A very simple framework for state-of-the-art Natural Language Processing (NLP)

v0.15.1发布于 2025-02-05 14:46 UTC14,399star +2 / 7 天2,095fork同步于 2026-11-10 17:27 UTC

怎么上手

由 README 提炼,执行前请确认
pip install flair
硬件要求按 README 说明

README 未说明显存、CPU 或 GPU 型号要求,仅说明需要 Python 3.9+。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于 PyTorch 的简单 NLP 框架,一行代码加载预训练模型做 NER、情感分析等任务

基于 PyTorch 的轻量级 NLP 框架,几行代码就能完成命名实体识别、词性标注、语义角色标注等序列标注任务的训练与微调。

核心能力

  • 用 Classifier.load 一行加载预训练模型并预测
  • 支持 NER、情感分析、词性标注、语义消歧与分类
  • 可组合使用 Flair embeddings 与多种 transformer 词/文档向量
  • 直接构建在 PyTorch 上,便于训练和实验自定义模型
  • 提供生物医学文本的专门支持与模型

容易踩的坑

首次运行 Classifier.load('ner') 等会从 Hugging Face 下载预训练模型,需联网;README 说明需 Python 3.9+,版本过低会装不上;书中示例针对旧版本 Flair,部分代码可能已失效。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v0.15.1
  1. 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

    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

    Bug Fixes

    New Contributors

    Full Changelog: https://github.com/flairNLP/flair/compare/v0.15.0...v0.15.1

  2. 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!

    Future releases will improve docstring coverage and further improve upon the documentation!

    PRs:

    Multi-GPU Support

    Flair now offers support for training models on multiple GPUs! Big thanks to @jeffpicard!

    PRs:

    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.clustering module, since we aren't aware of any usage of it, we decided to do a hard drop instead of a deprecation.

    Other Improvements

    New Datasets

    Performance Improvements

    New Features and Improvements

    Bugfixes

    Operations/Development

  3. 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 Syndrome 
    

    The 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:

    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
    
  4. 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

    Enhancements

    New Datasets

    New Contributors

    Full Changelog: https://github.com/flairNLP/flair/compare/v0.13.0...v0.13.1

  5. 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=True to the fine_tune method. 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:

    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

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    alt text alt text

    PyPI version GitHub Issues Contributions welcome License: MIT

    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:

    LanguageDatasetFlairBest publishedModel card & demo
    EnglishConll-03 (4-class)94.0994.3 (Yamada et al., 2020)Flair English 4-class NER demo
    EnglishOntonotes (18-class)90.9391.3 (Yu et al., 2020)Flair English 18-class NER demo
    GermanConll-03 (4-class)92.3190.3 (Yu et al., 2020)Flair German 4-class NER demo
    DutchConll-03 (4-class)95.2593.7 (Yu et al., 2020)Flair Dutch 4-class NER demo
    SpanishConll-03 (4-class)90.5490.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.

    Tutorials

    On our new :fire: Flair documentation page you will find many tutorials to get you started!

    In particular:

    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:

    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.