import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
base_model_name = "Qwen/Qwen2.5-7B-Instruct"
adapter_path = "diegogs1451/qwen2.5-7B-Instruct-dUO-finetuned-20260706-3epochs"
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
model = AutoModelForCausalLM.from_pretrained(
base_model_name,
torch_dtype=torch.bfloat16,
device_map="auto"
)
model = PeftModel.from_pretrained(model, adapter_path)
system_prompt = (
"Eres un asistente experto en resumir textos oficiales administrativos y jurídicos "
"del DOUE (Diario Oficial de la Unión Europea), del BOE (Boletín Oficial del Estado) "
"y del BOPA (Boletín Oficial del Principado de Asturias) en títulos breves, claros, "
"formales y listos para publicación. Conserva el significado administrativo esencial "
"eliminando detalles innecesarios y redundancias."
)
def generate_title(text):
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": text},
]
inputs = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
outputs = model.generate(inputs, max_new_tokens=128, temperature=0.1)
return tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)