1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5BASE = "Qwen/Qwen3-0.6B"
6ADAPTER = "src/core/sagemaker/results/qwen3-suicide-risk-v1"
7
8SYSTEM_PROMPT = (
9 "You are a clinical risk assessment assistant. "
10 "Read the social-media post and classify its suicide/self-harm risk level "
11 "using this rubric:\n"
12 "0 = no risk\n"
13 "1 = negative emotion / distress, but no suicidal or self-harm ideation\n"
14 "2 = passive death ideation\n"
15 "3 = active suicidal / self-harm ideation\n"
16 "4 = has method, plan, attempt, or imminent risk\n"
17 "Answer with ONLY the single digit (0, 1, 2, 3, or 4)."
18)
19
20tok = AutoTokenizer.from_pretrained(ADAPTER, trust_remote_code=True)
21base = AutoModelForCausalLM.from_pretrained(BASE, torch_dtype=torch.float32, device_map="auto")
22model = PeftModel.from_pretrained(base, ADAPTER).eval()
23
24def predict(text):
25 prompt = tok.apply_chat_template(
26 [{"role": "system", "content": SYSTEM_PROMPT},
27 {"role": "user", "content": f"Post:\n{text}\n\nRisk level:"}],
28 tokenize=False, add_generation_prompt=True, enable_thinking=False,
29 )
30 enc = tok(prompt, return_tensors="pt").to(model.device)
31 out = model.generate(**enc, max_new_tokens=4, do_sample=False)
32 return tok.decode(out[0, enc["input_ids"].shape[1]:], skip_special_tokens=True).strip()
33
34print(predict("i just want to sleep and never wake up"))