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e011b9cd3a62",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"# Introduction\n",
|
9 |
+
"## Goal\n",
|
10 |
+
"I have a dataset I want to embed for semantic search (or QA, or RAG), I want the easiest way to do embed this and put it in a new dataset.\n",
|
11 |
+
"\n",
|
12 |
+
"## Approach\n",
|
13 |
+
"Im using a dataset from my favorite subreddit [r/bestofredditorupdates](). Since it has such long entries, I will use the new [jinaai/jina-embeddings-v2-base-en](https://huggingface.co/jinaai/jina-embeddings-v2-base-en) since it has an 8k context length. Since Im GPU-poor I will deploy this using [Inference Endpoint](https://huggingface.co/inference-endpoints) to save money and time. To follow this you will need to add a payment method. To make it even easier, I'll make this fully API based."
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"cell_type": "markdown",
|
18 |
+
"id": "d2534669-003d-490c-9d7a-32607fa5f404",
|
19 |
+
"metadata": {},
|
20 |
+
"source": [
|
21 |
+
"# Setup"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "markdown",
|
26 |
+
"id": "b6f72042-173d-4a72-ade1-9304b43b528d",
|
27 |
+
"metadata": {},
|
28 |
+
"source": [
|
29 |
+
"## Imports"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": 1,
|
35 |
+
"id": "e2beecdd-d033-4736-bd45-6754ec53b4ac",
|
36 |
+
"metadata": {
|
37 |
+
"tags": []
|
38 |
+
},
|
39 |
+
"outputs": [],
|
40 |
+
"source": [
|
41 |
+
"import asyncio\n",
|
42 |
+
"from getpass import getpass\n",
|
43 |
+
"import json\n",
|
44 |
+
"from pathlib import Path\n",
|
45 |
+
"import time\n",
|
46 |
+
"\n",
|
47 |
+
"from aiohttp import ClientSession, ClientTimeout\n",
|
48 |
+
"from datasets import load_dataset, Dataset, DatasetDict\n",
|
49 |
+
"from huggingface_hub import notebook_login\n",
|
50 |
+
"import pandas as pd\n",
|
51 |
+
"import requests\n",
|
52 |
+
"from tqdm.auto import tqdm"
|
53 |
+
]
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"cell_type": "markdown",
|
57 |
+
"id": "5eece903-64ce-435d-a2fd-096c0ff650bf",
|
58 |
+
"metadata": {},
|
59 |
+
"source": [
|
60 |
+
"## Config\n",
|
61 |
+
"You need to fill this in with your desired repos. Note I used 5 for the `MAX_WORKERS` since `jina-embeddings-v2` are quite memory hungry. "
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "code",
|
66 |
+
"execution_count": 2,
|
67 |
+
"id": "dcd7daed-6aca-4fe7-85ce-534bdcd8bc87",
|
68 |
+
"metadata": {
|
69 |
+
"tags": []
|
70 |
+
},
|
71 |
+
"outputs": [],
|
72 |
+
"source": [
|
73 |
+
"dataset_in = 'derek-thomas/dataset-creator-reddit-bestofredditorupdates'\n",
|
74 |
+
"dataset_out = \"processed-bestofredditorupdates\"\n",
|
75 |
+
"endpoint_name = \"boru-jina-embeddings-demo\"\n",
|
76 |
+
"\n",
|
77 |
+
"MAX_WORKERS = 5 "
|
78 |
+
]
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"cell_type": "code",
|
82 |
+
"execution_count": 3,
|
83 |
+
"id": "88cdbd73-5923-4ae9-9940-b6be935f70fa",
|
84 |
+
"metadata": {
|
85 |
+
"tags": []
|
86 |
+
},
|
87 |
+
"outputs": [
|
88 |
+
{
|
89 |
+
"name": "stdin",
|
90 |
+
"output_type": "stream",
|
91 |
+
"text": [
|
92 |
+
"What is your Hugging Face 🤗 username? (with a credit card) ········\n",
|
93 |
+
"What is your Hugging Face 🤗 token? ········\n"
|
94 |
+
]
|
95 |
+
}
|
96 |
+
],
|
97 |
+
"source": [
|
98 |
+
"username = getpass(prompt=\"What is your Hugging Face 🤗 username? (with an added payment method)\")\n",
|
99 |
+
"hf_token = getpass(prompt='What is your Hugging Face 🤗 token?')"
|
100 |
+
]
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"cell_type": "markdown",
|
104 |
+
"id": "b972a719-2aed-4d2e-a24f-fae7776d5fa4",
|
105 |
+
"metadata": {},
|
106 |
+
"source": [
|
107 |
+
"## Get Dataset"
|
108 |
+
]
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"cell_type": "code",
|
112 |
+
"execution_count": 4,
|
113 |
+
"id": "27835fa4-3a4f-44b1-a02a-5e31584a1bba",
|
114 |
+
"metadata": {
|
115 |
+
"tags": []
|
116 |
+
},
|
117 |
+
"outputs": [
|
118 |
+
{
|
119 |
+
"data": {
|
120 |
+
"text/plain": [
|
121 |
+
"Dataset({\n",
|
122 |
+
" features: ['date_utc', 'title', 'flair', 'content', 'poster', 'permalink', 'id', 'content_length', 'score'],\n",
|
123 |
+
" num_rows: 9991\n",
|
124 |
+
"})"
|
125 |
+
]
|
126 |
+
},
|
127 |
+
"execution_count": 4,
|
128 |
+
"metadata": {},
|
129 |
+
"output_type": "execute_result"
|
130 |
+
}
|
131 |
+
],
|
132 |
+
"source": [
|
133 |
+
"dataset = load_dataset(dataset_in, token=hf_token)\n",
|
134 |
+
"dataset['train']"
|
135 |
+
]
|
136 |
+
},
|
137 |
+
{
|
138 |
+
"cell_type": "code",
|
139 |
+
"execution_count": 5,
|
140 |
+
"id": "8846087e-4d0d-4c0e-8aeb-ea95d9e97126",
|
141 |
+
"metadata": {
|
142 |
+
"tags": []
|
143 |
+
},
|
144 |
+
"outputs": [
|
145 |
+
{
|
146 |
+
"data": {
|
147 |
+
"text/plain": [
|
148 |
+
"(9991,\n",
|
149 |
+
" {'date_utc': Timestamp('2022-12-31 18:16:22'),\n",
|
150 |
+
" 'title': 'To All BORU contributors, Thank you :)',\n",
|
151 |
+
" 'flair': 'CONCLUDED',\n",
|
152 |
+
" 'content': '[removed]',\n",
|
153 |
+
" 'poster': 'IsItAcOnSeQuEnCe',\n",
|
154 |
+
" 'permalink': '/r/BestofRedditorUpdates/comments/10004zw/to_all_boru_contributors_thank_you/',\n",
|
155 |
+
" 'id': '10004zw',\n",
|
156 |
+
" 'content_length': 9,\n",
|
157 |
+
" 'score': 1})"
|
158 |
+
]
|
159 |
+
},
|
160 |
+
"execution_count": 5,
|
161 |
+
"metadata": {},
|
162 |
+
"output_type": "execute_result"
|
163 |
+
}
|
164 |
+
],
|
165 |
+
"source": [
|
166 |
+
"documents = dataset['train'].to_pandas().to_dict('records')\n",
|
167 |
+
"len(documents), documents[0]"
|
168 |
+
]
|
169 |
+
},
|
170 |
+
{
|
171 |
+
"cell_type": "markdown",
|
172 |
+
"id": "93096cbc-81c6-4137-a283-6afb0f48fbb9",
|
173 |
+
"metadata": {},
|
174 |
+
"source": [
|
175 |
+
"# Inference Endpoints\n",
|
176 |
+
"## Create Inference Endpoint\n",
|
177 |
+
"We are going to use the [API](https://huggingface.co/docs/inference-endpoints/api_reference) to create an [Inference Endpoint](https://huggingface.co/inference-endpoints). This should provide a few main benefits:\n",
|
178 |
+
"- It's convenient (No clicking)\n",
|
179 |
+
"- It's repeatable (We have the code to run it easily)\n",
|
180 |
+
"- It's cheaper (No time spent waiting for it to load, and automatically shut it down)"
|
181 |
+
]
|
182 |
+
},
|
183 |
+
{
|
184 |
+
"cell_type": "code",
|
185 |
+
"execution_count": 6,
|
186 |
+
"id": "3a8f67b9-6ac6-4b5e-91ee-e48463191e1b",
|
187 |
+
"metadata": {
|
188 |
+
"tags": []
|
189 |
+
},
|
190 |
+
"outputs": [],
|
191 |
+
"source": [
|
192 |
+
"headers = {\n",
|
193 |
+
"\t\"Authorization\": f\"Bearer {hf_token}\",\n",
|
194 |
+
"\t\"Content-Type\": \"application/json\"\n",
|
195 |
+
"}\n",
|
196 |
+
"base_url = f\"https://api.endpoints.huggingface.cloud/v2/endpoint/{username}\"\n",
|
197 |
+
"endpoint_url = f\"https://api.endpoints.huggingface.cloud/v2/endpoint/{username}/{endpoint_name}\""
|
198 |
+
]
|
199 |
+
},
|
200 |
+
{
|
201 |
+
"cell_type": "markdown",
|
202 |
+
"id": "0f2c97dc-34e8-49e9-b60e-f5b7366294c0",
|
203 |
+
"metadata": {},
|
204 |
+
"source": [
|
205 |
+
"There are a few design choices here:\n",
|
206 |
+
"- I'm using the `g5.2xlarge` since it is big and `jina-embeddings-v2` are memory hungry (remember the 8k context length). \n",
|
207 |
+
"- I didnt alter the default `MAX_BATCH_TOKENS` or `MAX_CONCURRENT_REQUESTS`\n",
|
208 |
+
" - You should consider this if you are making this production ready\n",
|
209 |
+
" - You will need to restrict these to match the HW you are running on\n",
|
210 |
+
"- As mentioned before, I chose the repo and the corresponding revision\n"
|
211 |
+
]
|
212 |
+
},
|
213 |
+
{
|
214 |
+
"cell_type": "code",
|
215 |
+
"execution_count": 7,
|
216 |
+
"id": "f1ea29cb-b69d-4340-859f-3646d650c68e",
|
217 |
+
"metadata": {
|
218 |
+
"tags": []
|
219 |
+
},
|
220 |
+
"outputs": [
|
221 |
+
{
|
222 |
+
"name": "stdout",
|
223 |
+
"output_type": "stream",
|
224 |
+
"text": [
|
225 |
+
"202\n"
|
226 |
+
]
|
227 |
+
}
|
228 |
+
],
|
229 |
+
"source": [
|
230 |
+
"data = {\n",
|
231 |
+
" \"accountId\": None,\n",
|
232 |
+
" \"compute\": {\n",
|
233 |
+
" \"accelerator\": \"gpu\",\n",
|
234 |
+
" \"instanceType\": \"g5.2xlarge\",\n",
|
235 |
+
" \"instanceSize\": \"medium\",\n",
|
236 |
+
" \"scaling\": {\n",
|
237 |
+
" \"maxReplica\": 1,\n",
|
238 |
+
" \"minReplica\": 1\n",
|
239 |
+
" }\n",
|
240 |
+
" },\n",
|
241 |
+
" \"model\": {\n",
|
242 |
+
" \"framework\": \"pytorch\",\n",
|
243 |
+
" \"image\": {\n",
|
244 |
+
" \"custom\": {\n",
|
245 |
+
" \"url\": \"ghcr.io/huggingface/text-embeddings-inference:0.3.0\",\n",
|
246 |
+
" \"health_route\": \"/health\",\n",
|
247 |
+
" \"env\": {\n",
|
248 |
+
" \"MAX_BATCH_TOKENS\": \"16384\",\n",
|
249 |
+
" \"MAX_CONCURRENT_REQUESTS\": \"512\",\n",
|
250 |
+
" \"MODEL_ID\": \"/repository\"\n",
|
251 |
+
" }\n",
|
252 |
+
" }\n",
|
253 |
+
" },\n",
|
254 |
+
" \"repository\": \"jinaai/jina-embeddings-v2-base-en\",\n",
|
255 |
+
" \"revision\": \"8705ed9657208b2d5220fffad1c3a30980d279d0\",\n",
|
256 |
+
" \"task\": \"sentence-embeddings\",\n",
|
257 |
+
" },\n",
|
258 |
+
" \"name\": endpoint_name,\n",
|
259 |
+
" \"provider\": {\n",
|
260 |
+
" \"region\": \"us-east-1\",\n",
|
261 |
+
" \"vendor\": \"aws\"\n",
|
262 |
+
" },\n",
|
263 |
+
" \"type\": \"protected\"\n",
|
264 |
+
"}\n",
|
265 |
+
"\n",
|
266 |
+
"response = requests.post(base_url, headers={**headers, 'accept': 'application/json'}, json=data)\n",
|
267 |
+
"\n",
|
268 |
+
"\n",
|
269 |
+
"print(response.status_code)"
|
270 |
+
]
|
271 |
+
},
|
272 |
+
{
|
273 |
+
"cell_type": "markdown",
|
274 |
+
"id": "96d173b2-8980-4554-9039-c62843d3fc7d",
|
275 |
+
"metadata": {},
|
276 |
+
"source": [
|
277 |
+
"## Wait until its running\n",
|
278 |
+
"Here we use `tqdm` as a pretty way of displaying our status. It took about ~30s for this model to get the Inference Endpoint running."
|
279 |
+
]
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"cell_type": "code",
|
283 |
+
"execution_count": 8,
|
284 |
+
"id": "b8aa66a9-3c8a-4040-9465-382c744f36cf",
|
285 |
+
"metadata": {
|
286 |
+
"tags": []
|
287 |
+
},
|
288 |
+
"outputs": [
|
289 |
+
{
|
290 |
+
"data": {
|
291 |
+
"application/vnd.jupyter.widget-view+json": {
|
292 |
+
"model_id": "a6f27d86f68b4000aa40e09ae079c6b0",
|
293 |
+
"version_major": 2,
|
294 |
+
"version_minor": 0
|
295 |
+
},
|
296 |
+
"text/plain": [
|
297 |
+
"Waiting for status to change: 0s [00:00, ?s/s]"
|
298 |
+
]
|
299 |
+
},
|
300 |
+
"metadata": {},
|
301 |
+
"output_type": "display_data"
|
302 |
+
},
|
303 |
+
{
|
304 |
+
"name": "stdout",
|
305 |
+
"output_type": "stream",
|
306 |
+
"text": [
|
307 |
+
"Status is 'running'.\n"
|
308 |
+
]
|
309 |
+
}
|
310 |
+
],
|
311 |
+
"source": [
|
312 |
+
"with tqdm(desc=\"Waiting for status to change\", unit=\"s\") as pbar:\n",
|
313 |
+
" while True:\n",
|
314 |
+
" response_json = requests.get(endpoint_url, headers=headers).json()\n",
|
315 |
+
" current_status = response_json['status']['state']\n",
|
316 |
+
"\n",
|
317 |
+
" if current_status == 'running':\n",
|
318 |
+
" print(\"Status is 'running'.\")\n",
|
319 |
+
" break\n",
|
320 |
+
"\n",
|
321 |
+
" pbar.set_description(f\"Status: {current_status}\")\n",
|
322 |
+
" time.sleep(2)\n",
|
323 |
+
" pbar.update(1)\n",
|
324 |
+
"\n",
|
325 |
+
"embedding_url = response_json['status']['url']"
|
326 |
+
]
|
327 |
+
},
|
328 |
+
{
|
329 |
+
"cell_type": "markdown",
|
330 |
+
"id": "063fa066-e4d0-4a65-a82d-cf17db4af8d8",
|
331 |
+
"metadata": {},
|
332 |
+
"source": [
|
333 |
+
"I found that even though the status is running, I want to get a test message to run first before running our batch in parallel."
|
334 |
+
]
|
335 |
+
},
|
336 |
+
{
|
337 |
+
"cell_type": "code",
|
338 |
+
"execution_count": 9,
|
339 |
+
"id": "66e00960-1d3d-490d-bedc-3eaf1924db76",
|
340 |
+
"metadata": {},
|
341 |
+
"outputs": [
|
342 |
+
{
|
343 |
+
"data": {
|
344 |
+
"application/vnd.jupyter.widget-view+json": {
|
345 |
+
"model_id": "4e03e5a3d07a498ca6b3631605724b62",
|
346 |
+
"version_major": 2,
|
347 |
+
"version_minor": 0
|
348 |
+
},
|
349 |
+
"text/plain": [
|
350 |
+
"Waiting for endpoint to accept requests: 0s [00:00, ?s/s]"
|
351 |
+
]
|
352 |
+
},
|
353 |
+
"metadata": {},
|
354 |
+
"output_type": "display_data"
|
355 |
+
},
|
356 |
+
{
|
357 |
+
"name": "stdout",
|
358 |
+
"output_type": "stream",
|
359 |
+
"text": [
|
360 |
+
"Endpoint is accepting requests\n"
|
361 |
+
]
|
362 |
+
}
|
363 |
+
],
|
364 |
+
"source": [
|
365 |
+
"payload = {\"inputs\": \"This sound track was beautiful! It paints the senery in your mind so well I would recomend it even to people who hate vid. game music!\"}\n",
|
366 |
+
"\n",
|
367 |
+
"with tqdm(desc=\"Waiting for endpoint to accept requests\", unit=\"s\") as pbar:\n",
|
368 |
+
" while True:\n",
|
369 |
+
" try:\n",
|
370 |
+
" response_json = requests.post(embedding_url, headers=headers, json=payload).json()\n",
|
371 |
+
"\n",
|
372 |
+
" # Assuming the successful response has a specific structure\n",
|
373 |
+
" if len(response_json[0]) == 768:\n",
|
374 |
+
" print(\"Endpoint is accepting requests\")\n",
|
375 |
+
" break\n",
|
376 |
+
"\n",
|
377 |
+
" except requests.ConnectionError as e:\n",
|
378 |
+
" pass\n",
|
379 |
+
"\n",
|
380 |
+
" # Delay between retries\n",
|
381 |
+
" time.sleep(5)\n",
|
382 |
+
" pbar.update(1)\n"
|
383 |
+
]
|
384 |
+
},
|
385 |
+
{
|
386 |
+
"cell_type": "markdown",
|
387 |
+
"id": "f7186126-ef6a-47d0-b158-112810649cd9",
|
388 |
+
"metadata": {},
|
389 |
+
"source": [
|
390 |
+
"# Get Embeddings"
|
391 |
+
]
|
392 |
+
},
|
393 |
+
{
|
394 |
+
"cell_type": "markdown",
|
395 |
+
"id": "1dadfd68-6d46-4ce8-a165-bfeb43b1f114",
|
396 |
+
"metadata": {},
|
397 |
+
"source": [
|
398 |
+
"Here I send a document, update it with the embedding, and return it. This happens in parallel with `MAX_WORKERS`."
|
399 |
+
]
|
400 |
+
},
|
401 |
+
{
|
402 |
+
"cell_type": "code",
|
403 |
+
"execution_count": 10,
|
404 |
+
"id": "ad3193fb-3def-42a8-968e-c63f2b864ca8",
|
405 |
+
"metadata": {
|
406 |
+
"tags": []
|
407 |
+
},
|
408 |
+
"outputs": [],
|
409 |
+
"source": [
|
410 |
+
"async def request(document, semaphore):\n",
|
411 |
+
" # Semaphore guard\n",
|
412 |
+
" async with semaphore:\n",
|
413 |
+
" payload = {\n",
|
414 |
+
" \"inputs\": document['content'] or document['title'] or '[deleted]',\n",
|
415 |
+
" \"truncate\": True\n",
|
416 |
+
" }\n",
|
417 |
+
" \n",
|
418 |
+
" timeout = ClientTimeout(total=10) # Set a timeout for requests (10 seconds here)\n",
|
419 |
+
"\n",
|
420 |
+
" async with ClientSession(timeout=timeout, headers=headers) as session:\n",
|
421 |
+
" async with session.post(embedding_url, json=payload) as resp:\n",
|
422 |
+
" if resp.status != 200:\n",
|
423 |
+
" raise RuntimeError(await resp.text())\n",
|
424 |
+
" result = await resp.json()\n",
|
425 |
+
" \n",
|
426 |
+
" document['embedding'] = result[0] # Assuming the API's output can be directly assigned\n",
|
427 |
+
" return document\n",
|
428 |
+
"\n",
|
429 |
+
"async def main(documents):\n",
|
430 |
+
" # Semaphore to limit concurrent requests. Adjust the number as needed.\n",
|
431 |
+
" semaphore = asyncio.BoundedSemaphore(MAX_WORKERS)\n",
|
432 |
+
"\n",
|
433 |
+
" # Creating a list of tasks\n",
|
434 |
+
" tasks = [request(document, semaphore) for document in documents]\n",
|
435 |
+
" \n",
|
436 |
+
" # Using tqdm to show progress. It's been integrated into the async loop.\n",
|
437 |
+
" for f in tqdm(asyncio.as_completed(tasks), total=len(documents)):\n",
|
438 |
+
" await f"
|
439 |
+
]
|
440 |
+
},
|
441 |
+
{
|
442 |
+
"cell_type": "code",
|
443 |
+
"execution_count": 11,
|
444 |
+
"id": "ec4983af-65eb-4841-808a-3738fb4d682d",
|
445 |
+
"metadata": {
|
446 |
+
"tags": []
|
447 |
+
},
|
448 |
+
"outputs": [
|
449 |
+
{
|
450 |
+
"data": {
|
451 |
+
"application/vnd.jupyter.widget-view+json": {
|
452 |
+
"model_id": "cb73af52244e40d2aab8bdac3a55d443",
|
453 |
+
"version_major": 2,
|
454 |
+
"version_minor": 0
|
455 |
+
},
|
456 |
+
"text/plain": [
|
457 |
+
" 0%| | 0/9991 [00:00<?, ?it/s]"
|
458 |
+
]
|
459 |
+
},
|
460 |
+
"metadata": {},
|
461 |
+
"output_type": "display_data"
|
462 |
+
},
|
463 |
+
{
|
464 |
+
"name": "stdout",
|
465 |
+
"output_type": "stream",
|
466 |
+
"text": [
|
467 |
+
"Embeddings = 9991 documents = 9991\n",
|
468 |
+
"32 min 14.53 sec\n"
|
469 |
+
]
|
470 |
+
}
|
471 |
+
],
|
472 |
+
"source": [
|
473 |
+
"start = time.perf_counter()\n",
|
474 |
+
"\n",
|
475 |
+
"# Get embeddings\n",
|
476 |
+
"await main(documents)\n",
|
477 |
+
"\n",
|
478 |
+
"# Make sure we got it all\n",
|
479 |
+
"count = 0\n",
|
480 |
+
"for document in documents:\n",
|
481 |
+
" if document['embedding'] and len(document['embedding']) == 768:\n",
|
482 |
+
" count += 1\n",
|
483 |
+
"print(f'Embeddings = {count} documents = {len(documents)}')\n",
|
484 |
+
"\n",
|
485 |
+
" \n",
|
486 |
+
"# Print elapsed time\n",
|
487 |
+
"elapsed_time = time.perf_counter() - start\n",
|
488 |
+
"minutes, seconds = divmod(elapsed_time, 60)\n",
|
489 |
+
"print(f\"{int(minutes)} min {seconds:.2f} sec\")"
|
490 |
+
]
|
491 |
+
},
|
492 |
+
{
|
493 |
+
"cell_type": "markdown",
|
494 |
+
"id": "bab97c7b-7bac-4bf5-9752-b528294dadc7",
|
495 |
+
"metadata": {},
|
496 |
+
"source": [
|
497 |
+
"## Pause Inference Endpoint\n",
|
498 |
+
"Now that we have finished, lets pause the endpoint so we don't incur any extra charges, this will also allow us to analyze the cost."
|
499 |
+
]
|
500 |
+
},
|
501 |
+
{
|
502 |
+
"cell_type": "code",
|
503 |
+
"execution_count": 12,
|
504 |
+
"id": "540a0978-7670-4ce3-95c1-3823cc113b85",
|
505 |
+
"metadata": {
|
506 |
+
"tags": []
|
507 |
+
},
|
508 |
+
"outputs": [
|
509 |
+
{
|
510 |
+
"name": "stdout",
|
511 |
+
"output_type": "stream",
|
512 |
+
"text": [
|
513 |
+
"200\n",
|
514 |
+
"paused\n"
|
515 |
+
]
|
516 |
+
}
|
517 |
+
],
|
518 |
+
"source": [
|
519 |
+
"response = requests.post(endpoint_url + '/pause', headers=headers)\n",
|
520 |
+
"\n",
|
521 |
+
"print(response.status_code)\n",
|
522 |
+
"print(response.json()['status']['state'])"
|
523 |
+
]
|
524 |
+
},
|
525 |
+
{
|
526 |
+
"cell_type": "markdown",
|
527 |
+
"id": "45ad65b7-3da2-4113-9b95-8fb4e21ae793",
|
528 |
+
"metadata": {},
|
529 |
+
"source": [
|
530 |
+
"# Push updated dataset to Hub\n",
|
531 |
+
"We now have our documents updated with the embeddings we wanted. First we need to convert it back to a `Dataset` format. I find its easiest to go from list of dicts -> `pd.DataFrame` -> `Dataset`"
|
532 |
+
]
|
533 |
+
},
|
534 |
+
{
|
535 |
+
"cell_type": "code",
|
536 |
+
"execution_count": 13,
|
537 |
+
"id": "9bb993f8-d624-4192-9626-8e9ed9888a1b",
|
538 |
+
"metadata": {
|
539 |
+
"tags": []
|
540 |
+
},
|
541 |
+
"outputs": [],
|
542 |
+
"source": [
|
543 |
+
"df = pd.DataFrame(documents)\n",
|
544 |
+
"dd = DatasetDict({'train': Dataset.from_pandas(df)})"
|
545 |
+
]
|
546 |
+
},
|
547 |
+
{
|
548 |
+
"cell_type": "code",
|
549 |
+
"execution_count": 14,
|
550 |
+
"id": "f48e7c55-d5b7-4ed6-8516-272ae38716b1",
|
551 |
+
"metadata": {
|
552 |
+
"tags": []
|
553 |
+
},
|
554 |
+
"outputs": [
|
555 |
+
{
|
556 |
+
"data": {
|
557 |
+
"application/vnd.jupyter.widget-view+json": {
|
558 |
+
"model_id": "84a481e0cf74494cb2eb9d9857701212",
|
559 |
+
"version_major": 2,
|
560 |
+
"version_minor": 0
|
561 |
+
},
|
562 |
+
"text/plain": [
|
563 |
+
"Pushing dataset shards to the dataset hub: 0%| | 0/1 [00:00<?, ?it/s]"
|
564 |
+
]
|
565 |
+
},
|
566 |
+
"metadata": {},
|
567 |
+
"output_type": "display_data"
|
568 |
+
},
|
569 |
+
{
|
570 |
+
"data": {
|
571 |
+
"application/vnd.jupyter.widget-view+json": {
|
572 |
+
"model_id": "b8f128dfe7c546bcbc8f04817e3ca48c",
|
573 |
+
"version_major": 2,
|
574 |
+
"version_minor": 0
|
575 |
+
},
|
576 |
+
"text/plain": [
|
577 |
+
"Creating parquet from Arrow format: 0%| | 0/10 [00:00<?, ?ba/s]"
|
578 |
+
]
|
579 |
+
},
|
580 |
+
"metadata": {},
|
581 |
+
"output_type": "display_data"
|
582 |
+
},
|
583 |
+
{
|
584 |
+
"data": {
|
585 |
+
"application/vnd.jupyter.widget-view+json": {
|
586 |
+
"model_id": "2dcc1d54036a49f1a1346a6be64e765a",
|
587 |
+
"version_major": 2,
|
588 |
+
"version_minor": 0
|
589 |
+
},
|
590 |
+
"text/plain": [
|
591 |
+
"Upload 1 LFS files: 0%| | 0/1 [00:00<?, ?it/s]"
|
592 |
+
]
|
593 |
+
},
|
594 |
+
"metadata": {},
|
595 |
+
"output_type": "display_data"
|
596 |
+
}
|
597 |
+
],
|
598 |
+
"source": [
|
599 |
+
"dd.push_to_hub(dataset_out, token=hf_token)"
|
600 |
+
]
|
601 |
+
},
|
602 |
+
{
|
603 |
+
"cell_type": "markdown",
|
604 |
+
"id": "41abea64-379d-49de-8d9a-355c2f4ce1ac",
|
605 |
+
"metadata": {},
|
606 |
+
"source": [
|
607 |
+
"# Analyze Usage\n",
|
608 |
+
"1. Go to your `dashboard_url` printed below\n",
|
609 |
+
"1. Click on the Usage & Cost tab\n",
|
610 |
+
"1. See how much you have spent"
|
611 |
+
]
|
612 |
+
},
|
613 |
+
{
|
614 |
+
"cell_type": "code",
|
615 |
+
"execution_count": 15,
|
616 |
+
"id": "16815445-3079-43da-b14e-b54176a07a62",
|
617 |
+
"metadata": {},
|
618 |
+
"outputs": [
|
619 |
+
{
|
620 |
+
"name": "stdout",
|
621 |
+
"output_type": "stream",
|
622 |
+
"text": [
|
623 |
+
"https://ui.endpoints.huggingface.co/HF-test-lab/endpoints/boru-jina-embeddings-demo\n"
|
624 |
+
]
|
625 |
+
}
|
626 |
+
],
|
627 |
+
"source": [
|
628 |
+
"dashboard_url = f'https://ui.endpoints.huggingface.co/{username}/endpoints/{endpoint_name}'\n",
|
629 |
+
"print(dashboard_url)"
|
630 |
+
]
|
631 |
+
},
|
632 |
+
{
|
633 |
+
"cell_type": "code",
|
634 |
+
"execution_count": 16,
|
635 |
+
"id": "81096c6f-d12f-4781-84ec-9066cfa465b3",
|
636 |
+
"metadata": {},
|
637 |
+
"outputs": [
|
638 |
+
{
|
639 |
+
"name": "stdin",
|
640 |
+
"output_type": "stream",
|
641 |
+
"text": [
|
642 |
+
"Hit enter to continue with the notebook \n"
|
643 |
+
]
|
644 |
+
},
|
645 |
+
{
|
646 |
+
"data": {
|
647 |
+
"text/plain": [
|
648 |
+
"''"
|
649 |
+
]
|
650 |
+
},
|
651 |
+
"execution_count": 16,
|
652 |
+
"metadata": {},
|
653 |
+
"output_type": "execute_result"
|
654 |
+
}
|
655 |
+
],
|
656 |
+
"source": [
|
657 |
+
"input(\"Hit enter to continue with the notebook\")"
|
658 |
+
]
|
659 |
+
},
|
660 |
+
{
|
661 |
+
"cell_type": "markdown",
|
662 |
+
"id": "b953d5be-2494-4ff8-be42-9daf00c99c41",
|
663 |
+
"metadata": {},
|
664 |
+
"source": [
|
665 |
+
"# Delete Endpoint\n",
|
666 |
+
"We should see a `200` if everything went correctly."
|
667 |
+
]
|
668 |
+
},
|
669 |
+
{
|
670 |
+
"cell_type": "code",
|
671 |
+
"execution_count": 17,
|
672 |
+
"id": "c310c0f3-6f12-4d5c-838b-3a4c1f2e54ad",
|
673 |
+
"metadata": {
|
674 |
+
"tags": []
|
675 |
+
},
|
676 |
+
"outputs": [
|
677 |
+
{
|
678 |
+
"name": "stdout",
|
679 |
+
"output_type": "stream",
|
680 |
+
"text": [
|
681 |
+
"200\n"
|
682 |
+
]
|
683 |
+
}
|
684 |
+
],
|
685 |
+
"source": [
|
686 |
+
"response = requests.delete(endpoint_url, headers=headers)\n",
|
687 |
+
"\n",
|
688 |
+
"print(response.status_code)"
|
689 |
+
]
|
690 |
+
},
|
691 |
+
{
|
692 |
+
"cell_type": "code",
|
693 |
+
"execution_count": null,
|
694 |
+
"id": "5db1b1c3-16c3-403a-9472-a97e730826d5",
|
695 |
+
"metadata": {},
|
696 |
+
"outputs": [],
|
697 |
+
"source": []
|
698 |
+
}
|
699 |
+
],
|
700 |
+
"metadata": {
|
701 |
+
"kernelspec": {
|
702 |
+
"display_name": "Python 3 (ipykernel)",
|
703 |
+
"language": "python",
|
704 |
+
"name": "python3"
|
705 |
+
},
|
706 |
+
"language_info": {
|
707 |
+
"codemirror_mode": {
|
708 |
+
"name": "ipython",
|
709 |
+
"version": 3
|
710 |
+
},
|
711 |
+
"file_extension": ".py",
|
712 |
+
"mimetype": "text/x-python",
|
713 |
+
"name": "python",
|
714 |
+
"nbconvert_exporter": "python",
|
715 |
+
"pygments_lexer": "ipython3",
|
716 |
+
"version": "3.10.8"
|
717 |
+
}
|
718 |
+
},
|
719 |
+
"nbformat": 4,
|
720 |
+
"nbformat_minor": 5
|
721 |
+
}
|