This model was developed as part of a research paper investigating the effectiveness of parameter-efficient fine-tuning (PEFT) methods — specifically QLoRA — for adapting large language models to mental health support tasks.
1flowchart TD
2 A([🗂️ Raw Datasets]) --> B[Data Engineering & Filtering]
3 B --> C[Mistral Chat Template Formatting]
4 C --> D[95 / 5 Train-Val Split\n6,768 train · 357 val]
5
6 D --> E([🤖 Mistral-7B-Instruct-v0.3])
7 E --> F[4-bit NF4 Quantization\nBitsAndBytesConfig]
8 F --> G[prepare_model_for_kbit_training]
9
10 G --> H[LoRA Adapter Injection\nr=16 · α=32 · 7 projection layers]
11 H --> I[🏋️ QLoRA Fine-Tuning\nSFTTrainer · 1 Epoch]
12
13 I --> J[Perplexity Evaluation\n200 held-out samples]
14 J --> K{Perplexity < 5.0?}
15 K -- ✅ 2.451 --> L[Save LoRA Adapter\n162 MB]
16 L --> M[Merge into Base Model\n13.5 GB]
17 M --> N([🚀 Push to HuggingFace Hub])
1pie title Dataset Composition (7,125 samples)
2 "CounselChat — Therapist Q&A" : 2598
3 "PHR Mental Therapy — Multi-turn" : 4527
1xychart-beta
2 title "Training vs Validation Loss"
3 x-axis [Step 100, Step 200, Step 300, Step 400, Final]
4 y-axis "Loss" 0.70 --> 0.86
5 line [0.8204, 0.7659, 0.7431, 0.7694, 0.8180]
6 line [0.8401, 0.8145, 0.7956, 0.7862, 0.7862]
Example 1 — Anxiety
User: I've been feeling really anxious lately and I can't seem to control my thoughts. Everything feels overwhelming.
Model: Hey there, it sounds like your mind is full of racing thoughts and it's getting pretty intense. It could be helpful to start by acknowledging what you're feeling — and that it's okay to feel overwhelmed. Try taking some time to sit with those thoughts. Instead of trying to control them, just observe them, notice them, and let them pass through...
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_id = "SpaceXerror/cognitive-ai-mental-health-7b"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype=torch.float16,
10 device_map="auto",
11)
12
13SYSTEM_PROMPT = """You are a compassionate, empathetic mental health support assistant.
14You listen carefully, validate feelings, and provide thoughtful, evidence-based guidance.
15Always prioritize the person's safety and wellbeing."""
16
17def chat(user_message: str) -> str:
18 prompt = f"<s>[INST] {SYSTEM_PROMPT}\n\n{user_message} [/INST]"
19 inputs = tokenizer(
20 prompt,
21 return_tensors="pt",
22 truncation=True,
23 max_length=512,
24 ).to(model.device)
25
26 with torch.no_grad():
27 outputs = model.generate(
28 **inputs,
29 max_new_tokens=300,
30 temperature=0.7,
31 top_p=0.9,
32 repetition_penalty=1.4,
33 no_repeat_ngram_size=4,
34 do_sample=True,
35 pad_token_id=tokenizer.eos_token_id,
36 )
37
38 response = tokenizer.decode(
39 outputs[0][inputs["input_ids"].shape[1]:],
40 skip_special_tokens=True,
41 )
42 return response.strip()
43
44print(chat("I have been feeling very anxious lately. What can I do?"))
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5base_model = AutoModelForCausalLM.from_pretrained(
6 "mistralai/Mistral-7B-Instruct-v0.3",
7 torch_dtype=torch.float16,
8 device_map="auto",
9)
10
11model = PeftModel.from_pretrained(
12 base_model,
13 "SpaceXerror/cognitive-ai-mental-health-7b",
14)
15
16tokenizer = AutoTokenizer.from_pretrained(
17 "SpaceXerror/cognitive-ai-mental-health-7b"
18)
1@misc{spacexerror2024mentalhealthllm,
2 title = {Fine-tuning Mistral-7B for Mental Health Support using QLoRA},
3 author = {SpaceXerror},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/SpaceXerror/cognitive-ai-mental-health-7b}
7}