File size: 13,060 Bytes
0685af6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ETL to get the text data from the playlist\n",
    "\n",
    "This notebook shows the process of building the corpus of transcripts from the YouTube playlist.\n",
    "\n",
    "**Extract**: Pull data (transcripts) from each video.  \n",
    "**Transform**:  \n",
    "**Load**: Load data into our database where it will be retrieved from.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from models import etl\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we load the video information. This includes the video IDs and titles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('data/single_video.json') as f:\n",
    "    video_info = json.load(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we must extract the transcripts using the YouTube Transcript API. This is done over all of the videos.  \n",
    "This produces a list of video segments with timestamps.  \n",
    "Next, we format the transcript by adding metadata so that the segments are easily identified for retreival later.  \n",
    "Since the original segments are small, they are batched with overlap to preserve semantic meaning."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "get_video_transcript took 0.84 seconds.\n",
      "Transcript for video 5sLYAQS9sWQ fetched.\n"
     ]
    }
   ],
   "source": [
    "videos = []\n",
    "for video in video_info:\n",
    "    video_id = video[\"id\"]\n",
    "    video_title = video[\"title\"]\n",
    "    transcript = etl.get_video_transcript(video_id)\n",
    "    print(f\"Transcript for video {video_id} fetched.\")\n",
    "    if transcript:\n",
    "        formatted_transcript = etl.format_transcript(transcript, video_id, video_title, batch_size=5, overlap=2)\n",
    "        \n",
    "        videos.extend(formatted_transcript)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The last step is to load the data into a database. We will use a Chromadb database.  \n",
    "The embedding function is the ____ model from HuggingFace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database created at data/single_video.db\n"
     ]
    }
   ],
   "source": [
    "# Initialize the database\n",
    "from utils.embedding_utils import MyEmbeddingFunction\n",
    "import chromadb\n",
    "\n",
    "embed_text = MyEmbeddingFunction()\n",
    "\n",
    "db_path = \"data/single_video.db\"\n",
    "client = chromadb.PersistentClient(path=db_path)\n",
    "\n",
    "client.create_collection(\n",
    "    name=\"huberman_videos\",\n",
    "    embedding_function=embed_text,\n",
    "    metadata={\"hnsw:space\": \"cosine\"}\n",
    ")\n",
    "\n",
    "print(f\"Database created at {db_path}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data loaded to database at data/single_video.db.\n"
     ]
    }
   ],
   "source": [
    "# Add the data to the database\n",
    "client = chromadb.PersistentClient(path=db_path)\n",
    "    \n",
    "collection = client.get_collection(\"huberman_videos\")\n",
    "\n",
    "documents = [segment['text'] for segment in videos]\n",
    "metadata = [segment['metadata'] for segment in videos]\n",
    "ids = [segment['metadata']['segment_id'] for segment in videos]\n",
    "\n",
    "collection.add(\n",
    "    documents=documents,\n",
    "    metadatas=metadata,\n",
    "    ids=ids\n",
    ")\n",
    "\n",
    "print(f\"Data loaded to database at {db_path}.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is some of the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of segments: 26\n"
     ]
    },
    {
     "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>ids</th>\n",
       "      <th>embeddings</th>\n",
       "      <th>metadatas</th>\n",
       "      <th>documents</th>\n",
       "      <th>uris</th>\n",
       "      <th>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5sLYAQS9sWQ__0</td>\n",
       "      <td>[-0.11489544063806534, -0.03262839838862419, -...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__0', 'source': 'ht...</td>\n",
       "      <td>GPT, or Generative Pre-trained Transformer, is...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5sLYAQS9sWQ__12</td>\n",
       "      <td>[0.094169981777668, -0.10430295020341873, 0.02...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__12', 'source': 'h...</td>\n",
       "      <td>Now foundation models are pre-trained on large...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5sLYAQS9sWQ__15</td>\n",
       "      <td>[0.042587604373693466, -0.061460819095373154, ...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__15', 'source': 'h...</td>\n",
       "      <td>I'm talking about things like code. Now, large...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5sLYAQS9sWQ__18</td>\n",
       "      <td>[-0.0245895367115736, -0.058405470103025436, -...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__18', 'source': 'h...</td>\n",
       "      <td>these models can be tens of gigabytes in size ...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5sLYAQS9sWQ__21</td>\n",
       "      <td>[0.05348338559269905, -0.016104578971862793, -...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__21', 'source': 'h...</td>\n",
       "      <td>So to put that into perspective, a text file t...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5sLYAQS9sWQ__24</td>\n",
       "      <td>[0.07004527002573013, -0.08996045589447021, -0...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__24', 'source': 'h...</td>\n",
       "      <td>A lot of words just in one Gb. And how many gi...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5sLYAQS9sWQ__27</td>\n",
       "      <td>[0.0283487681299448, -0.11020224541425705, -0....</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__27', 'source': 'h...</td>\n",
       "      <td>Yeah, that's truly a lot of text. And LLMs are...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5sLYAQS9sWQ__3</td>\n",
       "      <td>[-0.0700172707438469, -0.061202701181173325, -...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__3', 'source': 'ht...</td>\n",
       "      <td>And I've been using GPT in its various forms f...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5sLYAQS9sWQ__30</td>\n",
       "      <td>[-0.04904637485742569, -0.1277533322572708, -0...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__30', 'source': 'h...</td>\n",
       "      <td>and the more parameters a model has, the more ...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5sLYAQS9sWQ__33</td>\n",
       "      <td>[0.03286760300397873, -0.041724931448698044, 0...</td>\n",
       "      <td>{'segment_id': '5sLYAQS9sWQ__33', 'source': 'h...</td>\n",
       "      <td>All right, so how do they work? Well, we can t...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               ids                                         embeddings  \\\n",
       "0   5sLYAQS9sWQ__0  [-0.11489544063806534, -0.03262839838862419, -...   \n",
       "1  5sLYAQS9sWQ__12  [0.094169981777668, -0.10430295020341873, 0.02...   \n",
       "2  5sLYAQS9sWQ__15  [0.042587604373693466, -0.061460819095373154, ...   \n",
       "3  5sLYAQS9sWQ__18  [-0.0245895367115736, -0.058405470103025436, -...   \n",
       "4  5sLYAQS9sWQ__21  [0.05348338559269905, -0.016104578971862793, -...   \n",
       "5  5sLYAQS9sWQ__24  [0.07004527002573013, -0.08996045589447021, -0...   \n",
       "6  5sLYAQS9sWQ__27  [0.0283487681299448, -0.11020224541425705, -0....   \n",
       "7   5sLYAQS9sWQ__3  [-0.0700172707438469, -0.061202701181173325, -...   \n",
       "8  5sLYAQS9sWQ__30  [-0.04904637485742569, -0.1277533322572708, -0...   \n",
       "9  5sLYAQS9sWQ__33  [0.03286760300397873, -0.041724931448698044, 0...   \n",
       "\n",
       "                                           metadatas  \\\n",
       "0  {'segment_id': '5sLYAQS9sWQ__0', 'source': 'ht...   \n",
       "1  {'segment_id': '5sLYAQS9sWQ__12', 'source': 'h...   \n",
       "2  {'segment_id': '5sLYAQS9sWQ__15', 'source': 'h...   \n",
       "3  {'segment_id': '5sLYAQS9sWQ__18', 'source': 'h...   \n",
       "4  {'segment_id': '5sLYAQS9sWQ__21', 'source': 'h...   \n",
       "5  {'segment_id': '5sLYAQS9sWQ__24', 'source': 'h...   \n",
       "6  {'segment_id': '5sLYAQS9sWQ__27', 'source': 'h...   \n",
       "7  {'segment_id': '5sLYAQS9sWQ__3', 'source': 'ht...   \n",
       "8  {'segment_id': '5sLYAQS9sWQ__30', 'source': 'h...   \n",
       "9  {'segment_id': '5sLYAQS9sWQ__33', 'source': 'h...   \n",
       "\n",
       "                                           documents  uris  data  \n",
       "0  GPT, or Generative Pre-trained Transformer, is...  None  None  \n",
       "1  Now foundation models are pre-trained on large...  None  None  \n",
       "2  I'm talking about things like code. Now, large...  None  None  \n",
       "3  these models can be tens of gigabytes in size ...  None  None  \n",
       "4  So to put that into perspective, a text file t...  None  None  \n",
       "5  A lot of words just in one Gb. And how many gi...  None  None  \n",
       "6  Yeah, that's truly a lot of text. And LLMs are...  None  None  \n",
       "7  And I've been using GPT in its various forms f...  None  None  \n",
       "8  and the more parameters a model has, the more ...  None  None  \n",
       "9  All right, so how do they work? Well, we can t...  None  None  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "client = chromadb.PersistentClient('data/single_video.db')\n",
    "collection= client.get_collection('huberman_videos')\n",
    "\n",
    "num_segments = collection.count()\n",
    "sample_data = collection.peek()\n",
    "\n",
    "transcript_df = pd.DataFrame(sample_data)\n",
    "\n",
    "print(f\"Number of segments: {num_segments}\")\n",
    "transcript_df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}