Views
No views yet
Nota: es un causal LM entrenado para responder un solo dígito (0/1). Usa el chat template de Gemma y (opcionalmente) restringe la generación a {0,1}.
| split | accuracy | precision | recall | f1 | roc_auc |
|---|---|---|---|---|---|
| val | 0.8826 | 0.7985 | 0.9817 | 0.8807 | 0.9391 |
| split | accuracy | precision | recall | f1 | roc_auc |
|---|---|---|---|---|---|
| test | 0.8450 | 0.7397 | 0.9818 | 0.8438 | 0.8868 |
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3from transformers.generation.logits_process import LogitsProcessor, LogitsProcessorList
4
5repo_id = "Cristian11212/gemma3-1b-plaintech-original-20250921-004120"
6tokenizer = AutoTokenizer.from_pretrained(repo_id, use_fast=True)
7model = AutoModelForCausalLM.from_pretrained(repo_id, torch_dtype=torch.float16 if torch.cuda.is_available() else None)
8model.eval().to("cuda" if torch.cuda.is_available() else "cpu")
9
10SYS = "You are a binary classifier. Reply with a single digit: 0 (plain) or 1 (technical). No extra text."
11def build_prompt(text):
12 msgs = [{"role":"system","content":SYS}, {"role":"user","content":text}]
13 return tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
14
15zero_id = tokenizer.encode("0", add_special_tokens=False)[0]
16one_id = tokenizer.encode("1", add_special_tokens=False)[0]
17class OnlyAllowTokens(LogitsProcessor):
18 def __init__(self, allowed, device): self.allowed = torch.tensor(allowed, device=device)
19 def __call__(self, input_ids, scores):
20 mask = torch.full_like(scores, float("-inf")); mask[:, self.allowed] = 0.0; return scores + mask
21lp = LogitsProcessorList([OnlyAllowTokens([zero_id, one_id], model.device)])
22
23text = "urinary incontinence is the inability to willingly control bladder voiding..."
24enc = tokenizer([build_prompt(text)], return_tensors="pt").to(model.device)
25out = model.generate(**enc, max_new_tokens=1, do_sample=False, output_scores=True, return_dict_in_generate=True, logits_processor=lp)
26logits = out.scores[0].float(); probs = torch.softmax(logits, dim=-1)
27p1 = probs[0, one_id].item(); pred = 1 if p1 >= 0.5 else 0
28print("pred:", pred, "prob_technical:", p1)