danseith commited on
Commit
fce4c33
·
1 Parent(s): 3566540

Added TempScalePipe back in

Browse files
Files changed (1) hide show
  1. app.py +23 -20
app.py CHANGED
@@ -4,26 +4,29 @@ from transformers import pipeline, Pipeline
4
 
5
  unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")
6
 
7
- example = 'A crustless sandwich made from two slices of baked bread'
8
-
9
- # def add_mask(text, size=1):
10
- # split_text = text.split()
11
- # idx = np.random.randint(len(split_text), size=size)
12
- # for i in idx:
13
- # split_text[i] = '[MASK]'
14
- # return ' '.join(split_text)
15
-
16
- # class TempScalePipe(Pipeline):
17
- # def _forward(self, model_inputs):
18
- # outputs = self.model(**model_inputs)
19
- # return outputs
20
- #
21
- # def postprocess(self, model_outputs, temp=1e3):
22
- # out = model_outputs["logits"] / temp
23
- # idx = np.random.randint(-3, 0)
24
- # best_class = out.softmax(idx)
25
- # return best_class
26
- #
 
 
 
27
 
28
  def unmask(text):
29
  # text = add_mask(text)
 
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()
12
+ idx = np.random.randint(len(split_text), size=size)
13
+ for i in idx:
14
+ split_text[i] = '[MASK]'
15
+ return ' '.join(split_text)
16
+
17
+
18
+ class TempScalePipe(Pipeline):
19
+ def _forward(self, model_inputs):
20
+ outputs = self.model(**model_inputs)
21
+ return outputs
22
+
23
+ def postprocess(self, model_outputs, temp=1e3):
24
+ out = model_outputs["logits"] / temp
25
+ best_class = out.softmax(-1)
26
+ print(out)
27
+ probas = np.random.multinomial(1, best_class, 1)
28
+ return np.argmax(probas)
29
+
30
 
31
  def unmask(text):
32
  # text = add_mask(text)