|
from typing import Dict, List, Any |
|
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline |
|
import torch |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.model = AutoModelForSeq2SeqLM.from_pretrained(path) |
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]: |
|
""" |
|
Args: |
|
data (:obj:): |
|
Includes the deserialized image file as PIL.Image |
|
""" |
|
|
|
inputs = data.pop("inputs", data) |
|
parameters = data.pop("parameters", None) |
|
|
|
|
|
input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids |
|
|
|
|
|
if parameters is None: |
|
parameters = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
outputs = self.model.generate(input_ids, **parameters) |
|
|
|
|
|
prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
return [{"generated_text": prediction}] |