waddledee commited on
Commit
97d90b2
·
verified ·
1 Parent(s): ce90a2c

Upload 3行要約_Scraping.ipynb

Browse files
Files changed (1) hide show
  1. 3行要約_Scraping.ipynb +607 -0
3行要約_Scraping.ipynb ADDED
@@ -0,0 +1,607 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "f5308d26",
7
+ "metadata": {
8
+ "scrolled": true
9
+ },
10
+ "outputs": [
11
+ {
12
+ "name": "stderr",
13
+ "output_type": "stream",
14
+ "text": [
15
+ " 8%|▊ | 264/3200 [16:53<2:51:12, 3.50s/it]"
16
+ ]
17
+ },
18
+ {
19
+ "name": "stdout",
20
+ "output_type": "stream",
21
+ "text": [
22
+ "Exception\n"
23
+ ]
24
+ },
25
+ {
26
+ "name": "stderr",
27
+ "output_type": "stream",
28
+ "text": [
29
+ " 11%|█ | 350/3200 [22:08<2:46:27, 3.50s/it]"
30
+ ]
31
+ },
32
+ {
33
+ "name": "stdout",
34
+ "output_type": "stream",
35
+ "text": [
36
+ "Exception\n"
37
+ ]
38
+ },
39
+ {
40
+ "name": "stderr",
41
+ "output_type": "stream",
42
+ "text": [
43
+ " 14%|█▎ | 435/3200 [27:25<2:55:49, 3.82s/it]"
44
+ ]
45
+ },
46
+ {
47
+ "name": "stdout",
48
+ "output_type": "stream",
49
+ "text": [
50
+ "Exception\n"
51
+ ]
52
+ },
53
+ {
54
+ "name": "stderr",
55
+ "output_type": "stream",
56
+ "text": [
57
+ " 15%|█▍ | 479/3200 [30:17<2:46:24, 3.67s/it]"
58
+ ]
59
+ },
60
+ {
61
+ "name": "stdout",
62
+ "output_type": "stream",
63
+ "text": [
64
+ "Exception\n"
65
+ ]
66
+ },
67
+ {
68
+ "name": "stderr",
69
+ "output_type": "stream",
70
+ "text": [
71
+ " 29%|██▊ | 919/3200 [59:14<2:24:16, 3.79s/it]"
72
+ ]
73
+ },
74
+ {
75
+ "name": "stdout",
76
+ "output_type": "stream",
77
+ "text": [
78
+ "Exception\n"
79
+ ]
80
+ },
81
+ {
82
+ "name": "stderr",
83
+ "output_type": "stream",
84
+ "text": [
85
+ " 50%|████▉ | 1598/3200 [1:43:30<1:35:05, 3.56s/it]"
86
+ ]
87
+ },
88
+ {
89
+ "name": "stdout",
90
+ "output_type": "stream",
91
+ "text": [
92
+ "Exception\n"
93
+ ]
94
+ },
95
+ {
96
+ "name": "stderr",
97
+ "output_type": "stream",
98
+ "text": [
99
+ " 52%|█████▏ | 1667/3200 [1:47:59<1:31:01, 3.56s/it]"
100
+ ]
101
+ },
102
+ {
103
+ "name": "stdout",
104
+ "output_type": "stream",
105
+ "text": [
106
+ "Exception\n"
107
+ ]
108
+ },
109
+ {
110
+ "name": "stderr",
111
+ "output_type": "stream",
112
+ "text": [
113
+ " 52%|█████▎ | 1680/3200 [1:48:47<1:29:22, 3.53s/it]"
114
+ ]
115
+ },
116
+ {
117
+ "name": "stdout",
118
+ "output_type": "stream",
119
+ "text": [
120
+ "Exception\n"
121
+ ]
122
+ },
123
+ {
124
+ "name": "stderr",
125
+ "output_type": "stream",
126
+ "text": [
127
+ "\r",
128
+ " 53%|█████▎ | 1681/3200 [1:48:50<1:25:53, 3.39s/it]"
129
+ ]
130
+ },
131
+ {
132
+ "name": "stdout",
133
+ "output_type": "stream",
134
+ "text": [
135
+ "Exception\n"
136
+ ]
137
+ },
138
+ {
139
+ "name": "stderr",
140
+ "output_type": "stream",
141
+ "text": [
142
+ " 53%|█████▎ | 1696/3200 [1:49:46<1:30:02, 3.59s/it]"
143
+ ]
144
+ },
145
+ {
146
+ "name": "stdout",
147
+ "output_type": "stream",
148
+ "text": [
149
+ "Exception\n"
150
+ ]
151
+ },
152
+ {
153
+ "name": "stderr",
154
+ "output_type": "stream",
155
+ "text": [
156
+ " 53%|█████▎ | 1703/3200 [1:50:12<1:27:52, 3.52s/it]"
157
+ ]
158
+ },
159
+ {
160
+ "name": "stdout",
161
+ "output_type": "stream",
162
+ "text": [
163
+ "Exception\n"
164
+ ]
165
+ },
166
+ {
167
+ "name": "stderr",
168
+ "output_type": "stream",
169
+ "text": [
170
+ " 69%|██████▊ | 2193/3200 [2:20:32<59:49, 3.56s/it] "
171
+ ]
172
+ },
173
+ {
174
+ "name": "stdout",
175
+ "output_type": "stream",
176
+ "text": [
177
+ "Exception\n"
178
+ ]
179
+ },
180
+ {
181
+ "name": "stderr",
182
+ "output_type": "stream",
183
+ "text": [
184
+ " 70%|███████ | 2240/3200 [2:23:29<57:22, 3.59s/it] "
185
+ ]
186
+ },
187
+ {
188
+ "name": "stdout",
189
+ "output_type": "stream",
190
+ "text": [
191
+ "Exception\n"
192
+ ]
193
+ },
194
+ {
195
+ "name": "stderr",
196
+ "output_type": "stream",
197
+ "text": [
198
+ " 72%|███████▏ | 2309/3200 [2:27:49<52:10, 3.51s/it] "
199
+ ]
200
+ },
201
+ {
202
+ "name": "stdout",
203
+ "output_type": "stream",
204
+ "text": [
205
+ "Exception\n"
206
+ ]
207
+ },
208
+ {
209
+ "name": "stderr",
210
+ "output_type": "stream",
211
+ "text": [
212
+ " 75%|███████▌ | 2406/3200 [2:33:56<47:49, 3.61s/it]"
213
+ ]
214
+ },
215
+ {
216
+ "name": "stdout",
217
+ "output_type": "stream",
218
+ "text": [
219
+ "Exception\n"
220
+ ]
221
+ },
222
+ {
223
+ "name": "stderr",
224
+ "output_type": "stream",
225
+ "text": [
226
+ " 76%|███████▌ | 2421/3200 [2:34:53<46:57, 3.62s/it]"
227
+ ]
228
+ },
229
+ {
230
+ "name": "stdout",
231
+ "output_type": "stream",
232
+ "text": [
233
+ "Exception\n"
234
+ ]
235
+ },
236
+ {
237
+ "name": "stderr",
238
+ "output_type": "stream",
239
+ "text": [
240
+ " 78%|███████▊ | 2483/3200 [2:38:41<41:46, 3.50s/it]"
241
+ ]
242
+ },
243
+ {
244
+ "name": "stdout",
245
+ "output_type": "stream",
246
+ "text": [
247
+ "Exception\n"
248
+ ]
249
+ },
250
+ {
251
+ "name": "stderr",
252
+ "output_type": "stream",
253
+ "text": [
254
+ " 83%|███████��▎ | 2657/3200 [2:49:21<31:59, 3.53s/it]"
255
+ ]
256
+ },
257
+ {
258
+ "name": "stdout",
259
+ "output_type": "stream",
260
+ "text": [
261
+ "Exception\n"
262
+ ]
263
+ },
264
+ {
265
+ "name": "stderr",
266
+ "output_type": "stream",
267
+ "text": [
268
+ " 83%|████████▎ | 2667/3200 [2:49:57<30:42, 3.46s/it]"
269
+ ]
270
+ },
271
+ {
272
+ "name": "stdout",
273
+ "output_type": "stream",
274
+ "text": [
275
+ "Exception\n"
276
+ ]
277
+ },
278
+ {
279
+ "name": "stderr",
280
+ "output_type": "stream",
281
+ "text": [
282
+ " 84%|████████▍ | 2690/3200 [2:51:21<29:56, 3.52s/it]"
283
+ ]
284
+ },
285
+ {
286
+ "name": "stdout",
287
+ "output_type": "stream",
288
+ "text": [
289
+ "Exception\n"
290
+ ]
291
+ },
292
+ {
293
+ "name": "stderr",
294
+ "output_type": "stream",
295
+ "text": [
296
+ " 85%|████████▍ | 2708/3200 [2:52:27<28:48, 3.51s/it]"
297
+ ]
298
+ },
299
+ {
300
+ "name": "stdout",
301
+ "output_type": "stream",
302
+ "text": [
303
+ "Exception\n"
304
+ ]
305
+ },
306
+ {
307
+ "name": "stderr",
308
+ "output_type": "stream",
309
+ "text": [
310
+ " 85%|████████▍ | 2710/3200 [2:52:34<28:06, 3.44s/it]"
311
+ ]
312
+ },
313
+ {
314
+ "name": "stdout",
315
+ "output_type": "stream",
316
+ "text": [
317
+ "Exception\n"
318
+ ]
319
+ },
320
+ {
321
+ "name": "stderr",
322
+ "output_type": "stream",
323
+ "text": [
324
+ " 85%|████████▍ | 2713/3200 [2:52:44<27:39, 3.41s/it]"
325
+ ]
326
+ },
327
+ {
328
+ "name": "stdout",
329
+ "output_type": "stream",
330
+ "text": [
331
+ "Exception\n"
332
+ ]
333
+ },
334
+ {
335
+ "name": "stderr",
336
+ "output_type": "stream",
337
+ "text": [
338
+ " 85%|████████▌ | 2725/3200 [2:53:28<27:43, 3.50s/it]"
339
+ ]
340
+ },
341
+ {
342
+ "name": "stdout",
343
+ "output_type": "stream",
344
+ "text": [
345
+ "Exception\n"
346
+ ]
347
+ },
348
+ {
349
+ "name": "stderr",
350
+ "output_type": "stream",
351
+ "text": [
352
+ " 86%|████████▌ | 2749/3200 [2:54:56<26:28, 3.52s/it]"
353
+ ]
354
+ },
355
+ {
356
+ "name": "stdout",
357
+ "output_type": "stream",
358
+ "text": [
359
+ "Exception\n"
360
+ ]
361
+ },
362
+ {
363
+ "name": "stderr",
364
+ "output_type": "stream",
365
+ "text": [
366
+ " 86%|████████▋ | 2761/3200 [2:55:41<26:05, 3.57s/it]"
367
+ ]
368
+ },
369
+ {
370
+ "name": "stdout",
371
+ "output_type": "stream",
372
+ "text": [
373
+ "Exception\n"
374
+ ]
375
+ },
376
+ {
377
+ "name": "stderr",
378
+ "output_type": "stream",
379
+ "text": [
380
+ "100%|██████████| 3200/3200 [3:22:49<00:00, 3.80s/it]\n"
381
+ ]
382
+ }
383
+ ],
384
+ "source": [
385
+ "from urllib.request import urlopen\n",
386
+ "from bs4 import BeautifulSoup\n",
387
+ "from bs4.element import NavigableString\n",
388
+ "from pprint import pprint\n",
389
+ "import time\n",
390
+ "import tqdm\n",
391
+ "\n",
392
+ "# 収集するニュース記事のインデックス\n",
393
+ "start_index = 2800 # 開始インデックス\n",
394
+ "end_index = 6000 # 終了インデックス\n",
395
+ "\n",
396
+ "\n",
397
+ "\n",
398
+ "\n",
399
+ "# コンテンツの取得\n",
400
+ "def get_content(id):\n",
401
+ " # サーバに負荷をかけないように3秒スリープ\n",
402
+ " time.sleep(3)\n",
403
+ "\n",
404
+ " URL = 'https://news.livedoor.com/article/detail/'+id+'/'\n",
405
+ "# print(URL)\n",
406
+ " try:\n",
407
+ " with urlopen(URL) as res:\n",
408
+ " # 本文の抽出\n",
409
+ " output1 = ''\n",
410
+ " html = res.read().decode('euc_jp', 'ignore')\n",
411
+ " soup = BeautifulSoup(html, 'html.parser')\n",
412
+ " line_list = soup.select('.articleBody p')\n",
413
+ " for line in line_list:\n",
414
+ " if len(line.contents) > 0 and type(line.contents[0]) == NavigableString:\n",
415
+ " output1 += line.contents[0].strip()\n",
416
+ " if output1 == '': # 記事がない\n",
417
+ " return\n",
418
+ " \n",
419
+ "\n",
420
+ " # 要約の抽出\n",
421
+ " output0 = ''\n",
422
+ " summary_list = soup.select('.summaryList li')\n",
423
+ " for summary in summary_list:\n",
424
+ " output0 += summary.contents[0].strip()+'\\n'\n",
425
+ " output0 = output0.strip(\"\\n\")\n",
426
+ " if output0 == '': # 記事がない\n",
427
+ " return\n",
428
+ "\n",
429
+ " # 出力\n",
430
+ "# print(output0+output1)\n",
431
+ " \n",
432
+ " prompt = f\"\"\"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n",
433
+ "### Instruction:\n",
434
+ "以下の文を3行で要約しなさい\n",
435
+ "### Input:\n",
436
+ "{output1}\n",
437
+ "### Response:\n",
438
+ "{output0}\n",
439
+ "\"\"\"\n",
440
+ " return prompt\n",
441
+ " except Exception:\n",
442
+ " print('Exception')\n",
443
+ "\n",
444
+ "# IDリストの生成の取得\n",
445
+ "id_list = []\n",
446
+ "with open('downloaded_train.csv', mode='r') as f:\n",
447
+ " lines = f.readlines()\n",
448
+ " for line in lines:\n",
449
+ " id = line.strip().split(',')[3].split('.')[0]\n",
450
+ " id_list.append(id)\n",
451
+ "\n",
452
+ "# コンテンツの取得\n",
453
+ "output = []\n",
454
+ "for i in tqdm.tqdm(range(start_index, end_index)):\n",
455
+ "# print('index:', i)\n",
456
+ " gen = get_content(id_list[i])\n",
457
+ " if gen!= \"\":\n",
458
+ " output.append(gen)"
459
+ ]
460
+ },
461
+ {
462
+ "cell_type": "code",
463
+ "execution_count": 8,
464
+ "id": "309fe137",
465
+ "metadata": {},
466
+ "outputs": [
467
+ {
468
+ "name": "stdout",
469
+ "output_type": "stream",
470
+ "text": [
471
+ "CSVファイル 'three_line_summarize_2800_6000_.csv' が作成されました。\n"
472
+ ]
473
+ }
474
+ ],
475
+ "source": [
476
+ "import csv\n",
477
+ "\n",
478
+ "def create_csv_from_list(data_list, file_path):\n",
479
+ " # CSVファイルのカラム名\n",
480
+ " fieldnames = [\"text\"]\n",
481
+ "\n",
482
+ " # CSVファイルを書き込む\n",
483
+ " with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:\n",
484
+ " writer = csv.writer(csvfile)\n",
485
+ "\n",
486
+ " # カラム名を書き込む\n",
487
+ " writer.writerow(fieldnames)\n",
488
+ "\n",
489
+ " # データをリストとしてCSVファイルに書き込む\n",
490
+ " for item in data_list:\n",
491
+ " writer.writerow([item])\n",
492
+ "\n",
493
+ "\n",
494
+ "\n",
495
+ "# CSVファイルを作成する\n",
496
+ "csv_file_path = \"three_line_summarize_2800_6000_.csv\"\n",
497
+ "out = [i for i in output if i!=None]\n",
498
+ "create_csv_from_list(out, csv_file_path)\n",
499
+ "\n",
500
+ "print(f\"CSVファイル '{csv_file_path}' が作成されました。\")"
501
+ ]
502
+ },
503
+ {
504
+ "cell_type": "code",
505
+ "execution_count": 3,
506
+ "id": "7ce6ef7e",
507
+ "metadata": {},
508
+ "outputs": [
509
+ {
510
+ "data": {
511
+ "text/plain": [
512
+ "3200"
513
+ ]
514
+ },
515
+ "execution_count": 3,
516
+ "metadata": {},
517
+ "output_type": "execute_result"
518
+ }
519
+ ],
520
+ "source": [
521
+ "len(output)"
522
+ ]
523
+ },
524
+ {
525
+ "cell_type": "code",
526
+ "execution_count": 6,
527
+ "id": "7246a989",
528
+ "metadata": {},
529
+ "outputs": [],
530
+ "source": [
531
+ "out = [i for i in output if i!=None]"
532
+ ]
533
+ },
534
+ {
535
+ "cell_type": "code",
536
+ "execution_count": 7,
537
+ "id": "71015093",
538
+ "metadata": {},
539
+ "outputs": [
540
+ {
541
+ "data": {
542
+ "text/plain": [
543
+ "1454"
544
+ ]
545
+ },
546
+ "execution_count": 7,
547
+ "metadata": {},
548
+ "output_type": "execute_result"
549
+ }
550
+ ],
551
+ "source": [
552
+ "len(out)"
553
+ ]
554
+ },
555
+ {
556
+ "cell_type": "code",
557
+ "execution_count": null,
558
+ "id": "dfa94b6b",
559
+ "metadata": {},
560
+ "outputs": [],
561
+ "source": [
562
+ "import re\n",
563
+ "\n",
564
+ "def new_format(text):\n",
565
+ " # 正規表現を使用してInputとResponseを抽出\n",
566
+ " input_match = re.search(r'Input:(.*?)###', text, re.DOTALL)\n",
567
+ " response_match = re.search(r'Response:(.*?)$', text, re.DOTALL)\n",
568
+ "\n",
569
+ " if input_match and response_match:\n",
570
+ " input_text = input_match.group(1).strip()\n",
571
+ " response_text = response_match.group(1).strip()\n",
572
+ " new_format = f\"\"\"<START_Q>{input_text}<END_Q>\n",
573
+ "<START_A>{response_text}<END_A>\"\"\"\n",
574
+ " return new_format\n",
575
+ "\n",
576
+ "from datasets import load_dataset, DatasetDict,Dataset\n",
577
+ "from sklearn.model_selection import train_test_split\n",
578
+ "import pandas as pd\n",
579
+ "\n",
580
+ "# df=pd.read_csv(\"llama2_ft_data/three_line_summarize.csv\", index_col=0)\n",
581
+ "df=pd.read_csv(\"llama2_ft_data/three_line_summarize_concat.csv\")\n",
582
+ "df[\"text\"] = df[\"text\"].apply(new_format)"
583
+ ]
584
+ }
585
+ ],
586
+ "metadata": {
587
+ "kernelspec": {
588
+ "display_name": "llama2",
589
+ "language": "python",
590
+ "name": "llama2"
591
+ },
592
+ "language_info": {
593
+ "codemirror_mode": {
594
+ "name": "ipython",
595
+ "version": 3
596
+ },
597
+ "file_extension": ".py",
598
+ "mimetype": "text/x-python",
599
+ "name": "python",
600
+ "nbconvert_exporter": "python",
601
+ "pygments_lexer": "ipython3",
602
+ "version": "3.9.0"
603
+ }
604
+ },
605
+ "nbformat": 4,
606
+ "nbformat_minor": 5
607
+ }