Download handler.py from philschmid/roberta-base-squad2-optimized: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/philschmid/roberta-base-squad2-optimized/resolve/main/handler.py
- Command line
-
hf download hf://philschmid/roberta-base-squad2-optimized/handler.py
-
curl -L -o handler.py https://huggingface.co/philschmid/roberta-base-squad2-optimized/resolve/main/handler.py
1.03 kB
| from typing import Dict, List, Any | |
| from optimum.onnxruntime import ORTModelForQuestionAnswering | |
| from transformers import AutoTokenizer, pipeline | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| # load the optimized model | |
| self.model = ORTModelForQuestionAnswering.from_pretrained(path, file_name="model_optimized_quantized.onnx") | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| # create pipeline | |
| self.pipeline = pipeline("question-answering", model=self.model, tokenizer=self.tokenizer) | |
| def __call__(self, data: Any) -> List[List[Dict[str, float]]]: | |
| """ | |
| Args: | |
| data (:obj:): | |
| includes the input data and the parameters for the inference. | |
| Return: | |
| A :obj:`list`:. The list contains the answer and scores of the inference inputs | |
| """ | |
| inputs = data.get("inputs", data) | |
| # run the model | |
| prediction = self.pipeline(**inputs) | |
| # return prediction | |
| return prediction | |