Views
No views yet
verbatimcontext_marked, where the target mention is explicitly enclosed by <mention>...</mention><answer>...</answer> blockdo_sample=True<answer>...</answer> span1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5base_model_path = "Qwen/Qwen3-8B"
6adapter_path = "Tao-AI-Informatics/Qwen3-8B-LoRA-ContextBioEL-Rewriter-RL"
7
8tokenizer = AutoTokenizer.from_pretrained(base_model_path, trust_remote_code=True)
9base_model = AutoModelForCausalLM.from_pretrained(
10 base_model_path,
11 torch_dtype=torch.bfloat16,
12 device_map="auto",
13 trust_remote_code=True,
14)
15model = PeftModel.from_pretrained(base_model, adapter_path)
16
17messages = [
18 {
19 "role": "system",
20 "content": (
21 "You are a clinical terminology normalizer.\n"
22 "Given a clinical context where the target mention is explicitly marked by "
23 "<mention>...</mention>, rewrite/normalize that mention into a SNOMED CT-style expression.\n\n"
24 "Requirements:\n"
25 "1) Think before answer.\n"
26 "2) Output MUST contain two parts in order:\n"
27 " <think> ... <\\think>\n"
28 " <answer> ... <\\answer>\n"
29 "3) The answer should be short and term-like (close to SNOMED CT wording).\n"
30 "4) Use the mention inside <mention>...</mention> in the context as the primary target.\n"
31 ),
32 },
33 {
34 "role": "user",
35 "content": (
36 "Input:\n"
37 "verbatim:\nrenal failure\n\n"
38 "context_marked:\n"
39 "History significant for <mention>renal failure</mention> requiring dialysis.\n"
40 ),
41 },
42]
43
44text = tokenizer.apply_chat_template(
45 messages,
46 tokenize=False,
47 add_generation_prompt=True,
48)
49
50inputs = tokenizer(text, return_tensors="pt").to(model.device)
51
52with torch.no_grad():
53 outputs = model.generate(
54 **inputs,
55 max_new_tokens=256,
56 do_sample=True,
57 temperature=0.6,
58 top_p=0.95,
59 )
60
61print(tokenizer.decode(outputs[0], skip_special_tokens=False))
62