File size: 11,169 Bytes
ad51607 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
from transformers import AutoTokenizer, XLMRobertaForTokenClassification, Pipeline, AutoModelForTokenClassification, AutoModel, XLMRobertaTokenizerFast
from tokenizers.pre_tokenizers import Whitespace
from transformers.pipelines import PIPELINE_REGISTRY
from itertools import chain
from colorama import Fore, Back
from colorama import Style
import numpy as np
from transformers.models.xlm_roberta import XLMRobertaPreTrainedModel, XLMRobertaModel
from transformers.models.roberta import RobertaConfig
from transformers.modeling_outputs import TokenClassifierOutput
from transformers import PretrainedConfig
import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
from typing import List, Optional, Tuple, Union
class RefSegPipeline(Pipeline):
labels = [
'publisher', 'source', 'url', 'other', 'author', 'editor', 'lpage',
'volume', 'year', 'issue', 'title', 'fpage', 'edition'
]
iob_labels = list(chain.from_iterable([['B-' + x, 'I-' + x] for x in labels])) + ['O']
id2seg = {k: v for k, v in enumerate(iob_labels)}
id2ref = {k: v for k, v in enumerate(['B-ref', 'I-ref', ])}
def _sanitize_parameters(self, **kwargs):
if "id2seg" in kwargs:
self.id2seg = kwargs["id2seg"]
if "id2ref" in kwargs:
self.id2ref = kwargs["id2ref"]
return {}, {}, {}
def preprocess(self, sentence, offset_mapping=None):
model_inputs = self.tokenizer(
sentence,
return_offsets_mapping=True,
padding='max_length',
truncation=True,
max_length=512,
return_tensors="pt",
return_special_tokens_mask=True,
return_overflowing_tokens=True
)
if offset_mapping:
model_inputs["offset_mapping"] = offset_mapping
model_inputs["sentence"] = sentence
return model_inputs
def _forward(self, model_inputs):
special_tokens_mask = model_inputs.pop("special_tokens_mask")
offset_mapping = model_inputs.pop("offset_mapping", None)
sentence = model_inputs.pop("sentence")
overflow_mapping = model_inputs.pop("overflow_to_sample_mapping")
if self.framework == "tf":
logits = self.model(model_inputs.data)[0]
else:
logits = self.model(**model_inputs)[0]
return {
"logits": logits,
"special_tokens_mask": special_tokens_mask,
"offset_mapping": offset_mapping,
"overflow_mapping": overflow_mapping,
"sentence": sentence,
**model_inputs,
}
def postprocess(self, model_outputs):
# if ignore_labels is None:
ignore_labels = ["O"]
logits_seg = model_outputs["logits"][0].numpy()
logits_ref = model_outputs["logits"][1].numpy()
sentence = model_outputs["sentence"]
input_ids = model_outputs["input_ids"]
special_tokens_mask = model_outputs["special_tokens_mask"]
overflow_mapping = model_outputs["overflow_mapping"]
offset_mapping = model_outputs["offset_mapping"] if model_outputs["offset_mapping"] is not None else None
maxes_seg = np.max(logits_seg, axis=-1, keepdims=True)
shifted_exp_seg = np.exp(logits_seg - maxes_seg)
scores_seg = shifted_exp_seg / shifted_exp_seg.sum(axis=-1, keepdims=True)
maxes_ref = np.max(logits_ref, axis=-1, keepdims=True)
shifted_exp_ref = np.exp(logits_ref - maxes_ref)
scores_ref = shifted_exp_ref / shifted_exp_ref.sum(axis=-1, keepdims=True)
pre_entities = self.gather_pre_entities(
sentence, input_ids, scores_seg, scores_ref, offset_mapping, special_tokens_mask
)
grouped_entities = self.aggregate(pre_entities)
cleaned_groups = []
for group in grouped_entities:
entities = [
entity
for entity in group
if entity.get("entity_group", None) not in ignore_labels
]
cleaned_groups.append(entities)
return {
"number_of_references": len(cleaned_groups),
"references": cleaned_groups,
}
def gather_pre_entities(
self,
sentence: str,
input_ids: np.ndarray,
scores_seg: np.ndarray,
scores_ref: np.ndarray,
offset_mappings: Optional[List[Tuple[int, int]]],
special_tokens_masks: np.ndarray,
) -> List[dict]:
"""Fuse various numpy arrays into dicts with all the information needed for aggregation"""
pre_entities = []
for idx_list, (input_id, offset_mapping, special_tokens_mask, s_seg, s_ref) in enumerate(
zip(input_ids, offset_mappings, special_tokens_masks, scores_seg, scores_ref)):
for idx, iid in enumerate(input_id):
if special_tokens_mask[idx]:
continue
word = self.tokenizer.convert_ids_to_tokens(int(input_id[idx]))
if offset_mapping is not None:
start_ind, end_ind = offset_mapping[idx]
if not isinstance(start_ind, int):
if self.framework == "pt":
start_ind = start_ind.item()
end_ind = end_ind.item()
word_ref = sentence[start_ind:end_ind]
if getattr(self.tokenizer._tokenizer.model, "continuing_subword_prefix", None):
is_subword = len(word) != len(word_ref)
else:
is_subword = len(word) == len(word_ref)
if int(input_id[idx]) == self.tokenizer.unk_token_id:
word = word_ref
is_subword = False
else:
start_ind = None
end_ind = None
is_subword = False
pre_entity = {
"word": word,
"scores_seg": s_seg[idx],
"scores_ref": s_ref[idx],
"start": start_ind,
"end": end_ind,
"index": idx,
"is_subword": is_subword,
}
pre_entities.append(pre_entity)
return pre_entities
def aggregate(self, pre_entities: List[dict]) -> List[dict]:
entities = self.aggregate_words(pre_entities)
return self.group_entities(entities)
def aggregate_word(self, entities: List[dict]) -> dict:
word = self.tokenizer.convert_tokens_to_string([entity["word"] for entity in entities])
scores_seg = entities[0]["scores_seg"]
idx_seg = scores_seg.argmax()
score_seg = scores_seg[idx_seg]
entity_seg = self.id2seg[idx_seg]
scores_ref = np.stack([entity["scores_ref"] for entity in entities])
indices_ref = scores_ref.argmax(axis=1)
idx_ref = 1 if all(indices_ref) else 0
# score_ref = 1
entity_ref = self.id2ref[idx_ref]
new_entity = {
"entity_seg": entity_seg,
"score_seg": score_seg,
"entity_ref": entity_ref,
# "score_ref": score_ref,
"word": word,
"start": entities[0]["start"],
"end": entities[-1]["end"],
}
return new_entity
def aggregate_words(self, entities: List[dict]) -> List[dict]:
"""
Override tokens from a given word that disagree to force agreement on word boundaries.
Example: micro|soft| com|pany| B-ENT I-NAME I-ENT I-ENT will be rewritten with first strategy as microsoft|
company| B-ENT I-ENT
"""
word_entities = []
word_group = None
for entity in entities:
if word_group is None:
word_group = [entity]
elif entity["is_subword"]:
word_group.append(entity)
else:
word_entities.append(self.aggregate_word(word_group))
word_group = [entity]
word_entities.append(self.aggregate_word(word_group))
return word_entities
def group_entities(self, entities: List[dict]) -> List[dict]:
"""
Find and group together the adjacent tokens with the same entity predicted.
Args:
entities (`dict`): The entities predicted by the pipeline.
"""
entity_chunk = []
entity_chunk_disagg = []
for entity in entities:
if not entity_chunk_disagg:
entity_chunk_disagg.append(entity)
continue
bi_ref, tag_ref = self.get_tag(entity["entity_ref"])
last_bi_ref, last_tag_ref = self.get_tag(entity_chunk_disagg[-1]["entity_ref"])
if tag_ref == last_tag_ref and bi_ref != "B":
entity_chunk_disagg.append(entity)
else:
entity_chunk.append(entity_chunk_disagg)
entity_chunk_disagg = [entity]
if entity_chunk_disagg:
entity_chunk.append(entity_chunk_disagg)
entity_chunks_all = []
for chunk in entity_chunk:
entity_groups = []
entity_group_disagg = []
for entity in chunk:
if not entity_group_disagg:
entity_group_disagg.append(entity)
continue
bi_seg, tag_seg = self.get_tag(entity["entity_seg"])
last_bi_seg, last_tag_seg = self.get_tag(entity_group_disagg[-1]["entity_seg"])
if tag_seg == last_tag_seg and bi_seg != "B":
entity_group_disagg.append(entity)
else:
entity_groups.append(self.group_sub_entities(entity_group_disagg))
entity_group_disagg = [entity]
if entity_group_disagg:
entity_groups.append(self.group_sub_entities(entity_group_disagg))
entity_chunks_all.append(entity_groups)
return entity_chunks_all
def group_sub_entities(self, entities: List[dict]) -> dict:
"""
Group together the adjacent tokens with the same entity predicted.
Args:
entities (`dict`): The entities predicted by the pipeline.
"""
entity = entities[0]["entity_seg"].split("-")[-1]
scores = np.nanmean([entity["score_seg"] for entity in entities])
tokens = [entity["word"] for entity in entities]
entity_group = {
"entity_group": entity,
"score": np.mean(scores),
"word": " ".join(tokens),
"start": entities[0]["start"],
"end": entities[-1]["end"],
}
return entity_group
def get_tag(self, entity_name: str) -> Tuple[str, str]:
if entity_name.startswith("B-"):
bi = "B"
tag = entity_name[2:]
elif entity_name.startswith("I-"):
bi = "I"
tag = entity_name[2:]
else:
bi = "I"
tag = entity_name
return bi, tag
|