File size: 29,432 Bytes
bf45cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3381014
 
 
 
 
 
 
bf45cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3381014
 
 
 
 
 
 
 
 
 
 
 
bf45cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3381014
 
 
bf45cf1
 
 
 
 
 
 
 
 
 
 
3381014
bf45cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: transformers in /srv/conda/envs/notebook/lib/python3.9/site-packages (4.26.1)\n",
      "Requirement already satisfied: filelock in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (3.8.0)\n",
      "Requirement already satisfied: numpy>=1.17 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (1.23.3)\n",
      "Requirement already satisfied: requests in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (2.28.1)\n",
      "Requirement already satisfied: packaging>=20.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (21.3)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (5.4.1)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (2022.10.31)\n",
      "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (0.13.2)\n",
      "Requirement already satisfied: tqdm>=4.27 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (4.64.1)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.11.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (0.12.1)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.11.0->transformers) (4.3.0)\n",
      "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from packaging>=20.0->transformers) (3.0.9)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (2022.9.14)\n",
      "Requirement already satisfied: charset-normalizer<3,>=2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (2.1.1)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (1.26.11)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (3.3)\n"
     ]
    }
   ],
   "source": [
    "!pip install transformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('fine_tuned_tokenizer/tokenizer_config.json',\n",
       " 'fine_tuned_tokenizer/special_tokens_map.json',\n",
       " 'fine_tuned_tokenizer/vocab.json',\n",
       " 'fine_tuned_tokenizer/merges.txt',\n",
       " 'fine_tuned_tokenizer/added_tokens.json')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "\n",
    "\n",
    "class TextDataset(Dataset):\n",
    "    def __init__(self, file_path, tokenizer, max_seq_length):\n",
    "        with open(file_path, 'r', encoding='utf-8') as f:\n",
    "            self.text = f.read()\n",
    "        self.tokenizer = tokenizer\n",
    "        self.max_seq_length = max_seq_length\n",
    "        self.segments = []\n",
    "\n",
    "        for line in self.text.split('\\n'):\n",
    "            if len(line) > 0:\n",
    "                if len(line) > self.max_seq_length:\n",
    "                    # Split long lines into shorter segments\n",
    "                    segments = [line[i:i+self.max_seq_length] for i in range(0, len(line), self.max_seq_length)]\n",
    "                    self.segments.extend(segments)\n",
    "                else:\n",
    "                    self.segments.append(line)\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.segments)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        self.tokenizer.pad_token_id = 0\n",
    "        segment = self.segments[idx]\n",
    "        input_ids = self.tokenizer.encode(segment, add_special_tokens=True)\n",
    "        target_ids = input_ids[1:] + [self.tokenizer.pad_token_id]\n",
    "\n",
    "        if not input_ids:\n",
    "            return None\n",
    "        \n",
    "\n",
    "        # Pad the input sequence to the same length\n",
    "        if len(input_ids) > self.max_seq_length:\n",
    "            input_ids = input_ids[:self.max_seq_length]\n",
    "            target_ids = target_ids[:self.max_seq_length]\n",
    "        else:\n",
    "            padding_length = self.max_seq_length - len(input_ids)\n",
    "            input_ids = input_ids + [self.tokenizer.pad_token_id] * padding_length\n",
    "            target_ids = target_ids + [self.tokenizer.pad_token_id] * padding_length\n",
    "        #print(\"segment:\",segment,\"\\n input:\", input_ids, \"\\n targets\" ,target_ids)\n",
    "        #print(\"\\r segment:\",segment, end=\"\")\n",
    "        \n",
    "        return torch.tensor(input_ids), torch.tensor(target_ids)\n",
    "\n",
    "\n",
    "# Initialize the tokenizer and the pre-trained model\n",
    "tokenizer = GPT2Tokenizer.from_pretrained('gpt2')\n",
    "model = GPT2LMHeadModel.from_pretrained('gpt2').to(device)\n",
    "\n",
    "\n",
    "# Define the path to the training text file\n",
    "train_file = 'text.txt'\n",
    "\n",
    "# Define the training and validation datasets\n",
    "max_seq_length = 512\n",
    "\n",
    "train_dataset = TextDataset(train_file, tokenizer, max_seq_length)\n",
    "valid_dataset = TextDataset(train_file, tokenizer, max_seq_length)\n",
    "\n",
    "# Define the training hyperparameters\n",
    "batch_size = 8\n",
    "num_epochs = 7\n",
    "learning_rate = 5e-4\n",
    "\n",
    "# Define the data loader for the training and validation datasets\n",
    "train_loader = DataLoader(train_dataset,batch_size=batch_size, shuffle=True)\n",
    "valid_loader = DataLoader(valid_dataset,batch_size=batch_size, shuffle=True)\n",
    "\n",
    "# Define the loss function and the optimizer\n",
    "loss_fn = torch.nn.CrossEntropyLoss()\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)\n",
    "\n",
    "tokenizer.save_pretrained('fine_tuned_tokenizer')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/7, Train Loss: 0.5704409927129745, Valid Loss: 0.3897556330009205, lr: 0.0005\n",
      "Epoch 2/7, Train Loss: 0.403159585849541, Valid Loss: 0.24347291268953464, lr: 0.0001\n",
      "Epoch 3/7, Train Loss: 0.27275778398644634, Valid Loss: 0.13453135721203757, lr: 2e-05\n",
      "Epoch 4/7, Train Loss: 0.1717792261482739, Valid Loss: 0.07003633981775038, lr: 4.000000000000001e-06\n",
      "Epoch 5/7, Train Loss: 0.10565993448764813, Valid Loss: 0.03993069042065522, lr: 8.000000000000002e-07\n",
      "Epoch 6/7, Train Loss: 0.0703794076675322, Valid Loss: 0.02531332855408148, lr: 1.6000000000000003e-07\n",
      "Epoch 7/7, Train Batch 7/82, Train Loss: 0.0036747022645502556"
     ]
    }
   ],
   "source": [
    "# Train the model for a fixed number of epochs\n",
    "for epoch in range(num_epochs):\n",
    "    # Training loop\n",
    "    model.train()\n",
    "    train_loss = 0\n",
    "    i=0\n",
    "    for batch in train_loader:\n",
    "        i+=1\n",
    "        input_ids, target_ids = batch\n",
    "        input_ids, target_ids = input_ids.to(device), target_ids.to(device)\n",
    "        optimizer.zero_grad()\n",
    "        outputs = model(input_ids, labels=target_ids)\n",
    "        loss = loss_fn(outputs.logits.view(-1, outputs.logits.size(-1)), target_ids.view(-1))\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        train_loss += loss.item()\n",
    "        print(f'\\rEpoch {epoch+1}/{num_epochs}, Train Batch {i}/{len(train_loader)}, Train Loss: {train_loss/len(train_loader)}',end=\"\")\n",
    "\n",
    "\n",
    "    # Validation loop\n",
    "    model.eval()\n",
    "    valid_loss = 0\n",
    "    with torch.no_grad():\n",
    "        i=0\n",
    "        for batch in valid_loader:\n",
    "            i+=1\n",
    "            input_ids, target_ids = batch\n",
    "            input_ids, target_ids = input_ids.to(device), target_ids.to(device)\n",
    "            outputs = model(input_ids, labels=target_ids)\n",
    "            loss = loss_fn(outputs.logits.view(-1, outputs.logits.size(-1)), target_ids.view(-1))\n",
    "            valid_loss += loss.item()\n",
    "            print(f'\\rEpoch {epoch+1}/{num_epochs}, Valid Batch {i}/{len(valid_loader)}, Valid Loss: {valid_loss/len(valid_loader)}',end=\"\")\n",
    "\n",
    "    print(f'\\rEpoch {epoch+1}/{num_epochs}, Train Loss: {train_loss/len(train_loader)}, Valid Loss: {valid_loss/len(valid_loader)}, lr: {learning_rate}',end=\"\\n\")\n",
    "    learning_rate /= 5\n",
    "model.save_pretrained('brunosan/GPT2-impactscience')\n",
    "tokenizer.save_pretrained('brunosan/GPT2-impactscience')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_pretrained('brunosan/GPT2-impactscience')\n",
    "tokenizer.save_pretrained('brunosan/GPT2-impactscience')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Antonio, who died in 2015, was a brilliant engineer who was able to master complex networks of increasingly complex projects. He was also very involved on airborne radars, ground planes, and hovercrafts. In the mid-90s, several European nations, some being primary producers of CFC, began considering regulations, or even creating public funds to assist their efforts. While extremely conscious of the environmental impact, at the same time, he also advocated for non-proliferation, peaceful'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n",
    "\n",
    "# Define the device to run the model on\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "\n",
    "# Load the fine-tuned model\n",
    "model_path = 'brunosan/GPT2-impactscience'\n",
    "tokenizer = GPT2Tokenizer.from_pretrained(model_path)\n",
    "model = GPT2LMHeadModel.from_pretrained(model_path).to(device)\n",
    "\n",
    "# Set the pad_token_id to the same value as the unk_token_id\n",
    "#model.config.pad_token_id = tokenizer.unk_token_id\n",
    "\n",
    "# Set the generation parameters\n",
    "max_length = 100\n",
    "num_beams = 5\n",
    "no_repeat_ngram_size = 2\n",
    "temperature = 1.0\n",
    "\n",
    "# Generate text using beam search, n-grams, and other techniques\n",
    "prompt = \"The impact of climate change on \"\n",
    "\n",
    "def generate(prompt):\n",
    "    input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)\n",
    "    attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=device)\n",
    "    outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask,\n",
    "                          max_length=max_length, num_beams=num_beams,\n",
    "                          no_repeat_ngram_size=no_repeat_ngram_size,\n",
    "                          temperature=temperature, do_sample=True, top_p=0.95,\n",
    "                          top_k=50)\n",
    "\n",
    "    # Convert the generated output to string format\n",
    "    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
    "    return generated_text\n",
    "\n",
    "generate(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ece98d6d9c9149ca8f4caee4b2ad9e34",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Upload 1 LFS files:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6a183417e6304bd99a135703d3d88717",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "pytorch_model.bin:   0%|          | 0.00/510M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/brunosan/GPT2-impactscience/commit/3316819787943d7a4693aee44c786ba4c2b4fc7f', commit_message='Upload tokenizer', commit_description='', oid='3316819787943d7a4693aee44c786ba4c2b4fc7f', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.push_to_hub(\"brunosan/GPT2-impactscience\")\n",
    "tokenizer.push_to_hub(\"brunosan/GPT2-impactscience\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting gradio\n",
      "  Downloading gradio-3.19.1-py3-none-any.whl (14.2 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.2/14.2 MB\u001b[0m \u001b[31m36.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: markupsafe in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (2.1.1)\n",
      "Requirement already satisfied: pydantic in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (1.10.2)\n",
      "Collecting python-multipart\n",
      "  Downloading python_multipart-0.0.6-py3-none-any.whl (45 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.7/45.7 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: requests in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (2.28.1)\n",
      "Requirement already satisfied: typing-extensions in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (4.3.0)\n",
      "Collecting ffmpy\n",
      "  Downloading ffmpy-0.3.0.tar.gz (4.8 kB)\n",
      "  Preparing metadata (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25hRequirement already satisfied: pyyaml in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (5.4.1)\n",
      "Requirement already satisfied: aiohttp in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.8.1)\n",
      "Requirement already satisfied: websockets>=10.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (10.3)\n",
      "Requirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (1.23.3)\n",
      "Collecting altair>=4.2.0\n",
      "  Downloading altair-4.2.2-py3-none-any.whl (813 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m813.6/813.6 kB\u001b[0m \u001b[31m34.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting mdit-py-plugins<=0.3.3\n",
      "  Downloading mdit_py_plugins-0.3.3-py3-none-any.whl (50 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.5/50.5 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: fsspec in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (2022.8.2)\n",
      "Collecting pydub\n",
      "  Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
      "Requirement already satisfied: httpx in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.23.0)\n",
      "Requirement already satisfied: pandas in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (1.4.4)\n",
      "Requirement already satisfied: aiofiles in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.8.0)\n",
      "Requirement already satisfied: matplotlib in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.5.3)\n",
      "Requirement already satisfied: orjson in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.8.0)\n",
      "Requirement already satisfied: fastapi in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.85.0)\n",
      "Requirement already satisfied: pillow in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (9.2.0)\n",
      "Collecting pycryptodome\n",
      "  Downloading pycryptodome-3.17-cp35-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m75.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: jinja2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.1.2)\n",
      "Collecting markdown-it-py[linkify]>=2.0.0\n",
      "  Downloading markdown_it_py-2.2.0-py3-none-any.whl (84 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.5/84.5 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: uvicorn in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.18.3)\n",
      "Requirement already satisfied: jsonschema>=3.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from altair>=4.2.0->gradio) (4.16.0)\n",
      "Requirement already satisfied: toolz in /srv/conda/envs/notebook/lib/python3.9/site-packages (from altair>=4.2.0->gradio) (0.12.0)\n",
      "Requirement already satisfied: entrypoints in /srv/conda/envs/notebook/lib/python3.9/site-packages (from altair>=4.2.0->gradio) (0.4)\n",
      "Collecting mdurl~=0.1\n",
      "  Downloading mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n",
      "Collecting linkify-it-py<3,>=1\n",
      "  Downloading linkify_it_py-2.0.0-py3-none-any.whl (19 kB)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from pandas->gradio) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from pandas->gradio) (2022.2.1)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (1.7.2)\n",
      "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (2.1.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (22.1.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (1.2.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (6.0.2)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (4.0.2)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (1.3.1)\n",
      "Requirement already satisfied: starlette==0.20.4 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from fastapi->gradio) (0.20.4)\n",
      "Requirement already satisfied: anyio<5,>=3.4.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from starlette==0.20.4->fastapi->gradio) (3.6.1)\n",
      "Requirement already satisfied: httpcore<0.16.0,>=0.15.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (0.15.0)\n",
      "Requirement already satisfied: sniffio in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (1.3.0)\n",
      "Requirement already satisfied: certifi in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (2022.9.14)\n",
      "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (1.5.0)\n",
      "Requirement already satisfied: fonttools>=4.22.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (4.37.2)\n",
      "Requirement already satisfied: cycler>=0.10 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (0.11.0)\n",
      "Requirement already satisfied: pyparsing>=2.2.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (3.0.9)\n",
      "Requirement already satisfied: packaging>=20.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (21.3)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (1.4.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->gradio) (1.26.11)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->gradio) (3.3)\n",
      "Requirement already satisfied: click>=7.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from uvicorn->gradio) (8.0.4)\n",
      "Requirement already satisfied: h11>=0.8 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from uvicorn->gradio) (0.12.0)\n",
      "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from jsonschema>=3.0->altair>=4.2.0->gradio) (0.18.1)\n",
      "Collecting uc-micro-py\n",
      "  Downloading uc_micro_py-1.0.1-py3-none-any.whl (6.2 kB)\n",
      "Requirement already satisfied: six>=1.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas->gradio) (1.16.0)\n",
      "Building wheels for collected packages: ffmpy\n",
      "  Building wheel for ffmpy (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for ffmpy: filename=ffmpy-0.3.0-py3-none-any.whl size=4693 sha256=46e8ead5afd51fafc1a56d6493e03067d54d956c9883722536d9642763e1d1f4\n",
      "  Stored in directory: /home/jovyan/.cache/pip/wheels/91/e2/96/f676aa08bfd789328c6576cd0f1fde4a3d686703bb0c247697\n",
      "Successfully built ffmpy\n",
      "Installing collected packages: pydub, ffmpy, uc-micro-py, python-multipart, pycryptodome, mdurl, markdown-it-py, linkify-it-py, mdit-py-plugins, altair, gradio\n",
      "Successfully installed altair-4.2.2 ffmpy-0.3.0 gradio-3.19.1 linkify-it-py-2.0.0 markdown-it-py-2.2.0 mdit-py-plugins-0.3.3 mdurl-0.1.2 pycryptodome-3.17 pydub-0.25.1 python-multipart-0.0.6 uc-micro-py-1.0.1\n"
     ]
    }
   ],
   "source": [
    "!pip install gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/inputs.py:27: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
      "  warnings.warn(\n",
      "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
      "  warnings.warn(value)\n",
      "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `numeric` parameter is deprecated, and it has no effect\n",
      "  warnings.warn(value)\n",
      "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/outputs.py:22: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7861\n",
      "Running on public URL: https://6989ff3075913a1f9b.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://6989ff3075913a1f9b.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import torch\n",
    "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n",
    "\n",
    "# Load the fine-tuned model and tokenizer\n",
    "model_path = 'fine_tuned_model'\n",
    "tokenizer_path = 'fine_tuned_tokenizer'\n",
    "\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "tokenizer = GPT2Tokenizer.from_pretrained(tokenizer_path)\n",
    "model = GPT2LMHeadModel.from_pretrained(model_path).to(device)\n",
    "\n",
    "# Define the generation function\n",
    "def generate_text(prompt):\n",
    "    input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)\n",
    "    attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=device)\n",
    "    outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask,\n",
    "                              max_length=100, num_beams=9,\n",
    "                              no_repeat_ngram_size=2,\n",
    "                              temperature=1.0, do_sample=True,\n",
    "                              top_p=0.95, top_k=50)\n",
    "\n",
    "    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
    "    return generated_text\n",
    "\n",
    "# Create a Gradio interface\n",
    "input_text = gr.inputs.Textbox(lines=2, label=\"Enter the starting text\")\n",
    "output_text = gr.outputs.Textbox(label=\"Generated Text\")\n",
    "\n",
    "interface = gr.Interface(fn=generate_text, inputs=input_text, outputs=output_text,\n",
    "             title=\"GPT-2 Impact Science Text Generator\", description=\"Generate text using a fine-tuned GPT-2 model onthe Impact Science book.\")\n",
    "\n",
    "# Export the Gradio interface to the Hugging Face Model Hub\n",
    "interface.launch(share=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:notebook] *",
   "language": "python",
   "name": "conda-env-notebook-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "43343113618a7691a2af3d61372ef21fdddd71fdb8d3292e4208750f8afc007e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}