import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
MODEL_ID = "OmkarShewale/clintrial-qwen2.5-7b-sft"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype=torch.bfloat16, device_map="auto")
SYSTEM = (
"You are a clinical research assistant. You help patients and clinicians understand "
"clinical trials. Answer only from the information provided, be precise, and never "
"invent eligibility criteria, conditions, or outcomes."
)
criteria = """Inclusion Criteria:
1. Adults aged 18 years or older with confirmed type 2 diabetes
2. HbA1c between 7.0% and 10.5% at screening
Exclusion Criteria:
1. History of severe hypoglycaemia within the last 6 months
2. Pregnancy or breastfeeding
"""
messages = [
{"role": "system", "content": SYSTEM},
{"role": "user", "content":
'Extract the eligibility criteria from the trial text below into JSON with two lists, '
'"inclusion" and "exclusion". Copy each criterion verbatim; do not add any.\n\n'
f"Trial eligibility text:\n{criteria}"},
]
enc = tokenizer.apply_chat_template(messages, add_generation_prompt=True,
return_tensors="pt", return_dict=True).to(model.device)
out = model.generate(**enc, max_new_tokens=1024, do_sample=False)
print(tokenizer.decode(out[0, enc["input_ids"].shape[1]:], skip_special_tokens=True))