Upload QApipeline
Browse files- QAPipeline.py +52 -0
- config.json +1 -1
QAPipeline.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# qapipeline.py
|
| 2 |
+
|
| 3 |
+
from transformers import PreTrainedModel, Pipeline
|
| 4 |
+
from typing import Any, Dict
|
| 5 |
+
from transformers import Pipeline
|
| 6 |
+
from transformers import PreTrainedTokenizer
|
| 7 |
+
from transformers.utils import ModelOutput
|
| 8 |
+
|
| 9 |
+
from transformers import PreTrainedModel, Pipeline
|
| 10 |
+
from typing import Any, Dict, List
|
| 11 |
+
|
| 12 |
+
class QApipeline(Pipeline):
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
model: PreTrainedModel,
|
| 16 |
+
tokenizer: PreTrainedTokenizer,
|
| 17 |
+
**kwargs
|
| 18 |
+
):
|
| 19 |
+
super().__init__(
|
| 20 |
+
model=model,
|
| 21 |
+
**kwargs
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
print("in __init__")
|
| 25 |
+
|
| 26 |
+
def __call__( self, inputs: Dict[str, Any], **kwargs) -> Dict[str, Any]:
|
| 27 |
+
outputs = self.model.predict(inputs)
|
| 28 |
+
answer = self._process_output(outputs)
|
| 29 |
+
print("in __call___")
|
| 30 |
+
return answer
|
| 31 |
+
|
| 32 |
+
def _process_output(self, outputs: Any) -> str:
|
| 33 |
+
print("in process outputs")
|
| 34 |
+
format = {'guess': outputs[1], 'confidence': outputs[0]}
|
| 35 |
+
return format
|
| 36 |
+
|
| 37 |
+
def _sanitize_parameters(self, **kwargs):
|
| 38 |
+
print("in sanitize params")
|
| 39 |
+
return {}, {}, {}
|
| 40 |
+
|
| 41 |
+
def preprocess(self, inputs):
|
| 42 |
+
print("in preprocess")
|
| 43 |
+
return inputs
|
| 44 |
+
|
| 45 |
+
def postprocess(self, outputs):
|
| 46 |
+
print("in postprocess")
|
| 47 |
+
format = {'guess': outputs[1], 'confidence': float(outputs[0])}
|
| 48 |
+
return format
|
| 49 |
+
|
| 50 |
+
def _forward(self, input_tensors, **forward_parameters: Dict) -> ModelOutput:
|
| 51 |
+
print("in _forward")
|
| 52 |
+
return super()._forward(input_tensors, **forward_parameters)
|
config.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
| 9 |
},
|
| 10 |
"custom_pipelines": {
|
| 11 |
"demo-qa": {
|
| 12 |
-
"impl": "
|
| 13 |
"pt": [
|
| 14 |
"AutoModelForQuestionAnswering"
|
| 15 |
],
|
|
|
|
| 9 |
},
|
| 10 |
"custom_pipelines": {
|
| 11 |
"demo-qa": {
|
| 12 |
+
"impl": "QAPipeline.QApipeline",
|
| 13 |
"pt": [
|
| 14 |
"AutoModelForQuestionAnswering"
|
| 15 |
],
|