File size: 925 Bytes
fb274e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from typing import Dict, List, Any
from transformers import pipeline, AutoTokenizer, BartForConditionalGeneration
class EndpointHandler():
def __init__(self, path=""):
self.model = BartForConditionalGeneration.from_pretrained(path)
self.tokenizer = AutoTokenizer(path)
def __call__(self, data: str) -> str:
"""
data args:
inputs (:obj: `str`)
date (:obj: `str`)
Return:
A :obj:`list` | `dict`: will be serialized and returned
"""
# get inputs
text_tokenized = self.tokenizer(
[data], padding="max_length", truncation=True, max_length=1024,return_tensors='pt')
prediction_token = self.model.generate(text_tokenized["input_ids"], max_length = 256, num_beams = 6)
prediction_summary = self.tokenizer.decode(prediction_token[0][2:-1:1])
return prediction_summary |