|
from typing import Dict, List, Any |
|
from transformers import pipeline |
|
import 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}") |
|
|
|
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 |
|
""" |
|
|
|
input = data.get("inputs",data) |
|
date = data.get("date", None) |
|
|
|
|
|
texts = [input] |
|
|
|
outputs = self.pipeline(texts) |
|
|
|
|
|
label_mapping = {"LABEL_0": 0, "LABEL_1": 1} |
|
label_names = {0: "sin_intencion", 1: "intencion_suicida"} |
|
|
|
|
|
adjusted_results = [ |
|
{ |
|
"input": text, |
|
"clasiffication": str(label_mapping[result['label']]), |
|
"label": label_names[label_mapping[result['label']]] |
|
} |
|
for result, text in zip(outputs, texts) |
|
] |
|
return adjusted_results |