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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f133d1af",
   "metadata": {},
   "source": [
    "# Working with Data"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "231cb7ec",
   "metadata": {},
   "source": [
    "In this notebook, we are going to work on the datasets that are going to be used in order to fine tune our NLP models.  \n",
    "The different steps that we are going to follow are:\n",
    "\n",
    "- Scraping ads data from Moroccan youtube products channels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8313dd19",
   "metadata": {},
   "source": [
    "# Scraping Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "978c782a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.options import Options\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from bs4 import BeautifulSoup\n",
    "import time\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "5c643463",
   "metadata": {},
   "outputs": [],
   "source": [
    "def scrape_ads(url, channel_name, max_scroll=3, wait_time=2):\n",
    "    options = Options()\n",
    "    options.headless = True\n",
    "    options.add_argument(\"--disable-gpu\")\n",
    "    options.add_argument(\"--no-sandbox\")\n",
    "    \n",
    "    # Mute the audio\n",
    "    options.add_argument(\"--mute-audio\")\n",
    "\n",
    "    driver = webdriver.Edge(options=options)\n",
    "\n",
    "    driver.get(url)\n",
    "\n",
    "    # Scroll down to load more videos\n",
    "    height = driver.execute_script(\"return document.documentElement.scrollHeight\")\n",
    "    while True:\n",
    "        driver.execute_script(\"window.scrollTo(0, document.documentElement.scrollHeight);\")\n",
    "        time.sleep(3)  # Allow time for the page to load\n",
    "        new_height = driver.execute_script(\"return document.documentElement.scrollHeight\")\n",
    "        if new_height == height:\n",
    "            time.sleep(5)  # Wait a bit longer to check if more content loads\n",
    "            new_height = driver.execute_script(\"return document.documentElement.scrollHeight\")\n",
    "            if new_height == height:  # If the height is still the same, we are at the bottom\n",
    "                break\n",
    "        height = new_height\n",
    "\n",
    "    src = driver.page_source\n",
    "    soup = BeautifulSoup(src, 'lxml')\n",
    "    video_links = soup.find_all('a', {'class': 'yt-simple-endpoint focus-on-expand style-scope ytd-rich-grid-media'})\n",
    "    links = [\"https://www.youtube.com\" + link.get('href') for link in video_links if link.get('href')]\n",
    "\n",
    "    ads = []\n",
    "    titles = []\n",
    "    for link in links:\n",
    "        driver.get(link)\n",
    "        time.sleep(5)\n",
    "\n",
    "        src = driver.page_source\n",
    "        soup = BeautifulSoup(src, 'lxml')\n",
    "\n",
    "        ad_text_span = soup.find('span', class_='yt-core-attributed-string--link-inherit-color')\n",
    "        ad_text = ad_text_span.text.strip() if ad_text_span else \"No ad found\"\n",
    "        ads.append(ad_text)\n",
    "\n",
    "        title_tag = soup.find('h1', {'class': 'title style-scope ytd-video-primary-info-renderer'})\n",
    "        video_title = title_tag.text.strip() if title_tag else 'Title not found'\n",
    "        titles.append(video_title)\n",
    "\n",
    "    driver.quit()\n",
    "    return ads, titles, links"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "939fa26b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ADS\\AppData\\Local\\Temp\\ipykernel_14604\\116512856.py:3: DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new')\n",
      "  options.headless = True\n"
     ]
    }
   ],
   "source": [
    "channel = '@inwi'\n",
    "url = f\"https://www.youtube.com/{channel}/videos\"\n",
    "ads, titles, links = scrape_ads(url, channel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "188b9ba2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1380, 1380, 1380)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ads), len(titles), len(links)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "d6dedc02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# put ads, titles and links in a dataframe\n",
    "df3 = pd.DataFrame({'ad': ads, 'title': titles, 'link': links})\n",
    "\n",
    "# add channel column\n",
    "df3['channel'] = channel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "c7a6bb24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ad</th>\n",
       "      <th>title</th>\n",
       "      <th>link</th>\n",
       "      <th>channel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1375</th>\n",
       "      <td>initiative e-Madrassa proposée par inwi (Souti...</td>\n",
       "      <td>النشاط العلمي - حت التربة - الجزء الأول Janvie...</td>\n",
       "      <td>https://www.youtube.com/watch?v=9z2eSPv7dLo</td>\n",
       "      <td>@inwi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1376</th>\n",
       "      <td>initiative e-Madrassa proposée par inwi (Souti...</td>\n",
       "      <td>اللغة العربية -أنشطة تطبيقية - الجزء الثاني - ...</td>\n",
       "      <td>https://www.youtube.com/watch?v=IgsAKIPPjig</td>\n",
       "      <td>@inwi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1377</th>\n",
       "      <td>Le présent de l'indicatif des verbes pronominaux</td>\n",
       "      <td>Le présent de l'indicatif des verbes pronominaux</td>\n",
       "      <td>https://www.youtube.com/watch?v=NvYs05Ix3OY</td>\n",
       "      <td>@inwi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1378</th>\n",
       "      <td>Soutien scolaire - Français - Novembre 2012</td>\n",
       "      <td>Soutien scolaire - Français - Novembre 2012</td>\n",
       "      <td>https://www.youtube.com/watch?v=ThXZp-jRrUY</td>\n",
       "      <td>@inwi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1379</th>\n",
       "      <td>Les contes marocains par la voix de Rachi El O...</td>\n",
       "      <td>Emadrassa - L'espace de lecture présenté par R...</td>\n",
       "      <td>https://www.youtube.com/watch?v=OPaECpKBl6I</td>\n",
       "      <td>@inwi</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     ad   \n",
       "1375  initiative e-Madrassa proposée par inwi (Souti...  \\\n",
       "1376  initiative e-Madrassa proposée par inwi (Souti...   \n",
       "1377   Le présent de l'indicatif des verbes pronominaux   \n",
       "1378       Soutien scolaire - Français - Novembre 2012   \n",
       "1379  Les contes marocains par la voix de Rachi El O...   \n",
       "\n",
       "                                                  title   \n",
       "1375  النشاط العلمي - حت التربة - الجزء الأول Janvie...  \\\n",
       "1376  اللغة العربية -أنشطة تطبيقية - الجزء الثاني - ...   \n",
       "1377   Le présent de l'indicatif des verbes pronominaux   \n",
       "1378       Soutien scolaire - Français - Novembre 2012   \n",
       "1379  Emadrassa - L'espace de lecture présenté par R...   \n",
       "\n",
       "                                             link channel  \n",
       "1375  https://www.youtube.com/watch?v=9z2eSPv7dLo   @inwi  \n",
       "1376  https://www.youtube.com/watch?v=IgsAKIPPjig   @inwi  \n",
       "1377  https://www.youtube.com/watch?v=NvYs05Ix3OY   @inwi  \n",
       "1378  https://www.youtube.com/watch?v=ThXZp-jRrUY   @inwi  \n",
       "1379  https://www.youtube.com/watch?v=OPaECpKBl6I   @inwi  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "e9f0c14f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# concat df and df1 , df1, df3\n",
    "df_ads = pd.concat([df, df1, df2, df3], ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "94398c8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ad</th>\n",
       "      <th>title</th>\n",
       "      <th>link</th>\n",
       "      <th>channel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>بشراكة مع بنك التغدية، مؤسسة أورنج تلتزم بالمس...</td>\n",
       "      <td>Orange Maroc : خطوة خير</td>\n",
       "      <td>https://www.youtube.com/watch?v=3RoNBU-snrc</td>\n",
       "      <td>@orangemaroc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>خدمة Transfert internet كاينة مع أورنج 🧡\\n\\nعن...</td>\n",
       "      <td>Orange Maroc: Transfert de solde</td>\n",
       "      <td>https://www.youtube.com/watch?v=oQKRx9i2NdY</td>\n",
       "      <td>@orangemaroc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>أورنج ديما كاينة معاك !🧡\\n\\nمع خدمة solde dépa...</td>\n",
       "      <td>Orange Maroc: Solde dépannage</td>\n",
       "      <td>https://www.youtube.com/watch?v=ElW8plKHRrI</td>\n",
       "      <td>@orangemaroc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>لفراجة كاينة مع La Fibre d'Orange  !\\nنتا غير ...</td>\n",
       "      <td>Orange Maroc : La Fibre d'Orange  لفراجة كاينة مع</td>\n",
       "      <td>https://www.youtube.com/watch?v=sDt4tK29Vy8</td>\n",
       "      <td>@orangemaroc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>سخاوة النجمة 3 المضوبلة كاينة ! 😍\\nاستافدو من ...</td>\n",
       "      <td>Orange Maroc : النجمة 3 المضوبلة كاينة</td>\n",
       "      <td>https://www.youtube.com/watch?v=U2VOn8mhN8U</td>\n",
       "      <td>@orangemaroc</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  ad   \n",
       "0  بشراكة مع بنك التغدية، مؤسسة أورنج تلتزم بالمس...  \\\n",
       "1  خدمة Transfert internet كاينة مع أورنج 🧡\\n\\nعن...   \n",
       "2  أورنج ديما كاينة معاك !🧡\\n\\nمع خدمة solde dépa...   \n",
       "3  لفراجة كاينة مع La Fibre d'Orange  !\\nنتا غير ...   \n",
       "4  سخاوة النجمة 3 المضوبلة كاينة ! 😍\\nاستافدو من ...   \n",
       "\n",
       "                                               title   \n",
       "0                            Orange Maroc : خطوة خير  \\\n",
       "1                   Orange Maroc: Transfert de solde   \n",
       "2                      Orange Maroc: Solde dépannage   \n",
       "3  Orange Maroc : La Fibre d'Orange  لفراجة كاينة مع   \n",
       "4             Orange Maroc : النجمة 3 المضوبلة كاينة   \n",
       "\n",
       "                                          link       channel  \n",
       "0  https://www.youtube.com/watch?v=3RoNBU-snrc  @orangemaroc  \n",
       "1  https://www.youtube.com/watch?v=oQKRx9i2NdY  @orangemaroc  \n",
       "2  https://www.youtube.com/watch?v=ElW8plKHRrI  @orangemaroc  \n",
       "3  https://www.youtube.com/watch?v=sDt4tK29Vy8  @orangemaroc  \n",
       "4  https://www.youtube.com/watch?v=U2VOn8mhN8U  @orangemaroc  "
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_ads.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "bb4effba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3992 entries, 0 to 3991\n",
      "Data columns (total 4 columns):\n",
      " #   Column   Non-Null Count  Dtype \n",
      "---  ------   --------------  ----- \n",
      " 0   ad       3992 non-null   object\n",
      " 1   title    3992 non-null   object\n",
      " 2   link     3992 non-null   object\n",
      " 3   channel  3992 non-null   object\n",
      "dtypes: object(4)\n",
      "memory usage: 124.9+ KB\n"
     ]
    }
   ],
   "source": [
    "df_ads.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "76633f3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# push to huggingface dataset\n",
    "from datasets import Dataset\n",
    "\n",
    "dataset = Dataset.from_pandas(df_ads)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "7e41b632",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "40e4f65f82e44cfb8a28f6da043299bd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from huggingface_hub import notebook_login\n",
    "notebook_login()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "6b3bd971",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c35e547ccf024c4580e2433504954f89",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Pushing dataset shards to the dataset hub:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "48d39e651d034177bcd28097a735febd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/4 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b8dde35a9d784fcf81aeeb610837e73d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Deleting unused files from dataset repository:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "335d994ba0bf4e069f562eb0e68e1fca",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading metadata:   0%|          | 0.00/538 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Push the dataset to your Hugging Face account\n",
    "dataset.push_to_hub('MLINSEA/Moroccan_ads')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "undefined.undefined.undefined"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}