Views
No views yet
| Drug | F1 Score | Precision | Recall |
|---|---|---|---|
| Methamphetamine | 0.931 | 0.915 | 0.947 |
| Fentanyl | 0.950 | 0.927 | 0.974 |
| Injection Drug Use | 0.923 | 0.913 | 0.933 |
| Drug | Base F1 | Fine-tuned F1 | Improvement |
|---|---|---|---|
| Methamphetamine | 0.787 | 0.931 | +14.4pp |
| Fentanyl | 0.753 | 0.950 | +19.7pp |
1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = "fabriceyhc/Llama-DrugDetector-8B"
7model = AutoModelForCausalLM.from_pretrained(
8 base_model,
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12
13# Load LoRA adapter
14model = PeftModel.from_pretrained(model, "fabriceyhc/Llama-DrugDetector-8B-MFI")
15
16# Load tokenizer
17tokenizer = AutoTokenizer.from_pretrained("fabriceyhc/Llama-DrugDetector-8B-MFI")
18
19# Create prompt
20note_text = "Patient reports using meth daily for the past 2 weeks."
21
22prompt = f"""### Task Description:
23Please carefully review the following medical note and identify illicit drug use.
24
25**CRITICAL RULES:**
261. **Positive drug test → ALWAYS ILLICIT** (unless in medication list)
272. **PMH/History of use → ILLICIT** (even if historical)
283. **Substance use disorder → ILLICIT**
294. **Patient self-reports (endorses, reports, admits) → ILLICIT**
305. **Prescribed/medical use → NOT ILLICIT**:
31 - Medication lists with dosages
32 - "Given", "administered" in medical context
33 - Pain control, procedural use (fentanyl)
34 - Prescription quantities
35
36**Drugs to identify:**
37- **Methamphetamine**: Illicit amphetamine use (not prescribed Adderall for ADHD)
38- **Fentanyl**: Illicit fentanyl use (not prescribed patches/procedural use)
39- **Injection Drug Use**: IV drug use (IVDU, IVDA), including IV heroin, cocaine, meth
40
41**Temporal Classification** (for illicit cases only):
42- **Current**: Present tense, recent use, positive test results, "POA" (present on arrival)
43- **Historical**: Past tense, "history of", "former user", "in remission"
44- **Unknown**: Timeframe unclear or ambiguous
45
46### Desired Format:
47
48Methamphetamine Illicit Use: <True/False/Unknown>
49Fentanyl Illicit Use: <True/False/Unknown>
50Injection Drug Use: <True/False/Unknown>
51Methamphetamine Temporal Status: <Current/Historical/Unknown/N/A>
52Fentanyl Temporal Status: <Current/Historical/Unknown/N/A>
53Injection Drug Use Temporal Status: <Current/Historical/Unknown/N/A>
54
55### The medical note to evaluate:
56{note_text}
57
58### Answer:
59"""
60
61# Generate
62inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
63outputs = model.generate(
64 **inputs,
65 max_new_tokens=200,
66 do_sample=False,
67 pad_token_id=tokenizer.eos_token_id
68)
69
70# Decode
71result = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
72print(result)Methamphetamine Illicit Use: True
Fentanyl Illicit Use: False
Injection Drug Use: False
Methamphetamine Temporal Status: Current
Fentanyl Temporal Status: N/A
Injection Drug Use Temporal Status: N/A1@misc{llama-drugdetector-mfi-2025,
2 author = {Harel-Canada, Fabrice},
3 title = {Llama-DrugDetector-8B-MFI: Multi-task Clinical Drug Detection},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/fabriceyhc/Llama-DrugDetector-8B-MFI}
7}