Upload MyTestPipeline
Browse files- new_task.py +5 -5
new_task.py
CHANGED
|
@@ -2,10 +2,11 @@ from transformers import Text2TextGenerationPipeline, AutoModelForSeq2SeqLM, TFA
|
|
| 2 |
import torch
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
class MyTestPipeline(TextGenerationPipeline):
|
| 7 |
def preprocess(self, text, **kwargs):
|
| 8 |
-
prompt = 'Answer the following question/statement without any explanation, do not abbreviate names.'
|
| 9 |
txt = f"<|user|>\n{prompt} {text}\n<|end|>\n<|assistant|>"
|
| 10 |
return self.tokenizer(txt, return_tensors=self.framework)
|
| 11 |
|
|
@@ -15,7 +16,7 @@ class MyTestPipeline(TextGenerationPipeline):
|
|
| 15 |
elif self.framework == "tf":
|
| 16 |
in_b, input_length = tf.shape(model_inputs["input_ids"]).numpy()
|
| 17 |
|
| 18 |
-
outputs = self.model.generate(**model_inputs, **generate_kwargs, return_dict_in_generate=True, output_scores=True
|
| 19 |
|
| 20 |
output_ids = outputs.sequences
|
| 21 |
out_b = output_ids.shape[0]
|
|
@@ -35,8 +36,7 @@ class MyTestPipeline(TextGenerationPipeline):
|
|
| 35 |
log_probs = np.round(np.exp(transition_scores.cpu().numpy()), 3)[0]
|
| 36 |
guess_prob = np.product(log_probs)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
guess_prob = 1.0
|
| 40 |
|
| 41 |
-
return {'guess': guess_text, 'confidence':
|
| 42 |
|
|
|
|
| 2 |
import torch
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
+
import math
|
| 6 |
|
| 7 |
class MyTestPipeline(TextGenerationPipeline):
|
| 8 |
def preprocess(self, text, **kwargs):
|
| 9 |
+
prompt = 'Answer the following question/statement in English without any explanation, do not abbreviate names.'
|
| 10 |
txt = f"<|user|>\n{prompt} {text}\n<|end|>\n<|assistant|>"
|
| 11 |
return self.tokenizer(txt, return_tensors=self.framework)
|
| 12 |
|
|
|
|
| 16 |
elif self.framework == "tf":
|
| 17 |
in_b, input_length = tf.shape(model_inputs["input_ids"]).numpy()
|
| 18 |
|
| 19 |
+
outputs = self.model.generate(**model_inputs, **generate_kwargs, return_dict_in_generate=True, output_scores=True)
|
| 20 |
|
| 21 |
output_ids = outputs.sequences
|
| 22 |
out_b = output_ids.shape[0]
|
|
|
|
| 36 |
log_probs = np.round(np.exp(transition_scores.cpu().numpy()), 3)[0]
|
| 37 |
guess_prob = np.product(log_probs)
|
| 38 |
|
| 39 |
+
confidence = (math.exp(12*(guess_prob - 0.5))) / (1 + math.exp(12 * (guess_prob - 0.5)))
|
|
|
|
| 40 |
|
| 41 |
+
return {'guess': guess_text, 'confidence': confidence}
|
| 42 |
|