File size: 1,498 Bytes
8c0b646
 
13f7861
8c0b646
 
 
fce4c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bf7c7e
8c0b646
3566540
8c0b646
 
 
 
 
3566540
8c0b646
3566540
13f7861
8c0b646
b5b3297
38bdfc6
 
 
3566540
38bdfc6
 
 
 
 
 
 
 
 
 
 
8c0b646
 
 
 
3566540
8c0b646
 
3566540
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
import gradio as gr
import numpy as np
from transformers import pipeline, Pipeline

unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")

example = 'A crustless [MASK] made from two slices of baked bread'


def add_mask(text, size=1):
    split_text = text.split()
    idx = np.random.randint(len(split_text), size=size)
    for i in idx:
        split_text[i] = '[MASK]'
    return ' '.join(split_text)


class TempScalePipe(Pipeline):
    def _forward(self, model_inputs):
        outputs = self.model(**model_inputs)
        return outputs

    def postprocess(self, model_outputs, temp=1e3):
        out = model_outputs["logits"] / temp
        best_class = out.softmax(-1)
        print(out)
        probas = np.random.multinomial(1, best_class, 1)
        return np.argmax(probas)


def unmask(text):
    # text = add_mask(text)
    res = unmasker(text)
    out = {item["token_str"]: item["score"] for item in res}
    return out



textbox = gr.Textbox(label="Type language here", lines=5)
# import gradio as gr
from transformers import pipeline, Pipeline


# unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")
#
#

#
#
# def unmask(text):
#     text = add_mask(text)
#     res = unmasker(text)
#     out = {item["token_str"]: item["score"] for item in res}
#     return out
#
#
# textbox = gr.Textbox(label="Type language here", lines=5)
#
demo = gr.Interface(
    fn=unmask,
    inputs=textbox,
    outputs="label",
    examples=[example],
)

demo.launch()