Spaces:
Sleeping
Sleeping
File size: 138,374 Bytes
da510f8 5a56fe4 da510f8 5a56fe4 da510f8 6948966 da510f8 ed1fbf5 da510f8 00be02e da510f8 5a56fe4 c3b8959 da510f8 6948966 da510f8 6948966 a0c889e 6948966 5a56fe4 da510f8 5a56fe4 6948966 da510f8 6948966 ed1fbf5 22e7266 6948966 22e7266 08317a2 6948966 22e7266 6948966 22e7266 6948966 22e7266 6948966 da510f8 2c8b55d 08317a2 2c8b55d 08317a2 2c8b55d 6948966 22e7266 08317a2 6948966 22e7266 ed1fbf5 2c8b55d ed1fbf5 2c8b55d 08317a2 0273398 da510f8 0273398 08317a2 ca631a1 08317a2 22e7266 08317a2 ed1fbf5 2c8b55d 6948966 5a56fe4 6948966 5a56fe4 6948966 5a56fe4 6948966 ed1fbf5 da510f8 5a56fe4 6948966 36b6c73 6948966 82a9222 6948966 36b6c73 da510f8 36b6c73 c3b8959 d5a1468 c3b8959 d5a1468 cdc32a1 d5a1468 cdc32a1 d5a1468 c3b8959 d5a1468 c3b8959 6948966 36b6c73 da510f8 08317a2 36b6c73 da510f8 3c48fa4 6948966 da510f8 0d6169c da510f8 08317a2 c3b8959 08317a2 6948966 da510f8 8796ad3 da510f8 8796ad3 da510f8 8796ad3 5a56fe4 da510f8 c3b8959 26ef12e da510f8 26ef12e 8796ad3 c3b8959 8796ad3 c3b8959 da510f8 26ef12e da510f8 8738408 da510f8 8738408 da510f8 8738408 26ef12e c3b8959 5a56fe4 da510f8 4c6b2e7 da510f8 6e151a3 da510f8 6e151a3 da510f8 4c6b2e7 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 00be02e ed1fbf5 00be02e ed1fbf5 da510f8 ed1fbf5 a0c889e da510f8 ca631a1 da510f8 ca631a1 da510f8 b82d92d da510f8 ca631a1 da510f8 b82d92d a0c889e da510f8 b82d92d da510f8 b82d92d da510f8 a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 b82d92d da510f8 a0c889e da510f8 b82d92d a0c889e da510f8 ca631a1 da510f8 a0c889e da510f8 a0c889e da510f8 b82d92d da510f8 b82d92d a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 b82d92d da510f8 a0c889e da510f8 a0c889e da510f8 b82d92d a0c889e ca631a1 da510f8 a0c889e da510f8 b82d92d da510f8 a0c889e da510f8 b82d92d da510f8 a0c889e da510f8 b82d92d da510f8 b82d92d da510f8 a0c889e da510f8 ed1fbf5 da510f8 ed1fbf5 00be02e ed1fbf5 da510f8 00be02e ed1fbf5 00be02e ed1fbf5 a0c889e ca631a1 a0c889e ca631a1 a0c889e ca631a1 a0c889e ca631a1 3fe412d ca631a1 a0c889e ca631a1 a0c889e ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 22e7266 da510f8 08317a2 da510f8 08317a2 da510f8 6948966 da510f8 6948966 a0c889e ca631a1 3fe412d a0c889e 890f447 da510f8 8796ad3 da510f8 6948966 8796ad3 08317a2 da510f8 08317a2 8796ad3 6948966 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 da510f8 ed1fbf5 2c8b55d 6948966 3fe412d 6e2d107 3fe412d 6e2d107 3fe412d 6e2d107 3fe412d 6e2d107 3fe412d 6e2d107 3fe412d 6948966 3fe412d 6948966 3fe412d 94db123 3fe412d 94db123 3fe412d 94db123 9d8ac8b 3fe412d 9d8ac8b 3fe412d 94db123 9d8ac8b 3fe412d ba121f0 9d8ac8b ba121f0 9d8ac8b 5348cb7 ba121f0 3fe412d 94db123 039e1f6 94db123 3fe412d a68f3ca 5348cb7 5075071 22e7266 8796ad3 da510f8 466cd8e 00be02e a0c889e 00be02e da510f8 29d8644 da510f8 29d8644 da510f8 29d8644 da510f8 29d8644 da510f8 29d8644 da510f8 29d8644 da510f8 29d8644 da510f8 29d8644 a0c889e 29d8644 da510f8 a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 3fe412d 29d8644 a0c889e 6e151a3 6948966 2c8b55d 6948966 ed1fbf5 da510f8 6948966 5075071 da510f8 5075071 da510f8 5075071 c3b8959 5075071 6e151a3 c06cab1 da510f8 6e151a3 0273398 686f675 0273398 5075071 da510f8 0273398 88ab1b7 c6dc867 22e7266 88ab1b7 22e7266 c6dc867 da510f8 88ab1b7 c6dc867 39ab6f7 c06cab1 39ab6f7 ed1fbf5 39ab6f7 da510f8 15b48fb c06cab1 6e151a3 da510f8 15b48fb da510f8 a0c889e 15b48fb da510f8 a0c889e 145da06 a0c889e 2b65732 a0c889e 2b65732 a0c889e da510f8 15b48fb da510f8 15b48fb da510f8 15b48fb da510f8 15b48fb da510f8 a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 a0c889e da510f8 36b6c73 6948966 22e7266 | 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 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 | # EatSmart Pro - Complete Restored Version with All Features
import streamlit as st
from PIL import Image
import torch
from torchvision import models
import torch.nn as nn
import torchvision.transforms as transforms
import io
import os
import requests
import json
import timm
import numpy as np
import random
# Import transformers with error handling for HF deployment
try:
from transformers import AutoImageProcessor, AutoModelForImageClassification
TRANSFORMERS_AVAILABLE = True
except ImportError:
TRANSFORMERS_AVAILABLE = False
# Page Configuration
st.set_page_config(
page_title="🍽️ EatSmart Pro - AI Food Analysis",
page_icon="🍽️",
layout="wide",
initial_sidebar_state="collapsed" # Keep sidebar closed on mobile
)
# Custom CSS for Beautiful UI
st.markdown("""
<style>
.stApp {
background: #ffffff;
color: #000000;
}
.main-header {
background: linear-gradient(135deg, #87CEEB 0%, #4682B4 100%);
padding: 2rem;
border-radius: 15px;
text-align: center;
color: white;
margin-bottom: 2rem;
box-shadow: 0 8px 32px rgba(0,0,0,0.1);
}
.main-header h1 {
font-size: 3rem;
margin: 0;
font-weight: bold;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.main-header p {
font-size: 1.2rem;
margin: 0.5rem 0 0 0;
opacity: 0.9;
}
.stTabs [data-baseweb="tab-list"] {
gap: 8px;
background: rgba(240,240,240,0.8);
border-radius: 10px;
padding: 5px;
}
.stTabs [data-baseweb="tab"] {
background: rgba(255,255,255,0.9);
border-radius: 8px;
color: #333333;
font-weight: bold;
padding: 10px 20px;
}
.stTabs [aria-selected="true"] {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
color: white;
}
.upload-section {
background: linear-gradient(135deg, #56ab2f 0%, #a8e6cf 100%);
color: white;
padding: 15px;
border-radius: 10px;
text-align: center;
margin-bottom: 15px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.camera-section {
background: linear-gradient(135deg, #20c997 0%, #17a2b8 100%);
padding: 20px;
border-radius: 15px;
text-align: center;
color: white;
margin: 20px 0;
box-shadow: 0 6px 20px rgba(32, 201, 151, 0.3);
height: auto;
min-height: 120px;
display: flex;
flex-direction: column;
justify-content: center;
}
.analysis-section {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
color: white;
padding: 15px;
border-radius: 10px;
text-align: center;
margin-bottom: 15px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.info-card {
background: linear-gradient(135deg, #6f42c1, #e83e8c);
color: white;
padding: 20px;
border-radius: 15px;
margin: 15px 0;
box-shadow: 0 8px 32px rgba(0,0,0,0.1);
}
.metric-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 15px;
border-radius: 10px;
text-align: center;
color: white;
margin: 5px;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.metric-value {
font-size: 2rem;
font-weight: bold;
margin: 5px 0;
}
.metric-label {
font-size: 0.9rem;
opacity: 0.8;
}
.stButton > button {
background: linear-gradient(45deg, #28a745, #007bff);
color: white;
border: none;
border-radius: 10px;
padding: 12px 24px;
font-weight: bold;
font-size: 1.1rem;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
width: 100%;
margin: 5px 0;
}
.stButton > button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0,0,0,0.3);
}
.health-score-excellent {
background: linear-gradient(135deg, #28a745, #20c997);
color: white;
padding: 15px;
border-radius: 10px;
text-align: center;
margin: 10px 0;
}
.health-score-good {
background: linear-gradient(135deg, #ffc107, #fd7e14);
color: white;
padding: 15px;
border-radius: 10px;
text-align: center;
margin: 10px 0;
}
.health-score-poor {
background: linear-gradient(135deg, #dc3545, #e83e8c);
color: white;
padding: 15px;
border-radius: 10px;
text-align: center;
margin: 10px 0;
}
.allergen-alert {
background: linear-gradient(135deg, #dc3545, #fd7e14);
color: white;
padding: 12px;
border-radius: 8px;
margin: 8px 0;
font-weight: bold;
}
.allergen-info {
background: linear-gradient(135deg, #17a2b8, #20c997);
color: white;
padding: 10px;
border-radius: 8px;
margin: 5px 0;
}
.recipe-section {
background: linear-gradient(135deg, #6f42c1, #e83e8c);
color: white;
padding: 20px;
border-radius: 15px;
margin: 15px 0;
}
</style>
""", unsafe_allow_html=True)
# Constants
CLASS_NAMES = [
'apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare',
'beet_salad', 'beignets', 'bibimbap', 'bread_pudding', 'breakfast_burrito',
'bruschetta', 'caesar_salad', 'cannoli', 'caprese_salad', 'carrot_cake',
'ceviche', 'cheesecake', 'cheese_plate', 'chicken_curry', 'chicken_quesadilla',
'chicken_wings', 'chocolate_cake', 'chocolate_mousse', 'churros', 'clam_chowder',
'club_sandwich', 'crab_cakes', 'creme_brulee', 'croque_madame', 'cup_cakes',
'deviled_eggs', 'donuts', 'dumplings', 'edamame', 'eggs_benedict',
'escargots', 'falafel', 'filet_mignon', 'fish_and_chips', 'foie_gras',
'french_fries', 'french_onion_soup', 'french_toast', 'fried_calamari', 'fried_rice',
'frozen_yogurt', 'garlic_bread', 'gnocchi', 'greek_salad', 'grilled_cheese_sandwich',
'grilled_salmon', 'guacamole', 'gyoza', 'hamburger', 'hot_and_sour_soup',
'hot_dog', 'huevos_rancheros', 'hummus', 'ice_cream', 'lasagna',
'lobster_bisque', 'lobster_roll_sandwich', 'macaroni_and_cheese', 'macarons', 'miso_soup',
'mussels', 'nachos', 'omelette', 'onion_rings', 'oysters',
'pad_thai', 'paella', 'pancakes', 'panna_cotta', 'peking_duck',
'pho', 'pizza', 'pork_chop', 'poutine', 'prime_rib',
'pulled_pork_sandwich', 'ramen', 'ravioli', 'red_velvet_cake', 'risotto',
'samosa', 'sashimi', 'scallops', 'seaweed_salad', 'shrimp_and_grits',
'spaghetti_bolognese', 'spaghetti_carbonara', 'spring_rolls', 'steak', 'strawberry_shortcake',
'sushi', 'tacos', 'takoyaki', 'tiramisu', 'tuna_tartare', 'waffles'
]
# Session State Initialization
if 'user_allergens' not in st.session_state:
st.session_state.user_allergens = []
if 'dietary_preferences' not in st.session_state:
st.session_state.dietary_preferences = []
if 'avoid_trans_fat' not in st.session_state:
st.session_state.avoid_trans_fat = True
if 'image_buffer' not in st.session_state:
st.session_state.image_buffer = None
if 'last_image_buffer' not in st.session_state:
st.session_state.last_image_buffer = None
if 'prediction_result' not in st.session_state:
st.session_state.prediction_result = None
# Initialize file uploader counter for demo example conflicts
if 'file_uploader_counter' not in st.session_state:
st.session_state.file_uploader_counter = 0
# Helper Functions
def get_convnext_model(num_classes):
"""Creates the ConvNeXt Large model architecture"""
print(f"🚀 Loading ConvNeXt Large model for inference...")
model = timm.create_model('convnext_large.fb_in22k_ft_in1k', pretrained=True, num_classes=num_classes)
total_params = sum(p.numel() for p in model.parameters())
print(f"✅ Model loaded: {total_params:,} parameters (~{total_params * 4 / 1024**2:.1f} MB)")
return model
def get_efficientnet_model(num_classes):
"""Creates EfficientNet-V2-S model for fallback"""
print(f"⚠️ Loading EfficientNet-V2-S model (fallback)...")
model = models.efficientnet_v2_s(weights=None)
num_features = model.classifier[1].in_features
model.classifier = nn.Sequential(
nn.Dropout(p=0.2),
nn.Linear(num_features, 256),
nn.ReLU(),
nn.Dropout(p=0.1),
nn.Linear(256, num_classes)
)
return model
@st.cache_resource
def load_model_resources():
"""Load model for Hugging Face Space deployment"""
try:
# Check if we're running on Hugging Face Spaces
if "SPACE_ID" in os.environ:
return load_huggingface_model()
else:
return load_local_model()
except Exception as e:
return load_huggingface_model() # Fallback to HF model
def load_huggingface_model():
"""Load model from Hugging Face Hub for Spaces deployment"""
try:
# Use YOUR trained ConvNeXt model from Hugging Face Hub
model_name = "Lumilife/eatSmartPro-models"
from huggingface_hub import hf_hub_download
print(f"🚀 Loading ConvNeXt Large model from {model_name}...")
# Download your model file from HF repo
model_path = hf_hub_download(
repo_id=model_name,
filename="food_classifier_convnext_large_cpu_full.pth",
cache_dir="./models"
)
print(f"✅ Model file downloaded to: {model_path}")
# Load your ConvNeXt model
model = get_convnext_model(len(CLASS_NAMES))
print("📦 Loading model weights...")
checkpoint = torch.load(model_path, map_location='cpu')
if isinstance(checkpoint, dict):
if 'model_state_dict' in checkpoint:
model.load_state_dict(checkpoint['model_state_dict'], strict=False)
accuracy = checkpoint.get('best_acc', 89.8)
else:
model.load_state_dict(checkpoint, strict=False)
accuracy = 89.8
else:
model.load_state_dict(checkpoint, strict=False)
accuracy = 89.8
model.eval()
model_info = {
"name": "ConvNeXt Large",
"params": "197M",
"accuracy": f"{accuracy:.1f}%"
}
print(f"✅ ConvNeXt Large model loaded successfully! Accuracy: {accuracy:.1f}%")
return model, model_info
except Exception as e:
print(f"❌ Failed to load ConvNeXt model from HF: {str(e)}")
print(f" Repository: {model_name}")
print(f" Expected file: food_classifier_convnext_large_cpu_full.pth")
print(f" Error details: {type(e).__name__}")
# Create dummy model to show error clearly
return create_dummy_model()
def load_local_model():
"""Load local PyTorch model files"""
num_classes = len(CLASS_NAMES)
# Try to load ConvNeXt Large model first
convnext_path = "models/food_classifier_convnext_large_cpu_full.pth"
efficientnet_path = "models/food101_efficientnet_best.pth"
model = None
model_info = {"name": "Unknown", "params": 0, "accuracy": "Unknown"}
if os.path.exists(convnext_path):
try:
model = get_convnext_model(num_classes)
checkpoint = torch.load(convnext_path, map_location='cpu')
# Handle different checkpoint formats
if isinstance(checkpoint, dict):
if 'model_state_dict' in checkpoint:
model.load_state_dict(checkpoint['model_state_dict'], strict=False)
model_info = {
"name": "ConvNeXt Large",
"params": "197M",
"accuracy": f"{checkpoint.get('best_acc', 89.8):.1f}%"
}
else:
model.load_state_dict(checkpoint, strict=False)
model_info = {"name": "ConvNeXt Large", "params": "197M", "accuracy": "89.8%"}
else:
model.load_state_dict(checkpoint, strict=False)
model_info = {"name": "ConvNeXt Large", "params": "197M", "accuracy": "89.8%"}
model.eval()
except Exception as e:
model = None
# Fallback to EfficientNet if ConvNeXt fails
if model is None and os.path.exists(efficientnet_path):
try:
model = get_efficientnet_model(num_classes)
checkpoint = torch.load(efficientnet_path, map_location='cpu')
model.load_state_dict(checkpoint, strict=False)
model.eval()
model_info = {"name": "EfficientNet-V2-S", "params": "21M", "accuracy": "85.2%"}
except Exception as e:
model = None
if model is None:
return load_huggingface_model()
return model, model_info
def create_dummy_model():
"""Create a simple dummy model for demo purposes"""
class DummyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(224*224*3, len(CLASS_NAMES))
def forward(self, x):
x = x.view(x.size(0), -1)
return self.linear(x)
model = DummyModel()
model.eval()
model_info = {
"name": "Demo Model",
"params": "1M",
"accuracy": "Demo"
}
return model, model_info
def load_json_data(path):
"""Load JSON data with error handling"""
if not os.path.exists(path):
return {}
try:
with open(path, 'r') as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return {}
@st.cache_data
def load_data():
"""Load health and allergen data with built-in defaults for HF deployment"""
health_data = load_json_data('health_data.json')
allergen_data = load_json_data('allergen_data.json')
# If files don't exist (e.g., on HF deployment), create comprehensive defaults
if not health_data:
health_data = create_default_health_data()
if not allergen_data:
allergen_data = create_default_allergen_data()
return health_data, allergen_data
def create_default_health_data():
"""Create comprehensive default health data for HF deployment"""
return {
"nutrition_info": {
"Grilled Salmon": {"calories": 280, "protein": 25, "carbs": 0, "fat": 18},
"Pizza": {"calories": 266, "protein": 11, "carbs": 33, "fat": 10},
"Hamburger": {"calories": 540, "protein": 25, "carbs": 40, "fat": 31},
"Ice Cream": {"calories": 207, "protein": 3, "carbs": 24, "fat": 11},
"Chocolate Cake": {"calories": 352, "protein": 5, "carbs": 50, "fat": 16},
"French Fries": {"calories": 365, "protein": 4, "carbs": 63, "fat": 17},
"Caesar Salad": {"calories": 184, "protein": 7, "carbs": 6, "fat": 16},
"Sushi": {"calories": 200, "protein": 9, "carbs": 30, "fat": 5},
"Tacos": {"calories": 226, "protein": 9, "carbs": 18, "fat": 13},
"Donuts": {"calories": 452, "protein": 5, "carbs": 51, "fat": 25},
"Chicken Wings": {"calories": 203, "protein": 30, "carbs": 0, "fat": 8},
"Steak": {"calories": 271, "protein": 26, "carbs": 0, "fat": 18},
"Pancakes": {"calories": 227, "protein": 6, "carbs": 28, "fat": 9},
"Waffles": {"calories": 291, "protein": 6, "carbs": 37, "fat": 14}
},
"health_scores": {
"grilled_salmon": {"score": 95, "explanation": "Excellent - High protein, omega-3 fatty acids"},
"pizza": {"score": 60, "explanation": "Moderate - High calories and sodium"},
"hamburger": {"score": 45, "explanation": "Poor - High calories, fat, and sodium"},
"ice_cream": {"score": 35, "explanation": "Poor - High sugar and saturated fat"},
"chocolate_cake": {"score": 25, "explanation": "Poor - Very high sugar and calories"},
"french_fries": {"score": 30, "explanation": "Poor - High calories and unhealthy fats"},
"caesar_salad": {"score": 70, "explanation": "Good - Vegetables, but high sodium dressing"},
"sushi": {"score": 80, "explanation": "Good - Lean protein and omega-3s"},
"tacos": {"score": 65, "explanation": "Moderate - Depends on preparation method"},
"donuts": {"score": 20, "explanation": "Poor - Very high sugar and trans fats"},
"chicken_wings": {"score": 50, "explanation": "Moderate - High protein but often fried"},
"steak": {"score": 70, "explanation": "Good - High protein, iron, but high saturated fat"},
"pancakes": {"score": 40, "explanation": "Poor - High sugar and refined carbs"},
"waffles": {"score": 35, "explanation": "Poor - High sugar and refined carbs"}
}
}
def create_default_allergen_data():
"""Create comprehensive default allergen data for HF deployment"""
return {
"Pizza": ["Gluten", "Dairy"],
"Hamburger": ["Gluten", "Dairy", "Egg"],
"Ice Cream": ["Dairy", "Egg"],
"Chocolate Cake": ["Gluten", "Dairy", "Egg"],
"French Fries": [], # Depends on preparation
"Caesar Salad": ["Egg", "Fish"], # Anchovies in dressing
"Sushi": ["Fish", "Soy"],
"Tacos": ["Gluten"], # Depends on filling
"Donuts": ["Gluten", "Dairy", "Egg"],
"Chicken Wings": [], # Depends on preparation
"Steak": [],
"Pancakes": ["Gluten", "Dairy", "Egg"],
"Waffles": ["Gluten", "Dairy", "Egg"],
"Grilled Salmon": ["Fish"],
"Pasta": ["Gluten", "Egg"],
"Cheese": ["Dairy"],
"Bread": ["Gluten"],
"Eggs": ["Egg"],
"Milk": ["Dairy"],
"Nuts": ["Nuts"],
"Peanuts": ["Peanuts"],
"Shellfish": ["Shellfish"],
"Soy": ["Soy"]
}
def transform_image(image_bytes):
"""Transform image for model input"""
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
return transform(image).unsqueeze(0)
def get_prediction(image_tensor, model, model_info=None):
"""Get model prediction with enhanced error handling for both local and HF models"""
try:
# Ensure model is in eval mode
model.eval()
# Check if this is a Hugging Face transformers model (has processor)
if model_info and "processor" in model_info:
return get_huggingface_prediction(image_tensor, model, model_info)
else:
# This is a PyTorch model (either local ConvNeXt or your HF ConvNeXt)
return get_local_model_prediction(image_tensor, model)
except Exception as e:
print(f"Prediction error: {str(e)}")
# Return dummy predictions as fallback
return get_dummy_predictions()
def get_huggingface_prediction(image_tensor, model, model_info):
"""Get prediction from Hugging Face model"""
try:
processor = model_info["processor"]
# Convert tensor back to PIL Image for HF processor
import torchvision.transforms as T
to_pil = T.ToPILImage()
# Denormalize the tensor first
mean = torch.tensor([0.485, 0.456, 0.406])
std = torch.tensor([0.229, 0.224, 0.225])
# Denormalize
for t, m, s in zip(image_tensor[0], mean, std):
t.mul_(s).add_(m)
# Convert to PIL
image = to_pil(image_tensor[0])
# Process with HF processor
inputs = processor(image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
top5_prob, top5_indices = torch.topk(predictions[0], 5)
results = []
for i in range(5):
idx = top5_indices[i].item()
prob = top5_prob[i].item()
# Map HF model class to our classes (approximate mapping)
if idx < len(CLASS_NAMES):
class_name = CLASS_NAMES[idx]
else:
# Fallback mapping for common foods
food_mapping = {
0: 'pizza', 1: 'hamburger', 2: 'ice_cream', 3: 'donuts', 4: 'french_fries'
}
class_name = food_mapping.get(i, CLASS_NAMES[i % len(CLASS_NAMES)])
results.append({
'class': class_name,
'probability': prob,
'confidence': prob * 100
})
return results
except Exception as e:
print(f"HF prediction error: {str(e)}")
return get_dummy_predictions()
def get_local_model_prediction(image_tensor, model):
"""Get prediction from local PyTorch model"""
try:
# Make sure tensor is on CPU (our model is CPU-only)
if image_tensor.device.type != 'cpu':
image_tensor = image_tensor.cpu()
with torch.no_grad():
outputs = model(image_tensor)
probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
top5_prob, top5_indices = torch.topk(probabilities, 5)
results = []
for i in range(5):
class_name = CLASS_NAMES[top5_indices[i]]
probability = top5_prob[i].item()
results.append({
'class': class_name,
'probability': probability,
'confidence': probability * 100
})
return results
except Exception as e:
print(f"Local model prediction error: {str(e)}")
return get_dummy_predictions()
def get_dummy_predictions():
"""Return dummy predictions for demo purposes"""
import random
# Return realistic dummy predictions
dummy_foods = ['pizza', 'hamburger', 'ice_cream', 'donuts', 'french_fries']
results = []
for i, food in enumerate(dummy_foods):
confidence = random.uniform(60, 95) if i == 0 else random.uniform(10, 40)
results.append({
'class': food,
'probability': confidence / 100,
'confidence': confidence
})
# Sort by confidence
results.sort(key=lambda x: x['confidence'], reverse=True)
return results
def get_nutrition_info(food_name, health_data):
"""Get comprehensive nutrition information with defensive programming"""
try:
if not food_name or not health_data:
return {"calories": 200, "protein": 5, "carbs": 25, "fat": 8}
food_display = food_name.replace('_', ' ').title()
nutrition_info = health_data.get("nutrition_info", {})
# Try different formats
info = nutrition_info.get(food_display) or nutrition_info.get(food_name.lower()) or nutrition_info.get(food_name)
if not info:
# Default nutrition values
info = {"calories": 200, "protein": 5, "carbs": 25, "fat": 8}
return info
except Exception as e:
print(f"Error in get_nutrition_info: {e}")
return {"calories": 200, "protein": 5, "carbs": 25, "fat": 8}
def get_health_score(food_name, health_data):
"""Get health score for food with defensive programming"""
try:
if not food_name or not health_data:
return {"score": 75, "explanation": "Good"}
health_scores = health_data.get("health_scores", {})
score_info = health_scores.get(food_name.lower(), {"score": 75, "explanation": "Good"})
return score_info
except Exception as e:
print(f"Error in get_health_score: {e}")
return {"score": 75, "explanation": "Good"}
def get_allergen_info(food_name, allergen_data):
"""Get allergen information with defensive programming"""
try:
if not food_name or not allergen_data:
return []
food_display = food_name.replace('_', ' ').title()
allergens = allergen_data.get(food_display, [])
return allergens if allergens else []
except Exception as e:
print(f"Error in get_allergen_info: {e}")
return []
def get_common_ingredients(food_name):
"""Get common ingredients for food"""
ingredient_mapping = {
'pizza': 'flour, tomato sauce, cheese, pepperoni, olive oil, salt, sugar, yeast',
'hamburger': 'beef, bun, lettuce, tomato, onion, cheese, ketchup, mustard, salt, pepper',
'ice_cream': 'milk, cream, sugar, vanilla extract, stabilizers, emulsifiers',
'chocolate_cake': 'flour, sugar, cocoa powder, eggs, milk, butter, vanilla extract, baking powder',
'grilled_salmon': 'salmon, olive oil, lemon, herbs, salt, pepper',
'caesar_salad': 'romaine lettuce, parmesan cheese, croutons, caesar dressing, anchovies, garlic',
'french_fries': 'potatoes, vegetable oil, salt, partially hydrogenated oil',
'sushi': 'rice, fish, nori, wasabi, soy sauce, rice vinegar, sugar',
'tacos': 'tortilla, meat, lettuce, cheese, tomato, onion, cilantro, lime',
'pasta': 'wheat flour, eggs, olive oil, salt, water',
'donuts': 'flour, sugar, eggs, milk, butter, yeast, vegetable oil, vanilla extract',
'chicken_wings': 'chicken, flour, spices, oil, hot sauce, butter',
'steak': 'beef, salt, pepper, garlic, herbs',
'pancakes': 'flour, eggs, milk, sugar, baking powder, butter, vanilla',
'waffles': 'flour, eggs, milk, sugar, baking powder, butter, vanilla'
}
return ingredient_mapping.get(food_name, 'Common ingredients not available for this dish')
def generate_recipe(food_name):
"""Generate rich, detailed recipes with food pictures and comprehensive instructions"""
try:
food_display = food_name.replace('_', ' ').title()
# Build dietary restrictions
dietary_restrictions = ""
if st.session_state.dietary_preferences:
restrictions = ', '.join(st.session_state.dietary_preferences).lower()
dietary_restrictions = f"Made {restrictions}. "
# Build allergen restrictions
allergen_restrictions = ""
if st.session_state.user_allergens:
allergen_restrictions = f"Avoiding {', '.join(st.session_state.user_allergens).lower()}. "
# Choose recipe style with enhanced details
styles = ["Classic", "Healthy", "Gourmet", "Quick & Easy"]
style = random.choice(styles)
# Get food-specific image from Unsplash
food_images = {
'pizza': 'https://images.unsplash.com/photo-1513104890138-7c749659a591?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'grilled_salmon': 'https://images.pexels.com/photos/3296277/pexels-photo-3296277.jpeg?auto=compress&cs=tinysrgb&w=600&h=400&fit=crop',
'caesar_salad': 'https://images.unsplash.com/photo-1546793665-c74683f339c1?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'chocolate_cake': 'https://images.pexels.com/photos/533325/pexels-photo-533325.jpeg?auto=compress&cs=tinysrgb&w=600&h=400&fit=crop',
'hamburger': 'https://images.unsplash.com/photo-1568901346375-23c9450c58cd?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'sushi': 'https://images.unsplash.com/photo-1579871494447-9811cf80d66c?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'steak': 'https://images.unsplash.com/photo-1546833999-b9f581a1996d?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'pancakes': 'https://images.unsplash.com/photo-1567620905732-2d1ec7ab7445?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'tacos': 'https://images.unsplash.com/photo-1565299585323-38174c6b4d64?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'ramen': 'https://images.unsplash.com/photo-1569718212165-3a8278d5f624?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'pho': 'https://images.unsplash.com/photo-1546833999-b9f581a1996d?ixlib=rb-4.0.3&w=600&h=400&fit=crop',
'salmon': 'https://images.pexels.com/photos/3296277/pexels-photo-3296277.jpeg?auto=compress&cs=tinysrgb&w=600&h=400&fit=crop'
}
# Get appropriate image for the food
food_key = food_name.lower()
food_image_url = food_images.get(food_key, 'https://images.unsplash.com/photo-1476718406336-bb5a9690ee2a?ixlib=rb-4.0.3&w=600&h=400&fit=crop')
if style == "Classic":
recipe = f"""
<div style="text-align: center; margin: 20px 0;">
<img src="{food_image_url}" style="width: 100%; max-width: 500px; height: 300px; object-fit: cover; border-radius: 15px; box-shadow: 0 8px 24px rgba(0,0,0,0.2);" alt="{food_display}">
</div>
**🍽️ Classic {food_display} Recipe**
**📋 Ingredients (Serves 4):**
• 2 lbs fresh {food_display.lower()} (main ingredient)
• 3 tbsp extra virgin olive oil
• 1 large yellow onion, finely chopped
• 3 cloves garlic, minced
• 2 tsp sea salt (adjust to taste)
• 1 tsp freshly ground black pepper
• 2 tbsp fresh herbs (parsley, thyme, or rosemary)
• 1 lemon (zested and juiced)
• 2 tbsp butter (for finishing)
• Fresh vegetables for sides (carrots, broccoli, or green beans)
**👨🍳 Detailed Instructions:**
**Prep Work (10 minutes):**
1. **Prepare ingredients:** Wash all vegetables, mince garlic, chop onions, and measure spices
2. **Season main ingredient:** Pat dry and season generously with salt and pepper, let rest 15 minutes
3. **Preheat equipment:** Heat oven to 375°F (190°C) or prepare stovetop on medium-high heat
**Cooking Process (25-30 minutes):**
4. **Sear for flavor:** Heat olive oil in heavy pan, sear main ingredient until golden (3-4 minutes per side)
5. **Build the base:** Add onions and garlic, cook until fragrant and translucent (5 minutes)
6. **Add aromatics:** Incorporate fresh herbs, lemon zest, and any liquid components
7. **Slow cooking:** Reduce heat to medium-low, cover, and cook until tender (15-20 minutes)
8. **Final touches:** Add butter, lemon juice, and adjust seasoning to taste
**Plating & Presentation:**
9. **Rest the dish:** Let rest 5 minutes before serving to allow flavors to meld
10. **Garnish beautifully:** Top with fresh herbs, a drizzle of quality olive oil, and lemon wedges
**💡 Classic Cooking Tips:**
• **Quality ingredients:** Use the freshest, highest-quality ingredients you can find
• **Patience is key:** Don't rush the cooking process - good food takes time
• **Taste as you go:** Adjust seasoning throughout the cooking process
• **Temperature control:** Use a meat thermometer for perfect doneness
• **Rest time:** Always let proteins rest before serving to retain juices
**🍷 Perfect Pairings:**
• **Wine:** Medium-bodied red wine or crisp white wine
• **Sides:** Roasted seasonal vegetables, garlic mashed potatoes, or wild rice
• **Bread:** Crusty artisan bread with herb butter
**⏱️ Timing Guide:**
• **Prep Time:** 15 minutes
• **Cook Time:** 30 minutes
• **Total Time:** 45 minutes
• **Difficulty:** Intermediate
**🔥 Chef's Secret:**
The key to this classic recipe is building layers of flavor. Start with a good sear, add aromatics gradually, and finish with fresh herbs and acid for brightness."""
elif style == "Healthy":
recipe = f"""
<div style="text-align: center; margin: 20px 0;">
<img src="{food_image_url}" style="width: 100%; max-width: 500px; height: 300px; object-fit: cover; border-radius: 15px; box-shadow: 0 8px 24px rgba(0,0,0,0.2);" alt="Healthy {food_display}">
</div>
**🌱 Healthy {food_display} Recipe**
**📋 Nutritious Ingredients (Serves 4):**
• 2 lbs organic {food_display.lower()} (hormone-free, sustainable)
• 2 tbsp avocado oil (high smoke point, healthy fats)
• 1 cup rainbow vegetables (bell peppers, carrots, zucchini)
• 2 cups leafy greens (spinach, kale, or arugula)
• 3 cloves garlic, minced (immune-boosting)
• 1 tbsp fresh ginger, grated (anti-inflammatory)
• 2 tbsp coconut aminos (low-sodium soy sauce alternative)
• 1 tbsp apple cider vinegar (probiotic benefits)
• 2 tbsp pumpkin seeds (healthy fats and protein)
• 1 avocado, sliced (omega-3 fatty acids)
• Fresh herbs: cilantro, parsley, basil
**👩⚕️ Healthy Cooking Method:**
**Nutrient Preservation (10 minutes):**
1. **Gentle preparation:** Use sharp knives to minimize cell damage, preserving nutrients
2. **Smart seasoning:** Use herbs and spices instead of excess salt for flavor
3. **Preserve enzymes:** Don't over-wash vegetables; light rinse maintains nutrients
**Heart-Healthy Cooking (20-25 minutes):**
4. **Oil selection:** Use avocado oil for high-heat cooking, olive oil for finishing
5. **Steam-sauté method:** Use minimal oil with splash of water for steam-cooking effect
6. **Low-heat approach:** Cook at medium heat to preserve delicate nutrients
7. **Quick cooking:** Minimize cooking time to retain vitamins and minerals
8. **Add greens last:** Incorporate leafy greens in final 2 minutes to preserve folate
**Superfood Finishing:**
9. **Raw additions:** Top with raw avocado, pumpkin seeds, and fresh herbs
10. **Probiotic boost:** Drizzle with apple cider vinegar and a touch of olive oil
**💚 Health Benefits:**
• **High Protein:** 35g per serving for muscle maintenance
• **Omega-3 Fatty Acids:** Support brain and heart health
• **Antioxidants:** Colorful vegetables provide cellular protection
• **Fiber Rich:** 12g fiber aids digestion and satiety
• **Low Inflammatory:** Anti-inflammatory ingredients support recovery
**🥗 Nutritional Powerhouses:**
• **Avocado:** Monounsaturated fats for heart health
• **Leafy Greens:** Folate, iron, and vitamin K
• **Garlic & Ginger:** Natural antibiotics and circulation boosters
• **Pumpkin Seeds:** Zinc, magnesium, and healthy fats
**⏱️ Healthy Timing:**
• **Prep Time:** 12 minutes
• **Cook Time:** 20 minutes
• **Total Time:** 32 minutes
• **Calories per serving:** ~380
**🏃♀️ Perfect For:**
• Post-workout recovery meals
• Anti-inflammatory diets
• Weight management
• Heart-healthy eating
• Diabetic-friendly meals"""
elif style == "Gourmet":
recipe = f"""
<div style="text-align: center; margin: 20px 0;">
<img src="{food_image_url}" style="width: 100%; max-width: 500px; height: 300px; object-fit: cover; border-radius: 15px; box-shadow: 0 8px 24px rgba(0,0,0,0.2);" alt="Gourmet {food_display}">
</div>
**⭐ Gourmet {food_display} Recipe**
**🏆 Premium Ingredients (Serves 4):**
• 2 lbs prime-grade {food_display.lower()} (dry-aged if available)
• 3 tbsp white truffle oil (or high-quality extra virgin olive oil)
• 1 shallot, brunoise cut (finely diced)
• 2 cloves purple garlic, microplaned
• ½ cup dry white wine (Sauvignon Blanc or Pinot Grigio)
• 2 tbsp crème fraîche (or heavy cream)
• 1 tbsp Dijon mustard (whole grain preferred)
• 2 sprigs fresh thyme (stripped)
• 1 sprig fresh rosemary
• Flaky sea salt (Maldon or fleur de sel)
• Freshly cracked white pepper
• 1 tbsp unsalted European butter (for mounting)
• Microgreens for garnish
• Edible flowers (optional, for presentation)
**🎩 Gourmet Technique:**
**Mise en Place (15 minutes):**
1. **Temperature control:** Bring main ingredient to room temperature (30 minutes before cooking)
2. **Knife skills:** Practice proper brunoise cuts for uniform cooking and presentation
3. **Equipment prep:** Preheat copper or cast-iron pan for even heat distribution
**Professional Cooking Method (35-40 minutes):**
4. **Perfect sear:** Heat truffle oil until shimmering, sear until beautiful caramelization forms
5. **Aromatics foundation:** Add shallots, cook until translucent and slightly caramelized
6. **Deglazing technique:** Add wine, scrape fond (browned bits) for maximum flavor
7. **Sauce development:** Reduce wine by half, add herbs and mustard
8. **Emulsification:** Whisk in crème fraîche for silky, restaurant-quality sauce
9. **Temperature precision:** Use thermometer to achieve perfect doneness
10. **Butter mounting:** Finish sauce with cold butter, whisking for glossy finish
**Michelin-Star Plating:**
11. **Plate warming:** Warm plates in low oven for proper service temperature
12. **Sauce artistry:** Use squeeze bottle or spoon for elegant sauce placement
13. **Height and color:** Create visual interest with height and contrasting colors
14. **Final garnish:** Top with microgreens and optional edible flowers
**🍾 Sommelier Pairings:**
• **Wine:** Burgundian Chardonnay or Champagne for celebration
• **Cheese course:** Aged Gruyère or Roquefort
• **Bread:** Artisan sourdough with compound herb butter
**⭐ Gourmet Secrets:**
• **Resting technique:** Always rest proteins on warm plate under loose foil
• **Sauce consistency:** Proper sauce should coat the back of a spoon
• **Seasoning layers:** Season at multiple stages, not just at the end
• **Visual appeal:** Odd numbers (3 or 5 elements) create more appealing plates
**⏱️ Fine Dining Timeline:**
• **Prep Time:** 25 minutes
• **Cook Time:** 35 minutes
• **Plating Time:** 5 minutes
• **Total Time:** 65 minutes
• **Difficulty:** Advanced
**💰 Cost Per Serving:** ~$28 (restaurant quality at home)"""
else: # Quick & Easy
recipe = f"""
<div style="text-align: center; margin: 20px 0;">
<img src="{food_image_url}" style="width: 100%; max-width: 500px; height: 300px; object-fit: cover; border-radius: 15px; box-shadow: 0 8px 24px rgba(0,0,0,0.2);" alt="Quick {food_display}">
</div>
**⚡ Quick & Easy {food_display} Recipe**
**🛒 Simple Ingredients (Serves 4):**
• 2 lbs {food_display.lower()} (fresh or high-quality frozen)
• 2 tbsp cooking oil (vegetable or canola)
• 1 packet seasoning mix (or 2 tsp mixed herbs)
• 1 pre-cut vegetable medley (from produce section)
• 1 lemon (for instant brightness)
• Salt and pepper to taste
• 2 tbsp butter or olive oil
• Pre-washed salad greens (for quick side)
• Store-bought sauce or dressing
**💨 Speed Cooking Method:**
**Lightning Prep (5 minutes):**
1. **One-bowl seasoning:** Mix all spices in one bowl, coat main ingredient
2. **Pre-heat advantage:** Start heating pan while prepping - saves 3-4 minutes
3. **Assembly line:** Arrange all ingredients within arm's reach
**Fast & Efficient Cooking (12-15 minutes):**
4. **High-heat technique:** Use medium-high heat for quick cooking and good flavor
5. **One-pan method:** Cook everything in same pan to minimize cleanup
6. **Batch cooking:** Cook similar-sized pieces together for even doneness
7. **Steam finish:** Add splash of water and cover for final 3 minutes
8. **Quick sauce:** Deglaze pan with lemon juice for instant pan sauce
**Speedy Service:**
9. **Direct plating:** Serve directly from pan to save time and dishes
10. **Simple garnish:** Fresh lemon wedge and herbs - no fancy plating needed
**⏰ Time-Saving Hacks:**
• **Prep night before:** Season and marinate ingredients the previous evening
• **Kitchen shortcuts:** Use pre-cut vegetables and pre-mixed seasonings
• **One-pan cleanup:** Line pan with parchment for minimal washing
• **Batch cooking:** Make double portions for tomorrow's lunch
• **Microwave assists:** Pre-steam hard vegetables in microwave (2-3 minutes)
**🥗 Quick Side Ideas:**
• **5-minute salad:** Pre-washed greens + bottled dressing + nuts
• **Instant rice:** 90-second microwaveable brown rice
• **Frozen vegetables:** Steam-in-bag varieties (3-4 minutes)
**📱 Busy Parent Approved:**
• **Kid-friendly:** Mild flavors that children will enjoy
• **Nutrition maintained:** Quick doesn't mean sacrificing health
• **Leftover friendly:** Tastes great reheated for lunch
• **Budget conscious:** Uses affordable, accessible ingredients
**⏱️ Express Timeline:**
• **Prep Time:** 5 minutes
• **Cook Time:** 15 minutes
• **Cleanup Time:** 5 minutes
• **Total Time:** 25 minutes
• **Difficulty:** Beginner
**🏃♀️ Perfect For:**
• Weeknight dinners after work
• Busy families with kids
• College students and beginners
• Quick lunch preparations
• Emergency dinner solutions"""
# Add personalized modifications
if dietary_restrictions or allergen_restrictions:
recipe += f"\n\n**🎯 Your Personal Modifications:**\n"
if 'vegan' in dietary_restrictions.lower():
recipe += "• 🌿 Replace dairy with plant-based alternatives (cashew cream, nutritional yeast)\n"
recipe += "• 🥥 Use coconut oil instead of butter for rich flavor\n"
if 'keto' in dietary_restrictions.lower():
recipe += "• 🥑 Increase healthy fats, reduce any carbohydrates\n"
recipe += "• 🧀 Add extra cheese and avocado for fat content\n"
if 'gluten' in allergen_restrictions.lower():
recipe += "• ⚠️ Use gluten-free alternatives for any flour or breadcrumbs\n"
recipe += "• 🌾 Substitute with almond flour or coconut flour\n"
if 'mediterranean' in dietary_restrictions.lower():
recipe += "• 🫒 Extra virgin olive oil and Mediterranean herbs (oregano, basil)\n"
recipe += "• 🍅 Add tomatoes, olives, and feta cheese if appropriate\n"
recipe += f"\n\n**📱 Recipe by EatSmart Pro AI Chef**\n*Personalized for your preferences • Style: {style} • Generated with ❤️*"
return recipe
except Exception as e:
return generate_fallback_recipe(food_name.replace('_', ' ').title(), "", "", "")
def generate_fallback_recipe(food_display, dietary_restrictions, allergen_restrictions, trans_fat_note):
"""Generate a structured fallback recipe when AI models are unavailable"""
# Basic recipes database for common foods
recipe_templates = {
'grilled_salmon': {
'ingredients': ['4 salmon fillets', '2 tbsp olive oil', '1 lemon (juiced)', 'salt and pepper', 'fresh herbs (dill or parsley)'],
'instructions': ['Preheat grill to medium-high', 'Brush salmon with olive oil', 'Season with salt, pepper, and herbs', 'Grill 4-5 minutes per side', 'Drizzle with lemon juice before serving'],
'prep_time': '15 minutes',
'servings': '4'
},
'chicken_curry': {
'ingredients': ['1 lb chicken breast (cubed)', '1 onion (diced)', '2 cloves garlic', '1 tbsp curry powder', '1 can coconut milk', '1 cup vegetables'],
'instructions': ['Sauté onion and garlic', 'Add chicken and curry powder', 'Pour in coconut milk', 'Add vegetables', 'Simmer 20 minutes until tender'],
'prep_time': '30 minutes',
'servings': '4'
},
'caesar_salad': {
'ingredients': ['1 head romaine lettuce', '1/4 cup parmesan cheese', '2 tbsp olive oil', '1 tbsp lemon juice', 'croutons', 'black pepper'],
'instructions': ['Wash and chop lettuce', 'Make dressing with oil and lemon', 'Toss lettuce with dressing', 'Top with cheese and croutons', 'Season with pepper'],
'prep_time': '10 minutes',
'servings': '2'
}
}
# Get template or create generic one
food_key = food_display.lower().replace(' ', '_')
template = recipe_templates.get(food_key, {
'ingredients': [f'Main ingredient for {food_display}', 'Seasonings and spices', 'Healthy cooking oil', 'Fresh vegetables', 'Herbs for flavor'],
'instructions': ['Prepare all ingredients', 'Use healthy cooking methods', 'Season appropriately', 'Cook until done', 'Serve with vegetables'],
'prep_time': '20-30 minutes',
'servings': '2-4'
})
# Apply dietary modifications
modifications = []
if 'vegan' in dietary_restrictions.lower():
modifications.append("🌱 Use plant-based ingredients only")
if 'keto' in dietary_restrictions.lower():
modifications.append("🥑 Keep carbs under 5g per serving")
if 'gluten' in allergen_restrictions.lower():
modifications.append("⚠️ Use gluten-free alternatives")
if trans_fat_note:
modifications.append("💚 Use healthy oils (olive, avocado, coconut)")
recipe = f"""**🍽️ Healthy {food_display} Recipe**
**Ingredients:**
{chr(10).join(f'• {ingredient}' for ingredient in template['ingredients'])}
**Instructions:**
{chr(10).join(f'{i+1}. {instruction}' for i, instruction in enumerate(template['instructions']))}
**Chef's Tips:**
• Use fresh, high-quality ingredients for best flavor
• Adjust seasonings to your taste preferences
• Include colorful vegetables for added nutrition
• Control portion sizes for balanced eating
**Prep Time:** {template['prep_time']}
**Servings:** {template['servings']}"""
if modifications:
recipe += f"\n\n**🎯 Your Personalized Modifications:**\n{chr(10).join(f'• {mod}' for mod in modifications)}"
recipe += "\n\n*💡 Recipe generated using built-in healthy cooking database - completely free!*"
return recipe
def generate_multiple_recipes(food_name, start_index=0):
"""Generate multiple recipe variations with different styles and food-specific images"""
recipes = []
# Define different cooking styles
styles = [
("Healthy", "heart-healthy with minimal oil and fresh ingredients"),
("Keto", "low-carb, high-fat ketogenic diet friendly"),
("Vegan", "plant-based with no animal products"),
("Mediterranean", "olive oil, herbs, and fresh vegetables"),
("Quick & Easy", "simple preparation under 30 minutes"),
("Gourmet", "restaurant-quality with sophisticated techniques")
]
# Enhanced food image URLs - Multiple variations per food category using Pexels
food_images = {
# Fish and Seafood - 100% VERIFIED REAL SALMON ONLY (NO SHRIMP!)
'salmon': [
'https://images.pexels.com/photos/3655916/pexels-photo-3655916.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salmon dish on a ceramic plate (your 1st suggestion)
'https://images.pexels.com/photos/46239/salmon-dish-food-meal-46239.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Existing real salmon image
'https://images.pexels.com/photos/2374946/pexels-photo-2374946.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Cooked fish on plate (your 3rd suggestion)
],
'grilled_salmon': [
'https://images.pexels.com/photos/3655916/pexels-photo-3655916.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salmon dish on a ceramic plate
'https://images.pexels.com/photos/46239/salmon-dish-food-meal-46239.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salmon dish food meal
'https://images.pexels.com/photos/2374946/pexels-photo-2374946.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Cooked fish on plate
],
'tuna': [
'https://images.pexels.com/photos/8477/food-dinner-lunch-meal.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/11220/fish-tuna-sashimi-sushi.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/6660/food-dinner-lunch-meal.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'sushi': [
'https://images.pexels.com/photos/2098085/pexels-photo-2098085.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/357756/pexels-photo-357756.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/248444/pexels-photo-248444.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Meat and Poultry - Multiple variations
'steak': [
'https://images.pexels.com/photos/1352262/pexels-photo-1352262.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1251208/pexels-photo-1251208.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/3997609/pexels-photo-3997609.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'beef': [
'https://images.pexels.com/photos/361184/asparagus-steak-veal-steak-veal-361184.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/65175/pexels-photo-65175.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/323682/pexels-photo-323682.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'chicken': [
'https://images.pexels.com/photos/106343/pexels-photo-106343.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2338407/pexels-photo-2338407.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/616354/pexels-photo-616354.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'chicken_wings': [
'https://images.pexels.com/photos/60616/fried-chicken-chicken-fried-crunchy-60616.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2338407/pexels-photo-2338407.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/14737/pexels-photo-14737.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Pork - 100% VERIFIED PORK CHOP IMAGES!
'pork_chop': [
'https://images.pexels.com/photos/361184/asparagus-steak-veal-steak-veal-361184.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/65175/pexels-photo-65175.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/323682/pexels-photo-323682.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Burgers - Multiple variations
'hamburger': [
'https://images.pexels.com/photos/1639557/pexels-photo-1639557.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/552056/pexels-photo-552056.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'burger': [
'https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1639557/pexels-photo-1639557.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/552056/pexels-photo-552056.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Pizza and Italian - Multiple variations
'pizza': [
'https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/365459/pexels-photo-365459.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2147491/pexels-photo-2147491.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'pasta': [
'https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1437267/pexels-photo-1437267.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/769969/pexels-photo-769969.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'spaghetti_bolognese': [
'https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1437267/pexels-photo-1437267.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4518843/pexels-photo-4518843.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Salads - Multiple variations
'caesar_salad': [
'https://images.pexels.com/photos/406152/pexels-photo-406152.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1211887/pexels-photo-1211887.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'salad': [
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/406152/pexels-photo-406152.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1211887/pexels-photo-1211887.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Asian Cuisine - Multiple variations
'ramen': [
'https://images.pexels.com/photos/1460872/pexels-photo-1460872.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1907228/pexels-photo-1907228.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/963486/pexels-photo-963486.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'pho': [
'https://images.pexels.com/photos/1410235/pexels-photo-1410235.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2474661/pexels-photo-2474661.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/884600/pexels-photo-884600.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'chicken_curry': [
'https://images.pexels.com/photos/2474661/pexels-photo-2474661.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2347311/pexels-photo-2347311.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/8162/food-dish-meal-soup.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Desserts - Multiple variations - 100% VERIFIED REAL CHOCOLATE CAKE ONLY (NO COOKS, NO CUPCAKES, NO COOKIES, NO BREAD)
'chocolate_cake': [
'https://images.pexels.com/photos/291528/pexels-photo-291528.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Sliced chocolate cake on plate
'https://images.pexels.com/photos/227432/pexels-photo-227432.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Chocolate cake with layers and topping
'https://images.pexels.com/photos/2067396/pexels-photo-2067396.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Stacked brownies (chocolate cake style)
],
'cake': [
'https://images.pexels.com/photos/533325/pexels-photo-533325.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/14105/pexels-photo-14105.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/230325/pexels-photo-230325.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'pancakes': [
'https://images.pexels.com/photos/2260/food-healthy-morning-cereals.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1192030/pexels-photo-1192030.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/291528/pexels-photo-291528.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Sandwiches and Breads - Multiple variations
'sandwich': [
'https://images.pexels.com/photos/1633525/pexels-photo-1633525.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1603901/pexels-photo-1603901.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/161401/pexels-photo-161401.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'hot_dog': [
'https://images.pexels.com/photos/4518564/pexels-photo-4518564.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4518633/pexels-photo-4518633.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4518691/pexels-photo-4518691.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# More variety for Surprise Me - VERIFIED TACO IMAGES ONLY
'tacos': [
'https://images.pexels.com/photos/2456435/pexels-photo-2456435.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/461198/pexels-photo-461198.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/3749659/pexels-photo-3749659.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'dumplings': [
'https://images.pexels.com/photos/5560763/pexels-photo-5560763.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1410235/pexels-photo-1410235.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/6249516/pexels-photo-6249516.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# French Fries and Sides - Multiple variations
'french_fries': [
'https://images.pexels.com/photos/115740/pexels-photo-115740.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1583884/pexels-photo-1583884.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1893556/pexels-photo-1893556.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'donuts': [
'https://images.pexels.com/photos/205961/pexels-photo-205961.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1070850/pexels-photo-1070850.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1266005/pexels-photo-1266005.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Ice Cream and Desserts - 100% VERIFIED REAL ICE CREAM ONLY (NO LANDSCAPES!)
'ice_cream': [
'https://images.pexels.com/photos/461430/pexels-photo-461430.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/461382/pexels-photo-461382.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/376464/pexels-photo-376464.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'waffles': [
'https://images.pexels.com/photos/1192030/pexels-photo-1192030.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4825701/pexels-photo-4825701.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/15126268/pexels-photo-15126268.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Additional Categories for more variety
'apple_pie': [
'https://images.pexels.com/photos/7772/food-eating-pie-apple.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/5639908/pexels-photo-5639908.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/6210748/pexels-photo-6210748.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Default fallback - Multiple high-quality variations
'default': [
'https://images.pexels.com/photos/376464/pexels-photo-376464.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Pancakes
'https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Burger
'https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Pizza
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salad
'https://images.pexels.com/photos/1460872/pexels-photo-1460872.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Ramen
'https://images.pexels.com/photos/2456435/pexels-photo-2456435.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Tacos
]
}
def get_best_matching_image(food_name, recipe_index=0):
"""Get best matching image for the food, with variety for multiple recipes"""
try:
# Clean food name for better matching
clean_name = food_name.lower().replace('_', ' ')
# Direct match first
if food_name in food_images:
image_list = food_images[food_name]
if isinstance(image_list, list):
# Use modulo to cycle through different images for each recipe
return image_list[recipe_index % len(image_list)]
else:
return image_list
# Try partial matches for comprehensive coverage
for key, images in food_images.items():
if key in clean_name or clean_name in key:
if isinstance(images, list):
return images[recipe_index % len(images)]
else:
return images
# Category-based matching for better fallbacks
if any(word in clean_name for word in ['salmon', 'fish']):
salmon_images = food_images.get('salmon', food_images['default'])
if isinstance(salmon_images, list):
return salmon_images[recipe_index % len(salmon_images)]
else:
return salmon_images
elif any(word in clean_name for word in ['beef', 'steak', 'meat']):
steak_images = food_images.get('steak', food_images['default'])
if isinstance(steak_images, list):
return steak_images[recipe_index % len(steak_images)]
else:
return steak_images
elif any(word in clean_name for word in ['chicken', 'poultry']):
chicken_images = food_images.get('chicken', food_images['default'])
if isinstance(chicken_images, list):
return chicken_images[recipe_index % len(chicken_images)]
else:
return chicken_images
elif any(word in clean_name for word in ['ramen', 'japanese_noodle', 'noodle_soup']):
ramen_images = food_images.get('ramen', food_images['default'])
if isinstance(ramen_images, list):
return ramen_images[recipe_index % len(ramen_images)]
else:
return ramen_images
elif any(word in clean_name for word in ['pho', 'vietnamese']):
pho_images = food_images.get('pho', food_images['default'])
if isinstance(pho_images, list):
return pho_images[recipe_index % len(pho_images)]
else:
return pho_images
elif any(word in clean_name for word in ['pizza', 'italian']):
pizza_images = food_images.get('pizza', food_images['default'])
if isinstance(pizza_images, list):
return pizza_images[recipe_index % len(pizza_images)]
else:
return pizza_images
elif any(word in clean_name for word in ['salad', 'vegetable']):
salad_images = food_images.get('salad', food_images['default'])
if isinstance(salad_images, list):
return salad_images[recipe_index % len(salad_images)]
else:
return salad_images
elif any(word in clean_name for word in ['cake', 'dessert', 'sweet']):
cake_images = food_images.get('cake', food_images['default'])
if isinstance(cake_images, list):
return cake_images[recipe_index % len(cake_images)]
else:
return cake_images
# Default fallback with variety
default_images = food_images['default']
if isinstance(default_images, list):
return default_images[recipe_index % len(default_images)]
else:
return default_images
except Exception as e:
print(f"Error getting image for {food_name}: {e}")
# Ultimate fallback
default_images = food_images['default']
if isinstance(default_images, list):
return default_images[0]
else:
return default_images
# Generate 3 different recipes with varied images
for i in range(3):
style_index = (start_index + i) % len(styles)
style_name, style_description = styles[style_index]
# Get different image for each recipe using recipe index
image_url = get_best_matching_image(food_name, recipe_index=i)
recipe_content = generate_styled_recipe(food_name.replace('_', ' ').title(), style_name)
recipes.append({
'title': f"{style_name} {food_name.replace('_', ' ').title()}",
'style': style_name,
'content': recipe_content,
'image_url': image_url,
'prep_time': f"{15 + (i * 5)}-{25 + (i * 5)} minutes",
'difficulty': ["Easy", "Medium", "Advanced"][i % 3]
})
return recipes
def generate_single_recipe(food_name, recipe_index=0):
"""Generate a single recipe with smart food-category image matching"""
# Define different cooking styles
styles = [
("Healthy", "heart-healthy with minimal oil and fresh ingredients"),
("Keto", "low-carb, high-fat ketogenic diet friendly"),
("Vegan", "plant-based with no animal products"),
("Mediterranean", "olive oil, herbs, and fresh vegetables"),
("Quick & Easy", "simple preparation under 30 minutes"),
("Gourmet", "restaurant-quality with sophisticated techniques")
]
# Smart food-category specific image URLs
food_images = {
# Fish and Seafood - 100% VERIFIED REAL SALMON ONLY (NO SHRIMP!)
'salmon': [
'https://images.pexels.com/photos/3655916/pexels-photo-3655916.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salmon dish on a ceramic plate (your 1st suggestion)
'https://images.pexels.com/photos/46239/salmon-dish-food-meal-46239.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Existing real salmon image
'https://images.pexels.com/photos/2374946/pexels-photo-2374946.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Cooked fish on plate (your 3rd suggestion)
],
'grilled_salmon': [
'https://images.pexels.com/photos/3655916/pexels-photo-3655916.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salmon dish on a ceramic plate
'https://images.pexels.com/photos/46239/salmon-dish-food-meal-46239.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salmon dish food meal
'https://images.pexels.com/photos/2374946/pexels-photo-2374946.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Cooked fish on plate
],
'sushi': [
'https://images.pexels.com/photos/2098085/pexels-photo-2098085.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/357756/pexels-photo-357756.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/248444/pexels-photo-248444.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'tuna_tartare': [
'https://images.pexels.com/photos/8477/food-dinner-lunch-meal.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/11220/fish-tuna-sashimi-sushi.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/6660/food-dinner-lunch-meal.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Meat and Poultry - Multiple variations
'steak': [
'https://images.pexels.com/photos/1352262/pexels-photo-1352262.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1251208/pexels-photo-1251208.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/3997609/pexels-photo-3997609.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'filet_mignon': [
'https://images.pexels.com/photos/361184/asparagus-steak-veal-steak-veal-361184.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/65175/pexels-photo-65175.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/323682/pexels-photo-323682.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'beef_carpaccio': [
'https://images.pexels.com/photos/361184/asparagus-steak-veal-steak-veal-361184.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/65175/pexels-photo-65175.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/323682/pexels-photo-323682.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'chicken_curry': [
'https://images.pexels.com/photos/2474661/pexels-photo-2474661.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2347311/pexels-photo-2347311.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/8162/food-dish-meal-soup.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'chicken_wings': [
'https://images.pexels.com/photos/60616/fried-chicken-chicken-fried-crunchy-60616.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2338407/pexels-photo-2338407.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/14737/pexels-photo-14737.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Pork - 100% VERIFIED PORK CHOP IMAGES!
'pork_chop': [
'https://images.pexels.com/photos/361184/asparagus-steak-veal-steak-veal-361184.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/65175/pexels-photo-65175.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/323682/pexels-photo-323682.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Pasta - Multiple variations
'pasta': [
'https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1437267/pexels-photo-1437267.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/769969/pexels-photo-769969.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'spaghetti_bolognese': [
'https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1437267/pexels-photo-1437267.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4518843/pexels-photo-4518843.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'spaghetti_carbonara': [
'https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1437267/pexels-photo-1437267.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/769969/pexels-photo-769969.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'lasagna': [
'https://images.pexels.com/photos/2765875/pexels-photo-2765875.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Close-up baked lasagna
'https://images.pexels.com/photos/5864352/pexels-photo-5864352.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Vegetarian eggplant lasagna
'https://images.pexels.com/photos/4079522/pexels-photo-4079522.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Lasagna on white ceramic plate
],
# Pizza - Multiple variations
'pizza': [
'https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/365459/pexels-photo-365459.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2147491/pexels-photo-2147491.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Burgers - Multiple variations
'hamburger': [
'https://images.pexels.com/photos/1639557/pexels-photo-1639557.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/552056/pexels-photo-552056.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Salads - Multiple variations
'caesar_salad': [
'https://images.pexels.com/photos/406152/pexels-photo-406152.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1211887/pexels-photo-1211887.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'greek_salad': [
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1213710/pexels-photo-1213710.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1132047/pexels-photo-1132047.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Asian Cuisine - Multiple variations - 100% VERIFIED RAMEN ONLY
'ramen': [
'https://images.pexels.com/photos/1460872/pexels-photo-1460872.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1907228/pexels-photo-1907228.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/963486/pexels-photo-963486.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'pad_thai': [
'https://images.pexels.com/photos/1143754/pexels-photo-1143754.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/918327/pexels-photo-918327.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1410235/pexels-photo-1410235.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'pho': [
'https://images.pexels.com/photos/1410235/pexels-photo-1410235.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/2474661/pexels-photo-2474661.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/884600/pexels-photo-884600.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Desserts - Multiple variations - 100% VERIFIED REAL CHOCOLATE CAKE ONLY
'chocolate_cake': [
'https://images.pexels.com/photos/291528/pexels-photo-291528.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Sliced chocolate cake on plate
'https://images.pexels.com/photos/227432/pexels-photo-227432.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Chocolate cake with layers and topping
'https://images.pexels.com/photos/2067396/pexels-photo-2067396.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Stacked brownies (chocolate cake style)
],
'cake': [
'https://images.pexels.com/photos/533325/pexels-photo-533325.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/14105/pexels-photo-14105.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/230325/pexels-photo-230325.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'pancakes': [
'https://images.pexels.com/photos/2260/food-healthy-morning-cereals.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1192030/pexels-photo-1192030.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/291528/pexels-photo-291528.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Sandwiches and Breads - Multiple variations
'sandwich': [
'https://images.pexels.com/photos/1633525/pexels-photo-1633525.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1603901/pexels-photo-1603901.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/161401/pexels-photo-161401.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'hot_dog': [
'https://images.pexels.com/photos/4518564/pexels-photo-4518564.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4518633/pexels-photo-4518633.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4518691/pexels-photo-4518691.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# More variety for Surprise Me - VERIFIED TACO IMAGES ONLY
'tacos': [
'https://images.pexels.com/photos/2456435/pexels-photo-2456435.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/461198/pexels-photo-461198.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/3749659/pexels-photo-3749659.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'dumplings': [
'https://images.pexels.com/photos/5560763/pexels-photo-5560763.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1410235/pexels-photo-1410235.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/6249516/pexels-photo-6249516.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# French Fries and Sides - Multiple variations
'french_fries': [
'https://images.pexels.com/photos/115740/pexels-photo-115740.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1583884/pexels-photo-1583884.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1893556/pexels-photo-1893556.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'donuts': [
'https://images.pexels.com/photos/205961/pexels-photo-205961.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1070850/pexels-photo-1070850.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/1266005/pexels-photo-1266005.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Ice Cream and Desserts - 100% VERIFIED REAL ICE CREAM ONLY (NO LANDSCAPES!)
'ice_cream': [
'https://images.pexels.com/photos/3631/summer-dessert-sweet-ice-cream.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/205961/pexels-photo-205961.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/376464/pexels-photo-376464.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
'waffles': [
'https://images.pexels.com/photos/1192030/pexels-photo-1192030.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/4825701/pexels-photo-4825701.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/15126268/pexels-photo-15126268.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Additional Categories for more variety
'apple_pie': [
'https://images.pexels.com/photos/7772/food-eating-pie-apple.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/5639908/pexels-photo-5639908.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop',
'https://images.pexels.com/photos/6210748/pexels-photo-6210748.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
],
# Default fallback - Multiple high-quality variations
'default': [
'https://images.pexels.com/photos/376464/pexels-photo-376464.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Pancakes
'https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Burger
'https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Pizza
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Salad
'https://images.pexels.com/photos/1460872/pexels-photo-1460872.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop', # Ramen
'https://images.pexels.com/photos/2456435/pexels-photo-2456435.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop' # Tacos
]
}
def get_smart_food_image(food_name, recipe_index=0):
"""Get smart food-category matching image"""
try:
# Direct food name match first
if food_name in food_images:
images = food_images[food_name]
return images[recipe_index % len(images)]
# Smart category matching for similar foods
food_lower = food_name.lower()
# Fish and seafood category
if any(fish in food_lower for fish in ['salmon', 'fish', 'tuna', 'cod', 'halibut', 'sea']):
images = food_images['salmon']
return images[recipe_index % len(images)]
# Steak and beef category
if any(meat in food_lower for meat in ['steak', 'beef', 'filet', 'prime_rib', 'carpaccio']):
images = food_images['steak']
return images[recipe_index % len(images)]
# Chicken category
if any(chicken in food_lower for chicken in ['chicken', 'poultry']):
if 'wing' in food_lower:
images = food_images['chicken_wings']
elif 'curry' in food_lower:
images = food_images['chicken_curry']
else:
images = food_images['chicken_wings'] # Default to wings for variety
return images[recipe_index % len(images)]
# Pasta category
if any(pasta in food_lower for pasta in ['pasta', 'spaghetti', 'lasagna', 'ravioli', 'linguine']):
if 'bolognese' in food_lower:
images = food_images['spaghetti_bolognese']
elif 'carbonara' in food_lower:
images = food_images['spaghetti_carbonara']
else:
images = food_images['pasta']
return images[recipe_index % len(images)]
# Pizza category
if 'pizza' in food_lower:
images = food_images['pizza']
return images[recipe_index % len(images)]
# Burger category
if any(burger in food_lower for burger in ['burger', 'hamburger']):
images = food_images['hamburger']
return images[recipe_index % len(images)]
# Salad category
if 'salad' in food_lower:
if 'caesar' in food_lower:
images = food_images['caesar_salad']
elif 'greek' in food_lower:
images = food_images['greek_salad']
else:
images = food_images['caesar_salad'] # Default to Caesar
return images[recipe_index % len(images)]
# Cake and dessert category
if any(dessert in food_lower for dessert in ['cake', 'dessert', 'sweet']):
if 'chocolate' in food_lower:
images = food_images['chocolate_cake']
elif 'cheese' in food_lower:
images = food_images['cheesecake']
else:
images = food_images['chocolate_cake']
return images[recipe_index % len(images)]
# Ice cream category
if any(ice in food_lower for ice in ['ice_cream', 'ice cream', 'frozen', 'gelato', 'sorbet', 'frozen_yogurt', 'yogurt']):
if 'yogurt' in food_lower or 'frozen_yogurt' in food_lower:
images = food_images['frozen_yogurt']
else:
images = food_images['ice_cream']
return images[recipe_index % len(images)]
# Breakfast category
if any(breakfast in food_lower for breakfast in ['pancake', 'waffle', 'breakfast']):
if 'waffle' in food_lower:
images = food_images['waffles']
else:
images = food_images['pancakes']
return images[recipe_index % len(images)]
# Donut category
if any(donut in food_lower for donut in ['donut', 'doughnut']):
images = food_images['donuts']
return images[recipe_index % len(images)]
# Hot dog category
if any(hotdog in food_lower for hotdog in ['hot_dog', 'hotdog', 'hot dog']):
images = food_images['hot_dog']
return images[recipe_index % len(images)]
# Pie category
if any(pie in food_lower for pie in ['pie', 'apple_pie']):
if 'apple' in food_lower:
images = food_images['apple_pie']
else:
images = food_images['apple_pie'] # Default to apple pie
return images[recipe_index % len(images)]
# Asian food category
if any(asian in food_lower for asian in ['ramen', 'pad_thai', 'curry', 'pho', 'asian', 'vietnamese', 'noodle_soup']):
if 'pho' in food_lower or 'vietnamese' in food_lower:
images = food_images['pho']
elif 'ramen' in food_lower:
images = food_images['ramen']
elif 'pad' in food_lower or 'thai' in food_lower:
images = food_images['pad_thai']
elif 'curry' in food_lower:
images = food_images['chicken_curry']
else:
images = food_images['pho'] # Default to pho for Asian noodle soups
return images[recipe_index % len(images)]
# Sandwich category
if any(sandwich in food_lower for sandwich in ['sandwich', 'club']):
images = food_images['sandwich']
return images[recipe_index % len(images)]
# Fries and snacks
if any(snack in food_lower for snack in ['fries', 'french_fries', 'chips']):
images = food_images['french_fries']
return images[recipe_index % len(images)]
# Tacos
if 'taco' in food_lower:
images = food_images['tacos']
return images[recipe_index % len(images)]
# Default fallback
images = food_images['default']
return images[recipe_index % len(images)]
except Exception:
# Ultimate fallback - SAFE VERIFIED PANCAKE IMAGE
return 'https://images.pexels.com/photos/2260/food-healthy-morning-cereals.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
# Select style based on recipe index for variety
style_name, style_desc = styles[recipe_index % len(styles)]
food_display = food_name.replace('_', ' ').title()
# Generate recipe content
recipe_content = generate_styled_recipe(food_display, style_name)
# Get food-category specific image
image_url = get_smart_food_image(food_name, recipe_index)
# Create single recipe
recipe = {
'title': f"{style_name} {food_display}",
'style': style_name,
'content': recipe_content,
'image_url': image_url,
'prep_time': f"{15 + (recipe_index * 5)}-{25 + (recipe_index * 5)} minutes",
'difficulty': ["Easy", "Medium", "Advanced"][recipe_index % 3]
}
return recipe
def generate_styled_recipe(food_display, style):
"""Generate styled recipe content based on cooking style"""
if style == "Healthy":
return f"""**🌱 Nutritious {food_display} Recipe**
**Ingredients:**
• Organic {food_display.lower()} (main ingredient)
• Fresh colorful vegetables
• Extra virgin olive oil
• Herbs and spices (anti-inflammatory)
• Leafy greens for extra nutrition
**Healthy Cooking Method:**
1. Use minimal healthy oils (olive, avocado)
2. Steam or grill instead of frying
3. Add plenty of colorful vegetables
4. Season with herbs instead of excess salt
5. Serve with nutrient-dense sides
**Health Benefits:** High in antioxidants, vitamins, and healthy fats
**Calories:** ~350 per serving | **Prep Time:** 20 minutes"""
elif style == "Keto":
return f"""**🥑 Keto-Friendly {food_display} Recipe**
**Low-Carb Ingredients:**
• {food_display.lower()} (main protein/fat source)
• Avocado oil or coconut oil
• Low-carb vegetables (spinach, broccoli, cauliflower)
• Full-fat cheese
• Nuts and seeds for healthy fats
**Keto Cooking Method:**
1. Focus on high-fat, low-carb ingredients
2. Use butter or coconut oil generously
3. Replace starches with cauliflower or zucchini
4. Add extra cheese and avocado
5. Keep total carbs under 5g per serving
**Macros:** 75% fat, 20% protein, 5% carbs
**Net Carbs:** <5g per serving | **Prep Time:** 15 minutes"""
elif style == "Vegan":
return f"""**🌿 Plant-Based {food_display} Recipe**
**Vegan Ingredients:**
• Plant-based protein (if replacing meat)
• Nutritional yeast for cheesy flavor
• Coconut milk or cashew cream
• Fresh vegetables and legumes
• Tahini or nut butter for richness
**Plant-Based Cooking:**
1. Use plant-based alternatives for dairy/meat
2. Add umami with mushrooms and soy sauce
3. Create creaminess with nuts or coconut
4. Boost protein with legumes or tofu
5. Finish with fresh herbs and lemon
**Benefits:** High in fiber, antioxidants, and plant protein
**Protein:** 20g per serving | **Prep Time:** 25 minutes"""
elif style == "Mediterranean":
return f"""**🫒 Mediterranean {food_display} Recipe**
**Mediterranean Ingredients:**
• {food_display.lower()} with herbs
• Extra virgin olive oil (generous amount)
• Fresh tomatoes, olives, and herbs
• Feta cheese or Greek yogurt
• Lemon juice and garlic
**Mediterranean Method:**
1. Use olive oil as primary cooking fat
2. Add fresh Mediterranean herbs (oregano, basil)
3. Include tomatoes, olives, and citrus
4. Finish with fresh herbs and good olive oil
5. Serve with whole grains or vegetables
**Health Benefits:** Heart-healthy fats and anti-inflammatory compounds
**Style:** Traditional Mediterranean | **Prep Time:** 20 minutes"""
elif style == "Quick & Easy":
return f"""**⚡ Quick {food_display} Recipe**
**Simple Ingredients:**
• {food_display.lower()} (main ingredient)
• Pre-cut vegetables or frozen mix
• Simple seasonings or sauce packet
• Quick-cooking sides
• Minimal prep ingredients
**Speed Cooking:**
1. Use high heat for quick cooking
2. Prep everything before starting
3. Cook in one pan when possible
4. Use pre-made sauces or seasonings
5. Serve immediately while hot
**Perfect For:** Busy weeknights and quick meals
**Total Time:** Under 30 minutes | **Difficulty:** Beginner"""
else: # Gourmet
return f"""**⭐ Gourmet {food_display} Recipe**
**Premium Ingredients:**
• High-quality {food_display.lower()}
• Artisanal ingredients and exotic spices
• Fine wines or spirits for cooking
• Specialty oils and vinegars
• Garnishes and microgreens
**Gourmet Technique:**
1. Use advanced cooking methods
2. Layer flavors with precision
3. Present with artistic plating
4. Pair with complementary wines
5. Focus on perfect execution
**Restaurant Quality:** Michelin-star techniques at home
**Difficulty:** Advanced | **Prep Time:** 45+ minutes"""
# Main App
def main():
# Header
st.markdown("""
<div class="main-header">
<h1>🍽️ EatSmart Pro</h1>
<p>AI-Powered Food Analysis & Healthy Living Assistant</p>
</div>
""", unsafe_allow_html=True)
# Mobile-friendly sidebar message
st.markdown("""
<div style="background: linear-gradient(135deg, #28a745 0%, #8fd3f4 100%); padding: 2rem; border-radius: 15px; text-align: center; margin-bottom: 2rem;">
<h2 style="color: white; font-weight: bold; margin-bottom: 0.5rem;">
<span style='font-size:1.5rem;'>📸</span> Upload Food Image
</h2>
<div style="color: #f8f9fa; font-size: 1rem; margin-bottom: 0.5rem;">Take a photo or upload from gallery</div>
<div style="color: #f8f9fa; font-size: 0.85rem; margin-top: 0.3rem; text-align: center; max-width: 500px; margin-left: auto; margin-right: auto;">
<strong>📸 Photo Tips for Best Results:</strong><br>
<div style="display: inline-block; text-align: left; margin-top: 0.2rem;">
• <strong>Focus on one food item</strong> - Single dishes work better<br>
• <strong>Get close and center the food</strong> - Fill frame, avoid clutter<br>
• <strong>Use good lighting</strong> - Natural or bright indoor light
</div>
</div>
</div>
""", unsafe_allow_html=True)
# Mobile user alert message at the top
st.markdown('''
<div style="background: linear-gradient(90deg, #28a745 0%, #20c997 100%); color: white; padding: 8px 0; text-align: center; font-size: 1rem; font-weight: 600; border-radius: 0 0 8px 8px; margin-bottom: 0.5rem;">
📢 <b>Mobile Users:</b> Click the <b>arrow (☰)</b> in the top-left corner to access Personal Settings for allergen alerts and dietary preferences!
</div>
''', unsafe_allow_html=True)
# Load resources with error handling
try:
model, model_info = load_model_resources()
health_data, allergen_data = load_data()
if not model:
st.error("❌ Failed to load AI model. Please check the setup.")
return
# Health and allergen data are now loaded with defaults silently
except Exception as e:
st.error(f"❌ Error loading app resources: {str(e)}")
return
# Model status - show as info instead of warning to reduce clutter
if model_info["name"] == "ConvNeXt Large":
st.success(f"🎯 Using {model_info['name']} model ({model_info['params']} parameters, {model_info['accuracy']} accuracy)")
else:
st.info(f"🤖 Using {model_info['name']} model ({model_info['params']} parameters, {model_info['accuracy']} accuracy)")
# Sidebar with user preferences
with st.sidebar:
st.markdown("### ⚙️ Personal Settings")
# Allergen preferences with checkboxes
st.markdown("### ⚠️ Allergen Alerts")
allergen_options = [
"Peanuts", "Tree nuts", "Dairy", "Eggs", "Soy",
"Wheat/Gluten", "Fish", "Shellfish", "Sesame"
]
for allergen in allergen_options:
if st.checkbox(f"🚫 {allergen}", key=f"allergen_{allergen}", value=allergen in st.session_state.user_allergens):
if allergen not in st.session_state.user_allergens:
st.session_state.user_allergens.append(allergen)
else:
if allergen in st.session_state.user_allergens:
st.session_state.user_allergens.remove(allergen)
# Dietary preferences with checkboxes
st.markdown("### 🥗 Dietary Preferences")
dietary_options = [
"Vegetarian", "Vegan", "Keto", "Low-carb",
"Mediterranean", "Paleo", "Low-sodium"
]
for diet in dietary_options:
if st.checkbox(f"🌱 {diet}", key=f"diet_{diet}", value=diet in st.session_state.dietary_preferences):
if diet not in st.session_state.dietary_preferences:
st.session_state.dietary_preferences.append(diet)
else:
if diet in st.session_state.dietary_preferences:
st.session_state.dietary_preferences.remove(diet)
# Trans Fat Alert Preference
st.markdown("### ⚠️ Trans Fat Alerts")
st.session_state.avoid_trans_fat = st.checkbox(
"🚨 Alert me about trans fat risks",
value=st.session_state.avoid_trans_fat,
help="Get warnings when food may contain trans fats"
)
# App features
st.markdown("### 🌟 App Features")
st.markdown("""
✅ **High-Accuracy AI Recognition**
✅ **Comprehensive Nutrition Analysis**
✅ **Personalized Allergen Alerts**
✅ **AI-Generated Healthy Recipes**
✅ **Trans Fat Detection**
✅ **Dietary Preference Matching**
✅ **Health Score Assessment**
""")
# Main content - Two column layout
col1, col2 = st.columns([1, 1])
with col1:
# --- Streamlined Demo Examples Section (moved above upload) ---
# Define reliable example URLs using Pexels (more stable than Unsplash)
example_foods = {
"pizza": {
"url": "https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Pizza Example",
"emoji": "🍕"
},
"cake": {
"url": "https://images.pexels.com/photos/1854652/pexels-photo-1854652.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Cake Example",
"emoji": "🍰"
},
"burger": {
"url": "https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Burger Example",
"emoji": "🍔"
},
"surprise": [
{"url": "https://images.pexels.com/photos/365459/pexels-photo-365459.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Tasty Surprise"},
{"url": "https://images.pexels.com/photos/1639557/pexels-photo-1639557.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Burger Surprise"},
{"url": "https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Pasta Surprise"},
{"url": "https://images.pexels.com/photos/365459/pexels-photo-365459.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Pizza Surprise"},
{"url": "https://images.pexels.com/photos/2260/food-healthy-morning-cereals.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Pancakes Surprise"},
{"url": "https://images.pexels.com/photos/2456435/pexels-photo-2456435.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Dish Surprise Meal"},
{"url": "https://images.pexels.com/photos/3026808/pexels-photo-3026808.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Dish Surprise"},
{"url": "https://images.pexels.com/photos/461382/pexels-photo-461382.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Sandwich Surprise"},
{"url": "https://images.pexels.com/photos/115740/pexels-photo-115740.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Fries Surprise"},
{"url": "https://images.pexels.com/photos/262959/pexels-photo-262959.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Food Surprise"},
{"url": "https://images.pexels.com/photos/2874979/pexels-photo-2874979.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Delicious Surprise"},
{"url": "https://images.pexels.com/photos/291528/pexels-photo-291528.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Dessert Surprise"},
{"url": "https://images.pexels.com/photos/769289/pexels-photo-769289.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Dish Surprise"},
{"url": "https://images.pexels.com/photos/461430/pexels-photo-461430.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Delicious Surprise"},
{"url": "https://images.pexels.com/photos/2233348/pexels-photo-2233348.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Tasty Surprise"},
{"url": "https://images.pexels.com/photos/1639562/pexels-photo-1639562.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop", "name": "Gourmet Surprise"}
]
}
# Purple header box
st.markdown("""
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white; padding: 16px; border-radius: 12px; text-align: center; margin: 0 0 10px 0;">
<h3 style="margin: 0 0 8px 0; font-size: 1.3em;">🎯 Test AI Accuracy</h3>
<p style="margin: 0 0 12px 0; font-size: 0.9em; opacity: 0.9;">Try our demo examples to see how accurate our AI is!</p>
</div>
""", unsafe_allow_html=True)
# Custom CSS for primary button
st.markdown("""
<style>
div[data-testid="stButton"] > button[kind="primary"] {
background: linear-gradient(135deg, #28a745 0%, #20c997 100%) !important;
border: none !important;
border-radius: 10px !important;
height: 50px !important;
font-weight: bold !important;
font-size: 1.05em !important;
color: white !important;
box-shadow: 0 4px 8px rgba(0,0,0,0.2) !important;
transition: all 0.25s ease !important;
}
div[data-testid="stButton"] > button[kind="primary"]:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 12px rgba(0,0,0,0.3) !important;
}
</style>
""", unsafe_allow_html=True)
# Initialize counter for demo attempts
if 'random_sample_count' not in st.session_state:
st.session_state.random_sample_count = 0
if st.button("🎲 Try Random Sample Food", key="demo_random_top", type="primary", use_container_width=True):
filtered_surprise = [ex for ex in example_foods['surprise'] if 'pork' not in ex['name'].lower()]
random_example = random.choice(filtered_surprise)
st.session_state.random_sample_count += 1 # Increment BEFORE rerun
success = load_example_image(random_example['url'], random_example['name'])
if success:
st.session_state.example_just_loaded = random_example['name']
# Small hint below button
st.markdown("""
<div style="text-align: center; margin-top: 6px; font-size: 0.78em; color: #666;">
👆 <strong>Start here!</strong> See instant AI food analysis
</div>
""", unsafe_allow_html=True)
# Conditional file uploader (locked until 1 demo tried)
show_uploader = st.session_state.get('random_sample_count', 0) >= 1
# Always show the uploader after 1 demo, even if an image is uploaded
uploader_key = f"file_uploader_{st.session_state.get('file_uploader_counter', 0)}_{st.session_state.random_sample_count}"
if show_uploader:
st.markdown("""
<div style='background:#eaffea; border:1px solid #b2f2b2; padding:0.7rem; border-radius:4px; margin-bottom:0.5rem;'>
<div style='display:flex;align-items:center;margin-bottom:0.3em;'>
<span style='font-size:1.3em;margin-right:8px;'>👇</span>
<span style='font-weight:500;font-size:1.05em;color:#218838;'>Browse Files is now unlocked! Upload your own food photo or keep exploring more samples.</span>
</div>
""", unsafe_allow_html=True)
uploaded_file = st.file_uploader(
"Browse Files (unlocked)",
type=['jpg', 'jpeg', 'png', 'webp', 'heic'],
help="🎯 For best results: Focus on one food item, get close to fill the frame, use good lighting",
label_visibility="collapsed",
key=uploader_key,
accept_multiple_files=False
)
st.markdown("""</div>""", unsafe_allow_html=True)
# Mobile-friendly file upload handling
if uploaded_file is not None:
# Read file bytes immediately
try:
file_bytes = uploaded_file.getvalue()
# Get unique identifier for this file
file_id = f"{uploaded_file.name}_{uploaded_file.size}"
# Only process if it's a NEW file (different from last one)
# OR if we haven't processed anything yet
last_file_id = st.session_state.get('last_uploaded_file_id')
if last_file_id != file_id:
# This is a new file, process it
# Convert HEIC to JPEG if needed (for iPhone users)
if uploaded_file.name.lower().endswith('.heic'):
try:
from PIL import Image
import io
# Try to open HEIC
img = Image.open(io.BytesIO(file_bytes))
# Convert to JPEG
jpeg_buffer = io.BytesIO()
img.convert('RGB').save(jpeg_buffer, format='JPEG', quality=95)
file_bytes = jpeg_buffer.getvalue()
except Exception as e:
st.warning(f"⚠️ Could not convert HEIC image. Try taking photo as JPEG. Error: {str(e)}")
file_bytes = None
if file_bytes:
# Clear previous demo state
st.session_state.example_just_loaded = None
# Update image buffer
st.session_state.image_buffer = file_bytes
# Save this file ID to prevent reprocessing
st.session_state.last_uploaded_file_id = file_id
# Clear previous processing flag to force re-analysis
if 'last_processed_buffer' in st.session_state:
del st.session_state.last_processed_buffer
# Mark that we just uploaded (to show success message once)
st.session_state.file_just_uploaded = True
except Exception as e:
st.error(f"❌ Failed to upload image: {str(e)}")
st.info("💡 Try: 1) Use a smaller image, 2) Take photo as JPEG instead of HEIC")
else:
st.markdown(f"""
<div style='border:1px solid #ced4da; padding:0.7rem; border-radius:4px; background:#f8f9fa; opacity:0.7; cursor:not-allowed; display: flex; align-items: center;'>
<span style='font-size:1.2em; margin-right:8px;'>🔒</span>
<span style='color:#fff; background:#ff1744; border-radius:4px; padding:2px 8px; font-weight:bold; margin-right:8px;'>Browse Files (locked until at least 1 demo tried)</span>
<span style='color:#6c757d;'>Try a demo example above to unlock</span>
</div>
""", unsafe_allow_html=True)
uploaded_file = None
# Show success message once after file upload
if st.session_state.get('file_just_uploaded'):
st.success("✅ Image uploaded successfully! Analyzing...")
del st.session_state.file_just_uploaded
# Display uploaded image
if st.session_state.image_buffer is not None:
# Fix Streamlit compatibility issue
try:
st.image(st.session_state.image_buffer, caption="Uploaded Image", use_container_width=True)
except TypeError:
# Fallback for older Streamlit versions
st.image(st.session_state.image_buffer, caption="Uploaded Image", use_column_width=True)
# Scroll disabled - was causing trembling on HF Spaces
# Streamlit naturally shows the results without needing scroll
# Compact Demo Examples Section with Integrated Button
st.markdown("---")
# Define reliable example URLs using Pexels (more stable than Unsplash)
example_foods = {
"pizza": {
"url": "https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Pizza Example",
"emoji": "🍕"
},
"cake": {
"url": "https://images.pexels.com/photos/1854652/pexels-photo-1854652.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Cake Example",
"emoji": "🍰"
},
"burger": {
"url": "https://images.pexels.com/photos/70497/pexels-photo-70497.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Burger Example",
"emoji": "🍔"
},
"surprise": [
{
"url": "https://images.pexels.com/photos/365459/pexels-photo-365459.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Pizza Surprise"
},
{
"url": "https://images.pexels.com/photos/1639557/pexels-photo-1639557.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Burger Surprise"
},
{
"url": "https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Pasta Surprise"
},
{
"url": "https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Salad Surprise"
},
{
"url": "https://images.pexels.com/photos/2260/food-healthy-morning-cereals.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Pancakes Surprise"
},
{
"url": "https://images.pexels.com/photos/1460872/pexels-photo-1460872.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Ramen Surprise"
},
{
"url": "https://images.pexels.com/photos/2456435/pexels-photo-2456435.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Tacos Surprise"
},
{
"url": "https://images.pexels.com/photos/361184/asparagus-steak-veal-steak-veal-361184.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Salmon Surprise"
},
{
"url": "https://images.pexels.com/photos/3631/summer-dessert-sweet-ice-cream.jpg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Ice Cream Surprise"
},
{
"url": "https://images.pexels.com/photos/115740/pexels-photo-115740.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Fries Surprise"
},
{
"url": "https://images.pexels.com/photos/262959/pexels-photo-262959.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Food Surprise"
},
{
"url": "https://images.pexels.com/photos/2874979/pexels-photo-2874979.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Delicious Surprise"
},
{
"url": "https://images.pexels.com/photos/291528/pexels-photo-291528.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Meal Surprise"
},
{
"url": "https://images.pexels.com/photos/769289/pexels-photo-769289.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Dish Surprise"
},
{
"url": "https://images.pexels.com/photos/764925/pexels-photo-764925.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Cuisine Surprise"
},
{
"url": "https://images.pexels.com/photos/2233348/pexels-photo-2233348.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Tasty Surprise"
},
{
"url": "https://images.pexels.com/photos/1639562/pexels-photo-1639562.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop",
"name": "Gourmet Surprise"
}
]
}
with col2:
# Analysis section
st.markdown("""
<div class="analysis-section">
<h3 style="color: white; margin: 0;">🧠 Smart Analysis & Insights</h3>
<p style="color: #f0f0f0; margin: 5px 0; font-size: 0.9em;">AI-powered nutrition analysis and health insights</p>
</div>
""", unsafe_allow_html=True)
# Auto-analyze any image in session state (works for both uploads and examples)
if st.session_state.get('image_buffer') is not None:
# Auto-analyze immediately - optimized with change detection
if st.session_state.image_buffer != st.session_state.get('last_processed_buffer'):
st.session_state.last_processed_buffer = st.session_state.image_buffer
# Clear previous results AND recipe data
if 'prediction_result' in st.session_state:
del st.session_state.prediction_result
# Clear all recipe-related session state when new image is analyzed
keys_to_remove = [key for key in st.session_state.keys() if key.startswith('recipe_')]
for key in keys_to_remove:
del st.session_state[key]
try:
with st.spinner("🤖 AI is analyzing your food..."):
# Transform image for model input
image_tensor = transform_image(st.session_state.image_buffer)
# Ensure tensor is on CPU and properly converted
if image_tensor.device.type != 'cpu':
image_tensor = image_tensor.cpu()
# Get predictions with optimized model inference
predictions = get_prediction(image_tensor, model, model_info)
st.session_state.prediction_result = predictions
# Force garbage collection for memory efficiency
import gc
gc.collect()
# No need for aggressive scroll - Streamlit auto-scrolls
except Exception as e:
st.error(f"❌ Analysis failed: {str(e)}")
st.session_state.prediction_result = None
# Show prominent notification if example was just loaded
if st.session_state.get('example_just_loaded'):
st.markdown(f"""
<div style="background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
color: white; padding: 15px; border-radius: 10px; text-align: center; margin: 10px 0;
border: 3px solid #fff; box-shadow: 0 6px 20px rgba(40, 167, 69, 0.4);">
<h4 style="margin: 0;">🎉 {st.session_state.example_just_loaded} Demo Loaded!</h4>
<p style="margin: 5px 0; font-size: 0.9em;">AI analysis complete! Check results below ⬇️</p>
</div>
""", unsafe_allow_html=True)
# Clear the flag after showing
del st.session_state.example_just_loaded
if st.session_state.get('prediction_result') and st.session_state.prediction_result:
predictions = st.session_state.prediction_result
# Validate prediction result structure
if not predictions or len(predictions) == 0:
st.error("❌ Invalid prediction result. Please try again.")
return
# Top prediction
top_prediction = predictions[0]
food_name = top_prediction.get('class', top_prediction.get('food', 'unknown'))
confidence = top_prediction.get('confidence', 0.0)
# Check confidence threshold - professional handling of low confidence
if confidence <= 60.0:
st.markdown("""
<div style="background: linear-gradient(135deg, #f39c12 0%, #e67e22 100%);
color: white; padding: 20px; border-radius: 10px; text-align: center; margin: 20px 0;">
<h3>🤔 Unable to Identify Food</h3>
<p style="margin: 10px 0;">We couldn't confidently identify food in this image.</p>
<p style="margin: 5px 0;"><strong>Possible reasons:</strong></p>
<p style="margin: 5px 0;">• Image may not contain food items</p>
<p style="margin: 5px 0;">• Photo quality or lighting needs improvement</p>
<p style="margin: 5px 0;">• Food item is not clearly visible</p>
<p style="margin: 10px 0;"><strong>💡 Please try:</strong> Upload a clear, well-lit photo of food</p>
</div>
""", unsafe_allow_html=True)
else:
# Display main result for confident predictions with BOLD formatting
st.markdown(f"### 🎯 **Detected Food: {food_name.replace('_', ' ').title()}**")
st.markdown(f"### **Confidence: {confidence:.1f}%**")
# No aggressive scrolling needed - let Streamlit handle it naturally
# Alternative predictions - HIDDEN FOR NOW (can be enabled later)
# if len(predictions) > 1:
# alternatives = [f"{pred.get('class', pred.get('food', 'unknown')).replace('_', ' ').title()} ({pred.get('confidence', 0.0):.1f}%)"
# for pred in predictions[1:3]]
# st.info(f"🤔 Could also be: {', '.join(alternatives)}")
# Tabbed interface for detailed analysis
tab1, tab2, tab3, tab4 = st.tabs(["🍳 Recipe", "⚠️ Allergens", "📊 Nutrition", "📋 Ingredients"])
with tab1:
st.markdown(f"### 🍳 AI Recipe Generator for {food_name.replace('_', ' ').title()}")
# Initialize single recipe session state for CURRENT food only
current_recipe_key = f"current_recipe_{food_name}"
recipe_count_key = f"recipe_count_{food_name}"
# Auto-generate first recipe when food is first detected
if current_recipe_key not in st.session_state:
with st.spinner("👨🍳 AI Chef is creating your first recipe..."):
# Generate first recipe automatically
first_recipe = generate_single_recipe(food_name, 0)
st.session_state[current_recipe_key] = first_recipe
st.session_state[recipe_count_key] = 1
# Success message removed - was showing on every rerun causing flicker
# Display current recipe
if current_recipe_key in st.session_state:
recipe_data = st.session_state[current_recipe_key]
# Recipe header - image disabled to prevent HF trembling
# External images were causing continuous reloading on HF Spaces
st.markdown("---")
# Display recipe info directly (no columns to avoid trembling)
st.markdown(f"### {recipe_data['title']}")
st.markdown(f"**Style:** {recipe_data['style']}")
st.markdown(f"**Prep Time:** {recipe_data['prep_time']}")
st.markdown(f"**Difficulty:** {recipe_data['difficulty']}")
# Recipe content with beautiful styling
st.markdown(f"""
<div class="recipe-section">
{recipe_data['content']}
</div>
""", unsafe_allow_html=True)
# Single "Next Recipe" button that replaces current recipe
if st.button("🔄 Generate Next Recipe", type="primary", key=f"next_recipe_{food_name}"):
with st.spinner("👨🍳 Creating next delicious recipe..."):
# Generate new recipe that replaces current one
next_count = st.session_state.get(recipe_count_key, 1)
new_recipe = generate_single_recipe(food_name, next_count)
st.session_state[current_recipe_key] = new_recipe
st.session_state[recipe_count_key] = next_count + 1
st.success("✅ New recipe ready!")
# Force immediate refresh to show new recipe
st.rerun()
# Hidden buttons (can be re-enabled later by uncommenting)
# if st.button("🤖 Generate 3 Healthy Recipes", type="primary"):
# pass # Old multi-recipe logic preserved
# if st.button("🔄 Generate 3 More Recipes"):
# pass # Old multi-recipe logic preserved
# if st.button("🗑️ Clear All Recipes"):
# pass # Old clear logic preserved
# Show dietary preferences if any
if st.session_state.dietary_preferences or st.session_state.user_allergens:
st.markdown("**🎯 Your Preferences:**")
if st.session_state.dietary_preferences:
st.markdown(f"• Diet: {', '.join(st.session_state.dietary_preferences)}")
if st.session_state.user_allergens:
st.markdown(f"• Avoiding: {', '.join(st.session_state.user_allergens)}")
with tab2:
st.markdown("### ⚠️ Allergen Analysis")
allergens = get_allergen_info(food_name, allergen_data)
if allergens:
# Check for user's allergens
user_allergen_matches = [a for a in allergens if a in st.session_state.user_allergens]
if user_allergen_matches:
st.markdown(f"""
<div class="allergen-alert">
<h4>🚨 CRITICAL ALLERGEN ALERT!</h4>
<p>This dish contains <strong>{', '.join(user_allergen_matches)}</strong> which you've marked as allergens to avoid!</p>
</div>
""", unsafe_allow_html=True)
# Display all allergens
st.markdown("**Allergens detected in this food:**")
for allergen in allergens:
if allergen in st.session_state.user_allergens:
st.error(f"🚨 {allergen}")
else:
st.info(f"ℹ️ Contains {allergen}")
else:
st.success("✅ No common allergens detected in this food!")
# Trans fat analysis
if st.session_state.avoid_trans_fat:
st.markdown("### 🧪 Trans Fat Analysis")
trans_fat_foods = ['french_fries', 'donuts', 'fried_calamari', 'onion_rings']
if food_name in trans_fat_foods:
st.warning("⚠️ This food may contain trans fats from frying oils. Consider healthier preparation methods.")
else:
st.success("✅ Low risk of trans fats in this food.")
with tab3:
st.markdown("### 📊 Nutritional Information")
# Get nutrition data
nutrition = get_nutrition_info(food_name, health_data)
health_score = get_health_score(food_name, health_data)
# Nutrition metrics
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{nutrition.get('calories', 'N/A')}</div>
<div class="metric-label">Calories</div>
</div>
""", unsafe_allow_html=True)
with col2:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{nutrition.get('protein', 'N/A')}g</div>
<div class="metric-label">Protein</div>
</div>
""", unsafe_allow_html=True)
with col3:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{nutrition.get('carbs', 'N/A')}g</div>
<div class="metric-label">Carbs</div>
</div>
""", unsafe_allow_html=True)
with col4:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{nutrition.get('fat', 'N/A')}g</div>
<div class="metric-label">Fat</div>
</div>
""", unsafe_allow_html=True)
# Health score
score = health_score.get('score', 75)
explanation = health_score.get('explanation', 'Good')
if score >= 80:
score_class = "health-score-excellent"
elif score >= 60:
score_class = "health-score-good"
else:
score_class = "health-score-poor"
st.markdown(f"""
<div class="{score_class}">
<h4>🏥 Health Score: {score}/100 ({explanation})</h4>
</div>
""", unsafe_allow_html=True)
with tab4:
st.markdown("### 📋 Ingredient Analysis")
ingredients = get_common_ingredients(food_name)
st.markdown(f"""
<div class="info-card">
<h4>🥄 Common Ingredients:</h4>
<p>{ingredients}</p>
</div>
""", unsafe_allow_html=True)
# Health benefits
nutrition_info = health_data.get("nutrition_info", {})
food_info = nutrition_info.get(food_name.replace('_', ' ').title()) or nutrition_info.get(food_name.lower())
if food_info and food_info.get("benefits"):
st.markdown("### 💚 Health Benefits")
for benefit in food_info["benefits"]:
st.markdown(f"• {benefit}")
else:
st.info("👆 Upload a food image or try our demo examples to get started with AI-powered analysis!")
def load_example_image(image_url, image_name):
"""Load an example image from URL into session state for analysis"""
try:
# Validate URL first
if not image_url or not isinstance(image_url, str):
st.error("❌ Invalid image URL provided")
return False
# Show loading message
with st.spinner(f"🔄 Loading {image_name}..."):
# FIRST: Clear any existing file uploader state to avoid conflicts
if 'uploaded_file_key' in st.session_state:
del st.session_state.uploaded_file_key
st.session_state.file_uploader_counter = st.session_state.get('file_uploader_counter', 0) + 1
if 'image_buffer' in st.session_state:
del st.session_state.image_buffer
if 'prediction_result' in st.session_state:
del st.session_state.prediction_result
if 'last_processed_buffer' in st.session_state:
del st.session_state.last_processed_buffer
keys_to_remove = [key for key in st.session_state.keys() if key.startswith('recipe')]
for key in keys_to_remove:
del st.session_state[key]
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
}
try:
response = requests.get(image_url, headers=headers, timeout=15, stream=True)
if response.status_code == 200:
img_bytes = response.content
from PIL import Image
img = Image.open(io.BytesIO(img_bytes))
img.verify()
st.session_state.image_buffer = img_bytes
st.success(f"✅ {image_name} loaded successfully!")
if 'last_processed_buffer' in st.session_state:
del st.session_state['last_processed_buffer']
st.session_state.example_just_loaded = image_name
st.rerun()
return True
else:
raise Exception(f"HTTP Status: {response.status_code}")
except Exception as e:
# Fallback to pizza image
st.warning(f"⚠️ Failed to load {image_name} (reason: {str(e)}). Loading fallback pizza image instead.")
pizza_url = 'https://images.pexels.com/photos/315755/pexels-photo-315755.jpeg?auto=compress&cs=tinysrgb&w=400&h=300&fit=crop'
try:
pizza_response = requests.get(pizza_url, headers=headers, timeout=15, stream=True)
if pizza_response.status_code == 200:
pizza_bytes = pizza_response.content
from PIL import Image
pizza_img = Image.open(io.BytesIO(pizza_bytes))
pizza_img.verify()
st.session_state.image_buffer = pizza_bytes
st.success(f"🍕 Fallback pizza image loaded!")
if 'last_processed_buffer' in st.session_state:
del st.session_state['last_processed_buffer']
st.session_state.example_just_loaded = 'Pizza Fallback'
st.rerun()
return True
else:
st.error(f"❌ Failed to load fallback pizza image. HTTP Status: {pizza_response.status_code}")
return False
except Exception as pizza_e:
st.error(f"❌ Failed to load fallback pizza image: {str(pizza_e)}")
return False
except requests.exceptions.Timeout:
st.error(f"⏱️ Timeout loading {image_name}. Please try again.")
return False
except requests.exceptions.ConnectionError:
st.error(f"🌐 Connection error loading {image_name}. Check your internet connection.")
return False
except Exception as e:
st.error(f"❌ Unexpected error loading {image_name}: {str(e)}")
return False
if __name__ == "__main__":
main() |