Views
No views yet

1from unsloth import FastLanguageModel
2from peft import PeftModel
3import torch, numpy as np
4
5BASE = "unsloth/llama-3.3-70b-instruct-bnb-4bit"
6ADAPTER = "SehwanMoon/DepressLLM-llama3.3-70B" # HF adapter weights
7max_seq_length = 7000
8
9# Load base model + LoRA adapter
10model, tokenizer = FastLanguageModel.from_pretrained(
11 model_name = BASE,
12 max_seq_length = max_seq_length,
13 load_in_4bit = True,
14)
15model = PeftModel.from_pretrained(model, ADAPTER)
16device = "cuda" if torch.cuda.is_available() else "cpu"
17model.to(device)
18model.eval()
19
20# Example: single transcript
21sample = {
22 "messages": [
23 {"role": "user", "content": "A transcript of a participant talking about the topic of happiness and distress."},
24 ]
25}
26
27# Prompt
28prompt = (
29 "You will be given a transcript of a participant talking about the topic of happiness and distress.\n"
30 "Classify the transcript into one of the PHQ-9 scores (0–27).\n"
31 f"Narrative:\n{sample['messages'][0]['content']}"
32)
33
34inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=max_seq_length)
35inputs = {kk: vv.to(device) for kk, vv in inputs.items()}
36
37with torch.no_grad():
38 out = model.generate(
39 **inputs,
40 max_new_tokens=1, # 1-step next-token distribution
41 do_sample=False,
42 output_scores=True,
43 return_dict_in_generate=True,
44 )
45
46# Next-token distribution
47logits = torch.log_softmax(out.scores[0][0], dim=-1)
48topk_logp, topk_idx = torch.topk(logits, k=50)
49
50answer_list = []
51for logp, idx in zip(topk_logp.tolist(), topk_idx.tolist()):
52 token_str = tokenizer.decode([idx]).strip()
53 prob_pct = float(np.round(np.exp(logp) * 100, 2))
54 answer_list.append([token_str, prob_pct])
55
56# Post-processing
57def safe_convert_to_int(value):
58 try:
59 return int(value)
60 except ValueError:
61 return value
62
63answer_list = [[safe_convert_to_int(item[0]), item[1]] for item in answer_list]
64
65group_0_to_4, group_5_to_27 = 0, 0
66for prediction, probability in answer_list:
67 if isinstance(prediction, int):
68 if 0 <= prediction <= 4:
69 group_0_to_4 += probability
70 elif 5 <= prediction <= 27:
71 group_5_to_27 += probability
72
73normalized = [(group_0_to_4 / (group_0_to_4 + group_5_to_27)) * 100,
74 (group_5_to_27 / (group_0_to_4 + group_5_to_27)) * 100]
75
76print("P(Depression) %:", normalized[1])You will be given a transcript of a participant talking about happiness and distress.
Classify the transcript into one of the PHQ-9 scores (0–27).
Respond with only the score as an integer. Do not include any other text.
You will be given a transcript of a participant talking about happiness and distress.
Classify the transcript into one of the two classes: 0 (normal) or 1 (depression).
You will be given a transcript of a participant talking about happiness and distress.
Classify the transcript into one of the PHQ-9 scores (0–27).
You will be given a transcript of a participant talking about happiness and distress.
- Classify the transcript into one of the PHQ-9 scores (0–27).
- Write a brief explanation for your prediction by referring to evidence from the transcript.
- Highlight all significant words or phrases that influenced your decision, separated by commas.
Example output format:
<PHQ-9 score as integer>
Explanation: <Brief explanation, citing specific evidence from the transcript.>
Significant words/phrases: <phrase 1>, <phrase 2>, …
You will be given a transcript of a participant talking about happiness and distress.
- Classify the transcript into one of the PHQ-9 scores (0–27).
- Write a brief explanation for your prediction by referring to evidence from the transcript.
- Highlight all significant words or phrases that influenced your decision, separated by commas.
- Provide a confidence score (as a percentage) indicating how certain you are of your prediction.
Example output format:
<PHQ-9 score as integer>
Explanation: <Brief explanation, citing specific evidence from the transcript.>
Significant words/phrases: <phrase 1>, <phrase 2>, …
Confidence score: <percentage>%