File size: 1,518 Bytes
23f2bbc cd64bca 23f2bbc |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from typing import Dict, List, Any
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
import torch
class EndpointHandler:
def __init__(self, path=""):
# Load model and processor from 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
"""
# Process input
inputs = data.pop("inputs", data)
parameters = data.pop("parameters", None)
# Preprocess
input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids
# Modify parameters to increase max_length
if parameters is None:
parameters = {}
# parameters['max_length'] = 512 # Set your desired max_length here
# parameters['min_length'] = 100
# parameters['length_penalty'] = 2.0
# parameters['num_beams'] = 10
# parameters['early_stopping'] = True
# parameters['temperature'] = 0.0
# parameters['top_k'] = 10
# parameters['top_p'] = 1.0
# Generate output
outputs = self.model.generate(input_ids, **parameters)
# Postprocess the prediction
prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return [{"generated_text": prediction}] |