import re from typing import Dict, List, Any from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline import torch class EndpointHandler(): def __init__(self, path=""): # Preload all the elements you are going to need at inference. # pseudo: # self.model= load_model(path) # Load the fine-tuned model self.date_model_path = path + "/deberta-qa-finetuned" self.date_tokenizer = AutoTokenizer.from_pretrained(self.date_model_path) self.date_model = AutoModelForQuestionAnswering.from_pretrained(self.date_model_path) self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.date_model.to(self.device) def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: """ data args: inputs (:obj: `str` | `PIL.Image` | `np.array`) kwargs Return: A :obj:`list` | `dict`: will be serialized and returned """ start_date = self.remove_special_characters(self.extract_start_date(data["inputs"])) end_date = self.remove_special_characters(self.extract_end_date(data["inputs"])) return {"start_date": start_date, "end_date": end_date} def remove_special_characters(self, s): return re.sub(r'(?