ThinkTim21 commited on
Commit
174101d
·
verified ·
1 Parent(s): feb5be5

Upload load_model_test.ipynb

Browse files
Files changed (1) hide show
  1. load_model_test.ipynb +410 -0
load_model_test.ipynb ADDED
@@ -0,0 +1,410 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "849d6959-9a62-48a8-99ef-546326471ded",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "import os\n",
13
+ "os.environ['HF_HOME'] = \"/scratch/tar3kh/models/cache\"\n",
14
+ "import torch \n",
15
+ "from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n",
16
+ "from datasets import load_dataset #datasets is huggingface's dataset package\n",
17
+ "from peft import get_peft_model, LoraConfig, TaskType\n",
18
+ "import matplotlib.pyplot as plt\n",
19
+ "import numpy as np\n",
20
+ "import pandas as pd\n",
21
+ "import PIL\n",
22
+ "\n",
23
+ "import lm_eval"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 5,
29
+ "id": "2e1b8342-5173-4769-befe-f7b223b55bdb",
30
+ "metadata": {
31
+ "tags": []
32
+ },
33
+ "outputs": [
34
+ {
35
+ "data": {
36
+ "application/vnd.jupyter.widget-view+json": {
37
+ "model_id": "4bbdb7ede23f4270936e8dd44eddaf80",
38
+ "version_major": 2,
39
+ "version_minor": 0
40
+ },
41
+ "text/plain": [
42
+ "model.safetensors.index.json: 0%| | 0.00/23.9k [00:00<?, ?B/s]"
43
+ ]
44
+ },
45
+ "metadata": {},
46
+ "output_type": "display_data"
47
+ },
48
+ {
49
+ "data": {
50
+ "application/vnd.jupyter.widget-view+json": {
51
+ "model_id": "a243af727b72473f92b73655c02e99b2",
52
+ "version_major": 2,
53
+ "version_minor": 0
54
+ },
55
+ "text/plain": [
56
+ "Fetching 4 files: 0%| | 0/4 [00:00<?, ?it/s]"
57
+ ]
58
+ },
59
+ "metadata": {},
60
+ "output_type": "display_data"
61
+ },
62
+ {
63
+ "data": {
64
+ "application/vnd.jupyter.widget-view+json": {
65
+ "model_id": "2a5cff447f3d432e8c2cd09e5f300eb1",
66
+ "version_major": 2,
67
+ "version_minor": 0
68
+ },
69
+ "text/plain": [
70
+ "model-00001-of-00004.safetensors: 0%| | 0.00/4.98G [00:00<?, ?B/s]"
71
+ ]
72
+ },
73
+ "metadata": {},
74
+ "output_type": "display_data"
75
+ },
76
+ {
77
+ "data": {
78
+ "application/vnd.jupyter.widget-view+json": {
79
+ "model_id": "8152bab4fba2498ab1c329c218bc5a69",
80
+ "version_major": 2,
81
+ "version_minor": 0
82
+ },
83
+ "text/plain": [
84
+ "model-00004-of-00004.safetensors: 0%| | 0.00/1.17G [00:00<?, ?B/s]"
85
+ ]
86
+ },
87
+ "metadata": {},
88
+ "output_type": "display_data"
89
+ },
90
+ {
91
+ "data": {
92
+ "application/vnd.jupyter.widget-view+json": {
93
+ "model_id": "36ca4d5a7a934c06b0633212e77f0549",
94
+ "version_major": 2,
95
+ "version_minor": 0
96
+ },
97
+ "text/plain": [
98
+ "model-00002-of-00004.safetensors: 0%| | 0.00/5.00G [00:00<?, ?B/s]"
99
+ ]
100
+ },
101
+ "metadata": {},
102
+ "output_type": "display_data"
103
+ },
104
+ {
105
+ "data": {
106
+ "application/vnd.jupyter.widget-view+json": {
107
+ "model_id": "88435538f5cb43e992ac4ed4735b1fb0",
108
+ "version_major": 2,
109
+ "version_minor": 0
110
+ },
111
+ "text/plain": [
112
+ "model-00003-of-00004.safetensors: 0%| | 0.00/4.92G [00:00<?, ?B/s]"
113
+ ]
114
+ },
115
+ "metadata": {},
116
+ "output_type": "display_data"
117
+ },
118
+ {
119
+ "data": {
120
+ "application/vnd.jupyter.widget-view+json": {
121
+ "model_id": "c03c2b9db40b4e84b2fa1a5e34b29fca",
122
+ "version_major": 2,
123
+ "version_minor": 0
124
+ },
125
+ "text/plain": [
126
+ "Loading checkpoint shards: 0%| | 0/4 [00:00<?, ?it/s]"
127
+ ]
128
+ },
129
+ "metadata": {},
130
+ "output_type": "display_data"
131
+ },
132
+ {
133
+ "data": {
134
+ "application/vnd.jupyter.widget-view+json": {
135
+ "model_id": "0facfe4509ad491a8f3faf5630924ab1",
136
+ "version_major": 2,
137
+ "version_minor": 0
138
+ },
139
+ "text/plain": [
140
+ "generation_config.json: 0%| | 0.00/124 [00:00<?, ?B/s]"
141
+ ]
142
+ },
143
+ "metadata": {},
144
+ "output_type": "display_data"
145
+ }
146
+ ],
147
+ "source": [
148
+ "tokenizer = AutoTokenizer.from_pretrained(\"TheFinAI/Fino1-8B\")\n",
149
+ "model = AutoModelForCausalLM.from_pretrained(\"ThinkTim21/FinPlan-1\")"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": 6,
155
+ "id": "6c71bde4-febf-420d-af03-fc66884fca74",
156
+ "metadata": {
157
+ "tags": []
158
+ },
159
+ "outputs": [],
160
+ "source": [
161
+ "# Prepare the model and tokenizer \n",
162
+ "tokenizer.pad_token = tokenizer.eos_token # set padding token to EOS token\n",
163
+ "model.config.poad_token_id = tokenizer.pad_token_id # set the padding token for model"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": 7,
169
+ "id": "58d314e6-5d05-40f4-abbd-33745b98a1b7",
170
+ "metadata": {
171
+ "tags": []
172
+ },
173
+ "outputs": [],
174
+ "source": [
175
+ "budget = pd.read_csv(\"budget_dataset.csv\")\n",
176
+ "goals = pd.read_csv(\"goals_dataset.csv\")\n"
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "execution_count": 8,
182
+ "id": "a2639954-4a6a-421c-8353-c12a1825833e",
183
+ "metadata": {
184
+ "tags": []
185
+ },
186
+ "outputs": [],
187
+ "source": [
188
+ "budget['instruct_lora'] = budget.apply(\n",
189
+ " lambda row: f\"Q: {row['question']}\\n\\nA: \",\n",
190
+ " axis=1\n",
191
+ ")\n",
192
+ "\n",
193
+ "goals['instruct_lora'] = goals.apply(\n",
194
+ " lambda row: f\"Q: {row['question']}\\n\\nA: \",\n",
195
+ " axis=1\n",
196
+ ")"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": 9,
202
+ "id": "381c73e0-65ac-457e-9af9-8b6be214284f",
203
+ "metadata": {
204
+ "tags": []
205
+ },
206
+ "outputs": [
207
+ {
208
+ "data": {
209
+ "application/vnd.jupyter.widget-view+json": {
210
+ "model_id": "48c03562b88f4f8c833c4c16c2b62725",
211
+ "version_major": 2,
212
+ "version_minor": 0
213
+ },
214
+ "text/plain": [
215
+ "Map: 0%| | 0/2500 [00:00<?, ? examples/s]"
216
+ ]
217
+ },
218
+ "metadata": {},
219
+ "output_type": "display_data"
220
+ },
221
+ {
222
+ "data": {
223
+ "application/vnd.jupyter.widget-view+json": {
224
+ "model_id": "384114c4a98e4c9cb4bdaaefeb07bedf",
225
+ "version_major": 2,
226
+ "version_minor": 0
227
+ },
228
+ "text/plain": [
229
+ "Map: 0%| | 0/500 [00:00<?, ? examples/s]"
230
+ ]
231
+ },
232
+ "metadata": {},
233
+ "output_type": "display_data"
234
+ }
235
+ ],
236
+ "source": [
237
+ "from datasets import load_dataset, Dataset #datasets is huggingface's dataset package\n",
238
+ "budget = budget.sample(frac = 1, random_state = 42) # randomly shuffle DF\n",
239
+ "train_budget = budget[:2500]\n",
240
+ "val_budget = budget[2500:]\n",
241
+ "train_budget = Dataset.from_pandas(train_budget)\n",
242
+ "val_budget = Dataset.from_pandas(val_budget)\n",
243
+ "train_budget = train_budget.map(lambda samples: tokenizer(samples['instruct']), batched = True)\n",
244
+ "val_budget = val_budget.map(lambda samples: tokenizer(samples['instruct']), batched = True)"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": 10,
250
+ "id": "383bb5d8-122a-41f3-9955-25357326c6d8",
251
+ "metadata": {
252
+ "tags": []
253
+ },
254
+ "outputs": [
255
+ {
256
+ "data": {
257
+ "application/vnd.jupyter.widget-view+json": {
258
+ "model_id": "9182c49cc3b941c9814b050ff0bec026",
259
+ "version_major": 2,
260
+ "version_minor": 0
261
+ },
262
+ "text/plain": [
263
+ "Map: 0%| | 0/2500 [00:00<?, ? examples/s]"
264
+ ]
265
+ },
266
+ "metadata": {},
267
+ "output_type": "display_data"
268
+ },
269
+ {
270
+ "data": {
271
+ "application/vnd.jupyter.widget-view+json": {
272
+ "model_id": "bd3814a7314842e6bac6f6fc201bc36a",
273
+ "version_major": 2,
274
+ "version_minor": 0
275
+ },
276
+ "text/plain": [
277
+ "Map: 0%| | 0/500 [00:00<?, ? examples/s]"
278
+ ]
279
+ },
280
+ "metadata": {},
281
+ "output_type": "display_data"
282
+ }
283
+ ],
284
+ "source": [
285
+ "goals = goals.sample(frac = 1, random_state = 42) # randomly shuffle DF\n",
286
+ "train_goals = goals[:2500]\n",
287
+ "val_goals = goals[2500:]\n",
288
+ "train_goals = Dataset.from_pandas(train_goals)\n",
289
+ "val_goals = Dataset.from_pandas(val_goals)\n",
290
+ "train_goals = train_goals.map(lambda samples: tokenizer(samples['instruct']), batched = True)\n",
291
+ "val_goals = val_goals.map(lambda samples: tokenizer(samples['instruct']), batched = True)"
292
+ ]
293
+ },
294
+ {
295
+ "cell_type": "code",
296
+ "execution_count": 11,
297
+ "id": "4ae2b8aa-55bb-4e9c-b93b-c7040c7d3ed3",
298
+ "metadata": {
299
+ "tags": []
300
+ },
301
+ "outputs": [
302
+ {
303
+ "name": "stderr",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "/scratch/tar3kh/llm_course_2/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:631: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.6` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n",
307
+ " warnings.warn(\n",
308
+ "/scratch/tar3kh/llm_course_2/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:636: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.9` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.\n",
309
+ " warnings.warn(\n"
310
+ ]
311
+ },
312
+ {
313
+ "name": "stdout",
314
+ "output_type": "stream",
315
+ "text": [
316
+ "Q: My short term goal is to save for a $1774 vacation in the next year, my medium term goal is to save for down payment for a new car, around 5227 in the next 2 or 3 years, and my long term goal is to save for a down payment for a house around 151861 in the next ten years, can you help me integrate these goals into my budget as well as where I should store these savings?\n",
317
+ "\n",
318
+ "A: Thinking\n",
319
+ "\n",
320
+ "Alright, let's figure out how to make these savings goals work with your budget. First, I want to make sure I understand what you're aiming for. You've got three goals: saving for a vacation, a new car, and eventually a house. Let's break each down.\n",
321
+ "\n",
322
+ "For the vacation, you're looking to save $1774 in the next year. That's a pretty manageable goal, especially if you break it down into monthly chunks. Let's see, $1774 divided by 12 months gives you about $147.83 per month. Not too bad, right?\n",
323
+ "\n",
324
+ "Next up is saving for a new car. You're aiming for $5227 in 2-3 years. That's a bit longer, so let's spread it out. If you divide $5227 by 24 months, you get about $218.08 per month. That's a bit more than the vacation savings, but still doable.\n",
325
+ "\n",
326
+ "Now, onto the big one: saving for a house down payment. You're looking at $151,861 in ten years. That's a long-term goal, so let's think about it in terms of monthly contributions. If you divide $151,861 by 120 months, you get about $1265.09 per month. That's a significant amount, but it's spread out over ten years, so it's manageable.\n",
327
+ "\n",
328
+ "Okay, now let's think about where to store these savings. For the vacation fund, you could just keep it in a separate savings account at your bank. It's easy to access and won't earn much interest, but it's perfect for short-term goals.\n",
329
+ "\n",
330
+ "For the car fund, you might consider a high-yield savings account. It'll earn a bit more interest than a regular savings account, and it's still easily accessible.\n",
331
+ "\n",
332
+ "The house fund is a different story. Since it's a long-term goal, you might want to consider a dedicated savings account or even a certificate of deposit (CD) with a longer term. This will help you avoid dipping into the funds for other expenses and earn a bit more interest over time.\n",
333
+ "\n",
334
+ "Let's recap. For the vacation, keep it simple with a regular savings account. For the car, a high-yield savings account should work. And for the house, consider a dedicated savings account or a CD. Now, let's make sure you're on track to meet these goals by regularly reviewing your progress and adjusting as needed.\n",
335
+ "\n",
336
+ "Oh, and don't forget to automate these transfers so you don't miss a payment. Set up monthly transfers from your checking account to each of these savings accounts, and you'll be on your way to achieving these goals in no time!\n",
337
+ "\n",
338
+ "## Final Response\n",
339
+ "\n",
340
+ "To integrate these savings goals into your budget, let's break down each goal into manageable monthly contributions. \n",
341
+ "\n",
342
+ "1. **Vacation Savings**: Save $1774 in the next year. Divide this by 12 months: approximately $147.83 per month.\n",
343
+ "2. **Car Savings**: Save $5227 in 2-3 years. Divide this by 24 months: approximately $218.08 per month.\n",
344
+ "3. **House Savings**: Save $151,861 in ten years. Divide this by 120 months: approximately $1265.09 per month.\n",
345
+ "\n",
346
+ "For storing these savings, consider the following:\n",
347
+ "\n",
348
+ "- **Vacation Fund**: Keep in a separate savings account at your bank for easy access.\n",
349
+ "- **Car Fund**: Use a high-yield savings account to earn a bit more interest.\n",
350
+ "- **House Fund**: Consider a dedicated savings account or a certificate of deposit (CD) with a longer term to avoid dipping into the funds and earn more interest over time.\n",
351
+ "\n",
352
+ "To ensure you're on track, automate monthly transfers from your checking account to each of these savings accounts. Regularly review your progress and adjust as needed to meet these goals. By following this plan, you'll be well on your way to achieving your savings objectives.\n"
353
+ ]
354
+ }
355
+ ],
356
+ "source": [
357
+ "formatted_prompt = f\"Q: {val_goals[0]['question']}\\n\\nA: \"\n",
358
+ "inputs = tokenizer.encode(formatted_prompt, return_tensors = \"pt\").to(model.device)\n",
359
+ "output = model.generate(inputs, max_new_tokens = 800, pad_token_id = tokenizer.pad_token_id, do_sample = False)\n",
360
+ "generated_text = tokenizer.decode(output[0], skip_special_tokens = True)\n",
361
+ "print(generated_text)"
362
+ ]
363
+ },
364
+ {
365
+ "cell_type": "code",
366
+ "execution_count": null,
367
+ "id": "706fdd9f-ea8d-4ef5-9bd2-a4709dfb75cf",
368
+ "metadata": {
369
+ "tags": []
370
+ },
371
+ "outputs": [],
372
+ "source": [
373
+ "formatted_prompt = f\"Q: {val_budget[0]['question']}\\n\\nA: \"\n",
374
+ "inputs = tokenizer.encode(formatted_prompt, return_tensors = \"pt\").to(model.device)\n",
375
+ "output = model.generate(inputs, max_new_tokens = 800, pad_token_id = tokenizer.pad_token_id, do_sample = False)\n",
376
+ "generated_text = tokenizer.decode(output[0], skip_special_tokens = True)\n",
377
+ "print(generated_text)"
378
+ ]
379
+ },
380
+ {
381
+ "cell_type": "code",
382
+ "execution_count": null,
383
+ "id": "9444cd08-5469-432f-adba-5cf95068d5b5",
384
+ "metadata": {},
385
+ "outputs": [],
386
+ "source": []
387
+ }
388
+ ],
389
+ "metadata": {
390
+ "kernelspec": {
391
+ "display_name": "llm_course_2",
392
+ "language": "python",
393
+ "name": "llm_course_2"
394
+ },
395
+ "language_info": {
396
+ "codemirror_mode": {
397
+ "name": "ipython",
398
+ "version": 3
399
+ },
400
+ "file_extension": ".py",
401
+ "mimetype": "text/x-python",
402
+ "name": "python",
403
+ "nbconvert_exporter": "python",
404
+ "pygments_lexer": "ipython3",
405
+ "version": "3.11.11"
406
+ }
407
+ },
408
+ "nbformat": 4,
409
+ "nbformat_minor": 5
410
+ }