|
from typing import Dict, List, Any |
|
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification |
|
import torch |
|
|
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
guider_config = AutoConfig.from_pretrained(path) |
|
self.model = AutoModelForSequenceClassification.from_pretrained(path, config=guider_config) |
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: |
|
""" |
|
Args: |
|
data (:dict:): |
|
The payload with the text prompt. |
|
""" |
|
|
|
gen_outputs_no_input_decoded = data.pop("gen_outputs_no_input_decoded", data) |
|
|
|
|
|
guider_inputs = self.tokenizer([gen_output_no_input_decoded for gen_output_no_input_decoded in gen_outputs_no_input_decoded], |
|
return_tensors='pt', padding=True, truncation=True) |
|
|
|
guider_outputs = self.model(**guider_inputs) |
|
|
|
|
|
guider_predictions = torch.nn.functional.softmax(guider_outputs.logits, dim=-1)[:, 0].tolist() |
|
return {"guider_predictions": guider_predictions} |