File size: 4,020 Bytes
9ba106f
 
 
 
 
a01507f
c54351a
a01507f
77f9995
ddb0998
77f9995
9ba106f
 
 
 
a01507f
 
9ba106f
 
 
 
 
c54351a
9ba106f
 
 
c54351a
9ba106f
c54351a
ddb0998
9ba106f
 
 
c54351a
 
 
 
9ba106f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54351a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddb0998
 
 
 
c54351a
 
 
 
 
 
 
 
ddb0998
c54351a
 
ddb0998
 
 
 
c54351a
 
 
 
 
 
 
9ba106f
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
license: apache-2.0
library_name: peft
tags:
- generated_from_trainer
- mistral
- text-generation
- transformers
- Inference Endpoints
- pytorch
- text-generation-inference
base_model: mistralai/Mistral-7B-Instruct-v0.2
model-index:
- name: mental-health-mistral-7b-instructv0.2-finetuned-V2
  results: []
datasets:
- Amod/mental_health_counseling_conversations
---


# mental-health-mistral-7b-instructv0.2-finetuned-V2

This model is a fine-tuned version of [mistralai/Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2) on the [mental_health_counseling_conversations](https://huggingface.co/datasets/Amod/mental_health_counseling_conversations) dataset.     
It achieves the following results on the evaluation set:
- Loss: 0.6432

## Model description  

A Mistral-7B-Instruct-v0.2 model finetuned on a corpus of mental health conversations between a psychologist and a user.    
The intention was to create a mental health assistant, "Connor", to address user questions based on responses from a psychologist.       

## Training and evaluation data

The model is finetuned on a corpus of mental health conversations between a psychologist and a client, in the form of context - response pairs. This dataset is a collection of questions and answers sourced from two online counseling and therapy platforms. The questions cover a wide range of mental health topics, and the answers are provided by qualified psychologists.         
Dataset found here :-      
* [Kaggle](https://www.kaggle.com/datasets/thedevastator/nlp-mental-health-conversations)
* [Huggingface](https://huggingface.co/datasets/Amod/mental_health_counseling_conversations)

### Training hyperparameters

The following hyperparameters were used during training:
- learning_rate: 0.0002
- train_batch_size: 8
- eval_batch_size: 8
- seed: 42
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
- lr_scheduler_type: cosine
- lr_scheduler_warmup_ratio: 0.05
- num_epochs: 3
- mixed_precision_training: Native AMP

### Training results

| Training Loss | Epoch | Step | Validation Loss |
|:-------------:|:-----:|:----:|:---------------:|
| 1.4325        | 1.0   | 352  | 0.9064          |
| 1.2608        | 2.0   | 704  | 0.6956          |
| 1.1845        | 3.0   | 1056 | 0.6432          |

# Usage

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftConfig, PeftModel

base_model = "mistralai/Mistral-7B-Instruct-v0.2"
adapter = "GRMenon/mental-health-mistral-7b-instructv0.2-finetuned-V2"

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(
    base_model,
    add_bos_token=True,
    trust_remote_code=True,
    padding_side='left'
)

# Create peft model using base_model and finetuned adapter
config = PeftConfig.from_pretrained(adapter)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path,
                                             load_in_4bit=True,
                                             device_map='auto',
                                             torch_dtype='auto')
model = PeftModel.from_pretrained(model, adapter)

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()

# Prompt content:
messages = [
    {"role": "user", "content": "Hey Connor! I have been feeling a bit down lately.I could really use some advice on how to feel better?"}
]

input_ids = tokenizer.apply_chat_template(conversation=messages,
                                          tokenize=True,
                                          add_generation_prompt=True,
                                          return_tensors='pt').to(device)
output_ids = model.generate(input_ids=input_ids, max_new_tokens=512, do_sample=True, pad_token_id=2)
response = tokenizer.batch_decode(output_ids.detach().cpu().numpy(), skip_special_tokens = True)

# Model response: 
print(response[0])
```


### Framework versions

- PEFT 0.7.1
- Transformers 4.36.1
- Pytorch 2.0.0
- Datasets 2.1.0
- Tokenizers 0.15.0