izhan001 commited on
Commit
691ba65
·
verified ·
1 Parent(s): c6ee9c7

Upload 2 files

Browse files
Files changed (3) hide show
  1. .gitattributes +1 -0
  2. book1.pdf +3 -0
  3. emotional_buddy book2.ipynb +490 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ book1.pdf filter=lfs diff=lfs merge=lfs -text
book1.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b9f31116a8af081d0789518cdb8da191a1f5d3ca67988dd5f987781817458cf5
3
+ size 3174678
emotional_buddy book2.ipynb ADDED
@@ -0,0 +1,490 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
+ },
9
+ "kernelspec": {
10
+ "name": "python3",
11
+ "display_name": "Python 3"
12
+ },
13
+ "language_info": {
14
+ "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "code",
21
+ "execution_count": 2,
22
+ "metadata": {
23
+ "colab": {
24
+ "base_uri": "https://localhost:8080/"
25
+ },
26
+ "id": "HiY1rH4fuPeF",
27
+ "outputId": "7c4cb24c-0a80-41fa-9f3e-7a79d7231921"
28
+ },
29
+ "outputs": [
30
+ {
31
+ "output_type": "stream",
32
+ "name": "stdout",
33
+ "text": [
34
+ "Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.44.2)\n",
35
+ "Collecting datasets\n",
36
+ " Downloading datasets-3.1.0-py3-none-any.whl.metadata (20 kB)\n",
37
+ "Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (2.5.0+cu121)\n",
38
+ "Collecting faiss-cpu\n",
39
+ " Downloading faiss_cpu-1.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.4 kB)\n",
40
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.16.1)\n",
41
+ "Requirement already satisfied: huggingface-hub<1.0,>=0.23.2 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.24.7)\n",
42
+ "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (1.26.4)\n",
43
+ "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (24.1)\n",
44
+ "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (6.0.2)\n",
45
+ "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (2024.9.11)\n",
46
+ "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers) (2.32.3)\n",
47
+ "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.4.5)\n",
48
+ "Requirement already satisfied: tokenizers<0.20,>=0.19 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.19.1)\n",
49
+ "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers) (4.66.6)\n",
50
+ "Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (17.0.0)\n",
51
+ "Collecting dill<0.3.9,>=0.3.0 (from datasets)\n",
52
+ " Downloading dill-0.3.8-py3-none-any.whl.metadata (10 kB)\n",
53
+ "Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets) (2.2.2)\n",
54
+ "Collecting xxhash (from datasets)\n",
55
+ " Downloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)\n",
56
+ "Collecting multiprocess<0.70.17 (from datasets)\n",
57
+ " Downloading multiprocess-0.70.16-py310-none-any.whl.metadata (7.2 kB)\n",
58
+ "Collecting fsspec<=2024.9.0,>=2023.1.0 (from fsspec[http]<=2024.9.0,>=2023.1.0->datasets)\n",
59
+ " Downloading fsspec-2024.9.0-py3-none-any.whl.metadata (11 kB)\n",
60
+ "Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets) (3.10.10)\n",
61
+ "Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch) (4.12.2)\n",
62
+ "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch) (3.4.2)\n",
63
+ "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch) (3.1.4)\n",
64
+ "Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch) (1.13.1)\n",
65
+ "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch) (1.3.0)\n",
66
+ "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (2.4.3)\n",
67
+ "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.3.1)\n",
68
+ "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (24.2.0)\n",
69
+ "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.5.0)\n",
70
+ "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (6.1.0)\n",
71
+ "Requirement already satisfied: yarl<2.0,>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.17.0)\n",
72
+ "Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (4.0.3)\n",
73
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.4.0)\n",
74
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.10)\n",
75
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2.2.3)\n",
76
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2024.8.30)\n",
77
+ "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch) (3.0.2)\n",
78
+ "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2.8.2)\n",
79
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)\n",
80
+ "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)\n",
81
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.16.0)\n",
82
+ "Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.10/dist-packages (from yarl<2.0,>=1.12.0->aiohttp->datasets) (0.2.0)\n",
83
+ "Downloading datasets-3.1.0-py3-none-any.whl (480 kB)\n",
84
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m480.6/480.6 kB\u001b[0m \u001b[31m21.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
85
+ "\u001b[?25hDownloading faiss_cpu-1.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.5 MB)\n",
86
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m27.5/27.5 MB\u001b[0m \u001b[31m39.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
87
+ "\u001b[?25hDownloading dill-0.3.8-py3-none-any.whl (116 kB)\n",
88
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m116.3/116.3 kB\u001b[0m \u001b[31m801.7 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
89
+ "\u001b[?25hDownloading fsspec-2024.9.0-py3-none-any.whl (179 kB)\n",
90
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m179.3/179.3 kB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
91
+ "\u001b[?25hDownloading multiprocess-0.70.16-py310-none-any.whl (134 kB)\n",
92
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.8/134.8 kB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
93
+ "\u001b[?25hDownloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB)\n",
94
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m194.1/194.1 kB\u001b[0m \u001b[31m13.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
95
+ "\u001b[?25hInstalling collected packages: xxhash, fsspec, faiss-cpu, dill, multiprocess, datasets\n",
96
+ " Attempting uninstall: fsspec\n",
97
+ " Found existing installation: fsspec 2024.10.0\n",
98
+ " Uninstalling fsspec-2024.10.0:\n",
99
+ " Successfully uninstalled fsspec-2024.10.0\n",
100
+ "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
101
+ "gcsfs 2024.10.0 requires fsspec==2024.10.0, but you have fsspec 2024.9.0 which is incompatible.\u001b[0m\u001b[31m\n",
102
+ "\u001b[0mSuccessfully installed datasets-3.1.0 dill-0.3.8 faiss-cpu-1.9.0 fsspec-2024.9.0 multiprocess-0.70.16 xxhash-3.5.0\n"
103
+ ]
104
+ }
105
+ ],
106
+ "source": [
107
+ "!pip install transformers datasets torch faiss-cpu\n"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "source": [
113
+ "from datasets import load_dataset\n",
114
+ "\n",
115
+ "dataset = load_dataset(\"Amod/mental_health_counseling_conversations\")\n"
116
+ ],
117
+ "metadata": {
118
+ "id": "Zh2FSQW-uWkg"
119
+ },
120
+ "execution_count": 4,
121
+ "outputs": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "import torch\n",
127
+ "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, Trainer, TrainingArguments\n",
128
+ "from datasets import load_dataset\n",
129
+ "# Instead of using train_test_split, we'll use Dataset.train_test_split\n",
130
+ "#from sklearn.model_selection import train_test_split\n",
131
+ "\n",
132
+ "# Load dataset\n",
133
+ "dataset = load_dataset(\"Amod/mental_health_counseling_conversations\")\n",
134
+ "\n",
135
+ "# Split the dataset using Dataset.train_test_split\n",
136
+ "train_data = dataset['train'].train_test_split(test_size=0.2, seed=42)['train'] # 80% train\n",
137
+ "val_data = dataset['train'].train_test_split(test_size=0.2, seed=42)['test'] # 20% validation\n",
138
+ "\n",
139
+ "\n",
140
+ "# Load FLAN-T5 Small model and tokenizer\n",
141
+ "model_name = \"google/flan-t5-small\"\n",
142
+ "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
143
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
144
+ "\n",
145
+ "# Preprocess the dataset\n",
146
+ "def preprocess_data(examples):\n",
147
+ " # Tokenize the context and response as input-output pairs for the Seq2Seq model\n",
148
+ " inputs = tokenizer(examples['Context'], padding=\"max_length\", truncation=True, max_length=512)\n",
149
+ " targets = tokenizer(examples['Response'], padding=\"max_length\", truncation=True, max_length=128)\n",
150
+ " inputs['labels'] = targets['input_ids']\n",
151
+ " return inputs\n",
152
+ "\n",
153
+ "# Apply preprocessing to the train and validation datasets\n",
154
+ "train_data = train_data.map(preprocess_data, batched=True)\n",
155
+ "val_data = val_data.map(preprocess_data, batched=True)\n",
156
+ "\n",
157
+ "# Remove unnecessary columns (Context, Response) from the dataset after preprocessing\n",
158
+ "\n",
159
+ "\n",
160
+ "# Setup training arguments\n",
161
+ "training_args = TrainingArguments(\n",
162
+ " output_dir='./results',\n",
163
+ " evaluation_strategy=\"epoch\",\n",
164
+ " learning_rate=2e-5,\n",
165
+ " per_device_train_batch_size=2,\n",
166
+ " per_device_eval_batch_size=2,\n",
167
+ " num_train_epochs=5,\n",
168
+ " logging_dir='./logs',\n",
169
+ " logging_steps=10,\n",
170
+ " save_strategy=\"epoch\", # Changed save_strategy to 'epoch' to match evaluation_strategy\n",
171
+ " save_steps=500,\n",
172
+ " save_total_limit=2,\n",
173
+ " load_best_model_at_end=True,\n",
174
+ " metric_for_best_model='loss',\n",
175
+ ")\n",
176
+ "\n",
177
+ "# Initialize Trainer\n",
178
+ "trainer = Trainer(\n",
179
+ " model=model,\n",
180
+ " args=training_args,\n",
181
+ " train_dataset=train_data,\n",
182
+ " eval_dataset=val_data,\n",
183
+ " tokenizer=tokenizer,\n",
184
+ ")\n",
185
+ "\n",
186
+ "# Train the model\n",
187
+ "trainer.train()\n",
188
+ "\n",
189
+ "# Save the fine-tuned model\n",
190
+ "trainer.save_model(\"fine_tuned_flan_t5\")\n",
191
+ "\n",
192
+ "# Now the model is fine-tuned and saved, we can use it for RAG"
193
+ ],
194
+ "metadata": {
195
+ "colab": {
196
+ "base_uri": "https://localhost:8080/",
197
+ "height": 487
198
+ },
199
+ "id": "uZ2imdKguqeb",
200
+ "outputId": "2ad6eefe-2d44-4e23-8456-ad294a750aba"
201
+ },
202
+ "execution_count": 17,
203
+ "outputs": [
204
+ {
205
+ "metadata": {
206
+ "tags": null
207
+ },
208
+ "name": "stderr",
209
+ "output_type": "stream",
210
+ "text": [
211
+ "/usr/local/lib/python3.10/dist-packages/transformers/training_args.py:1525: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n",
212
+ " warnings.warn(\n"
213
+ ]
214
+ },
215
+ {
216
+ "data": {
217
+ "text/html": [
218
+ "\n",
219
+ " <div>\n",
220
+ " \n",
221
+ " <progress value='6038' max='7025' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
222
+ " [6038/7025 15:08 < 02:28, 6.65 it/s, Epoch 4.30/5]\n",
223
+ " </div>\n",
224
+ " <table border=\"1\" class=\"dataframe\">\n",
225
+ " <thead>\n",
226
+ " <tr style=\"text-align: left;\">\n",
227
+ " <th>Epoch</th>\n",
228
+ " <th>Training Loss</th>\n",
229
+ " <th>Validation Loss</th>\n",
230
+ " </tr>\n",
231
+ " </thead>\n",
232
+ " <tbody>\n",
233
+ " <tr>\n",
234
+ " <td>1</td>\n",
235
+ " <td>3.433000</td>\n",
236
+ " <td>3.051346</td>\n",
237
+ " </tr>\n",
238
+ " <tr>\n",
239
+ " <td>2</td>\n",
240
+ " <td>3.104000</td>\n",
241
+ " <td>2.949355</td>\n",
242
+ " </tr>\n",
243
+ " <tr>\n",
244
+ " <td>3</td>\n",
245
+ " <td>3.121100</td>\n",
246
+ " <td>2.920628</td>\n",
247
+ " </tr>\n",
248
+ " <tr>\n",
249
+ " <td>4</td>\n",
250
+ " <td>3.126200</td>\n",
251
+ " <td>2.906628</td>\n",
252
+ " </tr>\n",
253
+ " </tbody>\n",
254
+ "</table><p>"
255
+ ],
256
+ "text/plain": [
257
+ "<IPython.core.display.HTML object>"
258
+ ]
259
+ },
260
+ "metadata": {},
261
+ "output_type": "display_data"
262
+ },
263
+ {
264
+ "output_type": "display_data",
265
+ "data": {
266
+ "text/plain": [
267
+ "<IPython.core.display.HTML object>"
268
+ ],
269
+ "text/html": [
270
+ "\n",
271
+ " <div>\n",
272
+ " \n",
273
+ " <progress value='7025' max='7025' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
274
+ " [7025/7025 17:49, Epoch 5/5]\n",
275
+ " </div>\n",
276
+ " <table border=\"1\" class=\"dataframe\">\n",
277
+ " <thead>\n",
278
+ " <tr style=\"text-align: left;\">\n",
279
+ " <th>Epoch</th>\n",
280
+ " <th>Training Loss</th>\n",
281
+ " <th>Validation Loss</th>\n",
282
+ " </tr>\n",
283
+ " </thead>\n",
284
+ " <tbody>\n",
285
+ " <tr>\n",
286
+ " <td>1</td>\n",
287
+ " <td>3.433000</td>\n",
288
+ " <td>3.051346</td>\n",
289
+ " </tr>\n",
290
+ " <tr>\n",
291
+ " <td>2</td>\n",
292
+ " <td>3.104000</td>\n",
293
+ " <td>2.949355</td>\n",
294
+ " </tr>\n",
295
+ " <tr>\n",
296
+ " <td>3</td>\n",
297
+ " <td>3.121100</td>\n",
298
+ " <td>2.920628</td>\n",
299
+ " </tr>\n",
300
+ " <tr>\n",
301
+ " <td>4</td>\n",
302
+ " <td>3.126200</td>\n",
303
+ " <td>2.906628</td>\n",
304
+ " </tr>\n",
305
+ " <tr>\n",
306
+ " <td>5</td>\n",
307
+ " <td>3.051300</td>\n",
308
+ " <td>2.902490</td>\n",
309
+ " </tr>\n",
310
+ " </tbody>\n",
311
+ "</table><p>"
312
+ ]
313
+ },
314
+ "metadata": {}
315
+ },
316
+ {
317
+ "output_type": "stream",
318
+ "name": "stderr",
319
+ "text": [
320
+ "There were missing keys in the checkpoint model loaded: ['encoder.embed_tokens.weight', 'decoder.embed_tokens.weight'].\n"
321
+ ]
322
+ }
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "source": [
328
+ "trainer.evaluate(train_data)"
329
+ ],
330
+ "metadata": {
331
+ "colab": {
332
+ "base_uri": "https://localhost:8080/",
333
+ "height": 124
334
+ },
335
+ "id": "sLp70Cgw7YTW",
336
+ "outputId": "7410e2b7-d4a9-459d-8e36-18d36b155493"
337
+ },
338
+ "execution_count": 25,
339
+ "outputs": [
340
+ {
341
+ "output_type": "display_data",
342
+ "data": {
343
+ "text/plain": [
344
+ "<IPython.core.display.HTML object>"
345
+ ],
346
+ "text/html": [
347
+ "\n",
348
+ " <div>\n",
349
+ " \n",
350
+ " <progress value='1405' max='1405' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
351
+ " [1405/1405 01:05]\n",
352
+ " </div>\n",
353
+ " "
354
+ ]
355
+ },
356
+ "metadata": {}
357
+ },
358
+ {
359
+ "output_type": "execute_result",
360
+ "data": {
361
+ "text/plain": [
362
+ "{'eval_loss': 2.8036255836486816,\n",
363
+ " 'eval_runtime': 65.2915,\n",
364
+ " 'eval_samples_per_second': 43.022,\n",
365
+ " 'eval_steps_per_second': 21.519,\n",
366
+ " 'epoch': 5.0}"
367
+ ]
368
+ },
369
+ "metadata": {},
370
+ "execution_count": 25
371
+ }
372
+ ]
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "source": [
377
+ "trainer.evaluate(val_data)"
378
+ ],
379
+ "metadata": {
380
+ "colab": {
381
+ "base_uri": "https://localhost:8080/",
382
+ "height": 124
383
+ },
384
+ "id": "cSkT2mL77sVh",
385
+ "outputId": "7eba2cb2-6355-4686-c7cc-68a7cc657ad3"
386
+ },
387
+ "execution_count": 26,
388
+ "outputs": [
389
+ {
390
+ "output_type": "display_data",
391
+ "data": {
392
+ "text/plain": [
393
+ "<IPython.core.display.HTML object>"
394
+ ],
395
+ "text/html": [
396
+ "\n",
397
+ " <div>\n",
398
+ " \n",
399
+ " <progress value='1757' max='1405' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
400
+ " [1405/1405 01:49]\n",
401
+ " </div>\n",
402
+ " "
403
+ ]
404
+ },
405
+ "metadata": {}
406
+ },
407
+ {
408
+ "output_type": "execute_result",
409
+ "data": {
410
+ "text/plain": [
411
+ "{'eval_loss': 2.9024901390075684,\n",
412
+ " 'eval_runtime': 23.9355,\n",
413
+ " 'eval_samples_per_second': 29.371,\n",
414
+ " 'eval_steps_per_second': 14.706,\n",
415
+ " 'epoch': 5.0}"
416
+ ]
417
+ },
418
+ "metadata": {},
419
+ "execution_count": 26
420
+ }
421
+ ]
422
+ },
423
+ {
424
+ "cell_type": "code",
425
+ "source": [
426
+ "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",
427
+ "\n",
428
+ "# Load the fine-tuned FLAN-T5 model and tokenizer\n",
429
+ "model_name = \"/content/results/checkpoint-5620\" # Replace with the path to your fine-tuned model\n",
430
+ "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
431
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
432
+ "\n",
433
+ "# Function to perform inference\n",
434
+ "def generate_response(input_text):\n",
435
+ " # Encode the input text\n",
436
+ " inputs = tokenizer(input_text, return_tensors=\"pt\", padding=True, truncation=True, max_length=512)\n",
437
+ " # The model.generate function call should be indented inside the generate_response function\n",
438
+ " output_ids = model.generate(\n",
439
+ " inputs.input_ids,\n",
440
+ " max_length=500,\n",
441
+ " num_beams=4,\n",
442
+ " temperature=0.7,\n",
443
+ " top_p=0.9,\n",
444
+ " top_k=50,\n",
445
+ " do_sample=True, # Sampling instead of beam search\n",
446
+ " no_repeat_ngram_size=3, # Avoid repeating 3-grams\n",
447
+ " length_penalty=1.0, # Adjust this value based on your preference\n",
448
+ " early_stopping=True\n",
449
+ " )\n",
450
+ " response = tokenizer.decode(output_ids[0], skip_special_tokens=True)\n",
451
+ " return response\n",
452
+ "\n",
453
+ "\n",
454
+ "\n",
455
+ "# Example input text\n",
456
+ "input_text = \"I'm going through some things with my feelings and myself. I barely sleep and I do nothing but think about how I'm worthless and how I shouldn't be here. I've never tried or contemplated suicide. I've always wanted to fix my issues, but I never get around to it. How can I change my feeling of being worthless to everyone?\"\n",
457
+ "\n",
458
+ "# Perform inference\n",
459
+ "response = generate_response(input_text)\n",
460
+ "print(\"Response from the model:\", response)"
461
+ ],
462
+ "metadata": {
463
+ "colab": {
464
+ "base_uri": "https://localhost:8080/"
465
+ },
466
+ "id": "dWM3cU22ypx6",
467
+ "outputId": "047e04f5-4720-4c1c-ac30-108368bbdd09"
468
+ },
469
+ "execution_count": 34,
470
+ "outputs": [
471
+ {
472
+ "output_type": "stream",
473
+ "name": "stdout",
474
+ "text": [
475
+ "Response from the model: I'm sorry to hear that you haven't tried or contemplated suicide. It sounds like you've been able to change your feeling of being worthless to everyone. It's a good idea to have a conversation with a mental health professional to discuss your feelings. If you're not sure what you want to do, you may want to talk to a psychiatrist about what you'd like to do. You may also want to ask a therapist if you are willing to help you with your feelings of worthlessness.\n"
476
+ ]
477
+ }
478
+ ]
479
+ },
480
+ {
481
+ "cell_type": "code",
482
+ "source": [],
483
+ "metadata": {
484
+ "id": "sfrESf4s8dlP"
485
+ },
486
+ "execution_count": null,
487
+ "outputs": []
488
+ }
489
+ ]
490
+ }