Instructions to use raulgdp/roberta-large-ner-qlorafinetune with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use raulgdp/roberta-large-ner-qlorafinetune with PEFT:
from peft import PeftModel from transformers import AutoModelForTokenClassification base_model = AutoModelForTokenClassification.from_pretrained("FacebookAI/xlm-roberta-large") model = PeftModel.from_pretrained(base_model, "raulgdp/roberta-large-ner-qlorafinetune") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -27,7 +27,125 @@ More information needed
|
|
| 27 |
|
| 28 |
## Intended uses & limitations
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
## Training and evaluation data
|
| 33 |
|
|
|
|
| 27 |
|
| 28 |
## Intended uses & limitations
|
| 29 |
|
| 30 |
+
Para usar el modelo y tratar de que las etiquetas funcionen correctamente les comparto un script que usa el modelo
|
| 31 |
+
como pipeline:
|
| 32 |
+
from transformers import AutoConfig, AutoTokenizer, AutoModelForTokenClassification
|
| 33 |
+
from peft import PeftModel
|
| 34 |
+
import torch
|
| 35 |
+
|
| 36 |
+
# Paso 1: Configuración manual del modelo base
|
| 37 |
+
base_model_name = "xlm-roberta-large" # Usar nombre directo para evitar conflictos
|
| 38 |
+
num_labels = 9 # Total de clases NER (BIO)
|
| 39 |
+
|
| 40 |
+
# Definir mapeo de etiquetas (ajustar según tu dataset)
|
| 41 |
+
id2label = {
|
| 42 |
+
0: "O",
|
| 43 |
+
1: "B-PER",
|
| 44 |
+
2: "I-PER",
|
| 45 |
+
3: "B-ORG",
|
| 46 |
+
4: "I-ORG",
|
| 47 |
+
5: "B-LOC",
|
| 48 |
+
6: "I-LOC",
|
| 49 |
+
7: "B-MISC",
|
| 50 |
+
8: "I-MISC"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Cargar configuración del modelo BASE y modificar
|
| 54 |
+
config = AutoConfig.from_pretrained(
|
| 55 |
+
base_model_name,
|
| 56 |
+
num_labels=num_labels,
|
| 57 |
+
id2label=id2label,
|
| 58 |
+
label2id={v: k for k, v in id2label.items()}
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Paso 2: Cargar modelo base CON CONFIGURACIÓN CORRECTA
|
| 62 |
+
base_model = AutoModelForTokenClassification.from_pretrained(
|
| 63 |
+
base_model_name,
|
| 64 |
+
config=config,
|
| 65 |
+
ignore_mismatched_sizes=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Paso 3: Cargar adaptador PEFT
|
| 69 |
+
peft_model_id = "raulgdp/roberta-large-ner-qlorafinetune"
|
| 70 |
+
model = PeftModel.from_pretrained(base_model, peft_model_id)
|
| 71 |
+
model = model.merge_and_unload()
|
| 72 |
+
|
| 73 |
+
# Paso 4: Configurar tokenizador CORRECTAMENTE
|
| 74 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 75 |
+
base_model_name, # Usar el modelo base directamente
|
| 76 |
+
add_prefix_space=True, # Necesario para RoBERTa
|
| 77 |
+
use_fast=True
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Función mejorada de decodificación
|
| 81 |
+
def decode_predictions(text, predictions, word_ids):
|
| 82 |
+
current_word = ""
|
| 83 |
+
current_label = ""
|
| 84 |
+
previous_word_id = None
|
| 85 |
+
results = []
|
| 86 |
+
|
| 87 |
+
for idx, word_id in enumerate(word_ids):
|
| 88 |
+
if word_id is None:
|
| 89 |
+
continue
|
| 90 |
+
|
| 91 |
+
# Obtener token y etiqueta
|
| 92 |
+
token = tokenizer.decode(inputs["input_ids"][0][idx]).replace(" ", "")
|
| 93 |
+
label = model.config.id2label[predictions[idx]]
|
| 94 |
+
|
| 95 |
+
# Manejar subpalabras
|
| 96 |
+
if word_id != previous_word_id:
|
| 97 |
+
if previous_word_id is not None:
|
| 98 |
+
results.append((current_word.strip(), current_label))
|
| 99 |
+
current_word = token
|
| 100 |
+
current_label = label
|
| 101 |
+
else:
|
| 102 |
+
current_word += token.replace("##", "").replace("Ġ", "")
|
| 103 |
+
|
| 104 |
+
previous_word_id = word_id
|
| 105 |
+
|
| 106 |
+
if current_word:
|
| 107 |
+
results.append((current_word.strip(), current_label))
|
| 108 |
+
|
| 109 |
+
return results
|
| 110 |
+
|
| 111 |
+
# Texto de prueba
|
| 112 |
+
text = (
|
| 113 |
+
"La Federación Nacional de Cafeteros de Colombia es una entidad del estado, "
|
| 114 |
+
"creada en los años 70’s. El primer presidente el Dr Augusto Guerra contó con "
|
| 115 |
+
"el aval de la Asociación Colombiana de Aviación. En varias ciudades "
|
| 116 |
+
"colombianas; Cali, Medellín, Corozal funciona la entidad. Estas personas "
|
| 117 |
+
"vienen del Instituto Colombiano del Café ubicado en la calle Cali"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Tokenización profesional
|
| 121 |
+
inputs = tokenizer(
|
| 122 |
+
text,
|
| 123 |
+
return_tensors="pt",
|
| 124 |
+
truncation=True,
|
| 125 |
+
padding="max_length",
|
| 126 |
+
max_length=128,
|
| 127 |
+
return_offsets_mapping=False
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# Obtener word_ids CORRECTAMENTE
|
| 131 |
+
word_ids = [i if i is not None else -1 for i in inputs.word_ids(0)]
|
| 132 |
+
|
| 133 |
+
# Inferencia
|
| 134 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 135 |
+
model = model.to(device)
|
| 136 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 137 |
+
|
| 138 |
+
with torch.no_grad():
|
| 139 |
+
outputs = model(**inputs)
|
| 140 |
+
predictions = torch.argmax(outputs.logits, dim=-1)[0].cpu().numpy()
|
| 141 |
+
|
| 142 |
+
# Decodificar resultados
|
| 143 |
+
entities = decode_predictions(text, predictions, word_ids)
|
| 144 |
+
|
| 145 |
+
# Mostrar solo entidades relevantes
|
| 146 |
+
for word, label in entities:
|
| 147 |
+
if label != "O":
|
| 148 |
+
print(f"{word:35} {label}")
|
| 149 |
|
| 150 |
## Training and evaluation data
|
| 151 |
|