|
from typing import Dict, List, Any |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification,pipeline |
|
from transformers import pipeline |
|
import deepspeed |
|
|
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(path) |
|
model = AutoModelForSequenceClassification.from_pretrained(path) |
|
|
|
|
|
ds_model = deepspeed.init_inference( |
|
model=model, |
|
mp_size=1, |
|
dtype=torch.half, |
|
|
|
replace_method="auto", |
|
replace_with_kernel_inject=True, |
|
) |
|
|
|
|
|
self.pipeline = pipeline("text-classification", model=ds_model, tokenizer=tokenizer, device=0) |
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (:obj: `str`) |
|
date (:obj: `str`) |
|
Return: |
|
A :obj:`list` | `dict`: will be serialized and returned |
|
""" |
|
inputs = data.pop("inputs", data) |
|
parameters = data.pop("parameters", None) |
|
|
|
|
|
if parameters is not None: |
|
prediction = self.pipeline(inputs, **parameters) |
|
else: |
|
prediction = self.pipeline(inputs) |
|
|
|
return prediction |