|
|
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 |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
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 |