Xaver Maria Krückl commited on
Upload folder using huggingface_hub
Browse files- modeling_xlmedu.py +54 -6
modeling_xlmedu.py
CHANGED
|
@@ -9,12 +9,11 @@ Loading from the Hub (recommended):
|
|
| 9 |
config = AutoConfig.from_pretrained("xaver-krueckl/situation-entity-segmenter", trust_remote_code=True)
|
| 10 |
model = AutoModel.from_pretrained("xaver-krueckl/situation-entity-segmenter", trust_remote_code=True)
|
| 11 |
|
| 12 |
-
Inference:
|
| 13 |
from transformers import AutoTokenizer
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained("FacebookAI/xlm-roberta-large")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
tags = [model.config.id2label[i] for i in tag_ids[0]]
|
| 18 |
"""
|
| 19 |
|
| 20 |
from __future__ import annotations
|
|
@@ -104,8 +103,57 @@ class XLMEduModelHF(PreTrainedModel):
|
|
| 104 |
Returns one integer tag per sub-token (including special tokens).
|
| 105 |
During training, non-first sub-tokens were masked (-100 → O), so their
|
| 106 |
predictions are meaningless. Callers should use encoding.word_ids() to
|
| 107 |
-
read only the first sub-token of each word.
|
| 108 |
-
complete inference example.
|
| 109 |
"""
|
| 110 |
emissions = self.forward(input_ids, attention_mask)["logits"]
|
| 111 |
return self.crf.decode(emissions.float(), mask=attention_mask.bool())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
config = AutoConfig.from_pretrained("xaver-krueckl/situation-entity-segmenter", trust_remote_code=True)
|
| 10 |
model = AutoModel.from_pretrained("xaver-krueckl/situation-entity-segmenter", trust_remote_code=True)
|
| 11 |
|
| 12 |
+
Inference (one pre-split sentence):
|
| 13 |
from transformers import AutoTokenizer
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained("FacebookAI/xlm-roberta-large")
|
| 15 |
+
words = ["The", "cat", "sat", "."]
|
| 16 |
+
pairs = model.predict_text(words, tokenizer) # [(word, tag), ...]
|
|
|
|
| 17 |
"""
|
| 18 |
|
| 19 |
from __future__ import annotations
|
|
|
|
| 103 |
Returns one integer tag per sub-token (including special tokens).
|
| 104 |
During training, non-first sub-tokens were masked (-100 → O), so their
|
| 105 |
predictions are meaningless. Callers should use encoding.word_ids() to
|
| 106 |
+
read only the first sub-token of each word.
|
|
|
|
| 107 |
"""
|
| 108 |
emissions = self.forward(input_ids, attention_mask)["logits"]
|
| 109 |
return self.crf.decode(emissions.float(), mask=attention_mask.bool())
|
| 110 |
+
|
| 111 |
+
def predict_text(
|
| 112 |
+
self,
|
| 113 |
+
words: List[str],
|
| 114 |
+
tokenizer,
|
| 115 |
+
) -> List[tuple]:
|
| 116 |
+
"""Tag a single pre-tokenised sentence.
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
words: word-level tokens for one sentence (e.g. from spaCy).
|
| 120 |
+
tokenizer: the HuggingFace tokenizer for this model.
|
| 121 |
+
|
| 122 |
+
Returns:
|
| 123 |
+
List of (word, tag) pairs, one per input word.
|
| 124 |
+
"""
|
| 125 |
+
from collections import defaultdict
|
| 126 |
+
|
| 127 |
+
encoding = tokenizer(
|
| 128 |
+
words,
|
| 129 |
+
return_tensors="pt",
|
| 130 |
+
is_split_into_words=True,
|
| 131 |
+
truncation=True,
|
| 132 |
+
max_length=512,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
with torch.no_grad():
|
| 136 |
+
raw_tag_ids = self.predict(
|
| 137 |
+
encoding["input_ids"], encoding["attention_mask"]
|
| 138 |
+
)[0]
|
| 139 |
+
|
| 140 |
+
word_ids = encoding.word_ids(batch_index=0)
|
| 141 |
+
word_token_ids: dict = defaultdict(list)
|
| 142 |
+
for pos, word_idx in enumerate(word_ids):
|
| 143 |
+
if word_idx is not None:
|
| 144 |
+
word_token_ids[word_idx].append(
|
| 145 |
+
encoding["input_ids"][0, pos].item()
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
results: List[tuple] = []
|
| 149 |
+
prev_word_idx = None
|
| 150 |
+
for pos, word_idx in enumerate(word_ids):
|
| 151 |
+
if word_idx is None:
|
| 152 |
+
continue
|
| 153 |
+
if word_idx != prev_word_idx:
|
| 154 |
+
word = tokenizer.decode(word_token_ids[word_idx]).strip()
|
| 155 |
+
tag = self.config.id2label[raw_tag_ids[pos]]
|
| 156 |
+
results.append((word, tag))
|
| 157 |
+
prev_word_idx = word_idx
|
| 158 |
+
|
| 159 |
+
return results
|