Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 58,469 Bytes
a68743b |
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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from PIL import Image\n",
"import numpy as np\n",
"import os\n",
"\n",
"import torch\n",
"import torch.nn.functional as F\n",
"\n",
"# from src.data.embs import ImageDataset\n",
"from src.model.blip_embs import blip_embs\n",
"from src.data.transforms import transform_test\n",
"\n",
"from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer\n",
"import gradio as gr\n",
"# import spaces\n",
"\n",
"from langchain.chains import ConversationChain\n",
"from langchain_community.chat_message_histories import ChatMessageHistory\n",
"from langchain_core.runnables import RunnableWithMessageHistory\n",
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_groq import ChatGroq\n",
"\n",
"from dotenv import load_dotenv\n",
"from flask import Flask, request, render_template\n",
"from flask_cors import CORS\n",
"from flask_socketio import SocketIO, emit\n",
"\n",
"import json\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"# GROQ_API_KEY = os.getenv(\"GROQ_API_KEY\")\n",
"GROQ_API_KEY = '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'\n",
"OPENAI_API_KEY=\"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\"\n",
"load_dotenv(\".env\")\n",
"USER_AGENT = os.getenv(\"USER_AGENT\")\n",
"GROQ_API_KEY = os.getenv(\"GROQ_API_KEY\")\n",
"\n",
"SECRET_KEY = os.getenv(\"SECRET_KEY\")\n",
"\n",
"# Set environment variables\n",
"os.environ['USER_AGENT'] = USER_AGENT\n",
"os.environ[\"GROQ_API_KEY\"] = GROQ_API_KEY\n",
"os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY\n",
"os.environ[\"TOKENIZERS_PARALLELISM\"] = 'true'"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"# Initialize Flask app and SocketIO with CORS\n",
"app = Flask(__name__)\n",
"CORS(app)\n",
"socketio = SocketIO(app, cors_allowed_origins=\"*\", logger=True)\n",
"app.config['SESSION_COOKIE_SECURE'] = True # Use HTTPS\n",
"app.config['SESSION_COOKIE_HTTPONLY'] = True\n",
"app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'\n",
"app.config['SECRET_KEY'] = SECRET_KEY"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"# Initialize LLM\n",
"llm = ChatGroq(model=\"llama-3.1-8b-instant\", temperature=0, max_tokens=1024, max_retries=2)\n",
"\n",
"# Initialize Router\n",
"router = ChatGroq(model=\"llama-3.2-3b-preview\", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={\"response_format\": {\"type\": \"json_object\"}})\n",
"\n",
"# Initialized recommendation LLM\n",
"client = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
"class StoppingCriteriaSub(StoppingCriteria):\n",
"\n",
" def __init__(self, stops=[], encounters=1):\n",
" super().__init__()\n",
" self.stops = stops\n",
"\n",
" def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor):\n",
" for stop in self.stops:\n",
" if torch.all(input_ids[:, -len(stop):] == stop).item():\n",
" return True\n",
"\n",
" return False\n",
"\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"def get_blip_config(model=\"base\"):\n",
" config = dict()\n",
" if model == \"base\":\n",
" config[\n",
" \"pretrained\"\n",
" ] = \"https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth \"\n",
" config[\"vit\"] = \"base\"\n",
" config[\"batch_size_train\"] = 32\n",
" config[\"batch_size_test\"] = 16\n",
" config[\"vit_grad_ckpt\"] = True\n",
" config[\"vit_ckpt_layer\"] = 4\n",
" config[\"init_lr\"] = 1e-5\n",
" elif model == \"large\":\n",
" config[\n",
" \"pretrained\"\n",
" ] = \"https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_retrieval_coco.pth\"\n",
" config[\"vit\"] = \"large\"\n",
" config[\"batch_size_train\"] = 16\n",
" config[\"batch_size_test\"] = 32\n",
" config[\"vit_grad_ckpt\"] = True\n",
" config[\"vit_ckpt_layer\"] = 12\n",
" config[\"init_lr\"] = 5e-6\n",
"\n",
" config[\"image_size\"] = 384\n",
" config[\"queue_size\"] = 57600\n",
" config[\"alpha\"] = 0.4\n",
" config[\"k_test\"] = 256\n",
" config[\"negative_all_rank\"] = True\n",
"\n",
" return config"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creating model\n",
"load checkpoint from https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_retrieval_coco.pth\n",
"missing keys:\n",
"[]\n",
"Model Loaded !\n",
"==================================================\n"
]
}
],
"source": [
"print(\"Creating model\")\n",
"config = get_blip_config(\"large\")\n",
"\n",
"model = blip_embs(\n",
" pretrained=config[\"pretrained\"],\n",
" image_size=config[\"image_size\"],\n",
" vit=config[\"vit\"],\n",
" vit_grad_ckpt=config[\"vit_grad_ckpt\"],\n",
" vit_ckpt_layer=config[\"vit_ckpt_layer\"],\n",
" queue_size=config[\"queue_size\"],\n",
" negative_all_rank=config[\"negative_all_rank\"],\n",
" )\n",
"\n",
"model = model.to(device)\n",
"model.eval()\n",
"print(\"Model Loaded !\")\n",
"print(\"=\"*50)"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading Data\n",
"Loading Target Embedding\n"
]
}
],
"source": [
"transform = transform_test(384)\n",
"\n",
"print(\"Loading Data\")\n",
"df = pd.read_json(\"datasets/sidechef/my_recipes.json\")\n",
"\n",
"print(\"Loading Target Embedding\")\n",
"tar_img_feats = []\n",
"for _id in df[\"id_\"].tolist(): \n",
" tar_img_feats.append(torch.load(\"datasets/sidechef/blip-embs-large/{:07d}.pth\".format(_id)).unsqueeze(0))\n",
"\n",
"tar_img_feats = torch.cat(tar_img_feats, dim=0)"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"class Chat:\n",
"\n",
" def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda:0', stopping_criteria=None):\n",
" self.device = device\n",
" self.model = model\n",
" self.transform = transform\n",
" self.df = dataframe\n",
" self.tar_img_feats = tar_img_feats\n",
" self.img_feats = None\n",
" self.target_recipe = None\n",
" self.messages = []\n",
"\n",
" if stopping_criteria is not None:\n",
" self.stopping_criteria = stopping_criteria\n",
" else:\n",
" stop_words_ids = [torch.tensor([2]).to(self.device)]\n",
" self.stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])\n",
"\n",
" def encode_image(self, image_path):\n",
" img = Image.fromarray(image_path).convert(\"RGB\")\n",
" img = self.transform(img).unsqueeze(0)\n",
" img = img.to(self.device)\n",
" img_embs = model.visual_encoder(img)\n",
" img_feats = F.normalize(model.vision_proj(img_embs[:, 0, :]), dim=-1).cpu()\n",
"\n",
" self.img_feats = img_feats \n",
"\n",
" self.get_target(self.img_feats, self.tar_img_feats)\n",
"\n",
" def get_target(self, img_feats, tar_img_feats) : \n",
" score = (img_feats @ tar_img_feats.t()).squeeze(0).cpu().detach().numpy()\n",
" index = np.argsort(score)[::-1][0]\n",
" self.target_recipe = df.iloc[index]\n",
"\n",
" def ask(self):\n",
" return json.dumps(self.target_recipe.to_json())\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chat Initialized !\n"
]
}
],
"source": [
"chat = Chat(model,transform,df,tar_img_feats, device)\n",
"print(\"Chat Initialized !\")"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def answer_generator(formated_input):\n",
" # QA system prompt and chain\n",
" qa_system_prompt = \"\"\"\n",
" You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on provide context ans user query.\n",
" Operational Guidelines:\n",
" 1. Input Structure:\n",
" - Context: You may receive contextual information related to recipes, such as specific data sets, user preferences, dietary restrictions, or previously selected dishes.\n",
" - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient substitutions, cooking instructions, and more.\n",
" 2. Response Strategy:\n",
" - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance.\n",
" - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, generate a response based solely on the user's input and your trained knowledge.\n",
" Core Functionalities:\n",
" - Nutritional Information: Accurately provide nutritional values for each recipe, including calories, macronutrients (proteins, fats, carbohydrates), and essential vitamins and minerals, using contextual data when available.\n",
" - Ingredient Details: List all ingredients required for recipes, including substitute options for dietary restrictions or ingredient availability, utilizing context when relevant.\n",
" - Step-by-Step Cooking Instructions: Deliver clear, easy-to-follow instructions for preparing and cooking meals, informed by any provided contextual data.\n",
" - Recipe Recommendations: Suggest dishes based on user preferences, dietary restrictions, available ingredients, and contextual data if provided.\n",
" Additional Instructions:\n",
" - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.\n",
" - Clarity and Coherence: Ensure that all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.\n",
" - Substitute Suggestions: When suggesting ingredient substitutes, consider user preferences and dietary restrictions outlined in the context or user query.\n",
" - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.\n",
" Don't mention about context in the response, format the answer in a natural and friendly way.\n",
" Context:\n",
" {context}\n",
" \"\"\"\n",
" qa_prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\"system\", qa_system_prompt),\n",
" (\"human\", \"{input}\")\n",
" ]\n",
" )\n",
"\n",
" # Create the base chain\n",
" base_chain = qa_prompt | llm | StrOutputParser()\n",
"\n",
" # Wrap the chain with message history\n",
" question_answer_chain = RunnableWithMessageHistory(\n",
" base_chain,\n",
" lambda session_id: ChatMessageHistory(), # This creates a new history for each session\n",
" input_messages_key=\"input\",\n",
" history_messages_key=\"chat_history\"\n",
" )\n",
"\n",
" response = question_answer_chain.invoke(formated_input, config={\"configurable\": {\"session_id\": 'abc123'}})\n",
"\n",
" return response\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"### Router\n",
"import json\n",
"from langchain_core.messages import HumanMessage, SystemMessage\n",
"\n",
"def router_node(query):\n",
" # Prompt\n",
" router_instructions = \"\"\"You are an expert at determining the appropriate task for a user’s question based on chat history and the current query context. You have two available tasks:\n",
"\n",
" 1.\tRetrieval: Fetch information based on user's chat history and current query.\n",
" 2.\tRecommendation/Suggestion: Recommend recipes to users based on the query.\n",
"\n",
" Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.\n",
" \"\"\"\n",
"\n",
" \n",
"\n",
" response = router.invoke(\n",
" [SystemMessage(content=router_instructions)]\n",
" + [\n",
" HumanMessage(\n",
" content=query\n",
" )\n",
" ]\n",
" )\n",
" res = json.loads(response.content)\n",
" return res['task']"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
"def recommendation_node(query):\n",
" prompt = \"\"\"\n",
" You are a helpful assistant that writes Python code to filter recipes from a JSON filr based o the user query. \\n\n",
" JSON file path = 'recipes.json' \\n\n",
" The JSON file is a list of recipes with the following structure: \\n\n",
" {\n",
" \"recipe_name\": string,\n",
" \"recipe_time\": integer,\n",
" \"recipe_yields\": string,\n",
" \"recipe_ingredients\": list of ingredients,\n",
" \"recipe_instructions\": list of instruections,\n",
" \"recipe_image\": string,\n",
" \"blogger\": string,\n",
" \"recipe_nutrients\": JSON object with key value pairs such as \"protein: 10g\",\n",
" \"tags\": list of tags related to recipe\n",
" } \\n\n",
"\n",
" Here is the example of an recipe json object from the JSON data: \\n\n",
" {\n",
" \"recipe_name\": \"Asian Potato Salad with Seven Minute Egg\",\n",
" \"recipe_time\": 0,\n",
" \"recipe_yields\": \"4 servings\",\n",
" \"recipe_ingredients\": [\n",
" \"2 1/2 cup Multi-Colored Fingerling Potato\",\n",
" \"3/4 cup Celery\",\n",
" \"1/4 cup Red Onion\",\n",
" \"2 tablespoon Fresh Parsley\",\n",
" \"1/3 cup Mayonnaise\",\n",
" \"1 tablespoon Chili Garlic Sauce\",\n",
" \"1 teaspoon Hoisin Sauce\",\n",
" \"1 splash Soy Sauce\",\n",
" \"to taste Salt\",\n",
" \"to taste Ground Black Pepper\",\n",
" \"4 Egg\"\n",
" ],\n",
" \"recipe_instructions\": \"Fill a large stock pot with water.\\nAdd the Multi-Colored Fingerling Potato (2 1/2 cup) and bring water to a boil. Boil the potatoes for 20 minutes or until fork tender.\\nDrain the potatoes and let them cool completely.\\nMeanwhile, mix together in a small bowl Mayonnaise (1/3 cup), Chili Garlic Sauce (1 tablespoon), Hoisin Sauce (1 teaspoon), and Soy Sauce (1 splash).\\nTo make the Egg (4), fill a stock pot with water and bring to a boil Gently add the eggs to the water and set a timer for seven minutes.\\nThen move the eggs to an ice bath to cool completely. Once cooled, crack the egg slightly and remove the shell. Slice in half when ready to serve.\\nNext, halve the cooled potatoes and place into a large serving bowl. Add the Ground Black Pepper (to taste), Celery (3/4 cup), Red Onion (1/4 cup), and mayo mixture. Toss to combine adding Salt (to taste) and Fresh Parsley (2 tablespoon).\\nTop with seven minute eggs and serve. Enjoy!\",\n",
" \"recipe_image\": \"https://www.sidechef.com/recipe/eeeeeceb-493e-493d-8273-66c800821b13.jpg?d=1408x1120\",\n",
" \"blogger\": \"sidechef.com\",\n",
" \"recipe_nutrients\": {\n",
" \"calories\": \"80 calories\",\n",
" \"proteinContent\": \"2.1 g\",\n",
" \"fatContent\": \"6.2 g\",\n",
" \"carbohydrateContent\": \"3.9 g\",\n",
" \"fiberContent\": \"0.5 g\",\n",
" \"sugarContent\": \"0.4 g\",\n",
" \"sodiumContent\": \"108.0 mg\",\n",
" \"saturatedFatContent\": \"1.2 g\",\n",
" \"transFatContent\": \"0.0 g\",\n",
" \"cholesterolContent\": \"47.4 mg\",\n",
" \"unsaturatedFatContent\": \"3.8 g\"\n",
" },\n",
" \"tags\": [\n",
" \"Salad\",\n",
" \"Lunch\",\n",
" \"Brunch\",\n",
" \"Appetizers\",\n",
" \"Side Dish\",\n",
" \"Budget-Friendly\",\n",
" \"Vegetarian\",\n",
" \"Pescatarian\",\n",
" \"Eggs\",\n",
" \"Potatoes\",\n",
" \"Dairy-Free\",\n",
" \"Shellfish-Free\"\n",
" ]\n",
" } \\n\n",
"\n",
" Based on the user query, provide a Python function to filter the JSON data. The output of the function should be a list of json objects. \\n\n",
"\n",
" Your output instructions:\n",
" - The function name should be filter_recipes. The input to the function should be file name.\n",
" - The length of output recipes should not be more than 10.\n",
" - Only give me output function. Do not call the function.\n",
" - Give the python function as a key named \"code\" in a json format.\n",
" - Do not include any other text with the output, only give python code.\n",
" - If you do not follow the above given instructions, the chat may be terminated.\n",
" \"\"\"\n",
" max_tries = 3\n",
" while True:\n",
" try:\n",
" # llm = ChatGroq(model=\"llama-3.1-8b-instant\", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={\"response_format\": {\"type\": \"json_object\"}})\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": prompt},\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": query\n",
" }\n",
" ]\n",
" )\n",
"\n",
" content = response.choices[0].message.content\n",
"\n",
" res = json.loads(content)\n",
" script = res['code']\n",
" exec(script)\n",
" recipes = filter_recipes('recipes.json')\n",
" if recipes:\n",
" break\n",
" except Exception as e:\n",
" if max_tries <= 0:\n",
" return []\n",
" else:\n",
" max_tries -= 1\n",
" return recipes"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"CURR_CONTEXT = ''"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"# @spaces.GPU\n",
"def respond_to_user(image=[], message=''):\n",
" global curr_context\n",
" if len(image) > 0:\n",
" try:\n",
" # Process the image and message here\n",
" device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
" chat = Chat(model,transform,df,tar_img_feats, device)\n",
" chat.encode_image(image)\n",
" data = chat.ask()\n",
" curr_context = data\n",
" formated_input = {\n",
" 'input': message,\n",
" 'context': data\n",
" }\n",
" response = answer_generator(formated_input)\n",
" except Exception as e:\n",
" print(e)\n",
" response = {'content':\"An error occurred while processing your request.\"}\n",
" elif len(image) == 0 and message is not None:\n",
" print(\"I am here\")\n",
" task = router_node(message)\n",
" if task == 'retrieval':\n",
" response = recommendation_node(message)\n",
" if response:\n",
" response = {'content':\"An error occurred while processing your request.\"}\n",
" else:\n",
" formated_input = {\n",
" 'input': message,\n",
" 'context': curr_context\n",
" }\n",
" response = answer_generator(formated_input)\n",
"\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [],
"source": [
"image_path = \"./test_images/15-Second_Creamy_Scrambled_Eggs_0000200.png\"\n",
"message = \"give me nutritional information of this dish\""
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import numpy as np\n",
"\n",
"# Load the image\n",
"image = Image.open(image_path)\n",
"\n",
"# Convert the image to a NumPy array\n",
"image_array = np.array(image)"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('For the \"15-Second Creamy Scrambled Eggs\" recipe, you\\'ll need the following '\n",
" 'ingredients:\\n'\n",
" '\\n'\n",
" '1. 3 large Eggs\\n'\n",
" '2. 1 1/2 tablespoon Milk\\n'\n",
" '3. 1 3/4 teaspoon Corn Starch\\n'\n",
" '4. Salt (to taste)\\n'\n",
" '5. 3 tablespoon Unsalted Butter\\n'\n",
" '\\n'\n",
" 'These ingredients will help you create the creamiest, fastest, and easiest '\n",
" 'scrambled eggs ever!')\n"
]
}
],
"source": [
"import pprint\n",
"res = respond_to_user(image=image_array, message=\"give me ingredients fot his dish\")\n",
"pprint.pprint(res)"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"''"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"curr_context"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'recipe_name': 'Farmers Market Breakfast Pizza',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '2 servings',\n",
" 'recipe_ingredients': ['1/2 Pizza Dough',\n",
" '1/2 cup Kale',\n",
" '1/2 cup Onion',\n",
" '1/2 Zucchini',\n",
" '1/2 Yellow Squash',\n",
" '1/2 cup Shredded Mozzarella Cheese',\n",
" '3 Egg',\n",
" '1 tablespoon Coconut Oil',\n",
" '3 clove Garlic',\n",
" '1 cup Sweet Onion',\n",
" '1/4 cup Water',\n",
" '3 cup Cherry Tomato',\n",
" '1/4 teaspoon Salt',\n",
" '1/4 teaspoon Ground Black Pepper',\n",
" '1 teaspoon Granulated Sugar',\n",
" '1 tablespoon Dried Parsley',\n",
" '1 teaspoon Dried Basil',\n",
" '10 Fresh Basil Leaf',\n",
" 'as needed Coconut Oil Cooking Spray'],\n",
" 'recipe_instructions': 'For homemade pizza sauce, finely chop the Sweet Onion (1 cup), and mince the Garlic (3 clove). To a large sauce pan over medium heat, add the Coconut Oil (1 tablespoon), garlic and onions. Cook until onions are translucent, about 5 to 6 minutes.\\nAdd Water (1/4 cup), chopped Cherry Tomato (3 cup), Salt (1/4 teaspoon), Ground Black Pepper (1/4 teaspoon), Granulated Sugar (1 teaspoon), Dried Parsley (1 tablespoon), Dried Basil (1 teaspoon), finely chopped Fresh Basil Leaf (10), and continue cooking another 2 to 3 minutes.\\nReduce heat and simmer 5 more minutes, or until tomatoes have released their juices and cooked down.\\nRemove sauce pan from stove, allow to cool 2 to 3 minutes, then add to NutriBullet or food processor and process 8 to 10 seconds or until sauce reaches a thick, slightly chunky consistency.\\nPreheat oven to 400 degrees F (200 degrees C).\\nSpray pizza pan with Pizza Dough (1/2), roll out Coconut Oil Cooking Spray (as needed), and bake crust for 5 minutes.\\nThinly slice the Zucchini (1/2) and Yellow Squash (1/2).\\nRemove from the oven, add homemade sauce, chopped Kale (1/2 cup), sliced Onion (1/2 cup), zucchini, squash, and Shredded Mozzarella Cheese (1/2 cup), then top with the Egg (3). Bake an additional 5 minutes.\\nTurn heat up to 425 degrees F (220 degrees C), and bake 5 to 7 minutes or until the whites of the eggs are cooked through and the yoke is over medium.\\nEnjoy!',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/1cd15944-9411-4a9f-9cc9-18cb2050041e.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '315 calories',\n",
" 'proteinContent': '16.2 g',\n",
" 'fatContent': '10.3 g',\n",
" 'carbohydrateContent': '43.1 g',\n",
" 'fiberContent': '7.8 g',\n",
" 'sugarContent': '8.7 g',\n",
" 'sodiumContent': '586.3 mg',\n",
" 'saturatedFatContent': '5.3 g',\n",
" 'transFatContent': '0.0 g',\n",
" 'cholesterolContent': '144.3 mg',\n",
" 'unsaturatedFatContent': '2.5 g'},\n",
" 'tags': ['Breakfast',\n",
" 'Brunch',\n",
" 'Main Dish',\n",
" 'Budget-Friendly',\n",
" 'Vegetarian',\n",
" 'Pescatarian',\n",
" 'Eggs',\n",
" 'Pizza',\n",
" 'Vegetables',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Soy-Free',\n",
" \"Mothers' Day\",\n",
" \"Father's Day\",\n",
" 'Food Processor',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Oven',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000004'},\n",
" {'recipe_name': 'Fettuccini Carbonara',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '2 servings',\n",
" 'recipe_ingredients': ['2 Shallot',\n",
" '1 clove Garlic',\n",
" '2 Egg',\n",
" '6 slice Bacon',\n",
" '1/2 cup Heavy Cream',\n",
" '1/4 cup Grated Parmesan Cheese',\n",
" '8 ounce Fettuccine',\n",
" '1 tablespoon Olive Oil',\n",
" 'to taste Salt',\n",
" 'to taste Ground Black Pepper',\n",
" 'to taste Fresh Parsley'],\n",
" 'recipe_instructions': \"Put a generously salted pot of water on to boil for the pasta.\\nIn a pan over medium-low heat, add the Bacon (6 slice) and cook until done but flexible. Sauté Shallot (2) and Garlic (1 clove) until soft.\\nTurn off heat. Add Grated Parmesan Cheese (1/4 cup), Heavy Cream (1/2 cup), and Egg (2) to the shallots and bacon. Mix well.\\nBoil Fettuccine (8 ounce) until al dente. If you're using frozen fresh, pasta about 6 minutes. If it's fresh, 4 minutes. If using dry pasta, follow package instructions.\\nLift pasta out of pot and place into cream mixture. If a little of the pasta water gets into the sauce, that's ok, since the starch in the water with help thicken it up. Gently toss to combine. Add Salt (to taste) and Ground Black Pepper (to taste).\\nDrizzle with Fresh Parsley (to taste). Top with Olive Oil (1 tablespoon) and grated parmesan cheese.\",\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/9e5df75f-bf1a-4e68-b8a9-096842ea6bd6.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '495 calories',\n",
" 'proteinContent': '15.9 g',\n",
" 'fatContent': '27.1 g',\n",
" 'sugarContent': '3.2 g',\n",
" 'sodiumContent': '282.9 mg',\n",
" 'saturatedFatContent': '12.0 g',\n",
" 'transFatContent': '0.0 g',\n",
" 'cholesterolContent': '150.1 mg',\n",
" 'carbohydrateContent': '47.5 g',\n",
" 'fiberContent': '2.5 g',\n",
" 'unsaturatedFatContent': '9.9 g'},\n",
" 'tags': ['Pasta',\n",
" 'Dinner',\n",
" 'Side Dish',\n",
" 'Main Dish',\n",
" 'Quick and Easy',\n",
" 'Pork',\n",
" 'Eggs',\n",
" 'Cheese',\n",
" 'Date Night',\n",
" '30 or Less',\n",
" 'Comfort Food',\n",
" 'Easy',\n",
" 'Quick',\n",
" 'Italian',\n",
" 'Shellfish-Free',\n",
" 'Gluten-Free',\n",
" 'Soy-Free',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'Classic',\n",
" 'Tomato-Free',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000006'},\n",
" {'recipe_name': 'Huevos Rancheros',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '1 serving',\n",
" 'recipe_ingredients': ['2 Yellow Corn Tortilla',\n",
" '2 tablespoon Pinto Beans',\n",
" '2 Egg',\n",
" '2 tablespoon Salsa',\n",
" 'as needed Nonstick Cooking Spray',\n",
" 'to taste Avocado',\n",
" 'to taste Cotija Cheese',\n",
" 'to taste Bacon Bits',\n",
" 'to taste Fresh Cilantro'],\n",
" 'recipe_instructions': 'In a small frying pan, spray a little Nonstick Cooking Spray (as needed) in the pan and heat over medium-high heat. Once hot, place Yellow Corn Tortilla (2), then spray the top of tortilla with oil. Lightly fry for about 30 seconds on each side.\\nCook the Egg (2) to your liking: sunny side up, over easy, or scrambled.\\nAssemble Huevos Rancheros by spreading Salsa (2 tablespoon) over the fried tortillas. Top with cooked eggs and Pinto Beans (2 tablespoon).\\nAdd Fresh Cilantro (to taste), Cotija Cheese (to taste), Bacon Bits (to taste), and Avocado (to taste) if desired. Enjoy!',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/5284bc88-1305-4379-90c1-59b74a7e9660.jpeg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '290 calories',\n",
" 'proteinContent': '19.2 g',\n",
" 'fatContent': '10.2 g',\n",
" 'carbohydrateContent': '37.6 g',\n",
" 'fiberContent': '11.7 g',\n",
" 'sugarContent': '2.3 g',\n",
" 'sodiumContent': '380.4 mg',\n",
" 'saturatedFatContent': '3.1 g',\n",
" 'transFatContent': '0.0 g',\n",
" 'cholesterolContent': '364.6 mg',\n",
" 'unsaturatedFatContent': '5.5 g'},\n",
" 'tags': ['Breakfast',\n",
" 'Brunch',\n",
" 'Quick and Easy',\n",
" 'Beans and Legumes',\n",
" 'Eggs',\n",
" '30 or Less',\n",
" 'Easy',\n",
" 'Quick',\n",
" 'Mexican',\n",
" 'Shellfish-Free',\n",
" 'Full Meal',\n",
" 'Gluten-Free',\n",
" 'Soy-Free',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'Tomato-Free',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000009'},\n",
" {'recipe_name': 'Corn & Bacon Hash',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '2 servings',\n",
" 'recipe_ingredients': ['6 slice Thick-Cut Bacon',\n",
" '1 pound Red Potato',\n",
" '2 ear Corn',\n",
" '1 bunch Scallion',\n",
" '1/2 teaspoon Salt',\n",
" '1/4 teaspoon Ground Black Pepper',\n",
" '2 tablespoon Butter',\n",
" '2 Egg'],\n",
" 'recipe_instructions': 'Thinly slice the Scallion (1 bunch). Cut the Red Potato (1 pound) into small cubes. Cut the kernels from the Corn (2 ear). Dice the Thick-Cut Bacon (6 slice).\\nCook bacon in a large frying pan over medium heat until the fat is rendered. Once it is crisp, use a slotted spoon to remove the bacon to a plate lined with paper towels.\\nLeave the fat in the pan and add the potatoes. Increase the heat to medium-high. Season with half of the Salt (1/2 teaspoon) and Ground Black Pepper (1/4 teaspoon). Cook for 15 to 20 minutes or until potatoes can easily be pierced with a fork and are golden-brown on the the outside.\\nAdd corn to the skillet and bump the heat up just a bit. Cook the potatoes and corn together for 5 to 6 minutes, stirring frequently, until the corn browns a bit.\\nHeat a small frying pan over medium heat. Add the Butter (2 tablespoon) and crack the Egg (2) in, taking care not to break the yolk. Allow to cook for 1-2 minutes, then flip the egg to cook the other side. Cook for a minute more for an over-medium egg. Remove to a small plate.\\nWhile the eggs finish cooking, add the drained bacon and the green onions to the corn and potatoes and mix well. Turn off the heat and season to taste with the remaining salt and pepper.\\nServe a couple scoops of hash and top with one of the eggs.',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/385d1878-283d-47e1-9f3f-e591298a92b6.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '1230 calories',\n",
" 'proteinContent': '37.0 g',\n",
" 'fatContent': '32.0 g',\n",
" 'carbohydrateContent': '205.9 g',\n",
" 'fiberContent': '20.9 g',\n",
" 'sugarContent': '3.8 g',\n",
" 'sodiumContent': '973.3 mg',\n",
" 'saturatedFatContent': '11.4 g',\n",
" 'transFatContent': '0.0 g',\n",
" 'cholesterolContent': '132.8 mg',\n",
" 'unsaturatedFatContent': '12.0 g'},\n",
" 'tags': ['Breakfast',\n",
" 'Brunch',\n",
" 'Pork',\n",
" 'Eggs',\n",
" 'Potatoes',\n",
" 'Easy',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Gluten-Free',\n",
" 'Soy-Free',\n",
" 'Spring',\n",
" 'Summer',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'Tomato-Free',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000014'},\n",
" {'recipe_name': 'The 1-Minute Breakfast Sandwich',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '1 serving',\n",
" 'recipe_ingredients': ['1 English Muffin',\n",
" '1 Egg',\n",
" '1 slice Cheese',\n",
" 'to taste Fresh Spinach',\n",
" 'to taste Carrot',\n",
" 'to taste Alfalfa Sprouts',\n",
" 'to taste Butter',\n",
" 'to taste Mayonnaise',\n",
" 'to taste Sea Salt',\n",
" 'to taste Ground Black Pepper'],\n",
" 'recipe_instructions': 'Place a small pat of Ground Black Pepper (to taste) at the bottom of a small round microwave-safe dish. We like to use our 4 1/2-inch ramekin. Crack an Sea Salt (to taste) over the top. Add Egg (1) and Butter (to taste). Cover dish and microwave for 20 to 30 seconds.\\nMeanwhile place English Muffin (1) into your toaster and toast.\\nSpread Mayonnaise (to taste) over both side of the toasted muffin. Run a butter knife around the edge of the egg dish to release. Add it to the muffin. Add the Cheese (1 slice), Fresh Spinach (to taste), Carrot (to taste), and Alfalfa Sprouts (to taste).\\nTop with the other half of the muffin. Wrap in parchment and away you go!',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/93e294f3-99e8-4953-a4b6-09b452fd4fa6.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '310 calories',\n",
" 'proteinContent': '17.7 g',\n",
" 'fatContent': '15.3 g',\n",
" 'carbohydrateContent': '26.9 g',\n",
" 'fiberContent': '2.1 g',\n",
" 'sugarContent': '2.2 g',\n",
" 'sodiumContent': '649.5 mg',\n",
" 'saturatedFatContent': '6.9 g',\n",
" 'transFatContent': '0.0 g',\n",
" 'cholesterolContent': '214.4 mg',\n",
" 'unsaturatedFatContent': '3.2 g'},\n",
" 'tags': ['Sandwich',\n",
" 'Breakfast',\n",
" 'Brunch',\n",
" 'Vegetarian',\n",
" 'Low-Carb',\n",
" 'Pescatarian',\n",
" 'Eggs',\n",
" 'Easy',\n",
" 'Quick',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Full Meal',\n",
" 'Beginner',\n",
" 'Soy-Free',\n",
" 'Microwave',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'Tomato-Free',\n",
" 'Microwave',\n",
" ''],\n",
" 'id_': '0000017'},\n",
" {'recipe_name': 'Baked Cheesecake',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '1 serving',\n",
" 'recipe_ingredients': ['125 gram Butter',\n",
" '50 gram Caster Sugar',\n",
" '150 gram All-Purpose Flour',\n",
" '30 gram Corn Flour',\n",
" '1 teaspoon Vanilla Essence',\n",
" '1 pinch Salt',\n",
" '500 gram Cream Cheese',\n",
" '150 gram Granulated Sugar',\n",
" '7 Egg',\n",
" '150 gram Sour Cream',\n",
" '5 gram Vanilla Essence',\n",
" '1 Lemon'],\n",
" 'recipe_instructions': 'Put Butter (125 gram), Caster Sugar (50 gram), All-Purpose Flour (150 gram), Corn Flour (30 gram), Vanilla Essence (1 teaspoon), and Salt (1 pinch) in the bowl of a stand mixer, with the beater attachment. Mix on low speed until dough forms, don’t over mix.\\nFlatten into a disc, wrap with cling film and refrigerate for 30 minutes.\\nPreheat the oven to 180 degrees C (350 degrees F) steam bake.\\nPut Cream Cheese (500 gram), Granulated Sugar (150 gram), Egg (7), Sour Cream (150 gram), Vanilla Essence (5 gram), Lemon (1) into the bowl of a stand mixer, use the whisk attachment on slow speed until the mixture is smooth.\\nRoll pastry to about 2 to 3-millimeters thick on baking paper in a baking tray. Dock well, make sure pastry is larger than the cake ring.\\nPut the baking tray into shelf 2 and bake for 15 minutes.\\nAllow to cool slightly, press the cake ring into a disc and allow to cool completely, it will be very fragile. Reduce the oven temperature to 130 degrees C (270 degrees F).\\nPut the pastry on the greased and parchment-lined cake tin base, line the sides of the cake ring, and clamp.\\nPour in filling and bake for 35 minutes.\\nAfter 35 minutes, turn off the oven and keep the door closed for 1 hour. After that leave a gap in the oven door for another half hour. Take the cheesecake out of the oven and cool completely before putting it into the fridge.\\nAllow to cool completely before cutting, leaving overnight in the refrigerator is best. Serve.',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/8b1073e8-de06-4f2e-8715-1b563363ad36.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '4917 calories',\n",
" 'proteinContent': '98.4 g',\n",
" 'fatContent': '334.1 g',\n",
" 'carbohydrateContent': '386.7 g',\n",
" 'sugarContent': '233.7 g',\n",
" 'sodiumContent': '2354.3 mg',\n",
" 'saturatedFatContent': '195.3 g',\n",
" 'cholesterolContent': '2150.2 mg',\n",
" 'fiberContent': '8.6 g',\n",
" 'transFatContent': '6.0 g',\n",
" 'unsaturatedFatContent': '105.8 g'},\n",
" 'tags': ['Dessert',\n",
" 'Vegetarian',\n",
" 'Pescatarian',\n",
" 'Eggs',\n",
" 'Cheese',\n",
" 'Baked Goods',\n",
" 'Baking',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Weekend Project',\n",
" 'Soy-Free',\n",
" 'Entertaining',\n",
" 'Stand Mixer',\n",
" 'Fish-Free',\n",
" 'Fridge',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Tomato-Free',\n",
" 'Oven',\n",
" 'Electrolux APAC',\n",
" ''],\n",
" 'id_': '0000018'},\n",
" {'recipe_name': 'Keto Sausage and Egg McMuffin',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '1 serving',\n",
" 'recipe_ingredients': ['3 tablespoon Almond Flour',\n",
" '1/2 teaspoon Psyllium Powder',\n",
" '1/2 teaspoon Baking Powder',\n",
" '1 pinch Salt',\n",
" '1 tablespoon Butter',\n",
" '1 Large Egg',\n",
" '1 pound Ground Pork',\n",
" '1 teaspoon Ground Sage',\n",
" '1 teaspoon Dried Rosemary',\n",
" '1 teaspoon Salt',\n",
" '1 teaspoon Ground Black Pepper',\n",
" '1/8 teaspoon Chili Powder',\n",
" '1 tablespoon Olive Oil',\n",
" '2 Large Egg',\n",
" '1 slice Cheddar Cheese'],\n",
" 'recipe_instructions': 'To make the Keto Bread, add Almond Flour (3 tablespoon), Psyllium Powder (1/2 teaspoon), and Baking Powder (1/2 teaspoon) into a jug or bowl. Season with Salt (1 pinch). Mix everything until well combined.\\nAdd in Butter (1 tablespoon) and a Large Egg (1). Mix the dry and wet ingredients really well until smooth like a cake batter.\\nNow grease a ramekin with butter and add in the mixture, smoothing out all sides and tapping the ramekin to avoid large air pockets.\\nPlace into the microwave on high for 90 seconds, when you bring it out it will have shrunk in from the sides and risen slightly. Carefully remove the muffin and leave it aside.\\nIn the same sized ramekin that you used for the bread, grease the outside with butter, crack the Large Egg (2) into it, and gently break up the yolks.\\nMicrowave the eggs on medium for 60-90 seconds. Again it will shrink away from the sides and can be easily removed.\\nFor the sausage, into a bowl add in the Salt (1 teaspoon), season with Dried Rosemary (1 teaspoon), Ground Pork (1 pound), Chili Powder (1/8 teaspoon), and finally season with Ground Sage (1 teaspoon) and Ground Black Pepper (1 teaspoon). Mix gently with a fork, don’t over mix otherwise it will become tough and dry when we cook them.\\nRoll into balls and shape into patties the size of the ramekins used for the bread and eggs.\\nIn a nonstick pan over medium to high heat, add in Olive Oil (1 tablespoon) and the sausage patty. Cook the sausage patty for 3 minutes on each side. You’re after a nice crust on the outside but juicy and tender in the middle.\\nRemove the patty and leave to rest for 1 minute.\\nIn a nonstick pan, cut the bread in half and toast the muffin.\\nTo assemble, on the muffin, place the sausage patty, the egg, Cheddar Cheese (1 slice), and top with the other half of the muffin. Serve immediately.',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/2a4aec1f-edeb-49f5-be20-33477d41e5af.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'saturatedFatContent': '57.0 g',\n",
" 'calories': '1924 calories',\n",
" 'proteinContent': '109.3 g',\n",
" 'fiberContent': '4.4 g',\n",
" 'carbohydrateContent': '9.2 g',\n",
" 'fatContent': '160.5 g',\n",
" 'sugarContent': '1.0 g',\n",
" 'sodiumContent': '3327.9 mg',\n",
" 'transFatContent': '0 g',\n",
" 'cholesterolContent': '1021.2 mg',\n",
" 'unsaturatedFatContent': '79.7 g'},\n",
" 'tags': ['Lunch',\n",
" 'Dinner',\n",
" 'Main Dish',\n",
" 'Quick and Easy',\n",
" 'Eggs',\n",
" 'Cheese',\n",
" 'Weeknight Dinners',\n",
" 'Bread',\n",
" '30 or Less',\n",
" 'Easy',\n",
" 'Quick',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Gluten-Free',\n",
" 'Soy-Free',\n",
" 'Game Day',\n",
" \"Father's Day\",\n",
" 'Microwave',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Grain-Free',\n",
" 'Sugar-Free',\n",
" 'Tomato-Free',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000020'},\n",
" {'recipe_name': 'Croque-Monsieur With Poached Eggs (Croque-Madame)',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '2 servings',\n",
" 'recipe_ingredients': ['2 slice Bread',\n",
" '4 slice Black Forest Ham',\n",
" 'to taste Gruyère Cheese',\n",
" 'to taste Fresh Thyme Leaves',\n",
" '1 tablespoon Butter',\n",
" '2 tablespoon Onion',\n",
" 'to taste Kosher Salt',\n",
" '1 tablespoon All-Purpose Flour',\n",
" '1 cup Milk',\n",
" '1 Bay Leaf',\n",
" '2 Egg',\n",
" 'to taste Distilled White Vinegar',\n",
" 'to taste Freshly Ground Black Pepper'],\n",
" 'recipe_instructions': \"First, prepare pot for eggs: Fill a shallow saucepan with 2-3 inches water and bring to a simmer.\\nThen, prepare the béchamel: In a medium saucepan over medium heat, melt the Butter (1 tablespoon).\\nAdd the Onion (2 tablespoon) and the Kosher Salt (to taste) and cook about 5 to 7 minutes or until the onion is soft but has not begun to color.\\nTurn the heat to very low, add the All-Purpose Flour (1 tablespoon) and stir to combine it with the onion and butter.\\nContinue to cook over low heat until the flour is absorbed, stirring constantly so that it doesn't brown, about 2 minutes or so. Slowly stir in the Milk (1 cup). Drop in the Bay Leaf (1).\\nOver medium to medium-high heat, bring the mixture to a boil then reduce the heat to its lowest setting and cook for about 15 minutes, stirring occasionally to prevent the sauce from burning on the bottom of the pan.\\nTaste and cook longer if the taste of raw flour is still detectable. The mixture should be thick, but if it's too thick and becoming difficult to stir, you'll need to whisk in a little more milk. Remove the bay leaf and discard.\\nMeanwhile, preheat the broiler. Place the slices of Bread (2 slice) on a rack on a sheet pan (or a broiling pan) and broil them about a minute on each side. Remove pan from the oven.\\nSpread about 1 tablespoon of béchamel over each slice of bread. Top with Black Forest Ham (4 slice). Top with Gruyère Cheese (to taste). Set aside.\\nCrack Egg (2) into a small bowl or ramekin. Add Distilled White Vinegar (to taste) into the pot of simmering shallow water. Adjust the heat so that the water is barely simmering — get the water to a simmer, then turn it down so you don't see any bubbles.\\nUse the handle of a wooden spoon to make a whirlpool in the water, then drop one egg into the center of the whirlpool. Repeat with the other egg. Adjust the heat to keep the water just below a simmer. Set the timer for 3 minutes.\\nWhen the eggs have cooked for 3 minutes, place the toasts under the broiler and cook until the cheese is bubbling and starting to brown. Remove from the oven. Sprinkle with the Fresh Thyme Leaves (to taste).\\nMeanwhile, using a slotted spoon, lift one egg up from the water and shake it. The yolk should jiggle a little bit, but shouldn't look too loose. When the eggs look cooked to your liking, remove them with a slotted spoon and transfer to a paper towel-lined plate.\\nTop each sandwich with a poached egg. Sprinkle with a pinch of Kosher Salt (to taste) and fresh Freshly Ground Black Pepper (to taste).\",\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/e74cae36-f763-473b-b714-10a7a8e8915c.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '191 calories',\n",
" 'proteinContent': '11.2 g',\n",
" 'fatContent': '7.9 g',\n",
" 'carbohydrateContent': '18.1 g',\n",
" 'fiberContent': '0.8 g',\n",
" 'sugarContent': '5.4 g',\n",
" 'sodiumContent': '318.0 mg',\n",
" 'saturatedFatContent': '3.7 g',\n",
" 'transFatContent': '0.0 g',\n",
" 'cholesterolContent': '113.8 mg',\n",
" 'unsaturatedFatContent': '2.9 g'},\n",
" 'tags': ['Sandwich',\n",
" 'Breakfast',\n",
" 'Lunch',\n",
" 'Brunch',\n",
" 'Main Dish',\n",
" 'Low-Carb',\n",
" 'Eggs',\n",
" 'Sauce',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Advanced',\n",
" 'French',\n",
" 'Soy-Free',\n",
" 'Intermediate',\n",
" 'Fall',\n",
" 'Winter',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'International',\n",
" 'Tomato-Free',\n",
" 'Oven',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000022'},\n",
" {'recipe_name': 'Tacottata',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '4 servings',\n",
" 'recipe_ingredients': ['1 pound Tortilla Chips',\n",
" '1 pound Lean Ground Beef',\n",
" '1 carton Egg Beaters',\n",
" '10 ounce Queso Fresco',\n",
" '1 cup Shredded Cheddar Cheese',\n",
" '1 can Ro-Tel® Diced Tomatoes & Green Chilies',\n",
" '1/2 cup Onion',\n",
" '1/2 cup Egg Beaters',\n",
" '1 package Taco Seasoning',\n",
" 'to taste Lettuce',\n",
" '1/4 cup Canned Diced Tomatoes',\n",
" '1 cup Sour Cream',\n",
" '1 Jalapeño Pepper',\n",
" 'to taste Fresh Cilantro',\n",
" 'to taste Hot Sauce',\n",
" 'as needed Nonstick Cooking Spray',\n",
" 'to taste Salt',\n",
" 'to taste Ground Black Pepper'],\n",
" 'recipe_instructions': 'In a medium-large pan, cook up the Lean Ground Beef (1 pound) with some Salt (to taste) and Ground Black Pepper (to taste).\\nAdd in the Onion (1/2 cup) and Taco Seasoning (1 package). Set aside to cool completely.\\nIn a food processor (or with a zip bag and aggression) crush up the Tortilla Chips (1 pound) into coarse crumbs.\\nAdd in the Egg Beaters (1/2 cup) and pulse to combine.\\nSpray your frittata pan with Nonstick Cooking Spray (as needed) and gently press the Tortilla crumbs evenly into the pan, working them up the sides. Bake it in the oven at 350 degrees F (180 degrees C) for about 10-12 minutes, just until it’s slightly golden. Set aside.\\nAdd in the Jalapeño Pepper (1/2) as well, if you prefer extra heat.\\nFold in half of the Queso Fresco (10 ounce), and Shredded Cheddar Cheese (1 cup).\\nNow in a large bowl combine the Egg Beaters (1 carton) with the fully-cooked seasoned meat.\\nStir in the (drained) can of Ro-Tel® Diced Tomatoes & Green Chilies (1 can) and mix it all up. Pour the egg mixture into the tortilla crust, making sure not to go all the way up to the top (leave maybe a 1.8-inch lip around).\\nBake it at 350 degrees F (180 degrees C) for about 20 minutes, or until the eggs are slightly golden on top and firm all the way through.\\nNow, top it with the Sour Cream (1 cup), Fresh Cilantro (to taste), more queso fresco cheese, Jalapeño Pepper (1/2), Lettuce (to taste), some crumbled chips, and Canned Diced Tomatoes (1/4 cup). Serve with some Hot Sauce (to taste).',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/c8377cec-841c-419b-a848-a341adf987d7.jpeg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '334 calories',\n",
" 'proteinContent': '20.6 g',\n",
" 'fatContent': '16.8 g',\n",
" 'carbohydrateContent': '24.9 g',\n",
" 'fiberContent': '1.8 g',\n",
" 'sugarContent': '2.4 g',\n",
" 'sodiumContent': '598.7 mg',\n",
" 'saturatedFatContent': '7.4 g',\n",
" 'transFatContent': '0.5 g',\n",
" 'cholesterolContent': '47.7 mg',\n",
" 'unsaturatedFatContent': '7.4 g'},\n",
" 'tags': ['Lunch',\n",
" 'Dinner',\n",
" 'Brunch',\n",
" 'Appetizers',\n",
" 'Main Dish',\n",
" 'Beef',\n",
" 'Eggs',\n",
" 'Cheese',\n",
" 'Vegetables',\n",
" 'Baking',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Weekend Project',\n",
" 'Egg-Free',\n",
" 'Soy-Free',\n",
" 'Intermediate',\n",
" 'Entertaining',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'Oven',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000024'},\n",
" {'recipe_name': 'Egg in a Hole',\n",
" 'recipe_time': 0,\n",
" 'recipe_yields': '1 serving',\n",
" 'recipe_ingredients': ['1 tablespoon Salted Butter',\n",
" '1 slice Whole Wheat Bread',\n",
" '1 Egg',\n",
" 'to taste Salt',\n",
" 'to taste Ground Black Pepper'],\n",
" 'recipe_instructions': 'Heat Salted Butter (1 tablespoon) in a small skillet over medium heat.\\nUse a round cookie cutter or biscuit cutter to cut a hole out of the Whole Wheat Bread (1 slice).\\nOnce butter has fully melted and has begun to bubble slightly, place the bread into the skillet and the center piece to the side. Carefully crack the Egg (1) into the hole in the bread.\\nSprinkle a TINY bit of Salt (to taste) and Ground Black Pepper (to taste) on the egg. Cook for about two minutes.\\nThen carefully slide a spatula under the bread and flip. Sprinkle a bit more pepper on the second side, then flip the cut-out circle to grill the other side.\\nLift the bread onto a plate and eat. Use your little center circle to soak up the warm, luscious yolk!',\n",
" 'recipe_image': 'https://www.sidechef.com/recipe/9962c2f5-ad92-4acb-a176-4255f1cda802.jpg?d=1408x1120',\n",
" 'blogger': 'sidechef.com',\n",
" 'recipe_nutrients': {'calories': '298 calories',\n",
" 'proteinContent': '12.5 g',\n",
" 'fatContent': '17.9 g',\n",
" 'carbohydrateContent': '21.7 g',\n",
" 'sugarContent': '2.4 g',\n",
" 'sodiumContent': '388.3 mg',\n",
" 'saturatedFatContent': '9.2 g',\n",
" 'transFatContent': '0.5 g',\n",
" 'cholesterolContent': '212.8 mg',\n",
" 'fiberContent': '3.0 g',\n",
" 'unsaturatedFatContent': '7.2 g'},\n",
" 'tags': ['Breakfast',\n",
" 'Brunch',\n",
" 'Vegetarian',\n",
" 'Low-Carb',\n",
" 'Pescatarian',\n",
" 'Eggs',\n",
" 'Kid-Friendly',\n",
" 'Easy',\n",
" 'Quick',\n",
" 'American',\n",
" 'Shellfish-Free',\n",
" 'Beginner',\n",
" 'Soy-Free',\n",
" 'Fish-Free',\n",
" 'Peanut-Free',\n",
" 'Tree Nut-Free',\n",
" 'Sugar-Free',\n",
" 'Tomato-Free',\n",
" 'Stove',\n",
" ''],\n",
" 'id_': '0000037'}]"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def filter_recipes(file_name):\n",
" with open(file_name, 'r') as file:\n",
" recipes = json.load(file)\n",
"\n",
" high_protein_recipes = []\n",
" for recipe in recipes:\n",
" protein_content = recipe['recipe_nutrients'].get('proteinContent', '0 g')\n",
" protein_value = float(protein_content.split(' ')[0])\n",
" if protein_value > 10:\n",
" high_protein_recipes.append(recipe)\n",
" if len(high_protein_recipes) >= 10:\n",
" break\n",
"\n",
" return high_protein_recipes\n",
"\n",
"\n",
"filter_recipes(\"recipes.json\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "chatbot-env",
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|