from typing import Dict, List, Any from transformers import pipeline class EndpointHandler(): def __init__(self, path=""): self.pipeline = pipeline(model=path, truncation=True,) def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: """ data args: inputs (:obj: `str`) date (:obj: `str`) Return: A :obj:`list` | `dict`: will be serialized and returned """ # get inputs input = data.get("inputs",data) date = data.get("date", None) # Realizar la inferencia outputs = self.pipeline([input]) # Ajustar las etiquetas a 0 y 1 label_mapping = {"LABEL_0": 0, "LABEL_1": 1} label_names = {0: "sin_intencion", 1: "intencion_suicida"} # Modificar las salidas con el formato deseado adjusted_results = [ { "input": text, "clasiffication": str(label_mapping[result['label']]), # "clasiffication" como string "label": label_names[label_mapping[result['label']]] # Mapeo de etiquetas a nombres } for result, text in zip(results, texts) ] return adjusted_results