{ "cells": [ { "cell_type": "markdown", "id": "49fecb8b-833e-4f1d-979c-29de9ebc3e37", "metadata": {}, "source": [ "## Class Demo: Recipe generator" ] }, { "cell_type": "code", "execution_count": 1, "id": "73190c22-c0fe-4a74-972d-03543b7b9959", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n" ] } ], "source": [ "import torch\n", "import torch.nn as nn\n", "import numpy as np\n", "from torch.utils.data import Dataset, DataLoader\n", "import pandas as pd\n", "import os\n", "import re\n", "import sys\n", "from collections import Counter, defaultdict\n", "from urllib.request import urlopen\n", "import math" ] }, { "cell_type": "markdown", "id": "053617b4-3a67-4f7b-bf32-0a7dba9a15f6", "metadata": {}, "source": [ "### Data" ] }, { "cell_type": "markdown", "id": "5bc90cc6-d9bc-4747-bc91-ed5dcc065c47", "metadata": {}, "source": [ "This is a demo for recipe generation using PyTorch and Transformers. \n", "For the purpose of this demo, we'll sample 10_000 recipe titles from the corpus " ] }, { "cell_type": "code", "execution_count": 2, "id": "765fad1d-0cc9-4d88-a4d8-357a767fc072", "metadata": {}, "outputs": [], "source": [ "orig_recipes_df = pd.read_csv(\"../data/RAW_recipes.csv\")\n", "orig_recipes_df = orig_recipes_df.dropna()\n", "recipes_df = orig_recipes_df.sample(10_000)" ] }, { "cell_type": "code", "execution_count": 3, "id": "7839bff9-07e0-4e3d-be61-2aba64b9fefd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | name | \n", "id | \n", "minutes | \n", "contributor_id | \n", "submitted | \n", "tags | \n", "nutrition | \n", "n_steps | \n", "steps | \n", "description | \n", "ingredients | \n", "n_ingredients | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
124299 | \n", "lime cilantro grilled steak | \n", "29651 | \n", "140 | \n", "27783 | \n", "2002-05-29 | \n", "['weeknight', 'time-to-make', 'course', 'main-... | \n", "[6.3, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0] | \n", "7 | \n", "['combine lime juice , cilantro , water , jala... | \n", "i found this recipe in a recent issue of gemma... | \n", "['boneless beef chuck steaks', 'fresh lime jui... | \n", "8 | \n", "
158654 | \n", "persian rhubarb syrup | \n", "240370 | \n", "60 | \n", "414666 | \n", "2007-07-14 | \n", "['60-minutes-or-less', 'time-to-make', 'course... | \n", "[778.0, 0.0, 737.0, 0.0, 5.0, 0.0, 66.0] | \n", "10 | \n", "['put the rhubarb in a large stainless steel p... | \n", "add some seltzer or ginger ale and ice and thi... | \n", "['rhubarb', 'fresh lime juice', 'sugar', 'mint... | \n", "4 | \n", "
151872 | \n", "pampered chef style apple crisp for microwave... | \n", "63314 | \n", "65 | \n", "27783 | \n", "2003-05-28 | \n", "['weeknight', 'time-to-make', 'course', 'main-... | \n", "[461.9, 27.0, 192.0, 4.0, 7.0, 50.0, 24.0] | \n", "9 | \n", "['you will need a 12\" round stoneware baking p... | \n", "this is one of the recipes that was served at ... | \n", "['tart baking apples', 'graham cracker squares... | \n", "9 | \n", "
176661 | \n", "rocky road sundae pie | \n", "10409 | \n", "40 | \n", "8728 | \n", "2001-07-25 | \n", "['60-minutes-or-less', 'time-to-make', 'course... | \n", "[620.9, 58.0, 193.0, 9.0, 16.0, 92.0, 22.0] | \n", "33 | \n", "['preheat oven to 325', 'butter a 9-inch pan',... | \n", "great summer pie! | \n", "['chocolate ice cream', 'walnuts', 'chocolate ... | \n", "8 | \n", "
163677 | \n", "pork taco meat | \n", "85631 | \n", "70 | \n", "107334 | \n", "2004-03-03 | \n", "['time-to-make', 'course', 'preparation', 'ver... | \n", "[729.8, 73.0, 4.0, 13.0, 137.0, 81.0, 0.0] | \n", "7 | \n", "['cube the pork steaks , cutting off much of t... | \n", "this is the best pork taco meat i've ever had.... | \n", "['pork steak', 'oil', 'chili powder', 'salt', ... | \n", "7 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
98092 | \n", "grill a juicy burger with the only rub you ll ... | \n", "371492 | \n", "30 | \n", "37449 | \n", "2009-05-11 | \n", "['30-minutes-or-less', 'time-to-make', 'course... | \n", "[282.4, 23.0, 44.0, 62.0, 44.0, 29.0, 4.0] | \n", "7 | \n", "['combine sirloin with the ground chuck and cr... | \n", "want to ensure juicy great tasting burgers for... | \n", "['ground sirloin', 'ground chuck', 'ice', 'sea... | \n", "12 | \n", "
48942 | \n", "chipotle tortilla casserole | \n", "205081 | \n", "30 | \n", "83093 | \n", "2007-01-12 | \n", "['30-minutes-or-less', 'time-to-make', 'course... | \n", "[439.5, 35.0, 19.0, 31.0, 45.0, 30.0, 12.0] | \n", "10 | \n", "['preheat oven to 400f', 'arrange chips in bot... | \n", "this is a recipe attributed to rick bayless, f... | \n", "['corn tortilla chips', 'cooked chicken', 'tom... | \n", "11 | \n", "
184473 | \n", "shamakia | \n", "491426 | \n", "50 | \n", "2548229 | \n", "2012-12-07 | \n", "['60-minutes-or-less', 'time-to-make', 'course... | \n", "[430.0, 47.0, 10.0, 32.0, 28.0, 49.0, 8.0] | \n", "7 | \n", "['pre-heat oven to 350 degrees', 'cook sausage... | \n", "delicious casserole recipe that has been in my... | \n", "['fresh spinach', 'spicy sausage', 'sliced mus... | \n", "8 | \n", "
147699 | \n", "omelette | \n", "431785 | \n", "10 | \n", "1627002 | \n", "2010-07-02 | \n", "['15-minutes-or-less', 'time-to-make', 'prepar... | \n", "[370.0, 48.0, 4.0, 12.0, 37.0, 50.0, 0.0] | \n", "10 | \n", "['break the eggs into a jug and add the water'... | \n", "taken from 'food for health and vitality' - se... | \n", "['eggs', 'water', 'dried thyme', 'black pepper... | \n", "9 | \n", "
223230 | \n", "vichyssoise cream of leek | \n", "47763 | \n", "40 | \n", "20480 | \n", "2002-12-07 | \n", "['60-minutes-or-less', 'time-to-make', 'course... | \n", "[173.4, 15.0, 9.0, 31.0, 12.0, 28.0, 5.0] | \n", "6 | \n", "['in a 2 qt glass casserole place leeks , onio... | \n", "wonderful soup made in the microwave. it can b... | \n", "['leeks', 'onion', 'potatoes', 'butter', 'chic... | \n", "8 | \n", "
10000 rows × 12 columns
\n", "