Spaces:
Build error
Build error
danseith
commited on
Commit
·
d5cc744
1
Parent(s):
fce4c33
Added custom pipeline with fixed temperature scale.
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from transformers import pipeline, Pipeline
|
|
|
|
|
|
|
| 4 |
|
| 5 |
unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")
|
| 6 |
-
|
| 7 |
example = 'A crustless [MASK] made from two slices of baked bread'
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
def add_mask(text, size=1):
|
| 11 |
split_text = text.split()
|
|
@@ -15,17 +19,54 @@ def add_mask(text, size=1):
|
|
| 15 |
return ' '.join(split_text)
|
| 16 |
|
| 17 |
|
| 18 |
-
class TempScalePipe(
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
def unmask(text):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
from transformers import pipeline, Pipeline
|
| 5 |
+
from transformers.pipelines import PIPELINE_REGISTRY, FillMaskPipeline
|
| 6 |
+
from transformers import AutoConfig, AutoModel, AutoModelForMaskedLM
|
| 7 |
|
| 8 |
unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")
|
| 9 |
+
# unmasker = pipeline("temp-scale", model="anferico/bert-for-patents")
|
| 10 |
example = 'A crustless [MASK] made from two slices of baked bread'
|
| 11 |
+
example_dict = {}
|
| 12 |
+
example_dict['input_ids'] = example
|
| 13 |
|
| 14 |
def add_mask(text, size=1):
|
| 15 |
split_text = text.split()
|
|
|
|
| 19 |
return ' '.join(split_text)
|
| 20 |
|
| 21 |
|
| 22 |
+
class TempScalePipe(FillMaskPipeline):
|
| 23 |
+
def postprocess(self, model_outputs, top_k=5, target_ids=None):
|
| 24 |
+
# Cap top_k if there are targets
|
| 25 |
+
if target_ids is not None and target_ids.shape[0] < top_k:
|
| 26 |
+
top_k = target_ids.shape[0]
|
| 27 |
+
input_ids = model_outputs["input_ids"][0]
|
| 28 |
+
outputs = model_outputs["logits"]
|
| 29 |
+
|
| 30 |
+
masked_index = torch.nonzero(input_ids == self.tokenizer.mask_token_id, as_tuple=False).squeeze(-1)
|
| 31 |
+
# Fill mask pipeline supports only one ${mask_token} per sample
|
| 32 |
+
|
| 33 |
+
logits = outputs[0, masked_index, :] / 1e3
|
| 34 |
+
probs = logits.softmax(dim=-1)
|
| 35 |
+
if target_ids is not None:
|
| 36 |
+
probs = probs[..., target_ids]
|
| 37 |
+
|
| 38 |
+
values, predictions = probs.topk(top_k)
|
| 39 |
+
|
| 40 |
+
result = []
|
| 41 |
+
single_mask = values.shape[0] == 1
|
| 42 |
+
for i, (_values, _predictions) in enumerate(zip(values.tolist(), predictions.tolist())):
|
| 43 |
+
row = []
|
| 44 |
+
for v, p in zip(_values, _predictions):
|
| 45 |
+
# Copy is important since we're going to modify this array in place
|
| 46 |
+
tokens = input_ids.numpy().copy()
|
| 47 |
+
if target_ids is not None:
|
| 48 |
+
p = target_ids[p].tolist()
|
| 49 |
+
|
| 50 |
+
tokens[masked_index[i]] = p
|
| 51 |
+
# Filter padding out:
|
| 52 |
+
tokens = tokens[np.where(tokens != self.tokenizer.pad_token_id)]
|
| 53 |
+
# Originally we skip special tokens to give readable output.
|
| 54 |
+
# For multi masks though, the other [MASK] would be removed otherwise
|
| 55 |
+
# making the output look odd, so we add them back
|
| 56 |
+
sequence = self.tokenizer.decode(tokens, skip_special_tokens=single_mask)
|
| 57 |
+
proposition = {"score": v, "token": p, "token_str": self.tokenizer.decode([p]), "sequence": sequence}
|
| 58 |
+
row.append(proposition)
|
| 59 |
+
result.append(row)
|
| 60 |
+
if single_mask:
|
| 61 |
+
return result[0]
|
| 62 |
+
return result
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
PIPELINE_REGISTRY.register_pipeline(
|
| 66 |
+
"temp-scale",
|
| 67 |
+
pipeline_class=TempScalePipe,
|
| 68 |
+
pt_model=AutoModelForMaskedLM,
|
| 69 |
+
)
|
| 70 |
|
| 71 |
|
| 72 |
def unmask(text):
|