Modelo Joint BERT multi-head para clasificación de intención y slot filling,
especializado en consultas sobre indicadores macroeconómicos del Banco Central de Chile.
1import torch
2from transformers import AutoTokenizer, AutoConfig
34# Cargar tokenizer y config5tokenizer = AutoTokenizer.from_pretrained("BCCh/pibert", trust_remote_code=True)6config = AutoConfig.from_pretrained("BCCh/pibert", trust_remote_code=True)78# Cargar labels desde el repo9from huggingface_hub import hf_hub_download
10import os
1112label_dir = os.path.dirname(hf_hub_download("BCCh/pibert","labels/slot_label.txt"))1314# Leer intent y slot labels15defread_labels(path):16withopen(path)as f:17return[line.strip()for line in f if line.strip()]1819slot_labels = read_labels(os.path.join(label_dir,"slot_label.txt"))2021# Preparar intent_label_lst para cada head22intent_label_lst =[]23for head in['activity','calc_mode','investment','region','req_form']:24 intent_label_lst.append(read_labels(os.path.join(label_dir,f"{head}_label.txt")))2526# Cargar modelo con custom code27from transformers import AutoModelForTokenClassification
28from modeling_jointbert import JointBERT # auto-cargado con trust_remote_code2930model = JointBERT.from_pretrained(31"BCCh/pibert",32 config=config,33 intent_label_lst=intent_label_lst,34 slot_label_lst=slot_labels,35 trust_remote_code=True,36)37model.eval()
Predicción
python
1text ="cuál fue el imacec de agosto 2024"2tokens = tokenizer(text, return_tensors="pt", padding=True, truncation=True)34with torch.no_grad():5 outputs = model(**tokens)6# outputs contiene intent_logits (lista) y slot_logits