Spaces:
Sleeping
Sleeping
File size: 206,880 Bytes
b33f3aa |
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 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 |
# derm-ai-backend
Generated on: C:\Work\derm-ai\derm-ai-backend
## Project Structure
```
derm-ai-backend/
├── app
│ ├── config
│ │ ├── __init__.py
│ │ └── config.py
│ ├── database
│ │ ├── __init__.py
│ │ ├── database_query.py
│ │ └── db.py
│ ├── middleware
│ │ └── auth.py
│ ├── routers
│ │ ├── admin.py
│ │ ├── agent_chat.py
│ │ ├── auth.py
│ │ ├── chat.py
│ │ ├── chat_session.py
│ │ ├── language.py
│ │ ├── location.py
│ │ ├── preferences.py
│ │ ├── profile.py
│ │ └── questionnaire.py
│ ├── services
│ │ ├── RAG_evaluation.py
│ │ ├── __init__.py
│ │ ├── agentic_prompt.py
│ │ ├── chathistory.py
│ │ ├── environmental_condition.py
│ │ ├── google_agent_service.py
│ │ ├── image_classification_vit.py
│ │ ├── llm_model.py
│ │ ├── prompts.py
│ │ ├── skincare_scheduler.py
│ │ ├── tools.py
│ │ ├── vector_database_search.py
│ │ ├── websearch.py
│ │ └── wheel.py
│ ├── __init__.py
│ └── main.py
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── app.py
├── docker-compose.yml
├── document_code.py
└── pyproject.toml
```
## Source Code
### app\__init__.py
```python
# app/__init__.py
from app.main import app
__all__ = [
"app",
]
```
### app\config\__init__.py
```python
from app.config.config import Config
config = Config()
```
### app\config\config.py
```python
import os
from dotenv import load_dotenv
load_dotenv()
class Config:
JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY')
JWT_ACCESS_TOKEN_EXPIRES = int(os.getenv('JWT_ACCESS_TOKEN_EXPIRES'))
CORS_ORIGINS = ["http://localhost:3000"]
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'temp')
```
### app\database\__init__.py
```python
from app.database.db import get_db, db
from app.database.database_query import DatabaseQuery
__all__ = ["get_db", "db", "DatabaseQuery"]
```
### app\database\database_query.py
```python
from app.database.db import db
import re
from bson import ObjectId
from datetime import datetime, timezone, timedelta
from pymongo import DESCENDING
from typing import Optional
class DatabaseQuery:
def __init__(self):
pass
def create_chat_session(self, chat_session):
try:
db.chat_sessions.insert_one(chat_session)
except Exception as e:
raise Exception(f"Error creating chat session: {str(e)}")
def get_user_chat_sessions(self, user_id):
try:
sessions = list(db.chat_sessions.find(
{"user_id": user_id},
{"_id": 0}
).sort("last_accessed", -1))
return sessions
except Exception as e:
raise Exception(f"Error retrieving user chat sessions: {str(e)}")
def create_chat(self, chat_data):
try:
db.chats.insert_one(chat_data)
return True
except Exception as e:
raise Exception(f"Error creating chat: {str(e)}")
def update_last_accessed_time(self, session_id):
try:
db.chat_sessions.update_one(
{"session_id": session_id},
{"$set": {"last_accessed": datetime.now(timezone.utc)}}
)
except Exception as e:
raise Exception(f"Error updating last accessed time: {str(e)}")
def get_session_chats(self, session_id, user_id):
try:
chats = list(db.chats.find(
{"session_id": session_id, "user_id": user_id},
{"_id": 0}
).sort("timestamp", 1))
return chats
except Exception as e:
raise Exception(f"Error retrieving session chats: {str(e)}")
def get_user_by_identifier(self, identifier):
try:
user = db.users.find_one({'$or': [{'username': identifier}, {'email': identifier}]})
return user
except Exception as e:
raise Exception(f"Error retrieving user by identifier: {str(e)}")
def add_token_to_blacklist(self, jti):
try:
db.blacklist.insert_one({'jti': jti})
except Exception as e:
raise Exception(f"Error adding token to blacklist: {str(e)}")
def create_indexes(self):
try:
db.chat_sessions.create_index([("user_id", 1), ("last_accessed", -1)])
db.chat_sessions.create_index([("session_id", 1)])
db.chats.create_index([("session_id", 1), ("timestamp", 1)])
db.chats.create_index([("user_id", 1)])
except Exception as e:
raise Exception(f"Error creating indexes: {str(e)}")
def check_chat_session(self, session_id):
try:
chat_session = db.chat_sessions.find_one({'session_id': session_id})
return chat_session is not None
except Exception as e:
raise Exception(f"Error checking chat session: {str(e)}")
def get_user_profile(self, username):
try:
user = db.users.find_one({'username': username}, {'password': 0})
return user
except Exception as e:
raise Exception(f"Error getting user profile: {str(e)}")
def update_user_profile(self, username, update_fields):
try:
result = db.users.update_one(
{'username': username},
{'$set': update_fields}
)
return result.modified_count > 0
except Exception as e:
raise Exception(f"Error updating user profile: {str(e)}")
def delete_user_account(self, username):
try:
result = db.users.delete_one({'username': username})
return result.deleted_count > 0
except Exception as e:
raise Exception(f"Error deleting user account: {str(e)}")
def is_username_or_email_exists(self, username, email):
try:
user = db.users.find_one({'$or': [{'username': username}, {'email': email}]})
return user is not None
except Exception as e:
raise Exception(f"Error checking if username or email exists: {str(e)}")
def create_or_update_temp_user(self, username, email, temp_user_data):
try:
db.temp_users.update_one(
{'$or': [{'username': username}, {'email': email}]},
{'$set': temp_user_data},
upsert=True
)
except Exception as e:
raise Exception(f"Error creating/updating temp user: {str(e)}")
def get_temp_user_by_username(self, username):
try:
temp_user = db.temp_users.find_one({'username': username})
return temp_user
except Exception as e:
raise Exception(f"Error retrieving temp user by username: {str(e)}")
def delete_temp_user(self, username):
try:
db.temp_users.delete_one({'username': username})
except Exception as e:
raise Exception(f"Error deleting temp user: {str(e)}")
def create_user_from_data(self, user_data):
try:
db.users.insert_one(user_data)
return user_data
except Exception as e:
raise Exception(f"Error creating user from data: {str(e)}")
def create_user(self, username, email, hashed_password, name, age, created_at,
is_verified=False, verification_code=None, code_expiration=None):
try:
new_user = {
'username': username,
'email': email,
'password': hashed_password,
'name': name,
'age': age,
'created_at': created_at,
'is_verified': is_verified
}
if verification_code and code_expiration:
new_user['verification_code'] = verification_code
new_user['code_expiration'] = code_expiration
db.users.insert_one(new_user)
return new_user
except Exception as e:
raise Exception(f"Error creating user: {str(e)}")
def get_user_by_username(self, username):
try:
user = db.users.find_one({'username': username})
return user
except Exception as e:
raise Exception(f"Error retrieving user by username: {str(e)}")
def verify_user_email(self, username):
try:
result = db.users.update_one(
{'username': username},
{'$set': {'is_verified': True}, '$unset': {'verification_code': '', 'code_expiration': ''}}
)
return result.modified_count > 0
except Exception as e:
raise Exception(f"Error verifying user email: {str(e)}")
def update_verification_code(self, username, verification_code, code_expiration):
try:
result = db.users.update_one(
{'username': username},
{'$set': {'verification_code': verification_code, 'code_expiration': code_expiration}}
)
return result.modified_count > 0
except Exception as e:
raise Exception(f"Error updating verification code: {str(e)}")
def is_valid_email(self, email):
try:
email_regex = r'^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b'
return re.match(email_regex, email) is not None
except Exception as e:
raise Exception(f"Error validating email: {str(e)}")
def add_or_update_location(self, username, location):
try:
db.locations.update_one(
{'username': username},
{'$set': {'location': location, 'updated_at': datetime.now(timezone.utc)}},
upsert=True
)
except Exception as e:
raise Exception(f"Error adding/updating location: {str(e)}")
def get_location(self, username):
try:
location = db.locations.find_one({'username': username})
return location
except Exception as e:
raise Exception(f"Error retrieving location: {str(e)}")
def submit_questionnaire(self, user_id, answers):
try:
questionnaire_data = {
'user_id': user_id,
'answers': answers,
'created_at': datetime.now(timezone.utc),
'updated_at': datetime.now(timezone.utc)
}
result = db.questionnaires.insert_one(questionnaire_data)
return str(result.inserted_id)
except Exception as e:
raise Exception(f"Error submitting questionnaire: {str(e)}")
def get_latest_questionnaire(self, user_id):
try:
questionnaire = db.questionnaires.find_one(
{'user_id': user_id},
sort=[('created_at', -1)]
)
if questionnaire:
questionnaire['_id'] = str(questionnaire['_id'])
return questionnaire
except Exception as e:
raise Exception(f"Error getting latest questionnaire: {str(e)}")
def update_questionnaire(self, questionnaire_id, user_id, answers):
try:
result = db.questionnaires.update_one(
{'_id': ObjectId(questionnaire_id), 'user_id': user_id},
{
'$set': {
'answers': answers,
'updated_at': datetime.now(timezone.utc)
}
}
)
return result.modified_count > 0
except Exception as e:
raise Exception(f"Error updating questionnaire: {str(e)}")
def delete_questionnaire(self, questionnaire_id, user_id):
try:
result = db.questionnaires.delete_one(
{'_id': ObjectId(questionnaire_id), 'user_id': user_id}
)
return result.deleted_count > 0
except Exception as e:
raise Exception(f"Error deleting questionnaire: {str(e)}")
def count_answered_questions(self, username):
try:
answered_count = db.questions.count_documents({
'username': username,
'answer': {'$ne': None}
})
return answered_count
except Exception as e:
raise Exception(f"Error counting answered questions: {str(e)}")
def get_user_preferences(self, username):
try:
user_preferences = db.preferences.find_one({'username': username})
if not user_preferences:
return {
'keywords': False,
'references': False,
'websearch': False,
'personalized_recommendations': False,
'environmental_recommendations': False
}
return {
'keywords': user_preferences.get('keywords', False),
'references': user_preferences.get('references', False),
'websearch': user_preferences.get('websearch', False),
'personalized_recommendations': user_preferences.get('personalized_recommendations', False),
'environmental_recommendations': user_preferences.get('environmental_recommendations', False)
}
except Exception as e:
raise Exception(f"Error getting user preferences: {str(e)}")
def set_user_preferences(self, username, preferences):
try:
preferences_data = {
'username': username,
'keywords': bool(preferences.get('keywords', False)),
'references': bool(preferences.get('references', False)),
'websearch': bool(preferences.get('websearch', False)),
'personalized_recommendations': bool(preferences.get('personalized_recommendations', False)),
'environmental_recommendations': bool(preferences.get('environmental_recommendations', False)),
'updated_at': datetime.now(timezone.utc)
}
result = db.preferences.update_one(
{'username': username},
{'$set': preferences_data},
upsert=True
)
return preferences_data
except Exception as e:
raise Exception(f"Error setting user preferences: {str(e)}")
def get_user_theme(self, username):
try:
user_theme = db.user_themes.find_one({'username': username})
if not user_theme:
return 'light'
return user_theme.get('theme', 'light')
except Exception as e:
raise Exception(f"Error getting user theme: {str(e)}")
def set_user_theme(self, username, theme):
try:
theme_data = {
'username': username,
'theme': "dark" if theme else "light",
'updated_at': datetime.now(timezone.utc)
}
db.user_themes.update_one(
{'username': username},
{'$set': theme_data},
upsert=True
)
return theme_data
except Exception as e:
raise Exception(f"Error setting user theme: {str(e)}")
def verify_session(self, session_id, user_id):
try:
session = db.chat_sessions.find_one({
"session_id": session_id,
"user_id": user_id
})
return session is not None
except Exception as e:
raise Exception(f"Error verifying session: {str(e)}")
def update_chat_session_title(self, session_id, new_title):
try:
result = db.chat_sessions.update_one(
{"session_id": session_id},
{"$set": {"title": new_title}}
)
if result.matched_count == 0:
raise Exception("Chat session not found")
return result.modified_count > 0
except Exception as e:
raise Exception(f"Error updating chat session title: {str(e)}")
def delete_chat_session(self, session_id, user_id):
try:
session_result = db.chat_sessions.delete_one({
"session_id": session_id,
"user_id": user_id
})
chats_result = db.chats.delete_many({
"session_id": session_id,
"user_id": user_id
})
return {
"session_deleted": session_result.deleted_count > 0,
"chats_deleted": chats_result.deleted_count
}
except Exception as e:
raise Exception(f"Error deleting chat session and chats: {str(e)}")
def delete_all_user_sessions_and_chats(self, user_id):
try:
chats_result = db.chats.delete_many({"user_id": user_id})
sessions_result = db.chat_sessions.delete_many({"user_id": user_id})
return {
"deleted_chats": chats_result.deleted_count,
"deleted_sessions": sessions_result.deleted_count
}
except Exception as e:
raise Exception(f"Error deleting user sessions and chats: {str(e)}")
def get_all_user_chats(self, user_id):
try:
sessions = list(db.chat_sessions.find(
{"user_id": user_id},
{"_id": 0}
).sort("last_accessed", -1))
all_chats = []
for session in sessions:
session_chats = list(db.chats.find(
{"session_id": session["session_id"], "user_id": user_id},
{"_id": 0}
).sort("timestamp", 1))
all_chats.append({
"session_id": session["session_id"],
"title": session.get("title", "New Chat"),
"created_at": session.get("created_at"),
"last_accessed": session.get("last_accessed"),
"chats": session_chats
})
return all_chats
except Exception as e:
raise Exception(f"Error retrieving all user chats: {str(e)}")
def store_reset_token(self, email, token, expiration):
try:
db.password_resets.update_one(
{'email': email},
{
'$set': {
'token': token,
'expiration': expiration
}
},
upsert=True
)
except Exception as e:
raise Exception(f"Error storing reset token: {str(e)}")
def verify_reset_token(self, token):
try:
reset_info = db.password_resets.find_one({
'token': token,
'expiration': {'$gt': datetime.now(timezone.utc)}
})
return reset_info
except Exception as e:
raise Exception(f"Error verifying reset token: {str(e)}")
def update_password(self, email, hashed_password):
try:
db.users.update_one(
{'email': email},
{'$set': {'password': hashed_password}}
)
except Exception as e:
raise Exception(f"Error updating password: {str(e)}")
def delete_reset_token(self, token):
try:
db.password_resets.delete_one({'token': token})
except Exception as e:
raise Exception(f"Error deleting reset token: {str(e)}")
def delete_account_permanently(self, username):
try:
chat_deletion_result = self.delete_all_user_sessions_and_chats(username)
preferences_result = db.preferences.delete_one({'username': username})
theme_result = db.user_themes.delete_one({'username': username})
location_result = db.locations.delete_one({'username': username})
questionnaire_result = db.questionnaires.delete_many({'user_id': username})
user_result = db.users.delete_one({'username': username})
return {
'success': True,
'deleted_data': {
'chats': chat_deletion_result['deleted_chats'],
'chat_sessions': chat_deletion_result['deleted_sessions'],
'preferences': preferences_result.deleted_count,
'theme': theme_result.deleted_count,
'location': location_result.deleted_count,
'questionnaires': questionnaire_result.deleted_count,
'user_account': user_result.deleted_count
}
}
except Exception as e:
raise Exception(f"Error deleting account permanently: {str(e)}")
def store_reset_token(self, email, token, expiration):
try:
db.password_resets.update_one(
{'email': email},
{
'$set': {
'token': token,
'expiration': expiration
}
},
upsert=True
)
except Exception as e:
raise Exception(f"Error storing reset token: {str(e)}")
def verify_reset_token(self, token):
try:
reset_info = db.password_resets.find_one({
'token': token,
'expiration': {'$gt': datetime.now(timezone.utc)}
})
return reset_info
except Exception as e:
raise Exception(f"Error verifying reset token: {str(e)}")
def update_password(self, email, new_password):
try:
db.users.update_one(
{'email': email},
{'$set': {'password': new_password}}
)
except Exception as e:
raise Exception(f"Error updating password: {str(e)}")
def get_user_language(self, user_id):
try:
language = db.languages.find_one({'user_id': user_id})
return language.get('language') if language else None
except Exception as e:
raise Exception(f"Error retrieving user language: {str(e)}")
def set_user_language(self, user_id, language):
try:
language_data = {
'user_id': user_id,
'language': language,
'updated_at': datetime.now(timezone.utc)
}
result = db.languages.update_one(
{'user_id': user_id},
{'$set': language_data},
upsert=True
)
return language_data
except Exception as e:
raise Exception(f"Error setting user language: {str(e)}")
def delete_user_language(self, user_id):
try:
result = db.languages.delete_one({'user_id': user_id})
return result.deleted_count > 0
except Exception as e:
raise Exception(f"Error deleting user language: {str(e)}")
def get_today_schedule(self, user_id):
try:
# Get today's date at midnight UTC
today = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
tomorrow = today.replace(hour=23, minute=59, second=59)
schedule = db.skin_schedules.find_one({
"user_id": user_id,
"created_at": {
"$gte": today,
"$lte": tomorrow
}
})
return schedule
except Exception as e:
raise Exception(f"Error retrieving today's schedule: {str(e)}")
def save_schedule(self, user_id, schedule_data):
try:
existing_schedule = self.get_today_schedule(user_id)
if existing_schedule:
return str(existing_schedule["_id"])
schedule = {
"user_id": user_id,
"schedule_data": schedule_data,
"created_at": datetime.now(timezone.utc)
}
result = db.skin_schedules.insert_one(schedule)
return str(result.inserted_id)
except Exception as e:
raise Exception(f"Error saving schedule: {str(e)}")
def get_last_seven_days_schedules(self, user_id):
try:
seven_days_ago = datetime.now(timezone.utc) - timedelta(days=7)
schedules = db.skin_schedules.find({
"user_id": user_id,
"created_at": {"$gte": seven_days_ago}
}).sort("created_at", -1)
return list(schedules)
except Exception as e:
raise Exception(f"Error fetching last 7 days schedules: {str(e)}")
def save_rag_interaction(self, user_id: str, session_id: str, context: str, query: str,
response: str, rag_start_time: datetime, rag_end_time: datetime):
try:
interaction = {
"interaction_id": str(ObjectId()),
"user_id": user_id,
"session_id": session_id,
"context": context,
"query": query,
"response": response,
"rag_start_time": rag_start_time.astimezone(timezone.utc),
"rag_end_time": rag_end_time.astimezone(timezone.utc),
"created_at": datetime.now(timezone.utc)
}
result = db.rag_interactions.insert_one(interaction)
return interaction["interaction_id"]
except Exception as e:
raise Exception(f"Error saving RAG interaction: {str(e)}")
def get_rag_interactions(
self,
user_id: Optional[str] = None,
page: int = 1,
page_size: int = 5
) -> dict:
try:
query_filter = {}
if user_id:
query_filter["user_id"] = user_id
skip = (page - 1) * page_size
total = db.rag_interactions.count_documents(query_filter)
interactions = db.rag_interactions.find(
query_filter,
{"_id": 0}
).sort("created_at", DESCENDING).skip(skip).limit(page_size)
result_list = []
for interaction in interactions:
interaction["rag_start_time"] = interaction["rag_start_time"].isoformat()
interaction["rag_end_time"] = interaction["rag_end_time"].isoformat()
interaction["created_at"] = interaction["created_at"].isoformat()
result_list.append(interaction)
return {
"total_interactions": total,
"page": page,
"page_size": page_size,
"total_pages": (total + page_size - 1) // page_size,
"results": result_list
}
except Exception as e:
raise Exception(f"Error retrieving RAG interactions: {str(e)}")
def log_image_upload(self, user_id):
"""Log an image upload for a user"""
try:
timestamp = datetime.now(timezone.utc) # This is timezone-aware
db.image_uploads.insert_one({
"user_id": user_id,
"timestamp": timestamp
})
return True
except Exception as e:
raise Exception(f"Error logging image upload: {str(e)}")
def get_user_daily_uploads(self, user_id):
"""Get number of images uploaded by user in the last 24 hours"""
try:
now = datetime.now(timezone.utc)
yesterday = now - timedelta(days=1)
count = db.image_uploads.count_documents({
"user_id": user_id,
"timestamp": {"$gte": yesterday}
})
return count
except Exception as e:
raise Exception(f"Error retrieving user daily uploads: {str(e)}")
def get_user_last_upload_time(self, user_id):
"""Get the timestamp of user's most recent image upload"""
try:
last_upload = db.image_uploads.find_one(
{"user_id": user_id},
sort=[("timestamp", DESCENDING)]
)
return last_upload["timestamp"] if last_upload else None
except Exception as e:
raise Exception(f"Error retrieving last upload time: {str(e)}")
```
### app\database\db.py
```python
import os
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
uri = os.getenv('MONGO_URI')
mongo_uri = os.getenv('MONGO_URI')
if not mongo_uri:
raise ValueError("MONGO_URI environment variable is not set")
def get_db():
client = MongoClient(uri, server_api=ServerApi('1'))
try:
client.admin.command('ping')
except Exception as e:
print(e)
return client.get_database("dermai")
db = get_db()
```
### app\main.py
```python
# app/main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import os
from dotenv import load_dotenv
from app.config.config import Config
from app.routers import admin, auth, chat, location, preferences, profile, questionnaire, language, chat_session
from app.routers import agent_chat
load_dotenv()
app = FastAPI(title="Skin AI API")
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files for uploads
os.makedirs(Config.UPLOAD_FOLDER, exist_ok=True)
app.mount("/uploads", StaticFiles(directory=Config.UPLOAD_FOLDER), name="uploads")
# Register routers
app.include_router(admin.router, prefix="/api", tags=["admin"])
app.include_router(auth.router, prefix="/api", tags=["auth"])
app.include_router(chat.router, prefix="/api", tags=["chat"])
app.include_router(location.router, prefix="/api", tags=["location"])
app.include_router(preferences.router, prefix="/api", tags=["preferences"])
app.include_router(profile.router, prefix="/api", tags=["profile"])
app.include_router(questionnaire.router, prefix="/api", tags=["questionnaire"])
app.include_router(language.router, prefix="/api", tags=["language"])
app.include_router(chat_session.router, prefix="/api", tags=["chat_session"])
app.include_router(agent_chat.router, prefix="/api", tags=["agent_chat"])
@app.get("/")
async def root():
return {"message": "API is running", "status": "healthy"}
```
### app\middleware\auth.py
```python
# app/middleware/auth.py
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
import jwt
from datetime import datetime, timedelta
import os
security = HTTPBearer()
JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY')
JWT_ACCESS_TOKEN_EXPIRES = int(os.getenv('JWT_ACCESS_TOKEN_EXPIRES'))
def create_access_token(data: dict):
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(seconds=JWT_ACCESS_TOKEN_EXPIRES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, JWT_SECRET_KEY, algorithm="HS256")
return encoded_jwt
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = jwt.decode(credentials.credentials, JWT_SECRET_KEY, algorithms=["HS256"])
username: str = payload.get("sub")
if username is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return username
except jwt.PyJWTError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
def get_current_user(username: str = Depends(verify_token)):
return username
# For optional JWT authentication (some endpoints allow unauthenticated access)
def get_optional_user(authorization: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = jwt.decode(authorization.credentials, JWT_SECRET_KEY, algorithms=["HS256"])
username: str = payload.get("sub")
return username
except:
return None
```
### app\routers\admin.py
```python
# app/routers/admin.py
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
from typing import List
import os
from app.database.database_query import DatabaseQuery
from app.services.vector_database_search import VectorDatabaseSearch
from app.middleware.auth import get_current_user
from pydantic import BaseModel
router = APIRouter()
vector_db = VectorDatabaseSearch()
query = DatabaseQuery()
TEMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'temp')
os.makedirs(TEMP_DIR, exist_ok=True)
class SearchQuery(BaseModel):
query: str
k: int = 5
@router.get('/books')
async def get_books(username: str = Depends(get_current_user)):
try:
book_info = vector_db.get_book_info()
return {
'status': 'success',
'data': book_info
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post('/books', status_code=201)
async def add_books(files: List[UploadFile] = File(...), username: str = Depends(get_current_user)):
try:
pdf_paths = []
for file in files:
if file.filename.endswith('.pdf'):
safe_filename = os.path.basename(file.filename)
temp_path = os.path.join(TEMP_DIR, safe_filename)
with open(temp_path, "wb") as buffer:
content = await file.read()
buffer.write(content)
pdf_paths.append(temp_path)
if not pdf_paths:
raise HTTPException(status_code=400, detail="No valid PDF files provided")
success_count = 0
for pdf_path in pdf_paths:
if vector_db.add_pdf(pdf_path):
success_count += 1
# Clean up temporary files
for path in pdf_paths:
try:
if os.path.exists(path):
os.remove(path)
except Exception:
pass
return {
'status': 'success',
'message': f'Successfully added {success_count} of {len(pdf_paths)} books'
}
except Exception as e:
# Clean up temporary files in case of error
for path in pdf_paths:
try:
if os.path.exists(path):
os.remove(path)
except:
pass
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.post('/search')
async def search_books(search_data: SearchQuery, username: str = Depends(get_current_user)):
try:
query_text = search_data.query
k = search_data.k
results = vector_db.search(
query=query_text,
top_k=k
)
return {
'status': 'success',
'data': results
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\agent_chat.py
```python
from typing import Optional
import asyncio
import json
import logging
from fastapi import APIRouter, Depends, Header, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from app.middleware.auth import get_current_user
from app.services.google_agent_service import (
DEFAULT_MODEL_NAME,
GoogleAgentService,
)
router = APIRouter()
logger = logging.getLogger(__name__)
class AgentChatDocument(BaseModel):
path: str
name: Optional[str] = None
type: Optional[str] = None
extension: Optional[str] = None
class AgentChatImage(BaseModel):
path: str
name: Optional[str] = None
type: Optional[str] = None
extension: Optional[str] = None
prompt: Optional[str] = None
class AgentChatRequest(BaseModel):
session_id: Optional[str] = None
query: str
document: Optional[AgentChatDocument] = None
image: Optional[AgentChatImage] = None
async def stream_agent_response(agent_service: GoogleAgentService, query: str):
try:
async for event in agent_service.process_message_async(query):
event_type = event.get("type")
if event_type == "chunk":
payload = {"type": "chunk", "content": event.get("content", "")}
elif event_type == "tool_call":
payload = {
"type": "tool_call",
"tool_name": event.get("tool_name"),
"arguments": event.get("arguments", {}),
}
elif event_type == "tool_result":
payload = {
"type": "tool_result",
"tool_name": event.get("tool_name"),
"result": event.get("result", {}),
}
elif event_type == "completed":
payload = {
"type": "completed",
"saved": event.get("saved"),
"session_id": event.get("session_id"),
"response": event.get("response", ""),
"keywords": event.get("keywords", []),
"references": event.get("references", []),
"images": event.get("images", []),
}
elif event_type == "error":
payload = {"type": "error", "message": event.get("content", "")}
else:
payload = {"type": event_type or "unknown", "data": event}
yield f"data: {json.dumps(payload)}\n\n"
await asyncio.sleep(0.001)
yield "data: {\"type\": \"done\"}\n\n"
except Exception as exc:
logger.error("Streaming error: %s", exc, exc_info=True)
yield f"data: {json.dumps({'type': 'error', 'message': str(exc)})}\n\n"
@router.post("/agent-chat")
async def agent_chat(
request: AgentChatRequest,
authorization: str = Header(None),
username: str = Depends(get_current_user),
):
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Invalid authorization header")
token = authorization.split(" ", 1)[1]
try:
agent_service = GoogleAgentService(
token=token,
session_id=request.session_id,
document=request.document.dict() if request.document else None,
image=request.image.dict() if request.image else None,
)
except Exception as exc:
logger.error("Failed to initialise agent service: %s", exc, exc_info=True)
raise HTTPException(status_code=500, detail="Unable to initialise agent")
return StreamingResponse(
stream_agent_response(agent_service, request.query),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "text/event-stream",
"X-Accel-Buffering": "no",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
},
)
@router.get("/agent-status")
async def agent_status(username: str = Depends(get_current_user)):
try:
return {
"status": "available",
"model": DEFAULT_MODEL_NAME,
"features": [
"web_search",
"vector_search",
"image_search",
"streaming",
"tool_calls",
],
}
except Exception as exc:
logger.error("Agent status error: %s", exc, exc_info=True)
return {"status": "error", "message": str(exc)}
```
### app\routers\auth.py
```python
# app/routers/auth.py
import os
import random
import smtplib
import ssl
import string
from datetime import datetime, timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr
from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel, EmailStr
from werkzeug.security import generate_password_hash, check_password_hash
from app.database.database_query import DatabaseQuery
from app.middleware.auth import create_access_token, get_current_user
from dotenv import load_dotenv
load_dotenv()
SMTP_SERVER = os.getenv("SMTP_SERVER") # optional alias
_raw_host = os.getenv("SMTP_HOST")
_raw_port = os.getenv("SMTP_PORT")
# Be forgiving if env is swapped (e.g., SMTP_HOST=587 and SMTP_SERVER=smtp.gmail.com)
if _raw_host and _raw_host.strip().isdigit() and not _raw_port:
SMTP_HOST = SMTP_SERVER or "smtp.gmail.com"
SMTP_PORT = int(_raw_host.strip())
else:
SMTP_HOST = _raw_host or SMTP_SERVER or "smtp.gmail.com"
try:
SMTP_PORT = int(_raw_port) if _raw_port else 587
except Exception:
SMTP_PORT = 587
SMTP_USER = os.getenv("SMTP_USER")
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD")
EMAILS_FROM_EMAIL = os.getenv("EMAILS_FROM_EMAIL") or None
EMAILS_FROM_NAME = os.getenv("EMAILS_FROM_NAME") or None
def send_email(to_email: str, subject: str, html_content: str, failure_message: str = "Failed to send email") -> None:
if not all([SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD]):
raise HTTPException(status_code=500, detail="Email service is not configured properly")
message = MIMEMultipart("alternative")
from_email = EMAILS_FROM_EMAIL or SMTP_USER
from_header = formataddr((EMAILS_FROM_NAME, from_email)) if EMAILS_FROM_NAME else from_email
message["Subject"] = subject
message["From"] = from_header
message["To"] = to_email
if EMAILS_FROM_EMAIL and EMAILS_FROM_EMAIL != SMTP_USER:
message["Reply-To"] = EMAILS_FROM_EMAIL
message.attach(MIMEText(html_content, "html"))
try:
context = ssl.create_default_context()
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
server.starttls(context=context)
auth_password = SMTP_PASSWORD
if (("gmail" in (SMTP_HOST or "")) or ((SMTP_USER or "").lower().endswith("@gmail.com"))) and isinstance(SMTP_PASSWORD, str) and (" " in SMTP_PASSWORD):
auth_password = SMTP_PASSWORD.replace(" ", "")
server.login(SMTP_USER, auth_password)
server.sendmail(SMTP_USER, [to_email], message.as_string())
except Exception as exc:
raise HTTPException(status_code=500, detail=f"{failure_message}: {exc}")
router = APIRouter()
query = DatabaseQuery()
class LoginRequest(BaseModel):
identifier: str
password: str
class LoginResponse(BaseModel):
message: str
token: str
class RegisterRequest(BaseModel):
username: str
email: EmailStr
password: str
name: str
age: int
class VerifyEmailRequest(BaseModel):
username: str
code: str
class ResendCodeRequest(BaseModel):
username: str
class ForgotPasswordRequest(BaseModel):
email: EmailStr
class ResetPasswordRequest(BaseModel):
token: str
password: str
class ChatSessionCheck(BaseModel):
session_id: str
@router.post('/login', response_model=LoginResponse)
async def login(login_data: LoginRequest):
try:
identifier = login_data.identifier
password = login_data.password
user = query.get_user_by_identifier(identifier)
if user:
if not user.get('is_verified'):
raise HTTPException(status_code=401, detail="Please verify your email before logging in")
if check_password_hash(user['password'], password):
access_token = create_access_token({"sub": user['username']})
return {"message": "Login successful", "token": access_token}
raise HTTPException(status_code=401, detail="Invalid username/email or password")
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.post('/register', status_code=201)
async def register(register_data: RegisterRequest):
try:
username = register_data.username
email = register_data.email
password = register_data.password
name = register_data.name
age = register_data.age
if query.is_username_or_email_exists(username, email):
raise HTTPException(status_code=409, detail="Username or email already exists")
verification_code = ''.join(random.choices(string.digits, k=6))
code_expiration = datetime.utcnow() + timedelta(minutes=10)
hashed_password = generate_password_hash(password)
created_at = datetime.utcnow()
temp_user = {
'username': username,
'email': email,
'password': hashed_password,
'name': name,
'age': age,
'created_at': created_at,
'verification_code': verification_code,
'code_expiration': code_expiration
}
query.create_or_update_temp_user(username, email, temp_user)
try:
send_email(
to_email=email,
subject='Verify your email address',
html_content=f'''
<p>Hi {name},</p>
<p>Thank you for registering. Please use the following code to verify your email address:</p>
<h2>{verification_code}</h2>
<p>This code will expire in 10 minutes.</p>
''',
failure_message="Failed to send verification email",
)
except Exception:
raise HTTPException(status_code=500, detail="Failed to send verification email")
return {"message": "Registration successful. A verification code has been sent to your email."}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.post('/verify-email')
async def verify_email(verify_data: VerifyEmailRequest):
try:
username = verify_data.username
code = verify_data.code
temp_user = query.get_temp_user_by_username(username)
if not temp_user:
raise HTTPException(status_code=404, detail="User not found or already verified")
if temp_user['verification_code'] != code:
raise HTTPException(status_code=400, detail="Invalid verification code")
if datetime.utcnow() > temp_user['code_expiration']:
raise HTTPException(status_code=400, detail="Verification code has expired")
user_data = temp_user.copy()
user_data['is_verified'] = True
user_data.pop('verification_code', None)
user_data.pop('code_expiration', None)
user_data.pop('_id', None)
query.create_user_from_data(user_data)
query.delete_temp_user(username)
# Set default language to English
query.set_user_language(username, "English")
# Set default theme to light (passing false for dark theme)
query.set_user_theme(username, False)
default_preferences = {
'keywords': True,
'references': True,
'websearch': False,
'personalized_recommendations': True,
'environmental_recommendations': True
}
query.set_user_preferences(username, default_preferences)
return {"message": "Email verification successful"}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.post('/resend-code')
async def resend_code(resend_data: ResendCodeRequest):
try:
username = resend_data.username
temp_user = query.get_temp_user_by_username(username)
if not temp_user:
raise HTTPException(status_code=404, detail="User not found or already verified")
verification_code = ''.join(random.choices(string.digits, k=6))
code_expiration = datetime.utcnow() + timedelta(minutes=10)
temp_user['verification_code'] = verification_code
temp_user['code_expiration'] = code_expiration
query.create_or_update_temp_user(username, temp_user['email'], temp_user)
try:
send_email(
to_email=temp_user['email'],
subject='Your new verification code',
html_content=f'''
<p>Hi {temp_user['name']},</p>
<p>You requested a new verification code. Please use the following code to verify your email address:</p>
<h2>{verification_code}</h2>
<p>This code will expire in 10 minutes.</p>
''',
failure_message="Failed to send verification email",
)
except Exception:
raise HTTPException(status_code=500, detail="Failed to send verification email")
return {"message": "A new verification code has been sent to your email."}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.post('/checkChatsession')
async def check_chatsession(data: ChatSessionCheck, username: str = Depends(get_current_user)):
session_id = data.session_id
is_chat_exit = query.check_chat_session(session_id)
return {"ischatexit": is_chat_exit}
@router.get('/check-token')
async def check_token(username: str = Depends(get_current_user)):
try:
return {'valid': True, 'user': username}
except Exception as e:
raise HTTPException(status_code=401, detail=str(e))
@router.post('/forgot-password')
async def forgot_password(data: ForgotPasswordRequest):
try:
email = data.email
user = query.get_user_by_identifier(email)
if not user:
raise HTTPException(status_code=404, detail="Email not found")
reset_token = ''.join(random.choices(string.ascii_letters + string.digits, k=32))
expiration = datetime.utcnow() + timedelta(hours=1)
query.store_reset_token(email, reset_token, expiration)
reset_link = f"http://localhost:3000/reset-password?token={reset_token}"
try:
send_email(
to_email=email,
subject='Reset Your Password',
html_content=f'''
<p>Hi,</p>
<p>You requested to reset your password. Click the link below to reset it:</p>
<p><a href="{reset_link}">Reset Password</a></p>
<p>This link will expire in 1 hour.</p>
<p>If you didn't request this, please ignore this email.</p>
''',
failure_message="Failed to send password reset email",
)
except Exception:
raise HTTPException(status_code=500, detail="Failed to send password reset email")
return {"message": "Password reset instructions sent to email"}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.post('/reset-password')
async def reset_password(data: ResetPasswordRequest):
try:
token = data.token
new_password = data.password
if not token or not new_password:
raise HTTPException(status_code=400, detail="Token and new password are required")
reset_info = query.verify_reset_token(token)
if not reset_info:
raise HTTPException(status_code=400, detail="Invalid or expired reset token")
hashed_password = generate_password_hash(new_password)
query.update_password(reset_info['email'], hashed_password)
return {"message": "Password successfully reset"}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\chat.py
```python
# app/routers/chat.py
import logging
import os
import json
from datetime import datetime
from bson import ObjectId
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form, Header
from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel
from app.database.database_query import DatabaseQuery
from app.middleware.auth import get_current_user, get_optional_user
from app.services.skincare_scheduler import SkinCareScheduler
from app.services.wheel import EnvironmentalConditions
from app.services.RAG_evaluation import RAGEvaluation
router = APIRouter()
query = DatabaseQuery()
class ChatSessionTitleUpdate(BaseModel):
title: str
@router.get('/image/{filename}')
async def serve_image(filename: str):
try:
# Use an absolute path or environment variable to ensure consistency
upload_dir = os.path.abspath('uploads')
file_path = os.path.join(upload_dir, filename)
# Add logging to debug
print(f"Attempting to serve file from: {file_path}")
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
raise FileNotFoundError()
return FileResponse(file_path)
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Image not found")
@router.post('/chat-sessions', status_code=201)
async def create_chat_session(username: str = Depends(get_current_user)):
try:
session_id = str(ObjectId())
chat_session = {
"user_id": username,
"session_id": session_id,
"created_at": datetime.utcnow(),
"last_accessed": datetime.utcnow(),
"title": "New Chat"
}
query.create_chat_session(chat_session)
return {"message": "Chat session created", "session_id": session_id}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/chat-sessions')
async def get_user_chat_sessions(username: str = Depends(get_current_user)):
try:
sessions = query.get_user_chat_sessions(username)
return sessions
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/chat-sessions/{session_id}')
async def delete_chat_session(session_id: str, username: str = Depends(get_current_user)):
try:
result = query.delete_chat_session(session_id, username)
if result["session_deleted"]:
return {
"message": "Chat session and associated chats deleted successfully",
"chats_deleted": result["chats_deleted"]
}
raise HTTPException(status_code=404, detail="Chat session not found or unauthorized")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.put('/chat-sessions/{session_id}/title')
async def update_chat_title(
session_id: str,
title_data: ChatSessionTitleUpdate,
username: str = Depends(get_current_user)
):
try:
new_title = title_data.title
if not query.verify_session(session_id, username):
raise HTTPException(status_code=404, detail="Chat session not found or unauthorized")
if query.update_chat_session_title(session_id, new_title):
return {
'message': 'Chat session title updated successfully',
'session_id': session_id,
'new_title': new_title
}
raise HTTPException(status_code=500, detail="Failed to update chat session title")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/chat-sessions/all')
async def delete_all_sessions_and_chats(username: str = Depends(get_current_user)):
try:
result = query.delete_all_user_sessions_and_chats(username)
return {
"message": "Successfully deleted all chat sessions and chats",
"deleted_chats": result["deleted_chats"],
"deleted_sessions": result["deleted_sessions"]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/chats/session/{session_id}')
async def get_session_chats(session_id: str, username: str = Depends(get_current_user)):
try:
chats = query.get_session_chats(session_id, username)
return chats
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/export-chat/{session_id}')
async def export_chat(session_id: str, username: str = Depends(get_current_user)):
try:
if not query.verify_session(session_id, username):
raise HTTPException(status_code=404, detail="Chat session not found or unauthorized")
chats = query.get_session_chats(session_id, username)
formatted_chats = []
for chat in chats:
formatted_chat = {
'query': chat.get('query', ''),
'response': chat.get('response', ''),
'references': chat.get('references', []),
'page_no': chat.get('page_no', ''),
'date': chat.get('timestamp', ''),
'chat_id': chat.get('chat_id', '')
}
formatted_chats.append(formatted_chat)
export_data = {
'session_id': session_id,
'export_date': datetime.utcnow().isoformat(),
'chats': formatted_chats
}
return export_data
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/export-all-chats')
async def export_all_chats(username: str = Depends(get_current_user)):
try:
all_chats = query.get_all_user_chats(username)
formatted_sessions = []
for session in all_chats:
formatted_chats = []
for chat in session['chats']:
formatted_chat = {
'query': chat.get('query', ''),
'response': chat.get('response', ''),
'references': chat.get('references', []),
'page_no': chat.get('page_no', ''),
'timestamp': chat.get('timestamp', ''),
'chat_id': chat.get('chat_id', '')
}
formatted_chats.append(formatted_chat)
formatted_session = {
'session_id': session['session_id'],
'title': session['title'],
'created_at': session['created_at'],
'last_accessed': session['last_accessed'],
'chats': formatted_chats
}
formatted_sessions.append(formatted_session)
export_data = {
'user': username,
'export_date': datetime.utcnow().isoformat(),
'sessions': formatted_sessions
}
return export_data
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post('/report-analysis')
async def upload_report(
file: UploadFile = File(...),
session_id: str = Form(...),
authorization: str = Header(None),
username: str = Depends(get_current_user)
):
try:
_ = authorization.split(" ")[1]
if not file.filename:
return JSONResponse(
status_code=400,
content={"status": "error", "error": "Empty file provided"}
)
file_extension = file.filename.rsplit('.', 1)[1].lower() if '.' in file.filename else ''
allowed_extensions = {
'pdf',
'xlsx',
'xls',
'csv',
'jpg',
'jpeg',
'png',
'doc',
'docx',
'ppt',
'pptx',
'txt',
'html'
}
if file_extension not in allowed_extensions:
return JSONResponse(
status_code=400,
content={
"status": "error",
"error": f"Unsupported file type. Allowed types: {', '.join(sorted(allowed_extensions))}",
}
)
default_upload_root = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "..", "uploads")
)
uploads_root = os.getenv('DERMAI_UPLOAD_DIR', default_upload_root)
session_upload_dir = os.path.join(uploads_root, session_id)
os.makedirs(session_upload_dir, exist_ok=True)
timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
sanitized_name = file.filename.replace(' ', '_')
stored_filename = f"{timestamp}_{sanitized_name}"
stored_path = os.path.join(session_upload_dir, stored_filename)
content = await file.read()
with open(stored_path, 'wb') as f:
f.write(content)
relative_root = os.path.abspath(uploads_root)
absolute_path = os.path.abspath(stored_path)
relative_path = os.path.relpath(absolute_path, relative_root)
return {
"status": "success",
"message": "File uploaded successfully",
"file": {
"path": relative_path.replace('\\', '/'),
"name": file.filename,
"content_type": file.content_type,
"size": len(content),
"extension": file_extension,
}
}
except Exception as e:
logging.error(f"Error in upload_report: {str(e)}")
raise HTTPException(
status_code=500,
detail={
"status": "error",
"error": "Internal server error",
"details": str(e)
}
)
@router.get('/skin-care-schedule')
async def get_skin_care_schedule(
authorization: str = Header(None),
username: str = Depends(get_current_user)
):
try:
token = authorization.split(" ")[1]
scheduler = SkinCareScheduler(token, "session_id")
schedule = scheduler.createTable()
return json.loads(schedule)
except Exception as e:
logging.error(f"Error generating skin care schedule: {str(e)}")
raise HTTPException(
status_code=500,
detail={"error": "Failed to generate skin care schedule"}
)
@router.get('/skin-care-wheel')
async def get_skin_care_wheel(
authorization: str = Header(...),
username: str = Depends(get_current_user)
):
try:
token = authorization.split(" ")[1]
condition = EnvironmentalConditions(session_id=token)
condition_data = condition.get_conditon()
return condition_data
except Exception as e:
logging.error(f"Error generating skin care wheel: {str(e)}")
raise HTTPException(
status_code=500,
detail={
"error": "Failed to generate skin care wheel",
"message": "An unexpected error occurred"
}
)
@router.post('/image_disease_search')
async def upload_skin_image(
image: UploadFile = File(...),
session_id: str = Form(...),
query: str = Form(""),
num_results: int = Form(3),
num_images: int = Form(3),
authorization: str = Header(...),
username: str = Depends(get_current_user)
):
try:
_ = authorization.split(" ")[1]
if not image.filename:
return JSONResponse(
status_code=400,
content={"status": "error", "error": "Empty image file provided"},
)
allowed_extensions = {
"jpg",
"jpeg",
"png",
"bmp",
"webp",
"avif",
"avifs",
"heic",
"heif",
}
file_extension = (
image.filename.rsplit('.', 1)[1].lower() if '.' in image.filename else ''
)
if file_extension not in allowed_extensions:
return JSONResponse(
status_code=400,
content={
"status": "error",
"error": (
"Unsupported image type. Allowed types: "
f"{', '.join(sorted(allowed_extensions))}"
),
},
)
default_upload_root = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "..", "uploads")
)
uploads_root = os.getenv('DERMAI_UPLOAD_DIR', default_upload_root)
session_upload_dir = os.path.join(uploads_root, session_id)
os.makedirs(session_upload_dir, exist_ok=True)
timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
sanitized_name = image.filename.replace(' ', '_')
stored_filename = f"{timestamp}_{sanitized_name}"
stored_path = os.path.join(session_upload_dir, stored_filename)
content = await image.read()
with open(stored_path, 'wb') as f:
f.write(content)
absolute_path = os.path.abspath(stored_path)
relative_root = os.path.abspath(uploads_root)
relative_path = os.path.relpath(absolute_path, relative_root)
return {
"status": "success",
"message": "Image uploaded successfully",
"file": {
"path": relative_path.replace('\\', '/'),
"name": image.filename,
"content_type": image.content_type,
"size": len(content),
"extension": file_extension,
"original_query": query,
},
}
except Exception as e:
logging.error(f"Error in upload_skin_image: {str(e)}")
raise HTTPException(
status_code=500,
detail={
"status": "error",
"error": "Internal server error",
"details": str(e),
},
)
@router.post('/get_rag_evaluation')
async def rag_evaluation(
page: int = Form(3),
page_size: int = Form(3),
authorization: str = Header(...),
username: str = Depends(get_current_user)
):
try:
token = authorization.split(" ")[1]
evaluator = RAGEvaluation(
token=token,
page=page,
page_size=page_size
)
report = evaluator.generate_evaluation_report()
return {"response": report}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\chat_session.py
```python
# app/routers/chat_session.py
from datetime import datetime
from bson import ObjectId
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from app.database.database_query import DatabaseQuery
from app.middleware.auth import get_current_user
router = APIRouter()
query = DatabaseQuery()
class ChatSessionTitleUpdate(BaseModel):
title: str
@router.post('/chat-sessions', status_code=201)
async def create_chat_session(username: str = Depends(get_current_user)):
try:
session_id = str(ObjectId())
chat_session = {
"user_id": username,
"session_id": session_id,
"created_at": datetime.utcnow(),
"last_accessed": datetime.utcnow(),
"title": "New Chat"
}
query.create_chat_session(chat_session)
return {"message": "Chat session created", "session_id": session_id}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/chat-sessions')
async def get_user_chat_sessions(username: str = Depends(get_current_user)):
try:
sessions = query.get_user_chat_sessions(username)
return sessions
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/chat-sessions/{session_id}')
async def delete_chat_session(session_id: str, username: str = Depends(get_current_user)):
try:
result = query.delete_chat_session(session_id, username)
if result["session_deleted"]:
return {
"message": "Chat session and associated chats deleted successfully",
"chats_deleted": result["chats_deleted"]
}
raise HTTPException(status_code=404, detail="Chat session not found or unauthorized")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.put('/chat-sessions/{session_id}/title')
async def update_chat_title(
session_id: str,
title_data: ChatSessionTitleUpdate,
username: str = Depends(get_current_user)
):
try:
new_title = title_data.title
if not query.verify_session(session_id, username):
raise HTTPException(status_code=404, detail="Chat session not found or unauthorized")
if query.update_chat_session_title(session_id, new_title):
return {
'message': 'Chat session title updated successfully',
'session_id': session_id,
'new_title': new_title
}
raise HTTPException(status_code=500, detail="Failed to update chat session title")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/chat-sessions/all')
async def delete_all_sessions_and_chats(username: str = Depends(get_current_user)):
try:
result = query.delete_all_user_sessions_and_chats(username)
return {
"message": "Successfully deleted all chat sessions and chats",
"deleted_chats": result["deleted_chats"],
"deleted_sessions": result["deleted_sessions"]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/chats/session/{session_id}')
async def get_session_chats(session_id: str, username: str = Depends(get_current_user)):
try:
chats = query.get_session_chats(session_id, username)
return chats
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\language.py
```python
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from app.middleware.auth import get_current_user
from app.database.database_query import DatabaseQuery
router = APIRouter()
query = DatabaseQuery()
class LanguageSettings(BaseModel):
language: str
@router.post('/language', status_code=201)
async def set_language(
language_data: LanguageSettings,
username: str = Depends(get_current_user)
):
try:
language = language_data.language
if not language:
raise HTTPException(status_code=400, detail="Language is required")
result = query.set_user_language(username, language)
return {
"message": "Language set successfully",
"language": result["language"]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/language')
async def get_language(username: str = Depends(get_current_user)):
try:
language = query.get_user_language(username)
if language is None:
raise HTTPException(status_code=404, detail="Language not set")
return {
"message": "Language retrieved successfully",
"language": language
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/language')
async def delete_language(username: str = Depends(get_current_user)):
try:
result = query.delete_user_language(username)
if not result:
raise HTTPException(status_code=404, detail="Language not found or already deleted")
return {
"message": "Language deleted successfully"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\location.py
```python
# app/routers/location.py
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from app.database.database_query import DatabaseQuery
from app.middleware.auth import get_current_user
router = APIRouter()
query = DatabaseQuery()
class LocationData(BaseModel):
location: str
@router.post('/location', status_code=201)
async def add_location(location_data: LocationData, username: str = Depends(get_current_user)):
try:
location = location_data.location
if not location:
raise HTTPException(status_code=400, detail="Location is required")
query.add_or_update_location(username, location)
return {'message': 'Location added/updated successfully'}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/location')
async def get_location(username: str = Depends(get_current_user)):
try:
location_data = query.get_location(username)
if not location_data:
raise HTTPException(status_code=404, detail="No location found for this user")
return {'location': location_data['location']}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\preferences.py
```python
# app/routers/preferences.py
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from typing import Dict, Any
from app.database.database_query import DatabaseQuery
from app.middleware.auth import get_current_user
router = APIRouter()
query = DatabaseQuery()
class ThemeSettings(BaseModel):
theme: bool
@router.get('/preferences')
async def get_preferences(username: str = Depends(get_current_user)):
try:
user_preferences = query.get_user_preferences(username)
return {'preferences': user_preferences}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post('/preferences')
async def set_preferences(preferences: Dict[str, Any], username: str = Depends(get_current_user)):
try:
preferences_result = query.set_user_preferences(username, preferences)
return {
'message': 'Preferences updated successfully',
'preferences': preferences_result
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/theme')
async def get_theme(username: str = Depends(get_current_user)):
try:
user_theme = query.get_user_theme(username)
return {'theme': user_theme}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post('/theme')
async def set_theme(theme_data: ThemeSettings, username: str = Depends(get_current_user)):
try:
theme = theme_data.theme
theme_data = query.set_user_theme(username, theme)
return {
'message': 'Theme updated successfully',
'theme': theme_data['theme']
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\profile.py
```python
from fastapi import APIRouter, Depends, HTTPException, Body
from pydantic import BaseModel, EmailStr, validator
from typing import Optional
from werkzeug.security import generate_password_hash
from app.database.database_query import DatabaseQuery
from app.middleware.auth import get_current_user
router = APIRouter()
query = DatabaseQuery()
class ProfileUpdateRequest(BaseModel):
email: Optional[EmailStr] = None
password: Optional[str] = None
name: Optional[str] = None
age: Optional[int] = None
@validator('password')
def password_length(cls, v):
if v is not None and len(v) < 6:
raise ValueError('Password must be at least 6 characters')
return v
@validator('age')
def age_range(cls, v):
if v is not None and (v < 13 or v > 120):
raise ValueError('Age must be between 13 and 120')
return v
@router.get('/profile')
async def get_profile(username: str = Depends(get_current_user)):
try:
user = query.get_user_profile(username)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return {
'username': user['username'],
'email': user['email'],
'name': user['name'],
'age': user['age'],
'created_at': user['created_at']
}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.put('/profile')
async def update_profile(
update_data: ProfileUpdateRequest = Body(...),
username: str = Depends(get_current_user)
):
try:
update_fields = {}
if update_data.email:
if not query.is_valid_email(update_data.email):
raise HTTPException(status_code=400, detail="Invalid email format")
update_fields['email'] = update_data.email
if update_data.password:
update_fields['password'] = generate_password_hash(update_data.password)
if update_data.name:
update_fields['name'] = update_data.name
if update_data.age is not None:
update_fields['age'] = update_data.age
if update_fields:
if query.update_user_profile(username, update_fields):
return {"message": "Profile updated successfully"}
return {"message": "No changes made"}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/profile')
async def delete_account(username: str = Depends(get_current_user)):
try:
if query.delete_user_account(username):
return {"message": "Account deleted successfully"}
raise HTTPException(status_code=404, detail="User not found")
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/delete-account-permanently')
async def delete_account_permanently(username: str = Depends(get_current_user)):
try:
result = query.delete_account_permanently(username)
if result['success']:
return {
'message': 'Account and all associated data deleted successfully',
'details': result['deleted_data']
}
else:
raise HTTPException(status_code=500, detail="Failed to delete account")
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
```
### app\routers\questionnaire.py
```python
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from typing import Dict, Any
from app.database.database_query import DatabaseQuery
from app.middleware.auth import get_current_user
router = APIRouter()
query = DatabaseQuery()
class QuestionnaireSubmission(BaseModel):
answers: Dict[str, Any]
@router.post('/questionnaires', status_code=201)
async def submit_questionnaire(
submission: QuestionnaireSubmission,
username: str = Depends(get_current_user)
):
try:
if not submission.answers:
raise HTTPException(status_code=400, detail="Answers are required")
questionnaire_id = query.submit_questionnaire(username, submission.answers)
return {
'message': 'Questionnaire submitted successfully',
'questionnaire_id': questionnaire_id
}
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.get('/questionnaires')
async def get_questionnaire(username: str = Depends(get_current_user)):
try:
questionnaire = query.get_latest_questionnaire(username)
if not questionnaire:
return {'message': 'No questionnaire found', 'data': None}
return {'message': 'Success', 'data': questionnaire}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.put('/questionnaires/{questionnaire_id}')
async def update_questionnaire(
questionnaire_id: str,
submission: QuestionnaireSubmission,
username: str = Depends(get_current_user)
):
try:
if not submission.answers:
raise HTTPException(status_code=400, detail="Answers are required")
if query.update_questionnaire(questionnaire_id, username, submission.answers):
return {'message': 'Questionnaire updated successfully'}
raise HTTPException(
status_code=404,
detail='Questionnaire not found or unauthorized'
)
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.delete('/questionnaires/{questionnaire_id}')
async def delete_questionnaire(
questionnaire_id: str,
username: str = Depends(get_current_user)
):
try:
if query.delete_questionnaire(questionnaire_id, username):
return {'message': 'Questionnaire deleted successfully'}
raise HTTPException(
status_code=404,
detail='Questionnaire not found or unauthorized'
)
except Exception as e:
if isinstance(e, HTTPException):
raise e
raise HTTPException(status_code=500, detail=str(e))
@router.get('/check-answers')
async def check_answers(username: str = Depends(get_current_user)):
try:
answered_count = query.count_answered_questions(username)
return {'has_at_least_two_answers': answered_count >= 2}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get('/check-questionnaire')
async def check_questionnaire_submission(username: str = Depends(get_current_user)):
try:
questionnaire = query.get_latest_questionnaire(username)
has_questionnaire = questionnaire is not None
return {'has_questionnaire': has_questionnaire}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
```
### app\services\__init__.py
```python
# app/services/__init__.py
from app.services.image_classification_vit import SkinDiseaseClassifier
from app.services.llm_model import Model
from app.services.chathistory import ChatSession
from app.services.environmental_condition import EnvironmentalData
from app.services.prompts import *
from app.services.RAG_evaluation import RAGEvaluation
from app.services.skincare_scheduler import SkinCareScheduler
from app.services.vector_database_search import VectorDatabaseSearch
from app.services.websearch import WebSearch
from app.services.wheel import EnvironmentalConditions
__all__ = [
"AISkinDetector",
"SkinDiseaseClassifier",
"Model",
"ChatSession",
"EnvironmentalData",
"RAGEvaluation",
"SkinCareScheduler",
"VectorDatabaseSearch",
"WebSearch",
"EnvironmentalConditions",
]
```
### app\services\agentic_prompt.py
```python
from typing import Dict
def _append_personalization(prompt: str, user_data: Dict) -> str:
personalized_tool = user_data.get('personalized_tool_name')
if user_data.get('has_personalized_data') and personalized_tool:
prompt += (
"\n\n## Personalized Data Access:\n"
f"Call `{personalized_tool}` exactly once after search tools to retrieve patient context. "
"IMPORTANT: Analyze how the personalized data relates to the user's current query. "
"Don't just list the data - explain how their specific conditions, medications, or history "
"impacts the topic they're asking about. Make connections between their profile and the condition."
)
else:
prompt += (
"\n\nNo personalization data is available; omit the `## Personalization Recommendation` section."
)
environmental_tool = user_data.get('environmental_tool_name')
if user_data.get('has_environmental_data') and environmental_tool:
prompt += (
"\n\n## Environmental Data Access:\n"
f"Call `{environmental_tool}` exactly once when relevant. "
"CRITICAL: Don't just list environmental statistics. Instead, analyze how the specific "
"environmental factors (UV index, humidity, pollution, temperature) affect the skin condition "
"the user is asking about. Explain the mechanisms and provide actionable advice based on their location."
)
else:
prompt += (
"\n\nEnvironmental conditions are unavailable; omit the `## Environmental Condition` section."
)
if user_data.get('language', 'english').lower() != 'english':
prompt += (
f"\n\nRespond in {user_data.get('language')} while keeping the JSON structure intact."
)
return prompt
def _append_document_guidance(prompt: str, user_data: Dict) -> str:
document_info = user_data.get('document_info') or {}
tool_name = user_data.get('document_tool_name')
document_path = document_info.get('path')
if document_info and tool_name and document_path:
document_name = document_info.get('name') or 'Uploaded document'
prompt += (
"\n\n## Uploaded Document\n"
f"The user has provided `{document_name}` located at `{document_path}`. "
f"Call `{tool_name}` exactly once to convert it to Markdown. "
"Extract medically relevant findings and incorporate them into your response. "
"DO NOT include the document path or name in the references array - only web/vector search results belong there."
)
return prompt
def _append_image_guidance(prompt: str, user_data: Dict) -> str:
image_info = user_data.get('image_info') or {}
tool_name = user_data.get('image_tool_name')
image_path = image_info.get('path')
if image_info and tool_name and image_path:
image_name = image_info.get('name') or 'Uploaded image'
prompt += (
"\n\n## Uploaded Image\n"
f"The user shared `{image_name}` located at `{image_path}`. "
f"Call `{tool_name}` exactly once to analyze the skin photo. "
"Incorporate the analysis results into your response but DO NOT add image paths to references array. "
"References should only contain search result URLs/sources."
)
return prompt
def _format_json_guidance(user_data: Dict) -> str:
references_instruction = (
"Populate `references` ONLY with URLs/sources from get_web_search or get_vector_search results. "
"NEVER include uploaded document paths, image paths, or tool names in references. Please use same citation which you will includein response if you get similar citation from web search and vector datat with same book and page number then consider itas singlecitationand usesmae ciation in reponse and in citation/refrences.section"
if user_data.get('include_references', True)
else "Set `references` to an empty array."
)
keywords_instruction = (
"Provide 4-6 concise medical keywords related to the skin condition discussed."
if user_data.get('include_keywords', True)
else "Set `keywords` to an empty array."
)
images_instruction = (
"Include up to 3 image URLs from get_image_search when they help visualize the condition. and If needed image url more than one tool calling then do it together not one by one"
if user_data.get('include_images', True)
else "Set `images` to an empty array."
)
has_personalization = user_data.get('has_personalized_data')
has_environmental = user_data.get('has_environmental_data')
personalization_instruction = (
"In `## Personalization Recommendation`: Analyze how the user's specific medical history, "
"current medications, allergies, and conditions relate to their query. Don't just list their data - "
"explain the interactions and provide tailored advice based on their profile."
if has_personalization
else "Omit the `## Personalization Recommendation` section."
)
environmental_instruction = (
"In `## Environmental Condition`: Explain how current environmental factors in their location "
"specifically impact the skin condition they're asking about. Provide actionable advice for "
"protection and management based on UV levels, humidity, pollution, and temperature."
if has_environmental
else "Omit the `## Environmental Condition` section."
)
return (
"\nYour final response MUST be valid JSON Strictly follow below structure:\n"
"{\n"
" \"response\": \"Start with `## Response from References` containing evidence-based information with citations [1], [2]. "
"Add `## Personalization Recommendation` analyzing how their profile affects this condition. "
"Add `## Environmental Condition` explaining environmental impact on their skin.\",\n"
" \"references\": [\"ONLY search result URLs/sources, NO file paths\"],\n"
" \"keywords\": [\"keyword1\", \"keyword2\", ...],\n"
" \"images\": [\"image_urls_from_search\"]\n"
"}\n\n"
f"{references_instruction}\n"
f"{keywords_instruction}\n"
f"{images_instruction}\n"
f"{personalization_instruction}\n"
f"{environmental_instruction}\n\n"
"CRITICAL RULES:\n"
"- Citations [1], [2] should ONLY reference search results, not uploaded files\n"
"- Personalization and Environmental sections must analyze impact, not list data\n"
"- Always call get_image_search for visual conditions unless non-medical query\n"
)
def get_web_search_prompt(user_data: Dict) -> str:
prompt_lines = [
"You are Dr. DermAI, an evidence-based dermatology consultant. Do not hallucination , if you donot get context and not have valid answerjust say I do not know.",
"",
"## QUERY ASSESSMENT:",
"First determine if the query is medical/dermatological. If not, politely decline.",
"Please invoke each tool one at a time, waiting for the response before invoking the next. If you need to use the same tool multiple times—for example, for different topics—then call that tool multiple times just vector serach adn web search tool, but still one at a time.",
"if there multiple topics then call web search multiple time for each topic"
"if the query of user required more than one web search then do it but given answer by combining and use same structure do not strictly give 2 separte json do not make any silly mistake."
"## TOOL EXECUTION ORDER FOR MEDICAL QUERIES:",
]
step_num = 1
if user_data.get('image_info') and user_data.get('image_tool_name'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('image_tool_name')}` FIRST if skin image uploaded"
)
step_num += 1
prompt_lines.extend([
f"{step_num}. Call `get_web_search` with the user's query to gather current medical knowledge",
f"{step_num + 1}. Call `get_image_search` with relevant terms (e.g., 'eczema rash', 'psoriasis patches') "
f"to provide visual references UNLESS the condition is internal or non-visual",
])
step_num += 2
if user_data.get('document_info') and user_data.get('document_tool_name'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('document_tool_name')}` to analyze uploaded documents"
)
step_num += 1
if user_data.get('has_personalized_data'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('personalized_tool_name')}` to get user's medical profile"
)
step_num += 1
if user_data.get('has_environmental_data'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('environmental_tool_name')}` to get environmental factors"
)
step_num += 1
prompt_lines.extend([
f"{step_num}. Synthesize all information into structured JSON response",
"",
"## IMAGE SEARCH GUIDELINES:",
"ALWAYS use get_image_search for:",
"- Rashes, lesions, spots, patches, bumps",
"- Skin discoloration, texture changes",
"- Hair/nail conditions with visible symptoms",
"- Any condition where visual reference aids understanding",
"",
"SKIP get_image_search only for:",
"- Non-medical queries",
"- Internal conditions without skin manifestations",
"- General health questions without visual components",
"",
"## IMPORTANT NOTES:",
"- Maximum 1 call per tool type",
"- Web search should focus on recent, evidence-based sources",
"- Image search should use specific descriptive terms",
"- Personalization must analyze relevance to query, not list profile data",
"- Environmental section must explain impact on the specific condition",
])
prompt = "\n".join(prompt_lines)
recent_history = user_data.get('recent_history')
if recent_history:
prompt += (
"\n\n## Recent Conversation:\n"
f"{recent_history}\n"
)
prompt = _append_document_guidance(prompt, user_data)
prompt = _append_image_guidance(prompt, user_data)
prompt += _format_json_guidance(user_data)
prompt = _append_personalization(prompt, user_data)
return prompt.strip()
def get_vector_search_prompt(user_data: Dict) -> str:
prompt_lines = [
"You are Dr. DermAI with access to a curated dermatology knowledge base. Do not hallucination , if you donot get context and not have valid answerjust say I do not know.",
"",
"## QUERY ASSESSMENT:",
"Determine if medical/dermatological. If not, politely decline.",
"Please invoke each tool one at a time, waiting for the response before invoking the next. If you need to use the same tool multiple times—for example, for different topics—then call that tool multiple times just vector serach adn web search tool, but still one at a time.",
"if there multiple topics then call web search multiple time for each topic",
"if the query of user required more than one vector query search then do it but given answer by combining and use same structure do not strictly give 2 separte json do not make any silly mistake."
"## TOOL EXECUTION ORDER FOR MEDICAL QUERIES:",
]
step_num = 1
if user_data.get('image_info') and user_data.get('image_tool_name'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('image_tool_name')}` FIRST if skin image uploaded"
)
step_num += 1
prompt_lines.extend([
f"{step_num}. Call `get_vector_search` to retrieve authoritative medical passages",
f"{step_num + 1}. Call `get_image_search` with condition-specific terms "
f"(e.g., 'melanoma ABCDE', 'atopic dermatitis flexural') for visual aids",
])
step_num += 2
if user_data.get('document_info') and user_data.get('document_tool_name'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('document_tool_name')}` for document analysis"
)
step_num += 1
if user_data.get('has_personalized_data'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('personalized_tool_name')}` for user profile"
)
step_num += 1
if user_data.get('has_environmental_data'):
prompt_lines.append(
f"{step_num}. Call `{user_data.get('environmental_tool_name')}` for environmental data"
)
step_num += 1
prompt_lines.extend([
f"{step_num}. Create comprehensive JSON response",
"",
"## IMAGE SEARCH REQUIREMENTS:",
"MANDATORY for these conditions (use specific medical terms):",
"- Inflammatory: psoriasis, eczema, dermatitis, rosacea",
"- Infectious: fungal infections, bacterial infections, viral exanthems",
"- Neoplastic: melanoma, basal cell, squamous cell, keratosis",
"- Pigmentary: melasma, vitiligo, post-inflammatory changes",
"- Hair/Nail: alopecia patterns, onychomycosis, nail psoriasis",
"",
"Use descriptive search terms like:",
"- 'plaque psoriasis elbows knees'",
"- 'atopic dermatitis infant face'",
"- 'tinea corporis ring rash'",
"",
"## PERSONALIZATION ANALYSIS:",
"When using personalized data:",
"- Identify drug interactions with current medications",
"- Consider contraindications based on health conditions",
"- Adjust recommendations for age/pregnancy/allergies",
"- Explain how their specific profile affects treatment options",
"",
"## ENVIRONMENTAL IMPACT ANALYSIS:",
"When using environmental data:",
"- High UV: Explain photosensitivity risks, sun protection needs",
"- Low humidity: Impact on barrier function, moisturization needs",
"- High pollution: Oxidative stress, cleansing requirements",
"- Temperature extremes: Vasodilation/constriction effects",
"",
"Never just list the data - explain the mechanisms and provide targeted advice.",
])
prompt = "\n".join(prompt_lines)
recent_history = user_data.get('recent_history')
if recent_history:
prompt += (
"\n\n## Recent Conversation:\n"
f"{recent_history}\n"
)
prompt = _append_document_guidance(prompt, user_data)
prompt = _append_image_guidance(prompt, user_data)
prompt += _format_json_guidance(user_data)
prompt = _append_personalization(prompt, user_data)
return prompt.strip()
```
### app\services\chathistory.py
```python
from app.database.database_query import DatabaseQuery
import os
import jwt
from dotenv import load_dotenv
from typing import Optional, Dict, List
from bson import ObjectId
from datetime import datetime
load_dotenv()
jwt_secret_key = os.getenv('JWT_SECRET_KEY')
query = DatabaseQuery()
class ChatSession:
def __init__(self, token: str , session_id: str):
self.token = token
self.session_id = session_id
self.chats = []
self.identity = self._decode_token(token)
self.query = query
def _decode_token(self, token: str) -> str:
try:
decoded_token = jwt.decode(token, jwt_secret_key, algorithms=["HS256"])
identity = decoded_token['sub']
return identity
except jwt.ExpiredSignatureError:
raise ValueError("The token has expired.")
except jwt.InvalidTokenError:
raise ValueError("Invalid token.")
except Exception as e:
raise ValueError(f"Failed to decode token: {e}")
def get_user_preferences(self) -> dict:
current_user = self.identity
preferences = self.query.get_user_preferences(current_user)
if preferences is not None:
return preferences
raise ValueError("Failed to fetch user preferences.")
def get_personalized_recommendation(self) -> Optional[str]:
current_user = self.identity
response = self.query.get_latest_questionnaire(current_user)
if not response:
return None
answers = response.get('answers', {})
if not answers:
return None
def format_answer(answer):
if answer is None:
return None
if isinstance(answer, str):
stripped_answer = answer.strip().lower()
if stripped_answer in ['none', ''] or len(stripped_answer) < 3:
return None
return stripped_answer
if isinstance(answer, list):
filtered_answer = [item for item in answer if "Other" not in item
and item.strip().lower() not in ['none', '']
and len(item.strip()) >= 3]
return ", ".join(filtered_answer) if filtered_answer else None
return answer
questions = {
"skinType": "How would you describe your skin type?",
"currentConditions": "Do you currently have any skin conditions?",
"autoImmuneConditions": "Do you have a history of autoimmune or hormonal conditions?",
"allergies": "Do you have any known allergies to skincare ingredients?",
"medications": "Are you currently taking any medications that might affect your skin?",
"hormonal": "Do you experience hormonal changes that affect your skin?",
"diet": "Have you noticed any foods that trigger skin reactions?",
"diabetes": "Do you have diabetes?",
"outdoorTime": "How much time do you spend outdoors during the day?",
"sleep": "How many hours of sleep do you get on average?",
"familyHistory": "Do you have a family history of skin conditions?",
"products": "What skincare products are you currently using?"
}
valid_answers = {key: format_answer(answers.get(key))
for key in questions
if format_answer(answers.get(key)) is not None}
if not valid_answers:
return None
formatted_response = []
for key, answer in valid_answers.items():
question = questions.get(key)
formatted_response.append(f"question: {question}\nUser answer: {answer}")
profile = self.get_profile()
name = profile.get('name', 'Unknown')
age = profile.get('age', 'Unknown')
return f"user name: {name}\nuser age: {age}\n\n" + "\n\n".join(formatted_response)
def create_new_session(self, title: str = None) -> bool:
current_user = self.identity
session_id = str(ObjectId())
chat_session = {
"user_id": current_user,
"session_id": session_id,
"created_at": datetime.utcnow(),
"last_accessed": datetime.utcnow(),
"title": title if title else "New Chat"
}
try:
self.query.create_chat_session(chat_session)
self.session_id = session_id
return True
except Exception as e:
raise Exception(f"Failed to create session: {str(e)}")
def verify_session_exists(self, session_id: str) -> bool:
current_user = self.identity
return self.query.verify_session(session_id, current_user)
def validate_session(self, session_id: Optional[str] = None, title: str = None) -> bool:
if not session_id or not session_id.strip():
return self.create_new_session(title=title)
if self.verify_session_exists(session_id):
self.session_id = session_id
return self.load_chat_history()
return self.create_new_session(title=title)
def load_session(self, session_id: str) -> bool:
return self.validate_session(session_id)
def load_chat_history(self) -> bool:
if not self.session_id:
raise ValueError("No session ID provided.")
current_user = self.identity
try:
self.chats = self.query.get_session_chats(self.session_id, current_user)
return True
except Exception as e:
raise Exception(f"Failed to load chat history: {str(e)}")
def get_chat_history(self) -> List[Dict]:
return self.chats
def format_history(self) -> str:
formatted_chats = []
for chat in self.chats:
query = chat.get('query', '').strip()
response = chat.get('response', '').strip()
if query and response:
formatted_chats.append(f"User: {query}")
formatted_chats.append(f"dermatologist Dr DermAI: {response}")
return "\n".join(formatted_chats) if formatted_chats else ""
def save_chat(self, chat_data: Dict) -> bool:
if not self.session_id:
raise ValueError("No active session to save chat")
current_user = self.identity
data = {
"user_id": current_user,
"session_id": self.session_id,
"query": chat_data.get("query", "").strip(),
"response": chat_data.get("response", "").strip(),
"references": chat_data.get("references", []),
"page_no": chat_data.get("page_no", []),
"keywords": chat_data.get("keywords", []),
"images": chat_data.get("images", []),
"context": chat_data.get("context", ""),
"timestamp": datetime.utcnow(),
"chat_id": str(ObjectId())
}
try:
if self.query.create_chat(data):
self.query.update_last_accessed_time(self.session_id)
self.chats.append(data)
return True
return False
except Exception as e:
raise Exception(f"Failed to save chat: {str(e)}")
def get_name_and_age(self):
current_user = self.identity
try:
user_profile = self.query.get_user_profile(current_user)
return user_profile
except Exception as e:
raise Exception(f"Failed to get user name and age: {str(e)}")
def get_profile(self):
current_user = self.identity
try:
user = query.get_user_profile(current_user)
if not user:
return {'error': 'User not found'}
return {
'username': user['username'],
'email': user['email'],
'name': user['name'],
'age': user['age'],
'created_at': user['created_at']
}
except Exception as e:
return {'error': str(e)}
def update_title(self , sessionId , new_title):
query.update_chat_session_title(sessionId, new_title)
def get_city(self) -> Optional[str]:
current_user = self.identity
try:
location_data = self.query.get_location(current_user)
if location_data and 'location' in location_data:
return location_data['location']
return None
except Exception as e:
raise Exception(f"Failed to get user city: {str(e)}")
def get_language(self) -> Optional[str]:
current_user = self.identity
try:
language = self.query.get_user_language(current_user)
if not language :
return "english"
else:
return language
return None
except Exception as e:
raise Exception(f"Failed to get user city: {str(e)}")
def get_language(self) -> Optional[str]:
current_user = self.identity
try:
language = self.query.get_user_language(current_user)
if not language :
return "english"
else:
return language
return None
except Exception as e:
raise Exception(f"Failed to get user city: {str(e)}")
def get_today_schedule(self):
data = self.query.get_today_schedule(user_id=self.identity)
if not data:
return ""
return data
def save_schedule(self, schedule_data):
return self.query.save_schedule(user_id=self.identity, schedule_data=schedule_data)
def get_last_seven_days_schedules(self):
data = self.query.get_last_seven_days_schedules(user_id=self.identity)
if not data:
return ""
return data
def save_details(self, session_id, context, query, response, rag_start_time, rag_end_time):
data = self.query.save_rag_interaction(
user_id="admin",
session_id=session_id,
context=context,
query=query,
response=response,
rag_start_time=rag_start_time,
rag_end_time=rag_end_time
)
return data
def get_save_details(self, page: int, page_size: int) -> dict:
data = self.query.get_rag_interactions(
user_id="admin",
page=page,
page_size=page_size
)
return data
def log_user_image_upload(self):
"""Log an image upload for the current user"""
try:
return self.query.log_image_upload(self.identity)
except Exception as e:
raise ValueError(f"Failed to log image upload: {e}")
def get_user_daily_uploads(self):
"""Get number of images uploaded by current user in the last 24 hours"""
try:
return self.query.get_user_daily_uploads(self.identity)
except Exception as e:
raise ValueError(f"Failed to get user daily uploads: {e}")
def get_user_last_upload_time(self):
"""Get the timestamp of current user's most recent image upload"""
try:
return self.query.get_user_last_upload_time(self.identity)
except Exception as e:
raise ValueError(f"Failed to get user's last upload time: {e}")
```
### app\services\environmental_condition.py
```python
import requests
from bs4 import BeautifulSoup
class EnvironmentalData:
def __init__(self, city):
self.city = city
self.aqi_url = f"https://api.waqi.info/feed/{city}/?token=466cde4d55e7c5d6cc658ad9c391214b593f46b9"
self.uv_url = f"https://www.weatheronline.co.uk/Pakistan/{city}/UVindex.html"
def fetch_aqi_data(self):
try:
response = requests.get(self.aqi_url)
data = response.json()
if data["status"] == "ok":
return {
"Temperature": data["data"]["iaqi"].get("t", {}).get("v", "N/A"),
"Humidity": data["data"]["iaqi"].get("h", {}).get("v", "N/A"),
"Wind Speed": data["data"]["iaqi"].get("w", {}).get("v", "N/A"),
"Pressure": data["data"]["iaqi"].get("p", {}).get("v", "N/A"),
"AQI": data["data"].get("aqi", "N/A"),
"Dominant Pollutant": data["data"].get("dominentpol", "N/A"),
}
return self.get_default_aqi_data()
except:
return self.get_default_aqi_data()
def get_default_aqi_data(self):
return {
"Temperature": "N/A",
"Humidity": "N/A",
"Wind Speed": "N/A",
"Pressure": "N/A",
"AQI": "N/A",
"Dominant Pollutant": "N/A"
}
def fetch_uv_data(self):
try:
response = requests.get(self.uv_url)
soup = BeautifulSoup(response.text, 'html.parser')
gr1_elements = soup.find_all(class_='gr1')
if gr1_elements:
tr_elements = gr1_elements[0].find_all('tr')
if len(tr_elements) > 1:
second_tr = tr_elements[1]
td_elements = second_tr.find_all('td')
if len(td_elements) > 1:
return int(td_elements[1].text.strip())
return "N/A"
except:
return "N/A"
def get_environmental_data(self):
aqi_data = self.fetch_aqi_data()
uv_index = self.fetch_uv_data()
environmental_data = {
"Temperature": f"{aqi_data['Temperature']} °C" if aqi_data['Temperature'] != "N/A" else "N/A",
"Humidity": f"{aqi_data['Humidity']} %" if aqi_data['Humidity'] != "N/A" else "N/A",
"Wind Speed": f"{aqi_data['Wind Speed']} m/s" if aqi_data['Wind Speed'] != "N/A" else "N/A",
"Pressure": f"{aqi_data['Pressure']} hPa" if aqi_data['Pressure'] != "N/A" else "N/A",
"Air Quality Index": aqi_data['AQI'],
"Dominant Pollutant": aqi_data["Dominant Pollutant"],
"UV_Index": uv_index
}
return environmental_data
```
### app\services\google_agent_service.py
```python
import asyncio
import json
import logging
import os
import re
from datetime import datetime, timezone
from typing import Any, AsyncGenerator, Callable, Dict, List, Optional, Tuple
from google.adk.agents import Agent
from google.adk.agents.run_config import RunConfig, StreamingMode
from google.adk.runners import InMemoryRunner
from google.adk.tools import FunctionTool
from google.genai import types
from app.services.agentic_prompt import (
get_vector_search_prompt,
get_web_search_prompt,
)
from app.services.chathistory import ChatSession
from app.services.environmental_condition import EnvironmentalData
from app.services.tools import (
analyze_skin_image,
convert_document_to_markdown,
get_image_search,
get_vector_search,
get_web_search,
)
logger = logging.getLogger(__name__)
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")
DEFAULT_MODEL_NAME = os.getenv("GEMINI_MODEL", "gemini-2.0-flash-exp")
PERSONALIZED_TOOL_NAME = "get_personalized_context"
ENVIRONMENT_TOOL_NAME = "get_environmental_context"
DOCUMENT_CONVERSION_TOOL_NAME = "convert_uploaded_document"
IMAGE_ANALYSIS_TOOL_NAME = "analyze_skin_image"
if not os.getenv("GOOGLE_API_KEY") and GOOGLE_API_KEY:
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
if os.name == "nt":
try:
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
except Exception:
pass
class GoogleAgentService:
"""Chat orchestrator that streams responses from a Google ADK agent."""
def __init__(
self,
token: str,
session_id: Optional[str] = None,
document: Optional[Dict[str, Any]] = None,
image: Optional[Dict[str, Any]] = None,
) -> None:
self.token = token
self.session_id = session_id
self.chat_session = ChatSession(token, session_id)
self.user_preferences = self._load_user_preferences()
self.language = self.chat_session.get_language() or "english"
self.user_profile = self._load_user_profile()
self.user_city = self.chat_session.get_city()
self.environment_data = self._load_environmental_data()
self.document = document
self.image = image
async def process_message_async(
self, query: str
) -> AsyncGenerator[Dict[str, Any], None]:
if not GOOGLE_API_KEY:
error = "Google API key is not configured."
logger.error(error)
yield {"type": "error", "content": error}
return
try:
session_id = self._ensure_valid_session(query)
agent_mode = "web" if self.user_preferences.get("websearch") else "vector"
user_data = self._prepare_user_data()
agent = self._build_agent(agent_mode, user_data)
runner = InMemoryRunner(agent=agent)
await runner.session_service.create_session(
app_name=runner.app_name,
user_id=self.chat_session.identity,
session_id=session_id,
)
user_message = types.Content(
role="user",
parts=[types.Part(text=query)],
)
run_config = RunConfig(streaming_mode=StreamingMode.SSE)
tool_calls: List[Dict[str, Any]] = []
tool_call_map: Dict[str, Dict[str, Any]] = {}
collected_images: List[str] = []
collected_references: List[str] = []
streamed_text = ""
final_text = ""
pending_token_buffer = ""
def emit_word_chunks(delta: str, *, final: bool = False) -> List[str]:
nonlocal pending_token_buffer
pending_token_buffer += delta
chunks: List[str] = []
while pending_token_buffer:
match = re.search(r'\s', pending_token_buffer)
if not match:
break
idx = match.end()
token = pending_token_buffer[:idx]
pending_token_buffer = pending_token_buffer[idx:]
if token:
chunks.append(token)
if final and pending_token_buffer:
chunks.append(pending_token_buffer)
pending_token_buffer = ""
return chunks
async for event in runner.run_async(
user_id=self.chat_session.identity,
session_id=session_id,
new_message=user_message,
run_config=run_config,
):
if event.error_message:
logger.error("Agent error: %s", event.error_message)
yield {"type": "error", "content": event.error_message}
return
for function_call in event.get_function_calls():
call_entry = {
"id": function_call.id,
"tool_name": function_call.name,
"arguments": function_call.args or {},
}
tool_call_map[function_call.id] = call_entry
tool_calls.append(call_entry)
yield {
"type": "tool_call",
"id": function_call.id,
"tool_name": function_call.name,
"arguments": function_call.args or {},
}
for function_response in event.get_function_responses():
response_payload = function_response.response or {}
call_entry = tool_call_map.get(function_response.id)
if call_entry is not None:
call_entry["result"] = response_payload
if isinstance(response_payload, dict):
if function_response.name == "get_image_search":
collected_images.extend(response_payload.get("images", []))
if response_payload.get("references"):
collected_references.extend(response_payload["references"])
yield {
"type": "tool_result",
"id": function_response.id,
"tool_name": function_response.name,
"result": response_payload,
}
text_segment = self._extract_text(event)
if not text_segment:
continue
if event.partial:
streamed_text += text_segment
for token in emit_word_chunks(text_segment):
yield {"type": "chunk", "content": token}
else:
final_text = text_segment
if streamed_text and text_segment.startswith(streamed_text):
delta = text_segment[len(streamed_text) :]
else:
delta = text_segment
if text_segment:
streamed_text = text_segment
for token in emit_word_chunks(delta, final=True):
yield {"type": "chunk", "content": token}
for leftover in emit_word_chunks("", final=True):
if leftover:
yield {"type": "chunk", "content": leftover}
parsed_response = self._parse_agent_response(final_text or streamed_text)
response_text, keywords, response_images, response_refs = parsed_response
merged_images = self._dedupe_list(collected_images + response_images)
merged_references = self._dedupe_list(collected_references + response_refs)
context_chunks: List[str] = []
if self.document and self.document.get("path"):
context_chunks.append(f"document:{self.document.get('path')}")
if self.image and self.image.get("path"):
context_chunks.append(f"image:{self.image.get('path')}")
context_payload = " ".join(context_chunks)
chat_payload = {
"query": query,
"response": response_text,
"references": merged_references,
"keywords": keywords,
"images": merged_images,
"context": context_payload,
"timestamp": datetime.now(timezone.utc).isoformat(),
"session_id": session_id,
"tool_calls": tool_calls,
}
saved = self.chat_session.save_chat(chat_payload)
yield {
"type": "completed",
"saved": saved,
"session_id": session_id,
"response": response_text,
"keywords": keywords,
"references": merged_references,
"images": merged_images,
"tool_calls": tool_calls,
}
except Exception as exc:
logger.error("Agent streaming failure: %s", exc, exc_info=True)
yield {"type": "error", "content": f"Generation failed: {exc}"}
def _build_agent(self, mode: str, user_data: Dict[str, Any]) -> Agent:
prompt = (
get_web_search_prompt(user_data)
if mode == "web"
else get_vector_search_prompt(user_data)
)
search_tool = get_web_search if mode == "web" else get_vector_search
tools: List[FunctionTool] = []
if self.image and self.image.get("path"):
tools.append(FunctionTool(self._create_image_tool()))
tools.extend(
[
FunctionTool(search_tool),
FunctionTool(get_image_search),
]
)
if self.document and self.document.get("path"):
tools.append(FunctionTool(self._create_document_tool()))
if user_data.get("has_personalized_data"):
personalized_tool = self._create_personalized_data_tool(
user_data.get("personalized_data", "")
)
tools.append(FunctionTool(personalized_tool))
if user_data.get("has_environmental_data"):
environmental_tool = self._create_environmental_data_tool(
user_data.get("environmental_payload") or {}
)
tools.append(FunctionTool(environmental_tool))
agent = Agent(
name="DermAI",
model=DEFAULT_MODEL_NAME,
instruction=prompt,
tools=tools,
)
return agent
def _create_document_tool(self) -> Callable[..., Dict[str, Any]]:
document_record = self.document or {}
allowed_path = (document_record.get("path") or "").strip()
def run_document_conversion(
file_path: Optional[str] = None,
file_extension: Optional[str] = None,
) -> Dict[str, Any]:
target_path = (file_path or allowed_path or "").replace("\\", "/").strip()
if not target_path:
return {
"status": "error",
"error_message": "file_path is required to convert a document.",
}
allowed_normalized = allowed_path.replace("\\", "/").strip()
if allowed_normalized and target_path != allowed_normalized:
return {
"status": "error",
"error_message": "The provided file_path does not match the uploaded document for this session.",
}
result = convert_document_to_markdown(
file_path=target_path,
file_extension=file_extension or document_record.get("extension"),
)
if result.get("status") == "success":
text_content = result.get("text_content") or ""
result["preview"] = text_content[:1000]
result["character_count"] = len(text_content)
if allowed_path and result.get("source_path"):
# Normalize to relative path for transparency
result["source_path"] = allowed_path
return result
run_document_conversion.__name__ = DOCUMENT_CONVERSION_TOOL_NAME
run_document_conversion.__doc__ = (
"Convert the user's uploaded dermatology document into Markdown text. "
"Provide the `file_path` exactly as supplied in the conversation context."
)
return run_document_conversion
def _create_image_tool(self) -> Callable[..., Dict[str, Any]]:
image_record = self.image or {}
allowed_path = (image_record.get("path") or "").strip()
def run_image_analysis(
file_path: Optional[str] = None,
language: Optional[str] = None,
) -> Dict[str, Any]:
target_path = (file_path or allowed_path or "").replace("\\", "/").strip()
if not target_path:
return {
"status": "error",
"error_message": "file_path is required to analyse the image.",
}
allowed_normalized = allowed_path.replace("\\", "/").strip()
if allowed_normalized and target_path != allowed_normalized:
return {
"status": "error",
"error_message": "The provided file_path does not match the uploaded image for this session.",
}
result = analyze_skin_image(
file_path=target_path,
language=language or self.language,
)
if result.get("status") == "success" and allowed_path:
result["image_path"] = allowed_path
return result
run_image_analysis.__name__ = IMAGE_ANALYSIS_TOOL_NAME
run_image_analysis.__doc__ = (
"Analyse the user's uploaded skin image. Provide the `file_path` exactly as supplied "
"in the conversation context to run the classifier."
)
return run_image_analysis
def _create_personalized_data_tool(self, data: str) -> Callable[[], Dict[str, Any]]:
sanitized = (data or "").strip()
def personalized_tool() -> Dict[str, Any]:
return {
"status": "success",
"generated_at": datetime.now(timezone.utc).isoformat(),
"personalized_data": sanitized,
}
personalized_tool.__name__ = PERSONALIZED_TOOL_NAME
personalized_tool.__doc__ = (
"Return questionnaire-derived personalization details for the current user."
)
return personalized_tool
def _create_environmental_data_tool(
self, data: Dict[str, Any]
) -> Callable[[], Dict[str, Any]]:
snapshot = dict(data) if isinstance(data, dict) else {}
city = self.user_city
def environmental_tool() -> Dict[str, Any]:
return {
"status": "success",
"city": city,
"retrieved_at": datetime.now(timezone.utc).isoformat(),
"environmental_data": snapshot,
}
environmental_tool.__name__ = ENVIRONMENT_TOOL_NAME
environmental_tool.__doc__ = (
"Return the cached environmental conditions for the user's location."
)
return environmental_tool
def _load_user_preferences(self) -> Dict[str, Any]:
try:
return self.chat_session.get_user_preferences()
except Exception as exc:
logger.warning("Failed to load user preferences: %s", exc)
return {
"websearch": False,
"keywords": True,
"references": True,
"personalized_recommendations": False,
"environmental_recommendations": False,
}
def _load_user_profile(self) -> Dict[str, Any]:
try:
profile = self.chat_session.get_name_and_age() or {}
return {
"name": profile.get("name", "Patient"),
"age": profile.get("age", "Unknown"),
}
except Exception as exc:
logger.warning("Failed to load profile: %s", exc)
return {"name": "Patient", "age": "Unknown"}
def _load_environmental_data(self) -> Optional[Dict[str, Any]]:
try:
if (
self.user_preferences.get("environmental_recommendations")
and self.user_city
):
data = EnvironmentalData(self.user_city).get_environmental_data()
if data:
return data
except Exception as exc:
logger.warning("Failed to load environmental data: %s", exc)
return None
def _load_personalized_data(self) -> str:
try:
if self.user_preferences.get("personalized_recommendations"):
data = self.chat_session.get_personalized_recommendation()
return data or ""
except Exception as exc:
logger.warning("Failed to load personalized data: %s", exc)
return ""
def _prepare_user_data(self) -> Dict[str, Any]:
personalized_data = self._load_personalized_data()
environmental_payload = (
dict(self.environment_data)
if isinstance(self.environment_data, dict)
else {}
)
has_personalized_data = bool(personalized_data)
has_environmental_data = bool(environmental_payload)
document_info = None
if self.document and self.document.get("path"):
document_info = {
"path": self.document.get("path"),
"name": self.document.get("name") or "Uploaded document",
"type": self.document.get("type"),
"extension": self.document.get("extension"),
}
image_info = None
if self.image and self.image.get("path"):
image_info = {
"path": self.image.get("path"),
"name": self.image.get("name") or "Uploaded image",
"type": self.image.get("type"),
"extension": self.image.get("extension"),
"prompt": self.image.get("prompt"),
}
return {
"name": self.user_profile.get("name"),
"age": self.user_profile.get("age"),
"language": self.language,
"personalized_recommendations": self.user_preferences.get(
"personalized_recommendations"
),
"environmental_recommendations": self.user_preferences.get(
"environmental_recommendations"
),
"personalized_data": personalized_data,
"environmental_data": json.dumps(environmental_payload)
if has_environmental_data
else "",
"has_personalized_data": has_personalized_data,
"has_environmental_data": has_environmental_data,
"personalized_tool_name": PERSONALIZED_TOOL_NAME
if has_personalized_data
else None,
"environmental_tool_name": ENVIRONMENT_TOOL_NAME
if has_environmental_data
else None,
"environmental_payload": environmental_payload,
"include_keywords": self.user_preferences.get("keywords", True),
"include_references": self.user_preferences.get("references", True),
"include_images": True,
"recent_history": self._get_recent_history(),
"document_info": document_info,
"document_tool_name": DOCUMENT_CONVERSION_TOOL_NAME
if document_info
else None,
"image_info": image_info,
"image_tool_name": IMAGE_ANALYSIS_TOOL_NAME if image_info else None,
}
def _get_recent_history(self, limit: int = 10) -> str:
try:
if not self.session_id:
return ""
self.chat_session.load_chat_history()
history_items = self.chat_session.get_chat_history() or []
if not history_items:
return ""
recent = history_items[-limit:]
formatted = []
for entry in recent:
user_q = entry.get("query") or ""
bot_a = entry.get("response") or ""
if user_q:
formatted.append(f"User: {user_q}")
if bot_a:
formatted.append(f"Dr DermAI: {bot_a}")
return "\n".join(formatted[-limit * 2:])
except Exception as exc:
logger.warning("Failed to load recent history: %s", exc)
return ""
def _ensure_valid_session(self, title: Optional[str] = None) -> str:
if not self.session_id or not self.session_id.strip():
self.chat_session.create_new_session(title=title)
self.session_id = self.chat_session.session_id
else:
try:
if not self.chat_session.validate_session(self.session_id, title=title):
self.chat_session.create_new_session(title=title)
self.session_id = self.chat_session.session_id
except Exception:
self.chat_session.create_new_session(title=title)
self.session_id = self.chat_session.session_id
return self.session_id
@staticmethod
def _extract_text(event) -> str:
if not event.content or not event.content.parts:
return ""
parts: List[str] = []
for part in event.content.parts:
if part.text:
parts.append(part.text)
return "".join(parts)
@staticmethod
def _strip_code_fence(text: str) -> str:
stripped = text.strip()
if stripped.startswith("```") and stripped.endswith("```"):
body = stripped.strip("`")
if body.lower().startswith("json"):
body = body[4:]
stripped = body
return stripped.strip()
def _parse_agent_response(self, text: str) -> Tuple[str, List[str], List[str], List[str]]:
cleaned = self._strip_code_fence(text)
if not cleaned:
return "", [], [], []
try:
payload = json.loads(cleaned)
except json.JSONDecodeError:
logger.warning("Unable to parse agent JSON response; returning raw text.")
return cleaned, [], [], []
if not isinstance(payload, dict):
return cleaned, [], [], []
response_text = payload.get("response") or cleaned
raw_keywords = payload.get("keywords", [])
if isinstance(raw_keywords, list):
keywords = [str(item).strip() for item in raw_keywords if str(item).strip()]
elif raw_keywords:
keywords = [str(raw_keywords).strip()]
else:
keywords = []
raw_images = payload.get("images", [])
if isinstance(raw_images, list):
images = [str(item).strip() for item in raw_images if str(item).strip()]
elif raw_images:
images = [str(raw_images).strip()]
else:
images = []
raw_refs = payload.get("references", [])
if isinstance(raw_refs, list):
references = [str(item).strip() for item in raw_refs if str(item).strip()]
elif raw_refs:
references = [str(raw_refs).strip()]
else:
references = []
return response_text, keywords, images, references
@staticmethod
def _dedupe_list(items: List[str]) -> List[str]:
seen = set()
deduped: List[str] = []
for item in items:
if not item:
continue
if item in seen:
continue
seen.add(item)
deduped.append(item)
return deduped
```
### app\services\image_classification_vit.py
```python
import torch
from PIL import Image
import torch.nn.functional as F
from torchvision import transforms
from transformers import AutoModelForImageClassification, AutoConfig
import requests
from io import BytesIO
import os
from huggingface_hub import hf_hub_download
from dotenv import load_dotenv
load_dotenv()
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
# Set environment variables for better network handling
os.environ['HF_HUB_DOWNLOAD_TIMEOUT'] = '300' # Increase timeout to 5 minutes
os.environ['TRANSFORMERS_OFFLINE'] = '0' # Ensure online mode
class SkinDiseaseClassifier:
CLASS_NAMES = [
"Acne", "Basal Cell Carcinoma", "Benign Keratosis-like Lesions", "Chickenpox", "Eczema", "Healthy Skin",
"Measles", "Melanocytic Nevi", "Melanoma", "Monkeypox", "Psoriasis Lichen Planus and related diseases",
"Seborrheic Keratoses and other Benign Tumors", "Tinea Ringworm Candidiasis and other Fungal Infections",
"Vitiligo", "Warts Molluscum and other Viral Infections"
]
def __init__(self, repo_id="muhammadnoman76/skin-disease-classifier", cache_dir=None):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.repo_id = repo_id
self.model = self.load_trained_model()
self.transform = self.get_inference_transform()
def load_trained_model(self):
model_path= hf_hub_download(repo_id=self.repo_id, filename="healthy.pth", token=HUGGINGFACE_TOKEN)
checkpoint = torch.load(model_path, map_location=self.device, weights_only=True)
classifier_weight = checkpoint['model_state_dict']['classifier.3.weight']
num_classes = classifier_weight.size(0)
config = AutoConfig.from_pretrained("google/vit-base-patch16-224-in21k", num_labels=num_classes)
model = AutoModelForImageClassification.from_pretrained(
"google/vit-base-patch16-224-in21k",
config=config,
ignore_mismatched_sizes=True
)
in_features = model.classifier.in_features
model.classifier = torch.nn.Sequential(
torch.nn.Linear(in_features, 512),
torch.nn.ReLU(),
torch.nn.Dropout(0.3),
torch.nn.Linear(512, num_classes)
)
model.load_state_dict(checkpoint['model_state_dict'])
model = model.to(self.device)
if self.device.type == 'cuda':
model = model.half()
model.eval()
return model
def get_inference_transform(self):
return 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]),
])
def load_image(self, image_input):
try:
if isinstance(image_input, Image.Image):
image = image_input
elif isinstance(image_input, str):
if image_input.startswith(('http://', 'https://')):
response = requests.get(image_input)
image = Image.open(BytesIO(response.content))
else:
if not os.path.exists(image_input):
raise FileNotFoundError(f"Image file not found: {image_input}")
image = Image.open(image_input)
elif hasattr(image_input, 'read'):
image = Image.open(image_input)
else:
raise ValueError("Unsupported image input type")
return image.convert('RGB')
except Exception as e:
raise Exception(f"Error loading image: {str(e)}")
def predict(self, image_input, confidence_threshold=0.3):
try:
image = self.load_image(image_input)
image_tensor = self.transform(image).unsqueeze(0)
if self.device.type == 'cuda':
image_tensor = image_tensor.half()
image_tensor = image_tensor.to(self.device)
with torch.inference_mode():
outputs = self.model(pixel_values=image_tensor).logits
probabilities = F.softmax(outputs, dim=1)
confidence, predicted = torch.max(probabilities, 1)
confidence = confidence.item()
predicted_class_idx = predicted.item()
confidence_percentage = round(confidence * 100, 2)
predicted_class_name = self.CLASS_NAMES[predicted_class_idx]
return predicted_class_name, confidence_percentage
except Exception as e:
raise Exception(f"Error during prediction: {str(e)}")
```
### app\services\llm_model.py
```python
import json
from dotenv import load_dotenv
import os
import re
from g4f.client import Client
load_dotenv()
class Model:
def __init__(self):
self.gemini_api_key = os.getenv("GEMINI_API_KEY")
self.gemini_model = os.getenv("GEMINI_MODEL")
# Removed genai client initialization since it's not properly imported
def fall_back_llm(self, prompt):
"""Fallback method using gpt-4o-mini when Gemini fails"""
try:
response = Client().chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
web_search=False
)
return response.choices[0].message.content
except Exception as e:
return f"Both primary and fallback models failed. Error: {str(e)}"
def send_message_openrouter(self, prompt):
# Since genai client is not available, use fallback model directly
return self.fall_back_llm(prompt)
def llm(self, prompt, query):
# Since genai client is not available, use fallback model directly
combined_content = f"{prompt}\n\n{query}"
return self.fall_back_llm(combined_content)
def llm_image(self, text, image):
# Image processing with LLM is not available without genai client
print(f"Error in llm_image: genai client not available")
return f"Error: Image processing not available - genai client not configured"
def clean_json_response(self, response_text):
"""Clean the model's response to extract valid JSON."""
start = response_text.find('[')
end = response_text.rfind(']') + 1
if start != -1 and end != -1:
json_str = re.sub(r",\s*]", "]", response_text[start:end])
return json_str
return response_text
def skinScheduler(self, prompt, max_retries=3):
"""Generate a skincare schedule with retries and cleaning."""
# Since genai client is not available, use fallback model directly
try:
fallback_response = self.fall_back_llm(prompt)
cleaned_fallback = self.clean_json_response(fallback_response)
return json.loads(cleaned_fallback)
except json.JSONDecodeError:
return {"error": "Failed to produce valid JSON"}
except Exception as e:
return {"error": f"Model failed: {str(e)}"}
```
### app\services\prompts.py
```python
SKIN_CARE_SCHEDULER = """As a skincare expert, generate a daily schedule based on:
- User's skin profile: {personalized_condition}
- Current environmental conditions: {environmental_values}
- Historical routines: {historical_data}
Create EXACTLY 5 entries in this JSON format:
[
{{
"time": "6:00 AM - 8:00 AM",
"recommendation": "Cleanse with [Product Name]",
"icon": "💧",
"category": "morning"
}},
{{
"time": "8:00 AM - 10:00 AM",
"recommendation": "Apply [Sunscreen Name] SPF 50",
"icon": "☀️",
"category": "morning"
}},
{{
"time": "12:00 PM - 2:00 PM",
"recommendation": "Reapply sunscreen",
"icon": "🌤️",
"category": "afternoon"
}},
{{
"time": "6:00 PM - 8:00 PM",
"recommendation": "Evening cleansing routine",
"icon": "🌙",
"category": "evening"
}},
{{
"time": "9:00 PM - 11:00 PM",
"recommendation": "Night serum application",
"icon": "✨",
"category": "night"
}}
]
Important rules:
1. Use only double quotes
2. Maintain category order: morning, morning, afternoon, evening, night
3. Include specific product names from historical data when available
4. Never add comments or text outside the JSON array
5. Time ranges must follow "HH:MM AM/PM - HH:MM AM/PM" format
6. Use appropriate emojis for each activity
"""
DEFAULT_SCHEDULE = [
{
"time": "6:00 AM - 8:00 AM",
"recommendation": "Cleanse with a gentle cleanser",
"icon": "💧",
"category": "Dummy"
},
{
"time": "8:00 AM - 10:00 AM",
"recommendation": "Apply sunscreen SPF 30+",
"icon": "☀️",
"category": "morning"
},
{
"time": "12:00 PM - 2:00 PM",
"recommendation": "Reapply sunscreen if needed",
"icon": "🌤️",
"category": "afternoon"
},
{
"time": "6:00 PM - 8:00 PM",
"recommendation": "Evening cleansing routine",
"icon": "🌙",
"category": "evening"
},
{
"time": "9:00 PM - 11:00 PM",
"recommendation": "Apply night cream or serum",
"icon": "✨",
"category": "night"
}
]
ADVICE_REPORT_SUGGESTION = """
## Based on your Image Analysis:
We have identified the presence of {diseases_name} with a confidence level of {diseases_detection_confidence}.
{response}
"""
URDU_ADVICE_REPORT_SUGGESTION = """
## آپ کی تصویر کے تجزیے کی بنیاد پر:
ہم نے {diseases_detection_confidence} کی اعتماد کی سطح کے ساتھ {diseases_name} کی موجودگی کی شناخت کی ہے۔
{response}
"""
SKIN_NON_SKIN_PROMPT = """
You are an expert at analyzing whether an image shows human skin or not.
Your task is to determine if the given image should be processed by a skin disease model.
Examine the image carefully and provide a clear two-word response:
answer <YES> if the image shows human skin, otherwise answer <NO>.
"""
```
### app\services\RAG_evaluation.py
```python
from typing import Dict, Any
import re
from datetime import datetime
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from app.services.chathistory import ChatSession
import os
# # Set NLTK data path to a writable location
# nltk_data_dir = os.path.join(os.getcwd(), "nltk_data")
# os.makedirs(nltk_data_dir, exist_ok=True)
# nltk.data.path.append(nltk_data_dir)
# # Download NLTK resources to the specified directory
# nltk.download('stopwords', download_dir=nltk_data_dir)
# nltk.download('wordnet', download_dir=nltk_data_dir)
class RAGEvaluation:
def __init__(self, token: str, page: int = 1, page_size: int = 5):
self.chat_session = ChatSession(token, "session_id")
self.page = page
self.page_size = page_size
self.lemmatizer = WordNetLemmatizer()
self.stop_words = set(stopwords.words('english'))
def _preprocess_text(self, text: str) -> str:
text = re.sub(r'[^a-zA-Z0-9\s]', '', text.lower())
words = text.split()
lemmatized_words = [self.lemmatizer.lemmatize(word) for word in words]
filtered_words = [word for word in lemmatized_words if word not in self.stop_words]
seen = set()
cleaned_words = []
for word in filtered_words:
if word not in seen:
seen.add(word)
cleaned_words.append(word)
return ' '.join(cleaned_words)
def _calculate_cosine_similarity(self, context: str, response: str) -> float:
clean_context = self._preprocess_text(context)
clean_response = self._preprocess_text(response)
vectorizer = TfidfVectorizer(vocabulary=clean_context.split())
try:
context_vector = vectorizer.fit_transform([clean_context])
response_vector = vectorizer.transform([clean_response])
return cosine_similarity(context_vector, response_vector)[0][0]
except ValueError:
return 0.0
def _calculate_time_difference(self, start_time: str, end_time: str) -> float:
start = datetime.fromisoformat(start_time)
end = datetime.fromisoformat(end_time)
return (end - start).total_seconds()
def _process_interaction(self, interaction: Dict[str, Any]) -> Dict[str, Any]:
processed = interaction.copy()
processed['accuracy'] = self._calculate_cosine_similarity(
interaction['context'],
interaction['response']
)
processed['overall_time'] = self._calculate_time_difference(
interaction['rag_start_time'],
interaction['rag_end_time']
)
return processed
def generate_evaluation_report(self) -> Dict[str, Any]:
raw_data = self.chat_session.get_save_details(
page=self.page,
page_size=self.page_size
)
return {
'total_interactions': raw_data['total_interactions'],
'page': raw_data['page'],
'page_size': raw_data['page_size'],
'total_pages': raw_data['total_pages'],
'results': [self._process_interaction(i) for i in raw_data['results']]
}
```
### app\services\skincare_scheduler.py
```python
import json
import logging
from app.services.chathistory import ChatSession
from app.services.llm_model import Model
from app.services.environmental_condition import EnvironmentalData
from app.services.prompts import SKIN_CARE_SCHEDULER, DEFAULT_SCHEDULE
class SkinCareScheduler:
def __init__(self, token, session_id):
self.token = token
self.session_id = session_id
self.chat_session = ChatSession(token, session_id)
self.user_city = self.chat_session.get_city() or ''
self.environment_data = EnvironmentalData(self.user_city)
def get_historical_data(self):
"""Retrieve the last 7 days of schedules."""
schedules = self.chat_session.get_last_seven_days_schedules()
return [schedule["schedule_data"] for schedule in schedules]
def createTable(self):
"""Generate and return a daily skincare schedule."""
try:
# Check for an existing valid schedule
existing_schedule = self.chat_session.get_today_schedule()
if existing_schedule and isinstance(existing_schedule.get("schedule_data"), list):
return json.dumps(existing_schedule["schedule_data"], indent=2)
# Gather input data
historical_data = self.get_historical_data()
personalized_condition = self.chat_session.get_personalized_recommendation() or "No specific skin conditions provided"
environmental_data = self.environment_data.get_environmental_data()
# Format the prompt
formatted_prompt = SKIN_CARE_SCHEDULER.format(
personalized_condition=personalized_condition,
environmental_values=json.dumps(environmental_data, indent=2),
historical_data=json.dumps(historical_data, indent=2)
)
# Generate schedule with the model
model = Model()
result = model.skinScheduler(formatted_prompt)
# Handle errors by falling back to default schedule
if isinstance(result, dict) and "error" in result:
logging.error(f"Model error: {result['error']}")
result = DEFAULT_SCHEDULE
# Validate basic structure (optional, but ensures 5 entries)
if not isinstance(result, list) or len(result) != 5:
logging.warning("Generated schedule invalid; using default.")
result = DEFAULT_SCHEDULE
# Save and return the schedule
self.chat_session.save_schedule(result)
return json.dumps(result, indent=2)
except Exception as e:
logging.error(f"Schedule generation failed: {str(e)}")
return json.dumps(DEFAULT_SCHEDULE, indent=2)
```
### app\services\tools.py
```python
import io
import logging
import os
from pathlib import Path
from typing import Any, Dict, List, Optional
from PIL import Image
from app.services.vector_database_search import VectorDatabaseSearch
from app.services.websearch import WebSearch
from MagicConvert import MagicConvert
from app.services.prompts import (
ADVICE_REPORT_SUGGESTION,
URDU_ADVICE_REPORT_SUGGESTION,
)
from app.services.image_classification_vit import SkinDiseaseClassifier
try:
from pillow_heif import register_heif_opener
register_heif_opener()
_HEIF_SUPPORTED = True
except Exception:
_HEIF_SUPPORTED = False
logger = logging.getLogger(__name__)
def _clean_query(query: str) -> str:
return (query or '').strip()
_UPLOADS_ROOT = Path(
os.getenv(
"DERMAI_UPLOAD_DIR",
Path(__file__).resolve().parent.parent.parent / "uploads",
)
).resolve()
_magic_converter = MagicConvert()
_skin_classifier: Optional[SkinDiseaseClassifier] = None
_skin_classifier_error: Optional[str] = None
def get_web_search(query: str, num_results: int = 4) -> Dict[str, Any]:
"""Return up-to-date dermatology information from the public web."""
query = _clean_query(query)
if not query:
return {"status": "error", "error_message": "Query is required."}
try:
web = WebSearch(num_results=max(1, min(num_results or 4, 8)))
raw_results = web.search(query) or []
formatted: List[Dict[str, Any]] = []
references: List[str] = []
for idx, item in enumerate(raw_results, start=1):
link = item.get('link') or item.get('url') or ''
snippet = item.get('text') or item.get('snippet') or ''
title = item.get('title') or ''
entry = {
"source_number": idx,
"title": title,
"link": link,
"snippet": snippet,
}
formatted.append(entry)
if link:
references.append(link)
if not formatted:
return {
"status": "error",
"error_message": f"No web results found for '{query}'.",
}
return {
"status": "success",
"results": formatted,
"references": references,
}
except Exception as exc:
logger.exception("Web search failed: %s", exc)
return {
"status": "error",
"error_message": f"Web search failed: {exc}",
}
def get_vector_search(query: str, top_k: int = 5) -> Dict[str, Any]:
"""Return dermatology knowledge from the curated vector database."""
query = _clean_query(query)
if not query:
return {"status": "error", "error_message": "Query is required."}
try:
vector = VectorDatabaseSearch()
if not vector.is_available():
return {
"status": "error",
"error_message": "Vector database is not available.",
}
raw_results = vector.search(query, top_k=max(1, min(top_k or 5, 10)))
if not raw_results:
return {
"status": "error",
"error_message": f"No vector results found for '{query}'.",
}
formatted: List[Dict[str, Any]] = []
references: List[str] = []
for idx, item in enumerate(raw_results, start=1):
source = item.get('source') or 'Unknown'
page = item.get('page') or 0
content = item.get('content') or ''
confidence = item.get('confidence')
formatted.append(
{
"source_number": idx,
"source": source,
"page": page,
"content": content,
"confidence": confidence,
}
)
ref_label = f"{source} (page {page})" if page else source
references.append(ref_label)
return {
"status": "success",
"results": formatted,
"references": references,
}
except Exception as exc:
logger.exception("Vector search failed: %s", exc)
return {
"status": "error",
"error_message": f"Vector search failed: {exc}",
}
def get_image_search(query: str, max_images: int = 3) -> Dict[str, Any]:
"""Return dermatology-relevant image URLs for the given query."""
query = _clean_query(query)
if not query:
return {"status": "error", "error_message": "Query is required."}
try:
searcher = WebSearch(max_images=max(1, min(max_images or 3, 8)))
images = searcher.search_images(query) or []
unique_images = []
seen = set()
for url in images:
if url and url not in seen:
seen.add(url)
unique_images.append(url)
if len(unique_images) >= max_images:
break
if not unique_images:
return {
"status": "error",
"error_message": f"No images found for '{query}'.",
}
return {"status": "success", "images": unique_images}
except Exception as exc:
logger.exception("Image search failed: %s", exc)
return {
"status": "error",
"error_message": f"Image search failed: {exc}",
}
def _get_classifier() -> SkinDiseaseClassifier:
global _skin_classifier, _skin_classifier_error
if _skin_classifier is not None:
return _skin_classifier
if _skin_classifier_error:
raise RuntimeError(_skin_classifier_error)
try:
_skin_classifier = SkinDiseaseClassifier()
return _skin_classifier
except Exception as exc:
_skin_classifier_error = str(exc)
raise
def analyze_skin_image(
file_path: str,
language: Optional[str] = None,
) -> Dict[str, Any]:
"""Assess an uploaded image for dermatology analysis.
The tool verifies the file exists in the uploads directory and runs the skin
disease classifier directly. When confidence is below the 50% threshold it
reports the uncertainty instead of a diagnosis and nudges the user toward
alternative options.
"""
if not file_path or not str(file_path).strip():
return {"status": "error", "error_message": "file_path is required."}
try:
candidate = Path(file_path)
if not candidate.is_absolute():
candidate = (_UPLOADS_ROOT / candidate).resolve()
else:
candidate = candidate.resolve()
uploads_root = _UPLOADS_ROOT
uploads_root.mkdir(parents=True, exist_ok=True)
uploads_root = uploads_root.resolve()
if uploads_root not in candidate.parents and candidate != uploads_root:
return {
"status": "error",
"error_message": "Access to the requested file path is not permitted.",
}
if not candidate.exists() or not candidate.is_file():
return {
"status": "error",
"error_message": f"Image not found at '{candidate}'.",
}
try:
with candidate.open("rb") as fh:
image_bytes = fh.read()
image_stream = io.BytesIO(image_bytes)
pil_image = Image.open(image_stream)
pil_image.load()
pil_image = pil_image.convert("RGB")
except Exception as exc:
logger.exception("Unable to open image for analysis: %s", exc)
signature = image_bytes[:12] if 'image_bytes' in locals() else b''
looks_like_heif = signature.startswith(b"\x00\x00\x00\x20ftyp") and any(
codec in signature for codec in (b"heic", b"heix", b"hevc", b"avif")
)
if looks_like_heif and not _HEIF_SUPPORTED:
return {
"status": "error",
"error_message": (
"The uploaded image appears to be in HEIC/AVIF format, which is not supported. "
"Please convert the photo to JPG or PNG and try again."
),
}
return {
"status": "error",
"error_message": f"Unable to open the image: {exc}",
}
user_language = (language or "english").strip().lower()
# Skip skin-vs-non-skin classification and directly proceed to disease classification
try:
classifier = _get_classifier()
except Exception as exc:
logger.error("Skin classifier unavailable: %s", exc)
return {
"status": "error",
"error_message": (
"Skin analysis is temporarily unavailable. "
"Ensure the classifier weights are accessible (set SKIN_CLASSIFIER_WEIGHTS to a local file "
"or configure HUGGINGFACE_TOKEN with network access) and try again."
),
"details": str(exc),
}
disease_name, confidence = classifier.predict(pil_image, 5)
confidence_value = float(confidence)
below_threshold = confidence_value < 50.0
if user_language == "urdu":
unable_message = (
"معذرت، میں اس وقت جلد کی بیماری کی درست شناخت نہیں کر پا رہا۔ "
"براہ کرم بہتر روشنی میں ایک قریب کی تصویر اپ لوڈ کریں یا اپنی تشخیص کے لیے ڈاکٹر سے رجوع کریں۔"
)
diagnosis_message = (
f"مجھے لگتا ہے کہ یہ {disease_name} ہے اور میری اعتماد کی سطح {confidence_value:.2f}% ہے۔ "
"براہ کرم حتمی تشخیص کے لیے ماہر ڈرماٹولوجسٹ سے مشورہ کریں۔"
)
else:
unable_message = (
"I’m not confident enough to identify a condition from this photo. "
"Please upload a clearer close-up image with good lighting, or consult a dermatologist for an in-person diagnosis."
)
diagnosis_message = (
f"I suspect this may be {disease_name} with a confidence of {confidence_value:.2f}%. "
"Please consult a dermatologist for a professional evaluation and treatment plan."
)
message = unable_message if below_threshold else diagnosis_message
if user_language == "urdu":
advice_lines = ["## تصویری تجزیہ کی بنیاد پر"]
if not below_threshold:
advice_lines.append(
f"- مشتبہ بیماری: {disease_name} (اعتماد {confidence_value:.2f}%)."
)
advice_lines.append(
"- یہ نتیجہ تخمینی ہے، حتمی تشخیص کے لئے ماہر امراض جلد سے رجوع کریں۔"
)
else:
advice_lines.append(
"- ماڈل کا اعتماد 50٪ سے کم ہے، اس لئے قابل اعتماد تشخیص ممکن نہیں۔"
)
advice_lines.append(
"- متاثرہ جلد کی واضح اور روشنی میں تصویر لیں اور فلٹرز سے پرہیز کریں۔"
)
advice_lines.append(
"- اگر علامات بگڑتی یا پھیلتی ہیں تو فوری طبی معائنہ کروائیں۔"
)
else:
advice_lines = ["## Based on the Image Analysis"]
if not below_threshold:
advice_lines.append(
f"- Suspected condition: {disease_name} (confidence {confidence_value:.2f}%)."
)
advice_lines.append(
"- This prediction is probabilistic; please obtain confirmation from a dermatologist."
)
else:
advice_lines.append(
"- The model's confidence is below 50%, so no reliable diagnosis is available."
)
advice_lines.append(
"- Capture well-lit close-up photos of the affected area and avoid heavy filters."
)
advice_lines.append(
"- Seek urgent in-person care if symptoms worsen or spread rapidly."
)
advice = "\n".join(advice_lines)
return {
"status": "success",
"is_skin": True,
"diagnosis": None if below_threshold else disease_name,
"confidence": confidence_value,
"confidence_below_threshold": below_threshold,
"message": message,
"advice": advice,
"image_path": str(candidate.relative_to(uploads_root)).replace("\\", "/"),
}
except Exception as exc:
logger.exception("Unexpected error during image analysis: %s", exc)
return {
"status": "error",
"error_message": f"Unexpected error: {exc}",
}
def convert_document_to_markdown(
file_path: str,
file_extension: Optional[str] = None,
) -> Dict[str, Any]:
"""Convert an uploaded document into Markdown text for downstream analysis.
Args:
file_path: Path to the uploaded file. Accepts absolute paths or paths
relative to the backend uploads directory.
file_extension: Optional hint such as ".pdf" or ".docx" when the
extension cannot be inferred from the filename.
Returns:
A dictionary containing the Markdown representation (`text_content`),
detected title, and basic metadata. On failure the dictionary includes a
descriptive error message instead of raising.
"""
if not file_path or not str(file_path).strip():
return {"status": "error", "error_message": "file_path is required."}
try:
candidate = Path(file_path)
if not candidate.is_absolute():
candidate = (_UPLOADS_ROOT / candidate).resolve()
else:
candidate = candidate.resolve()
uploads_root = _UPLOADS_ROOT
uploads_root.mkdir(parents=True, exist_ok=True)
uploads_root = uploads_root.resolve()
if uploads_root not in candidate.parents and candidate != uploads_root:
return {
"status": "error",
"error_message": "Access to the requested file path is not permitted.",
}
if not candidate.exists() or not candidate.is_file():
return {
"status": "error",
"error_message": f"File not found at '{candidate}'.",
}
extension_hint = file_extension or candidate.suffix
if extension_hint and not extension_hint.startswith('.'):
extension_hint = f".{extension_hint}"
conversion = _magic_converter.magic(
str(candidate),
file_extension=extension_hint,
)
text_content = conversion.text_content if conversion else ""
character_count = len(text_content)
return {
"status": "success",
"text_content": text_content,
"title": getattr(conversion, "title", None),
"character_count": character_count,
"source_path": str(candidate),
}
except Exception as exc:
logger.exception("Unexpected error during document conversion: %s", exc)
return {
"status": "error",
"error_message": f"Unexpected error: {exc}",
}
```
### app\services\vector_database_search.py
```python
import os
import uuid
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_qdrant import Qdrant
from qdrant_client import QdrantClient, models
from qdrant_client.http.exceptions import UnexpectedResponse
from dotenv import load_dotenv
import logging
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
os.environ["GOOGLE_API_KEY"] = os.getenv("GEMINI_API_KEY")
QDRANT_URL = os.getenv("QDRANT_URL")
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
QDRANT_COLLECTION_NAME = os.getenv("QDRANT_COLLECTION_NAME", "dermatology_docs")
class VectorDatabaseSearch:
def __init__(self, collection_name=QDRANT_COLLECTION_NAME):
self.collection_name = collection_name
self.embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
self.client = None
self.vectorstore = None
self.is_initialized = False
# Initialize connection
self._initialize_connection()
def _initialize_connection(self):
"""Initialize Qdrant connection with proper error handling"""
try:
# Check if credentials are available
if not QDRANT_URL or not QDRANT_API_KEY:
logger.warning("Qdrant credentials not found. Vector search will be disabled.")
self.is_initialized = False
return
# Initialize Qdrant client
self.client = QdrantClient(
url=QDRANT_URL,
api_key=QDRANT_API_KEY,
timeout=30 # Add timeout
)
# Test connection
self.client.get_collections()
# Initialize collection
self._initialize_collection()
# Initialize vector store
self.vectorstore = Qdrant(
client=self.client,
collection_name=self.collection_name,
embeddings=self.embeddings
)
self.is_initialized = True
logger.info(f"Successfully connected to Qdrant collection: {self.collection_name}")
except UnexpectedResponse as e:
logger.error(f"Authentication error with Qdrant: {e}")
self.is_initialized = False
except Exception as e:
logger.error(f"Error initializing Qdrant connection: {e}")
self.is_initialized = False
def _initialize_collection(self):
"""Initialize Qdrant collection if it doesn't exist"""
if not self.client:
return
try:
collections = self.client.get_collections()
collection_exists = any(c.name == self.collection_name for c in collections.collections)
if not collection_exists:
self.client.create_collection(
collection_name=self.collection_name,
vectors_config=models.VectorParams(
size=768,
distance=models.Distance.COSINE
)
)
logger.info(f"Created new collection: {self.collection_name}")
else:
# Check if collection has data
collection_info = self.client.get_collection(self.collection_name)
logger.info(f"Collection {self.collection_name} exists with {collection_info.points_count} points")
except Exception as e:
logger.error(f"Error initializing collection: {e}")
self.is_initialized = False
def add_pdf(self, pdf_path):
"""Add PDF to vector database"""
if not self.is_initialized:
logger.error("Vector database not initialized. Cannot add PDF.")
return False
try:
loader = PyPDFLoader(pdf_path)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
split_docs = splitter.split_documents(docs)
book_name = os.path.splitext(os.path.basename(pdf_path))[0]
logger.info(f"Processing {book_name} with {len(split_docs)} chunks")
for doc in split_docs:
doc.metadata = {
"source": book_name,
"page": doc.metadata.get('page', 1),
"id": str(uuid.uuid4())
}
self.vectorstore.add_documents(split_docs)
logger.info(f"Successfully added {len(split_docs)} chunks from {book_name}")
return True
except Exception as e:
logger.error(f"Error adding PDF: {e}")
return False
def search(self, query, top_k=5):
"""Search documents based on query"""
if not self.is_initialized:
logger.warning("Vector database not initialized. Returning empty results.")
return []
try:
# Check if collection has any data
collection_info = self.client.get_collection(self.collection_name)
if collection_info.points_count == 0:
logger.warning(f"Collection {self.collection_name} is empty. No documents to search.")
return []
# Perform similarity search
results = self.vectorstore.similarity_search_with_score(query, k=top_k)
formatted = []
for doc, score in results:
# Convert score to confidence percentage (cosine similarity)
confidence = (1 - score) * 100 # Qdrant returns distance, not similarity
formatted.append({
"source": doc.metadata.get('source', 'Unknown'),
"page": doc.metadata.get('page', 0),
"content": doc.page_content[:500],
"confidence": round(confidence, 2)
})
logger.info(f"Found {len(formatted)} results for query: {query[:50]}...")
return formatted
except Exception as e:
logger.error(f"Search error: {e}")
return []
def get_book_info(self):
"""Retrieve list of unique book sources in the collection"""
if not self.is_initialized:
logger.warning("Vector database not initialized.")
return []
try:
# Check if collection exists
collections = self.client.get_collections()
if not any(c.name == self.collection_name for c in collections.collections):
logger.info(f"Collection {self.collection_name} does not exist yet")
return []
# Get collection info
collection_info = self.client.get_collection(self.collection_name)
if collection_info.points_count == 0:
logger.info("Collection is empty")
return []
# Get sample of points to extract sources
points = self.client.scroll(
collection_name=self.collection_name,
limit=min(1000, collection_info.points_count),
with_payload=True,
with_vectors=False
)[0]
books = set()
for point in points:
if hasattr(point, 'payload') and point.payload:
if 'metadata' in point.payload and 'source' in point.payload['metadata']:
books.add(point.payload['metadata']['source'])
elif 'source' in point.payload:
books.add(point.payload['source'])
logger.info(f"Found {len(books)} unique books in collection")
return list(books)
except Exception as e:
logger.error(f"Error retrieving book info: {e}")
return []
def is_available(self):
"""Check if vector database is available and has data"""
if not self.is_initialized:
return False
try:
collection_info = self.client.get_collection(self.collection_name)
return collection_info.points_count > 0
except:
return False
```
### app\services\websearch.py
```python
import re
import warnings
import requests
from bs4 import BeautifulSoup
import urllib.parse
import time
import random
from urllib.parse import urlparse, parse_qs
warnings.simplefilter('ignore', requests.packages.urllib3.exceptions.InsecureRequestWarning)
class WebSearch:
def __init__(self, num_results=4, max_chars_per_page=6000, max_images=10):
self.num_results = num_results
self.max_chars_per_page = max_chars_per_page
self.reference = []
self.results = []
self.max_images = max_images
self.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': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Connection': 'keep-alive',
}
# Common domains for direct content
self.content_domains = [
"wikipedia.org", "webmd.com", "mayoclinic.org", "healthline.com", "nih.gov",
"clevelandclinic.org", "nhs.uk", "cdc.gov", "medlineplus.gov", "hopkinsmedicine.org"
]
# Ad and tracking domains to filter out
self.blocked_domains = [
"ad.doubleclick.net", "googleadservices.com", "bing.com/aclick", "duckduckgo.com/y.js",
"amazon.com/s", "ads.google.com", "analytics", "tracker", "pixel", "adservice"
]
def is_valid_url(self, url):
"""Check if URL is valid and not an ad/tracking URL"""
if not url or len(url) < 10:
return False
try:
parsed = urlparse(url)
# Check if URL has a valid scheme and netloc
if not all([parsed.scheme, parsed.netloc]):
return False
# Filter out ad/tracking URLs
domain = parsed.netloc.lower()
path = parsed.path.lower()
query = parsed.query.lower()
# Block URLs containing ad-related indicators
for blocked in self.blocked_domains:
if blocked in domain or blocked in path:
return False
# Block URLs with ad-related query parameters
if any(param in query for param in ["ad", "click", "track", "clkid", "msclkid"]):
return False
# Extra check for redirect URLs
if "redirect" in path or "goto" in path or "go.php" in path:
return False
# Reject extremely long URLs (often tracking)
if len(url) > 500:
return False
return True
except Exception:
return False
def clean_url(self, url):
"""Clean the URL by removing tracking parameters"""
try:
parsed = urlparse(url)
# List of known tracking parameters to remove
tracking_params = [
'utm_', 'ref_', 'ref=', 'refid', 'fbclid', 'gclid', 'msclkid', 'dclid',
'zanpid', 'icid', 'igshid', 'mc_eid', '_hsenc', 'mkt_tok', 'yclid'
]
# Parse query parameters
query_params = parse_qs(parsed.query)
# Remove tracking parameters
filtered_params = {
k: v for k, v in query_params.items()
if not any(tracker in k.lower() for tracker in tracking_params)
}
# Rebuild query string
clean_query = urllib.parse.urlencode(filtered_params, doseq=True) if filtered_params else ""
# Reconstruct URL
clean_url = urllib.parse.urlunparse((
parsed.scheme,
parsed.netloc,
parsed.path,
parsed.params,
clean_query,
"" # Remove fragment
))
return clean_url
except Exception:
# If any error occurs, return the original URL
return url
def extract_real_url_from_redirect(self, url):
"""Extract the actual URL from a redirect URL"""
try:
parsed = urlparse(url)
# Handle DuckDuckGo redirects
if "duckduckgo.com" in parsed.netloc and "u3=" in parsed.query:
params = parse_qs(parsed.query)
if "u3" in params and params["u3"]:
redirect_url = params["u3"][0]
# Handle nested redirects (like Bing redirects inside DuckDuckGo)
if "bing.com/aclick" in redirect_url:
bing_parsed = urlparse(redirect_url)
bing_params = parse_qs(bing_parsed.query)
if "u" in bing_params and bing_params["u"]:
decoded_url = urllib.parse.unquote(bing_params["u"][0])
return self.clean_url(decoded_url)
return self.clean_url(redirect_url)
# Handle Bing redirects
if "bing.com/aclick" in url:
params = parse_qs(parsed.query)
if "u" in params and params["u"]:
return self.clean_url(urllib.parse.unquote(params["u"][0]))
return url
except Exception:
return url
def extract_text_from_webpage(self, html_content):
soup = BeautifulSoup(html_content, "html.parser")
# Remove non-content elements
for tag in soup(["script", "style", "header", "footer", "nav", "form", "svg",
"aside", "iframe", "noscript", "img", "figure", "button"]):
tag.extract()
# Extract text and normalize spacing
text = ' '.join(soup.stripped_strings)
text = re.sub(r'\s+', ' ', text).strip()
return text
def search(self, query):
results = []
encoded_query = urllib.parse.quote(query)
url = f'https://html.duckduckgo.com/html/?q={encoded_query}'
try:
with requests.Session() as session:
session.headers.update(self.headers)
response = session.get(url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
# Getting more results than needed to account for filtering
search_results = soup.find_all('div', class_='result')[:self.num_results * 2]
links = []
# Extract and process links
for result in search_results:
link_tag = result.find('a', class_='result__a')
if not link_tag or not link_tag.get('href'):
continue
original_link = link_tag['href']
# Process link to get the actual URL
clean_link = self.extract_real_url_from_redirect(original_link)
# Validate the URL
if self.is_valid_url(clean_link):
links.append(clean_link)
# Prioritize content domains
prioritized_links = []
other_links = []
for link in links:
if any(domain in link for domain in self.content_domains):
prioritized_links.append(link)
else:
other_links.append(link)
# Combine prioritized links first, then others
final_links = prioritized_links + other_links
# Limit to unique links up to num_results
unique_links = []
seen_domains = set()
for link in final_links:
domain = urlparse(link).netloc
if domain not in seen_domains and len(unique_links) < self.num_results:
unique_links.append(link)
seen_domains.add(domain)
from concurrent.futures import ThreadPoolExecutor, as_completed
def fetch_page(link):
try:
# Random delay to avoid being blocked
time.sleep(random.uniform(0.5, 1.5))
# Set a longer timeout for reliable fetching
page_response = session.get(link, timeout=10, verify=False)
# Only process HTML content
if 'text/html' not in page_response.headers.get('Content-Type', ''):
return None
page_soup = BeautifulSoup(page_response.text, 'lxml')
# Remove non-content elements
[tag.decompose() for tag in page_soup(['script', 'style', 'header', 'footer',
'nav', 'form', 'iframe', 'noscript'])]
# Extract text with better formatting
text = ' '.join(page_soup.stripped_strings)
text = re.sub(r'\s+', ' ', text).strip()
title = page_soup.title.string if page_soup.title else "Untitled Page"
return {
'link': link,
'title': title,
'text': text[:self.max_chars_per_page]
}
except Exception as e:
print(f"Error fetching {link}: {str(e)}")
return None
with ThreadPoolExecutor(max_workers=min(len(unique_links), 4)) as executor:
future_to_url = {executor.submit(fetch_page, link): link for link in unique_links}
for future in as_completed(future_to_url):
result = future.result()
if result:
results.append(result)
return results
except Exception as e:
print(f"Search error: {str(e)}")
return []
def search_images(self, query):
images = []
encoded_query = urllib.parse.quote(query)
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': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
# Try multiple sources for better results
image_sources = [
f"https://www.google.com/search?q={encoded_query}&tbm=isch&hl=en",
f"https://www.bing.com/images/search?q={encoded_query}&form=HDRSC2&first=1",
f"https://duckduckgo.com/?q={encoded_query}&iar=images&iax=images&ia=images"
]
for source_url in image_sources:
try:
time.sleep(random.uniform(0.5, 1.0)) # Polite delay
response = requests.get(source_url, headers=headers, verify=False, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract image URLs from img tags
for img in soup.find_all('img'):
src = img.get('src', '')
if src and src.startswith('http') and self.is_image_url(src):
cleaned_url = self.clean_url(src)
if self.is_valid_image(cleaned_url):
images.append(cleaned_url)
# Extract image URLs from scripts (useful for Google Images)
for script in soup.find_all('script'):
if script.string:
urls = re.findall(r'https?://[^\s<>"\']+?(?:\.(?:jpg|jpeg|png|gif|bmp|webp))', script.string)
for url in urls:
cleaned_url = self.clean_url(url)
if self.is_valid_image(cleaned_url):
images.append(cleaned_url)
except Exception as e:
print(f"Error searching images at {source_url}: {str(e)}")
continue
# Remove duplicates while preserving order
seen = set()
unique_images = [x for x in images if not (x in seen or seen.add(x))]
# Filter out small images and suspicious URLs
filtered_images = [img for img in unique_images if self.is_valid_image(img)]
return filtered_images[:self.max_images]
def is_image_url(self, url):
"""Check if URL points to an image file"""
image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')
return any(url.lower().endswith(ext) for ext in image_extensions)
def is_valid_image(self, url):
"""Additional validation for image URLs"""
try:
# Reject tiny images (often icons) and tracking pixels
if re.search(r'(?:icon|pixel|tracker|thumb|logo|button)\d*\.(?:jpg|png|gif)', url.lower()):
return False
# Avoid suspicious domains for images
parsed = urlparse(url)
if any(bad in parsed.netloc.lower() for bad in ["tracker", "pixel", "counter", "ad."]):
return False
# Avoid very short URLs (likely not valid images)
if len(url) < 30:
return False
return True
except:
return False
```
### app\services\wheel.py
```python
from app.services.chathistory import ChatSession
from app.services.environmental_condition import EnvironmentalData
def map_air_quality_index(aqi):
if aqi <= 50:
return {"displayValue": "Good", "value": aqi, "color": "#00C853"}
elif aqi <= 100:
return {"displayValue": "Moderate", "value": aqi, "color": "#FFB74D"}
elif aqi <= 150:
return {"displayValue": "Unhealthy Tolerate", "value": aqi, "color": "#FF7043"}
elif aqi <= 200:
return {"displayValue": "Unhealthy", "value": aqi, "color": "#E53935"}
else:
return {"displayValue": "Very Unhealthy", "value": aqi, "color": "#8E24AA"}
def map_pollution_level(aqi):
if aqi <= 50:
return 20
elif aqi <= 100:
return 40
elif aqi <= 150:
return 60
elif aqi <= 200:
return 80
else:
return 100
class CityNotProvidedError(Exception):
pass
class EnvironmentalConditions:
def __init__(self, session_id):
self.session_id = session_id
self.chat_session = ChatSession(session_id, "session_id")
self.user_city = self.chat_session.get_city()
if not self.user_city:
raise CityNotProvidedError("City information is required but not provided")
self.city = self.user_city
self.environment_data = EnvironmentalData(self.city)
def get_conditon(self):
data = self.environment_data.get_environmental_data()
formatted_data = [
{
"label": "Humidity",
# Handle decimal values by converting to float first
"value": int(float(data['Humidity'].strip(' %'))),
"color": "#4FC3F7",
"icon": "FaTint",
"type": "numeric"
},
{
"label": "UV Rays",
"value": data['UV_Index'] * 10,
"color": "#FFB74D",
"icon": "FaSun",
"type": "numeric"
},
{
"label": "Pollution",
"value": map_pollution_level(data['Air Quality Index']),
"color": "#F06292",
"icon": "FaLeaf",
"type": "numeric"
},
{
"label": "Air Quality",
**map_air_quality_index(data['Air Quality Index']),
"icon": "FaCloud",
"type": "categorical"
},
{
"label": "Wind",
"value": float(data['Wind Speed'].strip(' m/s')) * 10,
"color": "#9575CD",
"icon": "FaWind",
"type": "numeric"
},
{
"label": "Temperature",
"value": int(float(data['Temperature'].strip(' °C'))),
"color": "#FF7043",
"icon": "FaThermometerHalf",
"type": "numeric"
}
]
return formatted_data
```
### app.py
```python
import uvicorn
from app.main import app
if __name__ == "__main__":
uvicorn.run("app.main:app", host="0.0.0.0", port=5000, reload=True)
```
### docker-compose.yml
```yaml
version: '3'
services:
api:
build: .
ports:
- "5000:5000"
volumes:
- ./temp:/app/temp
- ./uploads:/app/uploads
env_file:
- .env
restart: unless-stopped
```
### document_code.py
```python
import os
from pathlib import Path
def generate_tree(path, prefix=""):
"""Generate tree structure"""
items = []
try:
entries = sorted(path.iterdir(), key=lambda x: (x.is_file(), x.name))
# Filter out ignored entries first
filtered_entries = [e for e in entries if not should_ignore(e)]
for i, entry in enumerate(filtered_entries):
is_last = i == len(filtered_entries) - 1
current_prefix = "└── " if is_last else "├── "
items.append(f"{prefix}{current_prefix}{entry.name}")
if entry.is_dir():
next_prefix = prefix + (" " if is_last else "│ ")
items.extend(generate_tree(entry, next_prefix))
except PermissionError:
pass
return items
def should_ignore(path):
"""Check if file/folder should be ignored"""
# Explicitly check for virtual environments and common ignored folders
if path.name in {'.venv', 'venv', '__pycache__', '.git', 'node_modules', '.idea', '.vscode'}:
return True
# Check if file is inside .venv or venv folder
if '.venv' in path.parts or 'venv' in path.parts:
return True
# Ignore all hidden files/folders (starting with .)
if path.name.startswith('.'):
return True
# Ignore specific files
ignore_files = {
'CODE_DOCUMENTATION.md', 'CODE_DOCUMENTATION.ipynb',
'CODE_DOCUMENTATION.html', 'CODE_DOCUMENTATION.pdf',
'CODE_DOCUMENTATION.docx', 'CODE_DOCUMENTATION.txt',
'CODE_DOCUMENTATION.csv', 'CODE_DOCUMENTATION.xlsx',
'CODE_DOCUMENTATION.pptx', 'CODE_DOCUMENTATION.ods',
'CODE_DOCUMENTATION.odp', 'CODE_DOCUMENTATION.odt',
'uv.lock', 'poetry.lock', 'Pipfile.lock',
'.DS_Store'
}
# Ignore by file extension
ignore_extensions = {'.pyc', '.pyo', '.pyd', '.so', '.egg-info'}
return (path.name in ignore_files or
path.suffix in ignore_extensions or
path.name.endswith('.egg-info'))
def get_code_files(directory):
"""Get all relevant code files"""
code_extensions = {'.py', '.js', '.ts', '.html', '.css', '.sql', '.yaml', '.yml', '.json', '.toml', '.cfg', '.ini'}
code_files = []
for file_path in directory.rglob("*"):
# Skip if it's a directory
if file_path.is_dir():
continue
# Skip ignored files/folders
if should_ignore(file_path):
continue
# Only include files with relevant extensions
if file_path.suffix.lower() in code_extensions:
code_files.append(file_path)
return sorted(code_files)
def main():
current_dir = Path.cwd()
output_file = current_dir / "CODE_DOCUMENTATION.md"
# Generate markdown
markdown = f"# {current_dir.name}\n\n"
markdown += f"Generated on: {Path.cwd()}\n\n"
# Add tree structure
markdown += "## Project Structure\n\n```\n"
markdown += f"{current_dir.name}/\n"
tree_items = generate_tree(current_dir)
for item in tree_items:
markdown += f"{item}\n"
markdown += "```\n\n"
# Get all code files
code_files = get_code_files(current_dir)
if code_files:
markdown += "## Source Code\n\n"
for file_path in code_files:
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
rel_path = file_path.relative_to(current_dir)
file_extension = file_path.suffix.lstrip('.')
# Use appropriate syntax highlighting
if file_extension == 'py':
lang = 'python'
elif file_extension in ['js', 'ts']:
lang = 'javascript'
elif file_extension in ['html']:
lang = 'html'
elif file_extension in ['css']:
lang = 'css'
elif file_extension in ['sql']:
lang = 'sql'
elif file_extension in ['yaml', 'yml']:
lang = 'yaml'
elif file_extension in ['json']:
lang = 'json'
else:
lang = file_extension
markdown += f"### {rel_path}\n\n"
markdown += f"```{lang}\n{content}\n```\n\n"
except Exception as e:
markdown += f"### {rel_path}\n\n"
markdown += f"*Could not read file: {str(e)}*\n\n"
continue
else:
markdown += "## Source Code\n\n*No code files found.*\n\n"
# Write output
try:
with open(output_file, 'w', encoding='utf-8') as f:
f.write(markdown)
print(f"✅ Documentation generated successfully: {output_file}")
print(f"📁 Total files documented: {len(code_files)}")
except Exception as e:
print(f"❌ Error writing documentation: {str(e)}")
if __name__ == "__main__":
main()
```
### pyproject.toml
```toml
[project]
name = "derm_ai"
version = "0.1.0"
description = "This is derm_ai backend"
authors = [
{ name = "Muhammad Noman", email = "muhammadnoman76@gmail.com" }
]
dependencies = [
"absolufy-imports==0.3.1",
"aiohappyeyeballs==2.6.1",
"aiohttp==3.12.15",
"aiosignal==1.4.0",
"alembic==1.16.5",
"annotated-types==0.7.0",
"anyio==4.10.0",
"attrs==25.3.0",
"Authlib==1.6.3",
"beautifulsoup4==4.13.4",
"Brotli==1.1.0",
"cachetools==5.5.2",
"certifi==2025.8.3",
"cffi==2.0.0",
"charset-normalizer==3.4.1",
"click==8.2.1",
"cloudpickle==3.1.1",
"cobble==0.1.4",
"colorama==0.4.6",
"cryptography==45.0.7",
"dataclasses-json==0.6.7",
"dnspython==2.8.0",
"docstring_parser==0.17.0",
"email-validator==2.3.0",
"fastapi==0.115.12",
"filelock==3.19.1",
"filetype==1.2.0",
"frozenlist==1.7.0",
"fsspec==2025.9.0",
"g4f==0.5.2.1",
"google-adk==1.14.0",
"google-ai-generativelanguage==0.6.18",
"google-api-core==2.25.1",
"google-api-python-client==2.181.0",
"google-auth==2.40.3",
"google-auth-httplib2==0.2.0",
"google-cloud-aiplatform==1.113.0",
"google-cloud-appengine-logging==1.6.2",
"google-cloud-audit-log==0.3.2",
"google-cloud-bigquery==3.37.0",
"google-cloud-bigtable==2.32.0",
"google-cloud-core==2.4.3",
"google-cloud-logging==3.12.1",
"google-cloud-resource-manager==1.14.2",
"google-cloud-secret-manager==2.24.0",
"google-cloud-spanner==3.57.0",
"google-cloud-speech==2.33.0",
"google-cloud-storage==2.19.0",
"google-cloud-trace==1.16.2",
"google-crc32c==1.7.1",
"google-genai==1.36.0",
"google-resumable-media==2.7.2",
"googleapis-common-protos==1.70.0",
"graphviz==0.21",
"greenlet==3.2.4",
"grpc-google-iam-v1==0.14.2",
"grpc-interceptor==0.15.4",
"grpcio==1.74.0",
"grpcio-status==1.74.0",
"h11==0.16.0",
"h2==4.3.0",
"hpack==4.1.0",
"httpcore==1.0.9",
"httplib2==0.31.0",
"httpx==0.28.1",
"httpx-sse==0.4.1",
"huggingface-hub==0.30.2",
"hyperframe==6.1.0",
"idna==3.10",
"importlib_metadata==8.7.0",
"jellyfish==1.2.0",
"Jinja2==3.1.6",
"joblib==1.5.2",
"jsonpatch==1.33",
"jsonpointer==3.0.0",
"jsonschema==4.25.1",
"jsonschema-specifications==2025.9.1",
"langchain==0.3.26",
"langchain-community==0.3.23",
"langchain-core==0.3.76",
"langchain-google-genai==2.1.4",
"langchain-qdrant==0.2.0",
"langchain-text-splitters==0.3.8",
"langsmith==0.3.45",
"lxml==6.0.1",
"Mako==1.3.10",
"mammoth==1.9.0",
"markdownify==1.1.0",
"MarkupSafe==3.0.2",
"marshmallow==3.26.1",
"mcp==1.14.0",
"mpmath==1.3.0",
"multidict==6.6.4",
"mypy_extensions==1.1.0",
"nest-asyncio==1.6.0",
"networkx==3.5",
"nltk==3.9.1",
"numpy==2.2.4",
"opentelemetry-api==1.37.0",
"opentelemetry-exporter-gcp-trace==1.9.0",
"opentelemetry-resourcedetector-gcp==1.9.0a0",
"opentelemetry-sdk==1.37.0",
"opentelemetry-semantic-conventions==0.58b0",
"orjson==3.11.3",
"packaging==25.0",
"pandas==2.2.3",
"pdfminer.six==20250416",
"pillow==11.2.1",
"portalocker==2.10.1",
"propcache==0.3.2",
"proto-plus==1.26.1",
"protobuf==6.32.1",
"puremagic==1.28",
"pyasn1==0.6.1",
"pyasn1_modules==0.4.2",
"pycparser==2.23",
"pycryptodome==3.23.0",
"pydantic==2.11.3",
"pydantic_core==2.33.1",
"pydantic-settings==2.10.1",
"PyJWT==1.7.1",
"pymongo==4.12.1",
"pyparsing==3.2.4",
"pypdf==5.4.0",
"pytesseract==0.3.13",
"python-dateutil==2.9.0.post0",
"python-dotenv==1.1.0",
"python-http-client==3.3.7",
"python-multipart==0.0.20",
"python-pptx==1.0.2",
"pytz==2025.2",
"PyYAML==6.0.2",
"qdrant-client==1.14.2",
"referencing==0.36.2",
"regex==2025.9.1",
"requests==2.32.5",
"requests-toolbelt==1.0.0",
"rpds-py==0.27.1",
"rsa==4.9.1",
"safetensors==0.6.2",
"scikit-learn==1.6.1",
"scipy==1.16.2",
"segtok==1.5.11",
"sendgrid==6.11.0",
"shapely==2.1.1",
"six==1.17.0",
"sniffio==1.3.1",
"soupsieve==2.8",
"SQLAlchemy==2.0.43",
"sqlalchemy-spanner==1.16.0",
"sqlparse==0.5.3",
"sse-starlette==3.0.2",
"starkbank-ecdsa==2.2.0",
"starlette==0.46.2",
"sympy==1.13.1",
"tabulate==0.9.0",
"tenacity==8.5.0",
"threadpoolctl==3.6.0",
"tokenizers==0.21.4",
"torch==2.5.1",
"torchvision==0.20.1",
"tqdm==4.67.1",
"transformers==4.51.3",
"typing_extensions==4.15.0",
"typing-inspect==0.9.0",
"typing-inspection==0.4.1",
"tzdata==2025.2",
"tzlocal==5.3.1",
"uritemplate==4.2.0",
"urllib3==2.5.0",
"uvicorn==0.34.1",
"watchdog==6.0.0",
"websockets==15.0.1",
"Werkzeug==3.1.3",
"xlsxwriter==3.2.8",
"yake==0.4.8",
"yarl==1.20.1",
"zipp==3.23.0",
"zstandard==0.23.0",
"MagicConvert==0.1.3",
]
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
```
|