tan-ta / handler.py
Deepakvictor's picture
Update handler.py
a1a19b0
raw
history blame
697 Bytes
from typing import Dict, List, Any
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
class EndpointHandler():
def __init__(self, path=""):
self.tokenizer = AutoTokenizer.from_pretrained(path)
self.model = AutoModelForSeq2SeqLM.from_pretrained(path)
def __call__(self, data: str) -> str:
inputs = data.pop("inputs", data)
parameters = data.pop("parameters", None)
inp = self.tokenizer(inputs, return_tensors="pt")
with torch.inference_mode():
out= self.model.generate(**inp)
final_output = self.tokenizer.batch_decode(out,skip_special_tokens=True)
return {"translation": final_output[0]}