AndresR2909's picture
Update handler.py
133bed4 verified
from typing import Dict, List, Any
from transformers import pipeline
import logging
# Configuración básica del logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class EndpointHandler():
def __init__(self, path=""):
logger.info(f"Modelo cargado desde el path: {path}")
# Cargar el pipeline con el modelo especificado
try:
self.pipeline = pipeline(model=path, truncation=True)
except Exception as e:
logger.exception(f"Error cargando el modelo desde el path {path}: {e}")
path = "AndresR2909/finetuning-bert-text-classification"
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)
# Textos para análisis
texts = [input]
# Realizar la inferencia
outputs = self.pipeline(texts)
# 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(outputs, texts)
]
return adjusted_results