Views
No views yet
This is fork of flair/ner-english-ontonotes-large implementing a customhandler.pyas an example for how to useflairmodels with inference-endpoints
| tag | meaning |
|---|---|
| NAME | Name of person |
| ORG | organizaiton name |
| GCNUMBER | GC tracking number |
| BGNUMBER | BG tracking number |
| COUNTRY | Country name |
| LOCATION | city and picode |
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("flair/ner-english-ontonotes-large")
6
7# make example sentence
8sentence = Sentence("On September 1st George won 1 dollar while watching Game of Thrones.")
9
10# predict NER tags
11tagger.predict(sentence)
12
13# print sentence
14print(sentence)
15
16# print predicted NER spans
17print('The following NER tags are found:')
18# iterate over entities and print
19for entity in sentence.get_spans('ner'):
20 print(entity)
21Span [2,3]: "September 1st" [− Labels: DATE (1.0)]
Span [4]: "George" [− Labels: PERSON (1.0)]
Span [6,7]: "1 dollar" [− Labels: MONEY (1.0)]
Span [10,11,12]: "Game of Thrones" [− Labels: WORK_OF_ART (1.0)]1from flair.data import Corpus
2from flair.datasets import ColumnCorpus
3from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
4
5# 1. load the corpus (Ontonotes does not ship with Flair, you need to download and reformat into a column format yourself)
6corpus: Corpus = ColumnCorpus(
7 "resources/tasks/onto-ner",
8 column_format={0: "text", 1: "pos", 2: "upos", 3: "ner"},
9 tag_to_bioes="ner",
10 )
11
12# 2. what tag do we want to predict?
13tag_type = 'ner'
14
15# 3. make the tag dictionary from the corpus
16tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
17
18# 4. initialize fine-tuneable transformer embeddings WITH document context
19from flair.embeddings import TransformerWordEmbeddings
20
21embeddings = TransformerWordEmbeddings(
22 model='xlm-roberta-large',
23 layers="-1",
24 subtoken_pooling="first",
25 fine_tune=True,
26 use_context=True,
27)
28
29# 5. initialize bare-bones sequence tagger (no CRF, no RNN, no reprojection)
30from flair.models import SequenceTagger
31
32tagger = SequenceTagger(
33 hidden_size=256,
34 embeddings=embeddings,
35 tag_dictionary=tag_dictionary,
36 tag_type='ner',
37 use_crf=False,
38 use_rnn=False,
39 reproject_embeddings=False,
40)
41
42# 6. initialize trainer with AdamW optimizer
43from flair.trainers import ModelTrainer
44
45trainer = ModelTrainer(tagger, corpus, optimizer=torch.optim.AdamW)
46
47# 7. run training with XLM parameters (20 epochs, small LR)
48from torch.optim.lr_scheduler import OneCycleLR
49
50trainer.train('resources/taggers/ner-english-ontonotes-large',
51 learning_rate=5.0e-6,
52 mini_batch_size=4,
53 mini_batch_chunk_size=1,
54 max_epochs=20,
55 scheduler=OneCycleLR,
56 embeddings_storage_mode='none',
57 weight_decay=0.,
58 )@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}
}