Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 55,430 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 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/ritesh.thawkar/Ritesh/nutrigenics/nutrigenics-chatbot/chatbot-env/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020\n",
" warnings.warn(\n",
"/Users/ritesh.thawkar/Ritesh/nutrigenics/nutrigenics-chatbot/chatbot-env/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import pandas as pd\n",
"import json\n",
"from PIL import Image\n",
"import numpy as np\n",
"import gradio as gr "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"from pathlib import Path\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"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from src.data.transforms import transform_test\n",
"#\n",
"transform = transform_test(384)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"import json \n",
"import numpy as np \n",
"from PIL import Image\n",
"import torch.nn.functional as F\n",
"import torch\n",
"from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer\n",
"\n",
"\n",
"\n",
"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",
"\n",
"\n",
"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 = self.model.visual_encoder(img)\n",
" img_feats = F.normalize(self.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",
" print(index)\n",
" self.target_recipe = self.df.iloc[index]\n",
"\n",
" def ask(self, msg):\n",
" if \"nutrition\" in msg or \"nutrients\" in msg : \n",
" return json.dumps(self.target_recipe[\"recipe_nutrients\"], indent=4)\n",
" elif \"instruction\" in msg :\n",
" return json.dumps(self.target_recipe[\"recipe_instructions\"], indent=4)\n",
" elif \"ingredients\" in msg :\n",
" return json.dumps(self.target_recipe[\"recipe_ingredients\"], indent=4)\n",
" elif \"tag\" in msg or \"class\" in msg :\n",
" return json.dumps(self.target_recipe[\"tags\"], indent=4)\n",
" else:\n",
" return \"Conversational capabilities will be included later.\"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"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": 6,
"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"
]
},
{
"data": {
"text/plain": [
"BLIPEmbs(\n",
" (visual_encoder): VisionTransformer(\n",
" (patch_embed): PatchEmbed(\n",
" (proj): Conv2d(3, 1024, kernel_size=(16, 16), stride=(16, 16))\n",
" (norm): Identity()\n",
" )\n",
" (pos_drop): Dropout(p=0.0, inplace=False)\n",
" (blocks): ModuleList(\n",
" (0): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): Identity()\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (1): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.004)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (2): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.009)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (3): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.013)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (4): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.017)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (5): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.022)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (6): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.026)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (7): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.030)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (8): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.035)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (9): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.039)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (10): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.043)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (11): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.048)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (12): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.052)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (13): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.057)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (14): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.061)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (15): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.065)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (16): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.070)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (17): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.074)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (18): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.078)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (19): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.083)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (20): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.087)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (21): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.091)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (22): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.096)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (23): Block(\n",
" (norm1): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (attn): Attention(\n",
" (qkv): Linear(in_features=1024, out_features=3072, bias=True)\n",
" (attn_drop): Dropout(p=0.0, inplace=False)\n",
" (proj): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (proj_drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (drop_path): DropPath(drop_prob=0.100)\n",
" (norm2): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" (mlp): Mlp(\n",
" (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",
" (act): GELU(approximate='none')\n",
" (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",
" (drop): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" )\n",
" (norm): LayerNorm((1024,), eps=1e-06, elementwise_affine=True)\n",
" )\n",
" (text_encoder): BertModel(\n",
" (embeddings): BertEmbeddings(\n",
" (word_embeddings): Embedding(30524, 768, padding_idx=0)\n",
" (position_embeddings): Embedding(512, 768)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" (encoder): BertEncoder(\n",
" (layer): ModuleList(\n",
" (0-11): 12 x BertLayer(\n",
" (attention): BertAttention(\n",
" (self): BertSelfAttention(\n",
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" (output): BertSelfOutput(\n",
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" )\n",
" (crossattention): BertAttention(\n",
" (self): BertSelfAttention(\n",
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
" (key): Linear(in_features=1024, out_features=768, bias=True)\n",
" (value): Linear(in_features=1024, out_features=768, bias=True)\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" (output): BertSelfOutput(\n",
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" )\n",
" (intermediate): BertIntermediate(\n",
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
" (intermediate_act_fn): GELUActivation()\n",
" )\n",
" (output): BertOutput(\n",
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" )\n",
" )\n",
" )\n",
" )\n",
" (vision_proj): Linear(in_features=1024, out_features=256, bias=True)\n",
" (text_proj): Linear(in_features=768, out_features=256, bias=True)\n",
")"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"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()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_json(\"datasets/sidechef/my_recipes.json\")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>recipe_name</th>\n",
" <th>recipe_time</th>\n",
" <th>recipe_yields</th>\n",
" <th>recipe_ingredients</th>\n",
" <th>recipe_instructions</th>\n",
" <th>recipe_image</th>\n",
" <th>blogger</th>\n",
" <th>recipe_nutrients</th>\n",
" <th>tags</th>\n",
" <th>id_</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Asian Potato Salad with Seven Minute Egg</td>\n",
" <td>0</td>\n",
" <td>4 servings</td>\n",
" <td>[2 1/2 cup Multi-Colored Fingerling Potato, 3/...</td>\n",
" <td>Fill a large stock pot with water.\\nAdd the Mu...</td>\n",
" <td>https://www.sidechef.com/recipe/eeeeeceb-493e-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '80 calories', 'proteinContent': ...</td>\n",
" <td>[Salad, Lunch, Brunch, Appetizers, Side Dish, ...</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Everything Breakfast Bombs</td>\n",
" <td>0</td>\n",
" <td>8 servings</td>\n",
" <td>[5 tablespoon Butter, 12 ounce Turkey Breakfas...</td>\n",
" <td>First, preheat the oven to 375 degrees F (190 ...</td>\n",
" <td>https://www.sidechef.com/recipe/525f6843-4337-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '56 calories', 'proteinContent': ...</td>\n",
" <td>[Breakfast, Brunch, Low-Carb, Eggs, American, ...</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Bacon Swiss Deviled Eggs</td>\n",
" <td>0</td>\n",
" <td>6 servings</td>\n",
" <td>[6 Egg, 1/4 cup Mayonnaise, 1/4 cup Avocado, 1...</td>\n",
" <td>Cut each hard boiled Egg (6) in half lengthwis...</td>\n",
" <td>https://www.sidechef.com/recipe/2075e8cf-4fa9-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '38 calories', 'proteinContent': ...</td>\n",
" <td>[Breakfast, Brunch, Low-Carb, Eggs, American, ...</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Farmers Market Breakfast Pizza</td>\n",
" <td>0</td>\n",
" <td>2 servings</td>\n",
" <td>[1/2 Pizza Dough, 1/2 cup Kale, 1/2 cup Onion,...</td>\n",
" <td>For homemade pizza sauce, finely chop the Swee...</td>\n",
" <td>https://www.sidechef.com/recipe/1cd15944-9411-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '315 calories', 'proteinContent':...</td>\n",
" <td>[Breakfast, Brunch, Main Dish, Budget-Friendly...</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Scrambled Eggs</td>\n",
" <td>0</td>\n",
" <td>2 servings</td>\n",
" <td>[3 Egg, 2 tablespoon Heavy Cream, 2 tablespoon...</td>\n",
" <td>Crack Egg (3) into a bowl.\\nPour in Heavy Crea...</td>\n",
" <td>https://www.sidechef.com/recipe/08d39a01-c030-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '127 calories', 'proteinContent':...</td>\n",
" <td>[Breakfast, Brunch, Vegetarian, Low-Carb, Pesc...</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Fettuccini Carbonara</td>\n",
" <td>0</td>\n",
" <td>2 servings</td>\n",
" <td>[2 Shallot, 1 clove Garlic, 2 Egg, 6 slice Bac...</td>\n",
" <td>Put a generously salted pot of water on to boi...</td>\n",
" <td>https://www.sidechef.com/recipe/9e5df75f-bf1a-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '495 calories', 'proteinContent':...</td>\n",
" <td>[Pasta, Dinner, Side Dish, Main Dish, Pork, Eg...</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Sausage Egg Muffins</td>\n",
" <td>0</td>\n",
" <td>6 servings</td>\n",
" <td>[1 pound Ground Pork, 1 1/2 teaspoon Fresh Par...</td>\n",
" <td>Preheat your oven to 350 degrees F (175 degree...</td>\n",
" <td>https://www.sidechef.com/recipe/49d5e5a3-4d16-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '44 calories', 'proteinContent': ...</td>\n",
" <td>[Keto, Breakfast, Brunch, Budget-Friendly, Low...</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Shakshuka</td>\n",
" <td>0</td>\n",
" <td>4 servings</td>\n",
" <td>[1 tablespoon Oil, 3 Tomato, 1 Green Chili Pep...</td>\n",
" <td>Preheat oven to 180 degrees C (350 degrees F) ...</td>\n",
" <td>https://www.sidechef.com/recipe/de00577b-38d4-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '99 calories', 'fatContent': '2.5...</td>\n",
" <td>[Breakfast, Brunch, Main Dish, Vegetarian, Pes...</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Huevos Rancheros</td>\n",
" <td>0</td>\n",
" <td>1 serving</td>\n",
" <td>[2 Yellow Corn Tortilla, 2 tablespoon Pinto Be...</td>\n",
" <td>In a small frying pan, spray a little Nonstick...</td>\n",
" <td>https://www.sidechef.com/recipe/5284bc88-1305-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '290 calories', 'proteinContent':...</td>\n",
" <td>[Breakfast, Brunch, Eggs, Quick, Mexican, Shel...</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Homemade Pasta</td>\n",
" <td>0</td>\n",
" <td>4 servings</td>\n",
" <td>[1 cup All-Purpose Flour, 1 teaspoon Salt, 1 Egg]</td>\n",
" <td>Mix All-Purpose Flour (1 cup) and Salt (1 teas...</td>\n",
" <td>https://www.sidechef.com/recipe/8528a7af-b6d8-...</td>\n",
" <td>sidechef.com</td>\n",
" <td>{'calories': '33 calories', 'proteinContent': ...</td>\n",
" <td>[Pasta, Budget-Friendly, Vegetarian, Pescatari...</td>\n",
" <td>10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" recipe_name recipe_time recipe_yields \\\n",
"0 Asian Potato Salad with Seven Minute Egg 0 4 servings \n",
"1 Everything Breakfast Bombs 0 8 servings \n",
"2 Bacon Swiss Deviled Eggs 0 6 servings \n",
"3 Farmers Market Breakfast Pizza 0 2 servings \n",
"4 Scrambled Eggs 0 2 servings \n",
"5 Fettuccini Carbonara 0 2 servings \n",
"6 Sausage Egg Muffins 0 6 servings \n",
"7 Shakshuka 0 4 servings \n",
"8 Huevos Rancheros 0 1 serving \n",
"9 Homemade Pasta 0 4 servings \n",
"\n",
" recipe_ingredients \\\n",
"0 [2 1/2 cup Multi-Colored Fingerling Potato, 3/... \n",
"1 [5 tablespoon Butter, 12 ounce Turkey Breakfas... \n",
"2 [6 Egg, 1/4 cup Mayonnaise, 1/4 cup Avocado, 1... \n",
"3 [1/2 Pizza Dough, 1/2 cup Kale, 1/2 cup Onion,... \n",
"4 [3 Egg, 2 tablespoon Heavy Cream, 2 tablespoon... \n",
"5 [2 Shallot, 1 clove Garlic, 2 Egg, 6 slice Bac... \n",
"6 [1 pound Ground Pork, 1 1/2 teaspoon Fresh Par... \n",
"7 [1 tablespoon Oil, 3 Tomato, 1 Green Chili Pep... \n",
"8 [2 Yellow Corn Tortilla, 2 tablespoon Pinto Be... \n",
"9 [1 cup All-Purpose Flour, 1 teaspoon Salt, 1 Egg] \n",
"\n",
" recipe_instructions \\\n",
"0 Fill a large stock pot with water.\\nAdd the Mu... \n",
"1 First, preheat the oven to 375 degrees F (190 ... \n",
"2 Cut each hard boiled Egg (6) in half lengthwis... \n",
"3 For homemade pizza sauce, finely chop the Swee... \n",
"4 Crack Egg (3) into a bowl.\\nPour in Heavy Crea... \n",
"5 Put a generously salted pot of water on to boi... \n",
"6 Preheat your oven to 350 degrees F (175 degree... \n",
"7 Preheat oven to 180 degrees C (350 degrees F) ... \n",
"8 In a small frying pan, spray a little Nonstick... \n",
"9 Mix All-Purpose Flour (1 cup) and Salt (1 teas... \n",
"\n",
" recipe_image blogger \\\n",
"0 https://www.sidechef.com/recipe/eeeeeceb-493e-... sidechef.com \n",
"1 https://www.sidechef.com/recipe/525f6843-4337-... sidechef.com \n",
"2 https://www.sidechef.com/recipe/2075e8cf-4fa9-... sidechef.com \n",
"3 https://www.sidechef.com/recipe/1cd15944-9411-... sidechef.com \n",
"4 https://www.sidechef.com/recipe/08d39a01-c030-... sidechef.com \n",
"5 https://www.sidechef.com/recipe/9e5df75f-bf1a-... sidechef.com \n",
"6 https://www.sidechef.com/recipe/49d5e5a3-4d16-... sidechef.com \n",
"7 https://www.sidechef.com/recipe/de00577b-38d4-... sidechef.com \n",
"8 https://www.sidechef.com/recipe/5284bc88-1305-... sidechef.com \n",
"9 https://www.sidechef.com/recipe/8528a7af-b6d8-... sidechef.com \n",
"\n",
" recipe_nutrients \\\n",
"0 {'calories': '80 calories', 'proteinContent': ... \n",
"1 {'calories': '56 calories', 'proteinContent': ... \n",
"2 {'calories': '38 calories', 'proteinContent': ... \n",
"3 {'calories': '315 calories', 'proteinContent':... \n",
"4 {'calories': '127 calories', 'proteinContent':... \n",
"5 {'calories': '495 calories', 'proteinContent':... \n",
"6 {'calories': '44 calories', 'proteinContent': ... \n",
"7 {'calories': '99 calories', 'fatContent': '2.5... \n",
"8 {'calories': '290 calories', 'proteinContent':... \n",
"9 {'calories': '33 calories', 'proteinContent': ... \n",
"\n",
" tags id_ \n",
"0 [Salad, Lunch, Brunch, Appetizers, Side Dish, ... 1 \n",
"1 [Breakfast, Brunch, Low-Carb, Eggs, American, ... 2 \n",
"2 [Breakfast, Brunch, Low-Carb, Eggs, American, ... 3 \n",
"3 [Breakfast, Brunch, Main Dish, Budget-Friendly... 4 \n",
"4 [Breakfast, Brunch, Vegetarian, Low-Carb, Pesc... 5 \n",
"5 [Pasta, Dinner, Side Dish, Main Dish, Pork, Eg... 6 \n",
"6 [Keto, Breakfast, Brunch, Budget-Friendly, Low... 7 \n",
"7 [Breakfast, Brunch, Main Dish, Vegetarian, Pes... 8 \n",
"8 [Breakfast, Brunch, Eggs, Quick, Mexican, Shel... 9 \n",
"9 [Pasta, Budget-Friendly, Vegetarian, Pescatari... 10 "
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading Target Embedding\n"
]
}
],
"source": [
"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": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([8333, 256])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tar_img_feats.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def respond_to_user(image, message):\n",
" # Process the image and message here\n",
" # For demonstration, I'll just return a simple text response\n",
" chat = Chat(model,transform,df,tar_img_feats)\n",
" chat.encode_image(image)\n",
" response = chat.ask(message)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import numpy as np\n",
"\n",
"# Load the image\n",
"image_path = '/home/fahadkhan/omkar/CoVR_old/Nutrigenics-flask-chatbot/datasets/sidechef/images/0000006.png' # Replace with your image path\n",
"img = Image.open(image_path)\n",
"\n",
"# Convert image to NumPy array\n",
"img_array = np.array(img)\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"('[\\n'\n",
" ' \"2 Shallot\",\\n'\n",
" ' \"1 clove Garlic\",\\n'\n",
" ' \"2 Egg\",\\n'\n",
" ' \"6 slice Bacon\",\\n'\n",
" ' \"1/2 cup Heavy Cream\",\\n'\n",
" ' \"1/4 cup Grated Parmesan Cheese\",\\n'\n",
" ' \"8 ounce Fettuccine\",\\n'\n",
" ' \"1 tablespoon Olive Oil\",\\n'\n",
" ' \"to taste Salt\",\\n'\n",
" ' \"to taste Ground Black Pepper\",\\n'\n",
" ' \"to taste Fresh Parsley\"\\n'\n",
" ']')\n"
]
}
],
"source": [
"res = respond_to_user(image=img_array, message=\"ingredients\")\n",
"\n",
"import pprint\n",
"\n",
"pprint.pprint(res)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7866\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"650px\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"# Define the custom CSS to add a footer\n",
"custom_css = \"\"\"\n",
"/* Footer style */\n",
".gradio-footer {\n",
" display: flex;\n",
" justify-content: center;\n",
" align-items: center;\n",
" padding: 10px;\n",
" background-color: #f8f9fa;\n",
" color: #333;\n",
" font-size: 0.9em;\n",
"}\n",
"\n",
".custom-header {\n",
" text-align: center;\n",
" padding: 12px;\n",
" background-color: #333; \n",
" color: white;\n",
" position: bottom;\n",
" bottom: 0;\n",
" width: 100%;\n",
" font-size: 0.8em;\n",
"}\n",
"\n",
".footer {\n",
" width: 100%;\n",
" background-color: #f2f2f2;\n",
" color: #555;\n",
" text-align: center;\n",
" padding: 10px 0;\n",
" position: absolute;\n",
" bottom: 0;\n",
" left: 0;\n",
"}\n",
"\n",
"/* Make sure the interface leaves space for the footer */\n",
".body {\n",
" margin-bottom: 50px;\n",
"}\n",
"\"\"\"\n",
"\n",
"# Add a custom footer by injecting HTML into the description\n",
"custom_footer_html = \"\"\"\n",
"<footer> <p> Reach out to us at {omkar.thawakar, muzammal.naseer}@mbzuai.ac.ae </p> </footer>\n",
"\"\"\"\n",
"\n",
"custom_header_html = \"\"\"\n",
"<div class='custom-header'>Nutrition-GPT Demo</div>\n",
"\"\"\"\n",
"\n",
"def respond_to_user(image, message):\n",
" # Process the image and message here\n",
" # For demonstration, I'll just return a simple text response\n",
" chat = Chat(model,transform,df,tar_img_feats)\n",
" chat.encode_image(image)\n",
" response = chat.ask(message)\n",
" return response\n",
"\n",
"iface = gr.Interface(\n",
" fn=respond_to_user,\n",
" inputs=[gr.Image(height=\"70%\"), gr.Textbox(label=\"Ask Query\"),],\n",
" outputs=[gr.Textbox(label=\"Nutrition-GPT\")],\n",
" title=custom_header_html, \n",
" # description=\"Upload an food image and ask queries!\",\n",
" css=custom_css,\n",
" # description=custom_footer_html \n",
")\n",
"\n",
"iface.launch(show_error=True, height=\"650px\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# example_texts = gr.Dataset(components=[gr.Textbox(visible=False)],\n",
" # label=\"Prompt Examples\",\n",
" # samples=[\n",
" # [\"Provide nutritional information for given food image.\"],\n",
" # [\"What are the nutrients available in given food image.\"],\n",
" # [\"Could you provide a detailed nutritional data of the given food image?\"],\n",
" # [\"Describe the instructions to prepare given food.\"],\n",
" # [\"What are the key ingredients in this food image?\"],\n",
" # [\"Could you highlight the dietary tags for this food image?\"],\n",
" # ],)\n",
"\n",
"# example_images = gr.Dataset(components=[image], label=\"Food Examples\",\n",
"# samples=[\n",
"# [os.path.join(os.path.dirname(\"./\"), \"./datasets/sidechef/sample_images/0000018.png\")],\n",
"# [os.path.join(os.path.dirname(\"./\"), \"./datasets/sidechef/sample_images/0000021.png\")],\n",
"# [os.path.join(os.path.dirname(\"./\"), \"./datasets/sidechef/sample_images/0000035.png\")],\n",
"# [os.path.join(os.path.dirname(\"./\"), \"./datasets/sidechef/sample_images/0000038.png\")],\n",
"# [os.path.join(os.path.dirname(\"./\"), \"./datasets/sidechef/sample_images/0000090.png\")],\n",
"# [os.path.join(os.path.dirname(\"./\"), \"./datasets/sidechef/sample_images/0000122.png\")],\n",
"# ])\n",
"\n"
]
}
],
"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
}
|