import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizer base = "Qwen/Qwen3-1.7B-Base"tok = AutoTokenizer.from_pretrained(base)model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(base, dtype=torch.bfloat16, device_map="auto"), "christophe-ringot/p14-triage-qwen3-1.7b-lora").eval() labels = ["urgence_maximale", "urgence_moderee", "differee"]cost = torch.tensor([[0, 4, 10], [1, 0, 4], [2, 1, 0]], dtype=torch.float)cost = torch.where(torch.triu(torch.ones(3, 3), 1) > 0, cost * 0.5, cost) # alpha = 0.5 on under-triageprompt = "### Cas clinique :\n<case>\n\n### Question :\n<question>\n\n### Réponse :\n[Niveau de priorité estimé :" def logp(label): # Context and continuation are tokenized apart, as in the evaluation. context = tok(prompt, add_special_tokens=False).input_ids continuation = tok(f" {label}]", add_special_tokens=False).input_ids ids = torch.tensor([context + continuation], device=model.device) with torch.no_grad(): logits = model(input_ids=ids).logits[0, len(context) - 1:-1].float().log_softmax(-1) return logits.gather(1, torch.tensor(continuation, device=model.device).unsqueeze(1)).sum().item() scores = torch.tensor([logp(l) for l in labels])proba = (scores / 2.25).softmax(-1) # temperature fitted on validationlevel = labels[int((proba @ cost).argmin())] # minimum expected cost