Skip to content

Commit 044db0f

Browse files
check if data directory exists before creating it
1 parent 0a856bb commit 044db0f

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

python-recipes/recommendation-systems/content_filtering.ipynb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@
4141
},
4242
{
4343
"cell_type": "code",
44-
"execution_count": 1,
44+
"execution_count": 11,
4545
"metadata": {},
4646
"outputs": [],
4747
"source": [
4848
"## IMPORTS\n",
4949
"import pandas as pd\n",
5050
"import ast\n",
5151
"import os\n",
52+
"import pickle\n",
53+
"import requests\n",
54+
"\n",
5255
"# Replace values below with your own if using Redis Cloud instance\n",
5356
"REDIS_HOST = os.getenv(\"REDIS_HOST\", \"localhost\") # ex: \"redis-18374.c253.us-central1-1.gce.cloud.redislabs.com\"\n",
5457
"REDIS_PORT = os.getenv(\"REDIS_PORT\", \"6379\") # ex: 18374\n",
@@ -74,7 +77,7 @@
7477
},
7578
{
7679
"cell_type": "code",
77-
"execution_count": 2,
80+
"execution_count": 12,
7881
"metadata": {},
7982
"outputs": [
8083
{
@@ -236,7 +239,7 @@
236239
"4 1914 /title/tt0004457/ "
237240
]
238241
},
239-
"execution_count": 2,
242+
"execution_count": 12,
240243
"metadata": {},
241244
"output_type": "execute_result"
242245
}
@@ -251,7 +254,8 @@
251254
" r = requests.get(url)\n",
252255
"\n",
253256
" #save the file as a csv\n",
254-
" os.mkdir('./datasets/content_filtering')\n",
257+
" if not os.path.exists('./datasets/content_filtering'):\n",
258+
" os.makedirs('./datasets/content_filtering')\n",
255259
" with open('./datasets/content_filtering/25k_imdb_movie_dataset.csv', 'wb') as f:\n",
256260
" f.write(r.content)\n",
257261
" df = pd.read_csv(\"datasets/content_filtering/25k_imdb_movie_dataset.csv\")\n",
@@ -272,7 +276,7 @@
272276
},
273277
{
274278
"cell_type": "code",
275-
"execution_count": 3,
279+
"execution_count": 13,
276280
"metadata": {},
277281
"outputs": [
278282
{
@@ -290,7 +294,7 @@
290294
"dtype: int64"
291295
]
292296
},
293-
"execution_count": 3,
297+
"execution_count": 13,
294298
"metadata": {},
295299
"output_type": "execute_result"
296300
}
@@ -310,7 +314,7 @@
310314
"df['keywords'] = df['keywords'].apply(ast.literal_eval) # convert string representation of list to list\n",
311315
"df['cast'] = df['cast'].apply(ast.literal_eval) # convert string representation of list to list\n",
312316
"df = df[~df['overview'].isnull()] # drop rows with missing overviews\n",
313-
"df = df[~df['overview'].isin(['none'])] # drop rows with 'none' as the overview\n",
317+
"df = df[~df['overview'].isin(['none'])] # drop rows with 'none' as the overview\n",
314318
"\n",
315319
"# make sure we've filled all missing values\n",
316320
"df.isnull().sum()"
@@ -337,7 +341,7 @@
337341
},
338342
{
339343
"cell_type": "code",
340-
"execution_count": 4,
344+
"execution_count": 14,
341345
"metadata": {},
342346
"outputs": [
343347
{
@@ -346,7 +350,7 @@
346350
"'The Story of the Kelly Gang. Story of Ned Kelly, an infamous 19th-century Australian outlaw. ned kelly, australia, historic figure, australian western, first of its kind, directorial debut, australian history, 19th century, victoria australia, australian'"
347351
]
348352
},
349-
"execution_count": 4,
353+
"execution_count": 14,
350354
"metadata": {},
351355
"output_type": "execute_result"
352356
}
@@ -366,7 +370,6 @@
366370
"# this step will take a while, but only needs to be done once for your entire dataset\n",
367371
"# currently taking 10 minutes to run, so we've gone ahead and saved the vectors to a file for you\n",
368372
"# if you don't want to wait, you can skip the cell and load the vectors from the file in the next cell\n",
369-
"import pickle\n",
370373
"from redisvl.utils.vectorize import HFTextVectorizer\n",
371374
"\n",
372375
"vectorizer = HFTextVectorizer(model = 'sentence-transformers/paraphrase-MiniLM-L6-v2')\n",
@@ -377,12 +380,10 @@
377380
},
378381
{
379382
"cell_type": "code",
380-
"execution_count": 5,
383+
"execution_count": 15,
381384
"metadata": {},
382385
"outputs": [],
383386
"source": [
384-
"import pickle\n",
385-
"\n",
386387
"try:\n",
387388
" with open('datasets/content_filtering/text_embeddings.pkl', 'rb') as vector_file:\n",
388389
" df['embedding'] = pickle.load(vector_file)\n",
@@ -416,14 +417,14 @@
416417
},
417418
{
418419
"cell_type": "code",
419-
"execution_count": 6,
420+
"execution_count": 17,
420421
"metadata": {},
421422
"outputs": [
422423
{
423424
"name": "stdout",
424425
"output_type": "stream",
425426
"text": [
426-
"15:15:43 redisvl.index.index INFO Index already exists, overwriting.\n"
427+
"09:44:30 redisvl.index.index INFO Index already exists, overwriting.\n"
427428
]
428429
}
429430
],
@@ -452,7 +453,7 @@
452453
},
453454
{
454455
"cell_type": "code",
455-
"execution_count": 7,
456+
"execution_count": 18,
456457
"metadata": {},
457458
"outputs": [],
458459
"source": [
@@ -473,18 +474,18 @@
473474
},
474475
{
475476
"cell_type": "code",
476-
"execution_count": 8,
477+
"execution_count": 19,
477478
"metadata": {},
478479
"outputs": [
479480
{
480481
"name": "stdout",
481482
"output_type": "stream",
482483
"text": [
483-
"{'id': 'movie:be648c0ed83b460d9e01f03940c7c7cf', 'vector_distance': '0.584870040417', 'title': 'The Odyssey', 'overview': 'The aquatic adventure of the highly influential and fearlessly ambitious pioneer, innovator, filmmaker, researcher, and conservationist, Jacques-Yves Cousteau, covers roughly thirty years of an inarguably rich in achievements life.'}\n",
484-
"{'id': 'movie:bc1375b4d7dd47e2a117c94bebdffa28', 'vector_distance': '0.63329231739', 'title': 'The Inventor', 'overview': 'Inventing flying contraptions, war machines and studying cadavers, Leonardo da Vinci tackles the meaning of life itself with the help of French princess Marguerite de Nevarre.'}\n",
485-
"{'id': 'movie:469d553ae60846b9b8c7128fc57fe079', 'vector_distance': '0.658123672009', 'title': 'Ruin', 'overview': 'The film follows a nameless ex-Nazi captain who navigates the ruins of post-WWII Germany determined to atone for his crimes during the war by hunting down the surviving members of his former SS Death Squad.'}\n",
486-
"{'id': 'movie:493527b640bc48a2941ee46df71a4018', 'vector_distance': '0.688094437122', 'title': 'The Raven', 'overview': 'A man with incredible powers is sought by the government and military.'}\n",
487-
"{'id': 'movie:bd123271d6a04128acb93dc48b8e5847', 'vector_distance': '0.694671392441', 'title': 'Get the Girl', 'overview': 'Sebastain \"Bash\" Danye, a legendary gun for hire hangs up his weapon to retire peacefully with his \\'it\\'s complicated\\' partner Renee. Their quiet lives are soon interrupted when they find an unconscious woman on their property, Maddie. While nursing her back to health, some bad me... Read all'}\n"
484+
"{'id': 'movie:b64fc099d6af440a891e1dd8314e5af7', 'vector_distance': '0.584870040417', 'title': 'The Odyssey', 'overview': 'The aquatic adventure of the highly influential and fearlessly ambitious pioneer, innovator, filmmaker, researcher, and conservationist, Jacques-Yves Cousteau, covers roughly thirty years of an inarguably rich in achievements life.'}\n",
485+
"{'id': 'movie:2fbd7803b51a4bf9a8fb1aa79244ad64', 'vector_distance': '0.63329231739', 'title': 'The Inventor', 'overview': 'Inventing flying contraptions, war machines and studying cadavers, Leonardo da Vinci tackles the meaning of life itself with the help of French princess Marguerite de Nevarre.'}\n",
486+
"{'id': 'movie:224a785ca7ea4006bbcdac8aad5bf1bc', 'vector_distance': '0.658123672009', 'title': 'Ruin', 'overview': 'The film follows a nameless ex-Nazi captain who navigates the ruins of post-WWII Germany determined to atone for his crimes during the war by hunting down the surviving members of his former SS Death Squad.'}\n",
487+
"{'id': 'movie:b7d0af286515427fbbf3866bc7e9b739', 'vector_distance': '0.688094437122', 'title': 'The Raven', 'overview': 'A man with incredible powers is sought by the government and military.'}\n",
488+
"{'id': 'movie:9aceb1903b584648b3a5b5d1bdd383b2', 'vector_distance': '0.694671392441', 'title': 'Get the Girl', 'overview': 'Sebastain \"Bash\" Danye, a legendary gun for hire hangs up his weapon to retire peacefully with his \\'it\\'s complicated\\' partner Renee. Their quiet lives are soon interrupted when they find an unconscious woman on their property, Maddie. While nursing her back to health, some bad me... Read all'}\n"
488489
]
489490
}
490491
],
@@ -518,7 +519,7 @@
518519
},
519520
{
520521
"cell_type": "code",
521-
"execution_count": 9,
522+
"execution_count": 20,
522523
"metadata": {},
523524
"outputs": [],
524525
"source": [
@@ -558,7 +559,7 @@
558559
},
559560
{
560561
"cell_type": "code",
561-
"execution_count": 10,
562+
"execution_count": 21,
562563
"metadata": {},
563564
"outputs": [
564565
{
@@ -603,7 +604,7 @@
603604
},
604605
{
605606
"cell_type": "code",
606-
"execution_count": null,
607+
"execution_count": 24,
607608
"metadata": {},
608609
"outputs": [],
609610
"source": [

0 commit comments

Comments
 (0)