Spaces:
Sleeping
Sleeping
File size: 163,947 Bytes
7e453aa 2ac8811 |
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 |
# FinRyver - Visual System Architecture
## 🏗️ **SYSTEM ARCHITECTURE OVERVIEW**
```
┌─────────────────────────────────────────────────────────────────────────────────┐
│ FINRYVER SYSTEM │
│ FINANCIAL STATEMENT GENERATION │
└─────────────────────┬─────────────────────┬─────────────────────┬─────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ CLIENT/UI │ │ FASTAPI │ │ LANGGRAPH │
│ (Streamlit) │ │ ENDPOINTS │ │ WORKFLOWS │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ EXCEL INPUT │ │ DATA PROCESSING│ │ AI TOOLS │
│ FILES │ │ SCRIPTS │ │ (SUBPROCESS) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ JSON DATA │ │ EXCEL GENERATION│ │ OUTPUT │
│ STRUCTURES │ │ SCRIPTS │ │ FILES │
└─────────────────┘ └─────────────────┘ └─────────────────┘
```
---
## 📁 **FILE STRUCTURE MAP**
```
FinRyver/
├── app.py # 🚀 MAIN API SERVER
├── agents/
│ ├── simple_tools.py # 🛠️ LANGCHAIN TOOLS
│ ├── langgraph.py # 🔄 WORKFLOW ENGINE
│ └── rlhf_workflows.py # 🎯 RLHF ENHANCED
├── notes/ # 📝 NOTES PROCESSING
│ ├── data_extraction.py # 📊 EXCEL → JSON
│ ├── notes_generator.py # 🤖 RULE-BASED NOTES
│ ├── json_to_excel.py # 📋 JSON → EXCEL
│ ├── llm_notes_generator.py # 🧠 AI NOTES (MISTRAL)
│ └── notes_template.py # 📄 TEMPLATES
├── bs/ # 🏦 BALANCE SHEET
│ ├── bl_llm.py # 🤖 AI PROCESSING
│ ├── csv_json_bs.py # 🔄 CSV → JSON
│ └── sircodebs.py # 📈 BS GENERATION
├── pnl/ # 💰 P&L STATEMENT
│ ├── csv_json_pnl.py # 🔄 DATA PROCESSING
│ └── pnl_note.py # 🧮 CALCULATIONS
├── cf/ # 💧 CASH FLOW
│ ├── cash_flow_data_extractor.py # 📊 DATA EXTRACTION
│ ├── cash_flow_csv_to_json_converter.py # 🔄 CSV → JSON
│ ├── cash_flow_data_processor.py # 🧮 PROCESSING
│ └── cash_flow_statement_generator.py # 📋 EXCEL OUTPUT
├── config/ # ⚙️ CONFIGURATION
│ ├── mapping1.json # 🗺️ ACCOUNT MAPPINGS
│ └── rules1.json # 📋 BUSINESS RULES
└── data/ # 💾 DATA STORAGE
├── input/ # 📥 UPLOADS
├── output/ # 📤 GENERATED FILES
├── generated_notes/ # 📝 AI NOTES
└── clean_financial_data_*.json # 🔄 PROCESSED DATA
```
---
## 🔄 **API ENDPOINTS FLOW**
```
┌─────────────────────────────────────────────────────────────────────────────────┐
│ FASTAPI ENDPOINTS │
└─────────────────────┬─────────────────────┬─────────────────────┬─────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ POST /notes │ │ POST /notes-llm │ │ POST /bs │
│ │ │ │ │ │
│ 🤖 RULE-BASED │ │ 🤖 AI-POWERED │ │ 🏦 BALANCE SHEET│
│ 📝 NOTES │ │ 📝 NOTES │ │ 📊 GENERATION │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ POST /pnl │ │ POST /cf │ │ RLHF ENHANCED │
│ │ │ │ │ │
│ 💰 P&L STATEMENT│ │ 💧 CASH FLOW │ │ 🎯 QUALITY │
│ 📈 GENERATION │ │ 📊 STATEMENT │ │ 🚀 IMPROVEMENT │
└─────────────────┘ └─────────────────┘ └─────────────────┘
```
---
## 📊 **BALANCE SHEET GENERATION FLOW**
```
📥 EXCEL UPLOAD
↓
🏦 POST /bs
↓
💾 data/input/filename.xlsx
↓
🔄 LANGGRAPH WORKFLOW
↓
🛠️ generate_balance_sheet()
↓
┌─────────────────────────────────────┐
│ STEP 1: DATA EXTRACTION │
│ balance_sheet_data_extractor.py │
│ → Extract from Excel sheets │
│ → Create CSV files │
│ → data/csv_notes_bs/ │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 2: CSV → JSON │
│ csv_json_bs.py │
│ → Process CSV data │
│ → Apply business rules │
│ → clean_financial_data_bs.json │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 3: AI PROCESSING │
│ bl_llm.py + sircodebs.py │
│ → Claude 3.5 Sonnet AI │
│ → Account classification │
│ → Balance calculations │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 4: EXCEL GENERATION │
│ → Professional formatting │
│ → Balance sheet layout │
│ → data/output/balance_sheet_*.xlsx │
└─────────────────────────────────────┘
↓
📤 FILE DOWNLOAD
```
---
## 💰 **P&L STATEMENT GENERATION FLOW**
```
📥 EXCEL UPLOAD
↓
💰 POST /pnl
↓
💾 data/input/filename.xlsx
↓
🔄 LANGGRAPH WORKFLOW
↓
🛠️ generate_pnl_statement()
↓
┌─────────────────────────────────────┐
│ STEP 1: DATA EXTRACTION │
│ pnl_data_extractor.py │
│ → Extract P&L accounts │
│ → Identify revenue/expenses │
│ → Create structured data │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 2: DATA PROCESSING │
│ csv_json_pnl.py │
│ → Convert to JSON format │
│ → Apply account mappings │
│ → clean_financial_data_pnl.json │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 3: CALCULATIONS │
│ pnl_note.py │
│ → Revenue calculations │
│ → Expense calculations │
│ → Profit calculations │
│ → EBITDA, EBIT, PBT, PAT │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 4: EXCEL GENERATION │
│ → Professional P&L format │
│ → Comparative columns │
│ → data/pnl_statement.xlsx │
└─────────────────────────────────────┘
↓
📤 FILE DOWNLOAD
```
---
## 💧 **CASH FLOW STATEMENT GENERATION FLOW**
```
📥 EXCEL UPLOAD
↓
💧 POST /cf
↓
💾 data/input/filename.xlsx
↓
🔄 LANGGRAPH WORKFLOW
↓
🛠️ generate_cash_flow_statement()
↓
┌─────────────────────────────────────┐
│ STEP 1: DATA EXTRACTION │
│ cash_flow_data_extractor.py │
│ → Extract from Excel sheets │
│ → Note 16-23, 2-8, 9, 10-15, 24-30 │
│ → Create CSV files │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 2: CSV → JSON │
│ cash_flow_csv_to_json_converter.py │
│ → Process all CSV files │
│ → Create structured JSON │
│ → clean_financial_data_cfs.json │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 3: DATA PROCESSING │
│ cash_flow_data_processor.py │
│ → Extract P&L data │
│ → Process working capital changes │
│ → Calculate cash flow components │
│ → data/extracted_cfs_data.json │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 4: EXCEL GENERATION │
│ cash_flow_statement_generator.py │
│ → Operating activities │
│ → Investing activities │
│ → Financing activities │
│ → data/cash_flow_statements.xlsx │
└─────────────────────────────────────┘
↓
📤 FILE DOWNLOAD
```
---
## 📝 **NOTES GENERATION FLOW (RULE-BASED)**
```
📥 EXCEL UPLOAD
↓
📝 POST /notes
↓
💾 data/input/filename.xlsx
↓
🔄 LANGGRAPH WORKFLOW
↓
🛠️ generate_notes_full_pipeline_from_path()
↓
┌─────────────────────────────────────┐
│ STEP 1: DATA EXTRACTION │
│ data_extraction.py │
│ → extract_trial_balance_data() │
│ → Process Excel trial balance │
│ → Create structured data │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 2: ANALYSIS & SAVE │
│ analyze_and_save_results() │
│ → Validate data completeness │
│ → data/output1/parsed_trial_balance.json
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 3: NOTES GENERATION │
│ notes_generator.py │
│ → process_json() │
│ → Apply config/rules1.json │
│ → Use config/mapping1.json │
│ → Generate financial notes │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 4: JSON NORMALIZATION │
│ → Wrap in {"notes": [...]} format │
│ → data/output2/notes_output_wrapped.json
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 5: EXCEL GENERATION │
│ json_to_excel.py │
│ → json_to_xlsx() │
│ → Professional formatting │
│ → data/output3/final_output.xlsx │
└─────────────────────────────────────┘
↓
📤 FILE DOWNLOAD
```
---
## 🤖 **NOTES GENERATION FLOW (AI-POWERED)**
```
📥 EXCEL UPLOAD
↓
📝 POST /notes-llm
↓
💾 data/input/filename.xlsx
↓
🔄 LANGGRAPH WORKFLOW
↓
🛠️ generate_llm_notes()
↓
┌─────────────────────────────────────┐
│ STEP 1: DATA EXTRACTION │
│ llm_notes_data_processor.py │
│ → extract_trial_balance_data() │
│ → Process Excel data │
│ → data/output1/parsed_trial_balance.json
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 2: AI NOTES GENERATION │
│ llm_notes_generator.py │
│ → FlexibleFinancialNoteGenerator │
│ → Mistral AI (mixtral-8x7b) │
│ → OpenRouter API │
│ → data/generated_notes/notes.json │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 3: VALIDATION │
│ → JSON structure validation │
│ → Content completeness check │
│ → Account classification │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 4: EXCEL GENERATION │
│ llm_notes_excel_converter.py │
│ → normalize_llm_notes_json() │
│ → Professional Excel format │
│ → data/generated_notes_excel/notes.xlsx
└─────────────────────────────────────┘
↓
📤 FILE DOWNLOAD
```
---
## 🎯 **RLHF ENHANCEMENT FLOW**
```
📥 EXCEL UPLOAD + RLHF=TRUE
↓
🎯 POST /notes?use_rlhf=true
↓
🔄 RLHF WORKFLOW MANAGER
↓
┌─────────────────────────────────────┐
│ STEP 1: STATE CREATION │
│ RLHFFinancialAgentState │
│ → statement_id: uuid │
│ → file_path: input file │
│ → candidates_generated: [] │
│ → best_candidate_index: None │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 2: CANDIDATE GENERATION │
│ → Generate 3 note variations │
│ → Different processing approaches │
│ → Store in candidates array │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 3: QUALITY PREDICTION │
│ → ML reward model evaluation │
│ → Score each candidate │
│ → Select best candidate │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ STEP 4: OUTPUT SELECTION │
│ → Return highest quality output │
│ → Update model with feedback │
│ → Continuous improvement │
└─────────────────────────────────────┘
↓
📤 FILE DOWNLOAD
```
---
## 🔧 **ENVIRONMENT VARIABLES MAP**
```
┌─────────────────────────────────────────────────────────────────────────────────┐
│ ENVIRONMENT VARIABLES │
├─────────────────────┬─────────────────────┬─────────────────────┬─────────────┤
│ OPENROUTER_API_KEY │ CFS_EXCEL_FILE_PATH │ CFS_OUTPUT_FOLDER │ CFS_JSON_INPUT │
│ 🤖 AI API Access │ 📁 Input Excel Path │ 📁 CSV Output Dir │ 📄 Input JSON │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ CFS_JSON_OUTPUT │ CFS_EXTRACTED_FILE │ CFS_OUTPUT_FILE │ CFS_TAX_PAID │
│ 📄 Output JSON │ 📄 Extracted Data │ 📄 Final Excel │ 💰 Tax Amount │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ CFS_SKIPROWS │ CFS_NOTE_SHEETS │ INPUT_FILE │ OUTPUT_FOLDER │
│ ⏭️ Excel Skip Rows │ 📋 Sheet Names │ 📄 Input File │ 📁 Output Dir │
└─────────────────────┴─────────────────────┴─────────────────────┴─────────────┘
```
---
## ⚡ **COMPONENT INTERACTION MATRIX**
```
┌─────────────────────────────────────────────────────────────────────────────────┐
│ COMPONENT INTERACTION MATRIX │
├─────────────────────┬─────────────────────┬─────────────────────┬─────────────┤
│ COMPONENT │ INPUT │ PROCESS │ OUTPUT │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ app.py │ HTTP Requests │ FastAPI Routing │ FileResponse │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ langgraph.py │ File Path + Type │ Workflow Orchestration│ Success/Error │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ simple_tools.py │ File Path │ Subprocess Calls │ Excel Path │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ *_extractor.py │ Excel File │ Data Extraction │ CSV Files │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ *_converter.py │ CSV Files │ JSON Processing │ JSON Data │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ *_generator.py │ JSON Data │ Calculations │ Excel File │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ llm_*.py │ JSON Data │ AI Processing │ Enhanced Data│
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────┤
│ rlhf_*.py │ File Path │ Quality Enhancement │ Best Output │
└─────────────────────┴─────────────────────┴─────────────────────┴─────────────┘
```
---
## 🚨 **ERROR HANDLING FLOW**
```
┌─────────────────────────────────────┐ ┌─────────────────────────────────────┐
│ ERROR DETECTED │ │ SUCCESS PATH │
│ • File not found │ │ • All steps complete │
│ • Invalid Excel format │ │ • Output file created │
│ • API connection failed │ │ • Validation passed │
│ • Calculation errors │ │ • FileResponse ready │
└─────────────────────┬─────────────┘ └─────────────────────┬─────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ ERROR HANDLING │ │ SUCCESS │
│ LOGIC │ │ RESPONSE │
└─────────────────┘ └─────────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ • Log Error │ │ • Return Excel │
│ • Return Error │ │ • Status: Success│
│ • Status: Error │ │ • File Download │
└─────────────────┘ └─────────────────┘
```
---
## 🔄 **DATA TRANSFORMATION PIPELINE**
```
EXCEL FILE
↓
┌─────────────────────────────────────┐
│ DATA EXTRACTION │
│ • Read Excel sheets │
│ • Extract account data │
│ • Create structured records │
│ • Output: CSV files │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ DATA PROCESSING │
│ • Parse CSV files │
│ • Apply business rules │
│ • Account classification │
│ • Output: JSON structures │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ CALCULATIONS │
│ • Financial calculations │
│ • Balance validations │
│ • AI enhancement (optional) │
│ • Output: Processed data │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ EXCEL GENERATION │
│ • Professional formatting │
│ • Headers and styling │
│ • Multiple worksheets │
│ • Output: Excel file │
└─────────────────────────────────────┘
↓
FILE DOWNLOAD
```
---
## 🤖 **AI INTEGRATION WORKFLOW**
```
┌─────────────────────────────────────┐
│ AI REQUEST │
│ • Trial balance data │
│ • Processing instructions │
│ • Account mappings │
└─────────────────────┬─────────────┘
│
▼
┌─────────────────┐
│ OPENROUTER API │
│ • Claude 3.5 │
│ • Mistral AI │
│ • API Key Auth │
└─────────────────┘
│
▼
┌─────────────────┐
│ AI PROCESSING │
│ • Account analysis│
│ • Note generation│
│ • Classification │
└─────────────────┘
│
▼
┌─────────────────────────────────────┐
│ RESPONSE VALIDATION │
│ • JSON structure check │
│ • Content completeness │
│ • Financial logic validation │
│ • Fallback model if needed │
└─────────────────────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ ENHANCED OUTPUT │
│ • AI-improved data │
│ • Better classifications │
│ • Intelligent notes │
└─────────────────────────────────────┘
```
---
## 📊 **PERFORMANCE MONITORING FLOW**
```
┌─────────────────────────────────────┐
│ REQUEST START │
│ • Timestamp recording │
│ • Execution ID generation │
│ • Resource monitoring │
└─────────────────────┬─────────────┘
│
▼
┌─────────────────┐
│ PROCESSING │
│ • Step timing │
│ • Memory usage │
│ • API calls │
└─────────────────┘
│
▼
┌─────────────────────────────────────┐
│ METRICS COLLECTION │
│ • Total execution time │
│ • Step-by-step timing │
│ • Success/error rates │
│ • Resource utilization │
└─────────────────────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ LOGGING & REPORTING │
│ • Comprehensive logs │
│ • Performance dashboards │
│ • Error tracking │
│ • Optimization insights │
└─────────────────────────────────────┘
```
## 🤖 **Part 1.5: LLM Notes Generation (Simple Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file via Streamlit/API │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /notes-llm request to FastAPI with file upload │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI saves file to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_workflow(file_path, "notes-llm") called from app.py │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ LangGraph creates FinancialAgentState with file_path │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ LangGraph invokes generate_llm_notes tool │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔍 DATA EXTRACTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 1: Run llm_notes_data_processor.py via subprocess │
│ - Calls extract_trial_balance_data(file_path) │
│ - Processes Excel trial balance data │
│ - Creates structured trial balance records │
│ - Saves to data/output1/parsed_trial_balance.json │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🤖 LLM PROCESSING PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 2: Run llm_notes_generator.py via subprocess │
│ - Loads parsed_trial_balance.json │
│ - Uses FlexibleFinancialNoteGenerator class │
│ - Calls OpenRouter API with Mistral AI models: │
│ * Primary: mistralai/mixtral-8x7b-instruct │
│ * Fallback: mistralai/mistral-7b-instruct-v0.2 │
│ - Generates intelligent financial notes using LLM │
│ - Supports specific note numbers or all notes │
│ - Saves to data/generated_notes/notes.json │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📊 EXCEL GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 3: Run llm_notes_excel_converter.py via subprocess │
│ - Loads data/generated_notes/notes.json │
│ - Applies normalize_llm_notes_json() transformation │
│ - Creates professional Excel format with formatting │
│ - Adds headers and styling for financial notes │
│ - Saves to data/generated_notes_excel/notes.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
✅ VALIDATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ LLM Response Validation │
│ - Validates JSON structure from LLM response │
│ - Checks for required note fields and data completeness │
│ - Verifies account classifications and amounts │
│ - Logs validation results and any parsing issues │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ LangGraph returns success with output_xlsx_path │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with notes Excel file │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client downloads LLM-generated financial notes Excel │
└─────────────────────────────────────────────────────────────┘
```
## 🏦 **Part 2: Balance Sheet Generation (Simple Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /bs request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ File saved to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_workflow(file_path, "bs") from LangGraph │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ LangGraph invokes generate_balance_sheet tool │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔍 DATA EXTRACTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 1: Run balance_sheet_data_extractor.py via subprocess │
│ - Extracts trial balance data │
│ - Processes account classifications │
│ - Creates clean_financial_data_bs.json │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Step 2: Run csv_json_bs.py via subprocess │
│ - Converts extracted data to structured format │
│ - Applies business logic and validations │
│ - Prepares data for balance sheet generation │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🏗️ BALANCE SHEET GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 3: Run sircodebs.py via subprocess │
│ - Loads clean_financial_data_bs.json │
│ - Uses EnhancedBalanceSheetGenerator class │
│ - Extracts data using template structure │
│ - Applies AI-assisted extraction if needed │
│ - Calculates totals and balances │
│ - Validates balance sheet equation │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📊 EXCEL FORMATTING PHASE
┌─────────────────────────────────────────────────────────────┐
│ Balance Sheet Excel Generation │
│ - Creates professional Excel format │
│ - Applies corporate styling and formatting │
│ - Adds headers: "BALANCE SHEET As at March 31, 2024" │
│ - Structures: Equity & Liabilities vs Assets │
│ - Saves to data/output/balance_sheet_{timestamp}.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
✅ VALIDATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Balance Sheet Validation │
│ - Verifies Assets = Equity + Liabilities │
│ - Checks for balance differences │
│ - Logs validation results │
│ - Reports any discrepancies │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI locates first .xlsx file in data/output/ │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FileResponse created with balance sheet Excel file │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client downloads professional balance sheet │
└─────────────────────────────────────────────────────────────┘
```
## 💰 **Part 3: P&L Generation (Simple Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /pnl request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ File saved to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_workflow(file_path, "pnl") from LangGraph │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ LangGraph invokes generate_pnl_statement tool │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔍 DATA EXTRACTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 1: Run PnL data extraction subprocess │
│ - Processes trial balance Excel file │
│ - Identifies revenue and expense accounts │
│ - Extracts income statement line items │
│ - Creates structured PnL data │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Step 2: Account Classification │
│ - Maps accounts using config/mapping1.json │
│ - Applies rules from config/rules1.json │
│ - Categorizes into: Revenue, COGS, Operating Expenses │
│ - Separates Other Income and Finance Costs │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🧮 PNL CALCULATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Revenue Calculations │
│ - Revenue from Operations (Sales, Service Income) │
│ - Other Income (Interest, Gains) │
│ - Total Income calculation │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Expense Calculations │
│ - Cost of Materials Consumed │
│ - Employee Benefits Expense │
│ - Finance Costs │
│ - Depreciation and Amortization │
│ - Other Expenses │
│ - Total Expenses calculation │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Profit Calculations │
│ - EBITDA = Total Income - Operating Expenses │
│ - EBIT = EBITDA - Depreciation & Amortization │
│ - PBT = EBIT - Finance Costs │
│ - PAT = PBT - Tax Expense │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📊 EXCEL GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ P&L Statement Excel Creation │
│ - Creates professional P&L format │
│ - Headers: "PROFIT AND LOSS STATEMENT" │
│ - Period: "For the year ended March 31, 2024" │
│ - Comparative columns: Current Year vs Previous Year │
│ - Applies accounting formatting and styling │
│ - Saves to data/pnl_statement.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with pnl_statement.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client downloads P&L statement Excel file │
└─────────────────────────────────────────────────────────────┘
```
## 💧 **Part 4: Cash Flow Generation (Simple Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /cf request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ File saved to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_workflow(file_path, "cf") from LangGraph │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ LangGraph invokes generate_cash_flow_statement tool │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔍 DATA EXTRACTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 1: Run cf_middlestep.py via subprocess │
│ - Executes FinancialDataExtractor class │
│ - Loads trial balance data │
│ - Extracts P&L data (Profit, Depreciation, Interest) │
│ - Extracts Working Capital changes │
│ - Extracts Investing Activities data │
│ - Extracts Financing Activities data │
│ - Creates data/extracted_cfs_data.json │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🧮 CASH FLOW CALCULATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Operating Activities Calculations │
│ - Start with Profit Before Tax (PBT) │
│ - Add: Depreciation and Amortization │
│ - Less: Interest Income │
│ - Operating Profit Before Working Capital Changes │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Working Capital Movements │
│ - (Increase)/Decrease in Trade Receivables │
│ - (Increase)/Decrease in Inventories │
│ - (Increase)/Decrease in Other Current Assets │
│ - Increase/(Decrease) in Trade Payables │
│ - Increase/(Decrease) in Other Current Liabilities │
│ - Cash Generated from Operations │
│ - Less: Direct Taxes Paid │
│ - Net Cash Flow from Operating Activities │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Investing Activities Calculations │
│ - Purchase of Fixed Assets (Outflow) │
│ - Sale of Fixed Assets (Inflow) │
│ - Interest Income (Inflow) │
│ - Net Cash Flow from Investing Activities │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Financing Activities Calculations │
│ - Proceeds from Long Term Borrowings │
│ - Repayment of Long Term Borrowings │
│ - Dividend Paid (Outflow) │
│ - Net Cash Flow from Financing Activities │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Net Cash Flow Calculation │
│ - Net Increase/Decrease in Cash = Operating + Investing + │
│ Financing │
│ - Cash at Beginning of Year │
│ - Cash at End of Year │
│ - Verification of Cash Reconciliation │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📊 EXCEL GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Step 2: Run cf_generation.py via subprocess │
│ - Loads extracted_cfs_data.json │
│ - Uses CashFlowStatementGenerator class │
│ - Creates professional Excel format │
│ - Headers: "CASH FLOW STATEMENT" │
│ - Period: "For the year ended March 31, 2024" │
│ - Three main sections: Operating, Investing, Financing │
│ - Components of Cash and Cash Equivalents │
│ - Applies professional styling and formatting │
│ - Saves to data/cash_flow_statements.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
✅ VALIDATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Cash Flow Validation │
│ - Verifies Net Change = Ending Cash - Beginning Cash │
│ - Checks mathematical accuracy of all sections │
│ - Validates working capital calculations │
│ - Reports any discrepancies or balancing issues │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with cash_flow_statements.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client downloads professional cash flow statement │
└─────────────────────────────────────────────────────────────┘
```
## 🤖 **Part 5: Notes Generation (RLHF-Enhanced Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
│ Streamlit checkbox: "Use RLHF" = TRUE │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /notes?use_rlhf=true request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI saves file to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🤖 RLHF WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_rlhf_workflow(file_path, "notes") called from app.py │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ RLHFWorkflowManager creates RLHFFinancialAgentState │
│ - statement_id: uuid.uuid4() │
│ - file_path: input file path │
│ - candidates_generated: [] │
│ - best_candidate_index: None │
│ - predicted_quality: None │
│ - confidence_score: None │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 CANDIDATE GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Check if Reward Model is Trained │
│ if self.reward_model.is_trained: Generate 3 Candidates │
│ else: Generate Single Statement │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 1 GENERATION │
│ _generate_candidates() calls generate_notes_full_pipeline_ │
│ from_path(file_path) with slight variations: │
│ - Same data extraction process │
│ - extract_trial_balance_data(file_location) │
│ - analyze_and_save_results() → parsed_trial_balance.json │
│ - process_json() with LLM variation 1 │
│ - json_to_xlsx() → candidate_1_notes.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 2 GENERATION │
│ Second call to generate_notes_full_pipeline_from_path(): │
│ - Same extraction and analysis │
│ - Different LLM prompt variations or temperature │
│ - Slightly different note generation approach │
│ - json_to_xlsx() → candidate_2_notes.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 3 GENERATION │
│ Third call to generate_notes_full_pipeline_from_path(): │
│ - Same extraction process │
│ - Third variation in LLM processing │
│ - Alternative formatting or structure │
│ - json_to_xlsx() → candidate_3_notes.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎯 QUALITY PREDICTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Feature Extraction for Candidate 1 │
│ reward_model.extract_features(): │
│ - File size and processing time metrics │
│ - Content analysis (word count, structure complexity) │
│ - Domain-specific metrics (account categories count) │
│ - Balance verification accuracy │
│ - Metadata features (statement type, complexity) │
│ → Feature Vector 1 (16 dimensions) │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Quality Prediction for Candidate 1 │
│ RandomForestRegressor.predict(features_1): │
│ - Model trained on human feedback data │
│ - Predicts quality score (1.0-5.0 scale) │
│ - Calculates confidence score │
│ → Predicted Quality: 3.2, Confidence: 0.85 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Feature Extraction & Prediction for Candidate 2 │
│ → Predicted Quality: 4.1, Confidence: 0.92 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Feature Extraction & Prediction for Candidate 3 │
│ → Predicted Quality: 3.8, Confidence: 0.88 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🏆 BEST CANDIDATE SELECTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ _select_best_candidate() Analysis: │
│ - Compare quality scores: [3.2, 4.1, 3.8] │
│ - Compare confidence scores: [0.85, 0.92, 0.88] │
│ - Select highest quality with sufficient confidence │
│ → Best Candidate: #2 (Quality: 4.1, Confidence: 0.92) │
│ - best_candidate_index = 1 │
│ - predicted_quality = 4.1 │
│ - confidence_score = 0.92 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
💾 STATEMENT STORAGE PHASE
┌─────────────────────────────────────────────────────────────┐
│ feedback_manager.store_generated_statement(): │
│ - statement_id: uuid.uuid4() │
│ - statement_type: "notes" │
│ - file_path: original input file │
│ - output_path: selected candidate Excel path │
│ - generation_time: end_time - start_time │
│ - predicted_quality: 4.1 │
│ - confidence_score: 0.92 │
│ - metadata: {candidates_count: 3, best_index: 1} │
│ → Stored in data/feedback/generated_statements.json │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 ENHANCED OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with best candidate Excel │
│ + Enhanced Headers: │
│ - X-RLHF-Statement-ID: uuid-123 │
│ - X-RLHF-Quality-Score: 4.1 │
│ - X-RLHF-Confidence: 0.92 │
│ - Content-Disposition: attachment; filename=notes.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client receives highest-quality notes with RLHF metadata │
│ Streamlit displays RLHF information to user │
└─────────────────────────────────────────────────────────────┘
🔄 FEEDBACK COLLECTION PHASE (Asynchronous)
┌─────────────────────────────────────────────────────────────┐
│ Human reviewer accesses /rlhf/pending-reviews │
│ Reviews generated notes statement │
│ Submits feedback via /rlhf/feedback: │
│ - statement_id: uuid-123 │
│ - calculation_accuracy: 4 (1-5 scale) │
│ - account_classification: 5 │
│ - statement_balance: 4 │
│ - accounting_standards: 4 │
│ - regulatory_compliance: 5 │
│ - completeness: 4 │
│ - professional_presentation: 4 │
│ - would_accept_for_audit: true │
│ - specific_errors: "Minor formatting in note headers" │
│ → overall_score: (4+5+4+4+5+4+4)/7 = 4.29 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎓 MODEL IMPROVEMENT PHASE
┌─────────────────────────────────────────────────────────────┐
│ feedback_manager.store_feedback() saves human evaluation │
│ RLHFTrainer.periodic_training_check(): │
│ - Check if enough samples for retraining (min 2-5) │
│ - If yes: Extract features + feedback for all statements │
│ - Retrain RandomForestRegressor with new feedback data │
│ - Update model weights and save to data/models/ │
│ - Log training metrics (R², MSE, feature importance) │
│ → Next statements will have improved quality predictions │
└─────────────────────────────────────────────────────────────┘
```
## 🤖 **Part 5.5: LLM Notes Generation (RLHF-Enhanced Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
│ Streamlit checkbox: "Use RLHF" = TRUE │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /notes-llm?use_rlhf=true request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI saves file to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🤖 RLHF WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_rlhf_workflow(file_path, "notes-llm") called from app.py│
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ RLHFWorkflowManager creates RLHFFinancialAgentState │
│ - statement_id: uuid.uuid4() │
│ - statement_type: "notes-llm" │
│ - candidates_generated: [] │
│ - best_candidate_index: None │
│ - predicted_quality: None │
│ - confidence_score: None │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 CANDIDATE GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Check if Reward Model is Trained │
│ if self.reward_model.is_trained: Generate 3 Candidates │
│ else: Generate Single Statement │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 1 GENERATION │
│ _generate_candidates() calls generate_llm_notes(): │
│ - Run llm_notes_data_processor.py (variant 1) │
│ - Apply different extraction parameters │
│ - llm_notes_generator.py with prompt variation 1: │
│ * Different LLM temperature or instructions │
│ * Alternative note structure approach │
│ - llm_notes_excel_converter.py with formatting style 1 │
│ → candidate_1_notes_llm.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 2 GENERATION │
│ Second call to generate_llm_notes(): │
│ - Same data extraction process │
│ - Different LLM model selection strategy: │
│ * Prefer Mixtral 8x7B over Mistral 7B │
│ * Alternative prompt engineering │
│ - Different Excel formatting and styling options │
│ → candidate_2_notes_llm.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 3 GENERATION │
│ Third call to generate_llm_notes(): │
│ - Same extraction foundation │
│ - Third LLM approach with different parameters: │
│ * Varied max_tokens and temperature settings │
│ * Alternative account classification prompts │
│ - Enhanced Excel formatting with different styling │
│ → candidate_3_notes_llm.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎯 QUALITY PREDICTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ LLM Notes Specific Feature Extraction: │
│ reward_model.extract_features() for each candidate: │
│ - LLM response quality and coherence metrics │
│ - Account classification accuracy in generated notes │
│ - Note completeness and structure validation │
│ - Professional formatting quality assessment │
│ - Content relevance to financial statement requirements │
│ - Processing time and API call efficiency │
│ - JSON structure validity and parsing success │
│ → LLM-specific feature vectors for quality prediction │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Quality Scoring for LLM Notes Candidates: │
│ RandomForestRegressor predictions with LLM focus: │
│ - Candidate 1: Quality 3.7, Confidence 0.88 │
│ - Candidate 2: Quality 4.5, Confidence 0.94 │
│ - Candidate 3: Quality 4.1, Confidence 0.91 │
│ LLM-specific quality criteria applied │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🏆 BEST CANDIDATE SELECTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ _select_best_candidate() for LLM Notes: │
│ - Prioritize LLM response quality and coherence │
│ - Evaluate account classification accuracy │
│ - Consider note completeness and professional presentation │
│ - Assess content relevance and structure │
│ → Best Candidate: #2 (Quality: 4.5, Confidence: 0.94) │
│ - Highest combined score for LLM-generated content │
└─────────────────────┬───────────────────────────────────────┘
│
▼
💾 STATEMENT STORAGE PHASE
┌─────────────────────────────────────────────────────────────┐
│ Store LLM Notes for Human Review: │
│ feedback_manager.store_generated_statement(): │
│ - statement_type: "notes-llm" │
│ - LLM-specific metadata: │
│ * llm_model_used: "mistralai/mixtral-8x7b-instruct" │
│ * api_calls_made: number of OpenRouter API calls │
│ * notes_generated_count: number of financial notes │
│ * average_response_time: API response time │
│ - predicted_quality: 4.5 │
│ - confidence_score: 0.94 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 ENHANCED OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with best LLM notes candidate │
│ Enhanced Headers for LLM Notes: │
│ - X-RLHF-Statement-ID: uuid-456 │
│ - X-RLHF-Quality-Score: 4.5 │
│ - X-RLHF-Confidence: 0.94 │
│ - X-RLHF-LLM-Model: mistralai/mixtral-8x7b-instruct │
│ - X-RLHF-API-Calls: [count] │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client receives highest-quality LLM notes with RLHF │
│ metadata including LLM model and API usage information │
└─────────────────────────────────────────────────────────────┘
🔄 LLM NOTES FEEDBACK COLLECTION
┌─────────────────────────────────────────────────────────────┐
│ Human reviewer evaluates LLM-generated notes quality: │
│ LLM Notes specific feedback metrics: │
│ - calculation_accuracy: Financial calculations (1-5) │
│ - account_classification: Account grouping accuracy (1-5) │
│ - statement_balance: Mathematical consistency (1-5) │
│ - accounting_standards: GAAP/IFRS compliance (1-5) │
│ - regulatory_compliance: Disclosure requirements (1-5) │
│ - completeness: All required notes present (1-5) │
│ - professional_presentation: Note formatting quality (1-5) │
│ - llm_coherence: Response clarity and logic (1-5) │
│ - content_relevance: Appropriateness for financials (1-5) │
│ - would_accept_for_audit: Audit trail approval │
│ - specific_errors: "LLM hallucinated account classification"│
│ → overall_score: LLM-weighted scoring algorithm │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎓 LLM NOTES MODEL IMPROVEMENT
┌─────────────────────────────────────────────────────────────┐
│ LLM Notes specific model enhancement: │
│ - Improve LLM response quality predictions │
│ - Enhance account classification accuracy assessment │
│ - Refine content relevance and coherence metrics │
│ - Update LLM model performance tracking │
│ - Optimize prompt engineering based on feedback │
│ → Better LLM notes quality predictions for future │
└─────────────────────────────────────────────────────────────┘
```
## 🏦 **Part 6: Balance Sheet Generation (RLHF-Enhanced Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
│ Streamlit checkbox: "Use RLHF" = TRUE │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /bs?use_rlhf=true request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI saves file to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🤖 RLHF WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_rlhf_workflow(file_path, "bs") called from app.py │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ RLHFWorkflowManager creates RLHFFinancialAgentState │
│ - statement_id: uuid.uuid4() │
│ - statement_type: "balance_sheet" │
│ - Initialize candidate tracking │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 CANDIDATE GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 1 GENERATION │
│ _generate_candidates() calls generate_balance_sheet(): │
│ - Run balance_sheet_data_extractor.py (variant 1) │
│ - Apply different extraction parameters or thresholds │
│ - csv_json_bs.py with variation 1 │
│ - sircodebs.py with EnhancedBalanceSheetGenerator │
│ - Different template structure priority │
│ - AI-assisted extraction with prompt variation 1 │
│ → candidate_1_balance_sheet.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 2 GENERATION │
│ Second call to generate_balance_sheet(): │
│ - Same data extraction process │
│ - Different AI prompt strategy for item extraction │
│ - Alternative account categorization approach │
│ - Different balance validation thresholds │
│ - Varied Excel formatting and styling options │
│ → candidate_2_balance_sheet.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 3 GENERATION │
│ Third call to generate_balance_sheet(): │
│ - Same extraction pipeline │
│ - Third AI variation for complex account handling │
│ - Alternative totaling and validation logic │
│ - Different professional formatting approach │
│ → candidate_3_balance_sheet.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎯 QUALITY PREDICTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Feature Extraction for Balance Sheet Candidates │
│ reward_model.extract_features() for each: │
│ - Balance sheet equation accuracy (Assets = Equity + Liab) │
│ - Number of line items extracted vs expected │
│ - Completeness of major sections (Current/Non-current) │
│ - Professional formatting quality metrics │
│ - Data consistency checks (zero balances, missing items) │
│ - Processing time and file size metrics │
│ → Feature vectors for quality prediction │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Quality Scoring for Balance Sheet Candidates │
│ RandomForestRegressor predictions: │
│ - Candidate 1: Quality 3.4, Confidence 0.89 │
│ - Candidate 2: Quality 4.3, Confidence 0.94 │
│ - Candidate 3: Quality 3.9, Confidence 0.87 │
│ Model considers BS-specific quality factors │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🏆 BEST CANDIDATE SELECTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ _select_best_candidate() for Balance Sheet: │
│ - Prioritize mathematical accuracy (balance equation) │
│ - Consider completeness of financial statement sections │
│ - Evaluate professional presentation quality │
│ → Best Candidate: #2 (Quality: 4.3, Confidence: 0.94) │
│ - Highest combined score for BS-specific metrics │
└─────────────────────┬───────────────────────────────────────┘
│
▼
💾 STATEMENT STORAGE PHASE
┌─────────────────────────────────────────────────────────────┐
│ Store Balance Sheet for Human Review: │
│ feedback_manager.store_generated_statement(): │
│ - statement_type: "balance_sheet" │
│ - Balance sheet specific metadata: │
│ * balance_difference: calculated difference │
│ * total_assets: sum of all assets │
│ * total_equity_liabilities: sum of equity + liabilities │
│ * line_items_count: number of extracted items │
│ - predicted_quality: 4.3 │
│ - confidence_score: 0.94 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 ENHANCED OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI locates best candidate balance sheet Excel file │
│ Enhanced Response Headers: │
│ - X-RLHF-Statement-ID: uuid-456 │
│ - X-RLHF-Quality-Score: 4.3 │
│ - X-RLHF-Confidence: 0.94 │
│ - X-RLHF-Balance-Accuracy: calculated accuracy % │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client receives highest-quality balance sheet with RLHF │
│ metadata indicating prediction confidence │
└─────────────────────────────────────────────────────────────┘
🔄 BALANCE SHEET FEEDBACK COLLECTION
┌─────────────────────────────────────────────────────────────┐
│ Human reviewer evaluates balance sheet quality: │
│ BS-specific feedback metrics: │
│ - calculation_accuracy: Mathematical correctness (1-5) │
│ - account_classification: Proper Current/Non-current (1-5) │
│ - statement_balance: Assets = Equity + Liabilities (1-5) │
│ - accounting_standards: GAAP/IFRS compliance (1-5) │
│ - regulatory_compliance: Legal requirements (1-5) │
│ - completeness: All required line items present (1-5) │
│ - professional_presentation: Format quality (1-5) │
│ - would_accept_for_audit: Boolean approval │
│ - specific_errors: "Fixed assets classification issue" │
│ → overall_score: Weighted average of all metrics │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎓 BALANCE SHEET MODEL IMPROVEMENT
┌─────────────────────────────────────────────────────────────┐
│ Balance Sheet specific model enhancement: │
│ - Update feature importance for BS quality factors │
│ - Improve balance equation accuracy predictions │
│ - Enhance account classification confidence │
│ - Refine professional formatting assessment │
│ → Better balance sheet quality predictions for future │
└─────────────────────────────────────────────────────────────┘
```
## 💰 **Part 7: P&L Generation (RLHF-Enhanced Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
│ Streamlit checkbox: "Use RLHF" = TRUE │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /pnl?use_rlhf=true request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI saves file to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🤖 RLHF WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_rlhf_workflow(file_path, "pnl") called from app.py │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ RLHFWorkflowManager creates RLHFFinancialAgentState │
│ - statement_type: "profit_and_loss" │
│ - Initialize P&L specific tracking │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 CANDIDATE GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 1 GENERATION │
│ _generate_candidates() calls generate_pnl_statement(): │
│ - Extract trial balance with revenue/expense focus │
│ - Apply mapping1.json with emphasis on income categories │
│ - Use rules1.json with P&L specific pattern matching │
│ - Revenue categorization approach 1: │
│ * Conservative revenue recognition │
│ * Detailed expense breakdown │
│ - Calculate EBITDA, EBIT, PBT, PAT with method 1 │
│ → candidate_1_pnl.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 2 GENERATION │
│ Second call to generate_pnl_statement(): │
│ - Same data extraction │
│ - Alternative expense categorization: │
│ * Different Cost of Materials grouping │
│ * Alternative Employee Benefits categorization │
│ * Varied Other Expenses classification │
│ - Different depreciation calculation approach │
│ - Alternative profit calculation sequence │
│ → candidate_2_pnl.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 3 GENERATION │
│ Third call to generate_pnl_statement(): │
│ - Same extraction foundation │
│ - Third approach to revenue/expense classification │
│ - Different handling of extraordinary items │
│ - Alternative tax calculation methodology │
│ - Varied comparative analysis with previous year │
│ → candidate_3_pnl.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎯 QUALITY PREDICTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ P&L Specific Feature Extraction: │
│ reward_model.extract_features() for each candidate: │
│ - Revenue recognition accuracy and completeness │
│ - Expense categorization correctness │
│ - Profit calculation accuracy (EBITDA → EBIT → PBT → PAT) │
│ - Comparative analysis quality (current vs previous year) │
│ - Compliance with accounting standards │
│ - Professional formatting and presentation │
│ - Mathematical consistency checks │
│ → P&L specific feature vectors │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Quality Scoring for P&L Candidates: │
│ RandomForestRegressor predictions with P&L focus: │
│ - Candidate 1: Quality 3.6, Confidence 0.87 │
│ - Candidate 2: Quality 4.2, Confidence 0.93 │
│ - Candidate 3: Quality 3.8, Confidence 0.89 │
│ P&L specific quality assessment criteria applied │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🏆 BEST CANDIDATE SELECTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ _select_best_candidate() for P&L Statement: │
│ - Prioritize revenue/expense accuracy │
│ - Evaluate profit calculation correctness │
│ - Consider accounting standards compliance │
│ - Assess comparative analysis quality │
│ → Best Candidate: #2 (Quality: 4.2, Confidence: 0.93) │
│ - Optimal balance of accuracy and presentation │
└─────────────────────┬───────────────────────────────────────┘
│
▼
💾 STATEMENT STORAGE PHASE
┌─────────────────────────────────────────────────────────────┐
│ Store P&L Statement for Human Review: │
│ feedback_manager.store_generated_statement(): │
│ - statement_type: "profit_and_loss" │
│ - P&L specific metadata: │
│ * total_revenue: calculated total income │
│ * total_expenses: calculated total costs │
│ * net_profit: final PAT calculation │
│ * expense_categories_count: number of expense types │
│ - predicted_quality: 4.2 │
│ - confidence_score: 0.93 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 ENHANCED OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with best P&L candidate │
│ Enhanced Headers for P&L: │
│ - X-RLHF-Statement-ID: uuid-789 │
│ - X-RLHF-Quality-Score: 4.2 │
│ - X-RLHF-Confidence: 0.93 │
│ - X-RLHF-Profit-Accuracy: calculated accuracy metric │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client receives highest-quality P&L with RLHF metadata │
└─────────────────────────────────────────────────────────────┘
🔄 P&L FEEDBACK COLLECTION
┌─────────────────────────────────────────────────────────────┐
│ Human reviewer evaluates P&L statement quality: │
│ P&L specific feedback metrics: │
│ - calculation_accuracy: Revenue/expense calculations (1-5) │
│ - account_classification: Proper income/expense grouping │
│ - statement_balance: Mathematical consistency (1-5) │
│ - accounting_standards: Revenue recognition standards (1-5)│
│ - regulatory_compliance: P&L reporting requirements (1-5) │
│ - completeness: All income/expense items present (1-5) │
│ - professional_presentation: P&L format quality (1-5) │
│ - would_accept_for_audit: Audit trail approval │
│ - specific_errors: "Depreciation calculation method" │
│ → overall_score: P&L weighted scoring algorithm │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎓 P&L MODEL IMPROVEMENT
┌─────────────────────────────────────────────────────────────┐
│ P&L specific model enhancement: │
│ - Improve revenue recognition accuracy predictions │
│ - Enhance expense categorization algorithms │
│ - Refine profit calculation sequence assessment │
│ - Update comparative analysis quality metrics │
│ → Better P&L quality predictions for future statements │
└─────────────────────────────────────────────────────────────┘
```
## 💧 **Part 8: Cash Flow Generation (RLHF-Enhanced Flow)**
```
📥 INPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ Client uploads Excel Trial Balance file │
│ Streamlit checkbox: "Use RLHF" = TRUE │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ POST /cf?use_rlhf=true request to FastAPI │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI saves file to data/input/{filename} │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🤖 RLHF WORKFLOW ORCHESTRATION
┌─────────────────────────────────────────────────────────────┐
│ run_rlhf_workflow(file_path, "cf") called from app.py │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ RLHFWorkflowManager creates RLHFFinancialAgentState │
│ - statement_type: "cash_flow" │
│ - Initialize Cash Flow specific tracking │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🔄 CANDIDATE GENERATION PHASE
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 1 GENERATION │
│ _generate_candidates() calls generate_cash_flow_statement():│
│ - cf_middlestep.py execution with approach 1: │
│ * FinancialDataExtractor with default parameters │
│ * Standard working capital calculation method │
│ * Conservative approach to cash flow classifications │
│ - cf_generation.py with formatting style 1: │
│ * Standard CashFlowStatementGenerator │
│ * Traditional indirect method presentation │
│ → candidate_1_cash_flow.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 2 GENERATION │
│ Second call to generate_cash_flow_statement(): │
│ - cf_middlestep.py with alternative approach: │
│ * Different working capital change calculations │
│ * Alternative depreciation and amortization handling │
│ * Varied investing activities categorization │
│ - cf_generation.py with enhanced formatting: │
│ * More detailed cash flow line items │
│ * Enhanced reconciliation and validation │
│ → candidate_2_cash_flow.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CANDIDATE 3 GENERATION │
│ Third call to generate_cash_flow_statement(): │
│ - cf_middlestep.py with comprehensive approach: │
│ * Detailed operating activities breakdown │
│ * Alternative financing activities treatment │
│ * Enhanced cash equivalents definition │
│ - cf_generation.py with advanced formatting: │
│ * Comprehensive components of cash section │
│ * Advanced validation and cross-checks │
│ → candidate_3_cash_flow.xlsx │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎯 QUALITY PREDICTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ Cash Flow Specific Feature Extraction: │
│ reward_model.extract_features() for each candidate: │
│ - Cash flow reconciliation accuracy │
│ - Operating activities calculation correctness │
│ - Working capital changes validation │
│ - Investing activities completeness │
│ - Financing activities accuracy │
│ - Net cash flow mathematical consistency │
│ - Beginning/ending cash reconciliation │
│ - Components of cash and equivalents detail │
│ → Cash flow specific feature vectors │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Quality Scoring for Cash Flow Candidates: │
│ RandomForestRegressor predictions with CF focus: │
│ - Candidate 1: Quality 3.5, Confidence 0.86 │
│ - Candidate 2: Quality 4.4, Confidence 0.95 │
│ - Candidate 3: Quality 4.0, Confidence 0.91 │
│ Cash flow specific quality criteria emphasized │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🏆 BEST CANDIDATE SELECTION PHASE
┌─────────────────────────────────────────────────────────────┐
│ _select_best_candidate() for Cash Flow Statement: │
│ - Prioritize cash reconciliation accuracy │
│ - Evaluate mathematical consistency across sections │
│ - Consider working capital calculation precision │
│ - Assess comprehensive activity coverage │
│ → Best Candidate: #2 (Quality: 4.4, Confidence: 0.95) │
│ - Highest accuracy in cash flow methodology │
└─────────────────────┬───────────────────────────────────────┘
│
▼
💾 STATEMENT STORAGE PHASE
┌─────────────────────────────────────────────────────────────┐
│ Store Cash Flow Statement for Human Review: │
│ feedback_manager.store_generated_statement(): │
│ - statement_type: "cash_flow" │
│ - Cash Flow specific metadata: │
│ * operating_cash_flow: net cash from operations │
│ * investing_cash_flow: net cash from investing │
│ * financing_cash_flow: net cash from financing │
│ * net_cash_change: total change in cash │
│ * cash_reconciliation_accuracy: percentage match │
│ - predicted_quality: 4.4 │
│ - confidence_score: 0.95 │
└─────────────────────┬───────────────────────────────────────┘
│
▼
📤 ENHANCED OUTPUT PHASE
┌─────────────────────────────────────────────────────────────┐
│ FastAPI creates FileResponse with best CF candidate │
│ Enhanced Headers for Cash Flow: │
│ - X-RLHF-Statement-ID: uuid-012 │
│ - X-RLHF-Quality-Score: 4.4 │
│ - X-RLHF-Confidence: 0.95 │
│ - X-RLHF-Cash-Reconciliation: reconciliation percentage │
└─────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Client receives highest-quality Cash Flow with RLHF data │
└─────────────────────────────────────────────────────────────┘
🔄 CASH FLOW FEEDBACK COLLECTION
┌─────────────────────────────────────────────────────────────┐
│ Human reviewer evaluates Cash Flow statement quality: │
│ Cash Flow specific feedback metrics: │
│ - calculation_accuracy: Cash flow calculations (1-5) │
│ - account_classification: Activity categorization (1-5) │
│ - statement_balance: Cash reconciliation accuracy (1-5) │
│ - accounting_standards: Cash flow standards compliance(1-5)│
│ - regulatory_compliance: CF reporting requirements (1-5) │
│ - completeness: All activities and components covered (1-5)│
│ - professional_presentation: CF format quality (1-5) │
│ - would_accept_for_audit: Cash flow audit acceptance │
│ - specific_errors: "Working capital calculation method" │
│ → overall_score: Cash flow weighted scoring algorithm │
└─────────────────────┬───────────────────────────────────────┘
│
▼
🎓 CASH FLOW MODEL IMPROVEMENT
┌─────────────────────────────────────────────────────────────┐
│ Cash Flow specific model enhancement: │
│ - Improve cash reconciliation accuracy predictions │
│ - Enhance working capital change calculations │
│ - Refine activity classification algorithms │
│ - Update cash flow methodology assessment │
│ → Better Cash Flow quality predictions for future │
└─────────────────────────────────────────────────────────────┘
🔄 CONTINUOUS IMPROVEMENT CYCLE
┌─────────────────────────────────────────────────────────────┐
│ All 4 RLHF Statement Types Feed into Model Improvement: │
│ - Notes feedback → Note generation quality enhancement │
│ - Balance Sheet feedback → BS accuracy improvement │
│ - P&L feedback → Revenue/expense classification refinement │
│ - Cash Flow feedback → Cash reconciliation enhancement │
│ │
│ Cross-Statement Learning: │
│ - Common quality patterns across all statement types │
│ - Professional presentation standards │
│ - Accounting compliance improvements │
│ - Mathematical accuracy enhancements │
│ │
│ → Unified Financial Statement Quality Prediction Model │
└─────────────────────────────────────────────────────────────┘
```
## 🔄 **RLHF Enhanced Processing Flow**
```
📤 CLIENT REQUEST
┌─────────────────────┐
│ POST /pnl? │
│ use_rlhf=true │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ FastAPI Endpoint │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ RLHF Workflow │
│ Manager │
└─────────┬───────────┘
│
▼
🎯 CANDIDATE GENERATION
┌─────────────────────┐
│ Generate P&L │
│ Statement │
│ [Candidate 1] │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Generate P&L │
│ Statement │
│ [Candidate 2] │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Generate P&L │
│ Statement │
│ [Candidate 3] │
└─────────┬───────────┘
│
▼
🤖 QUALITY PREDICTION
┌─────────────────────┐
│ Reward Model │
│ predict_quality() │
│ │
│ Candidate 1: 3.2 │
│ Confidence: 0.85 │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Reward Model │
│ predict_quality() │
│ │
│ Candidate 2: 4.1 │
│ Confidence: 0.92 │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Reward Model │
│ predict_quality() │
│ │
│ Candidate 3: 3.8 │
│ Confidence: 0.88 │
└─────────┬───────────┘
│
▼
🎖️ BEST SELECTION
┌─────────────────────┐
│ select_best_ │
│ candidate() │
│ │
│ → Candidate 2 │
│ (Highest Score) │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Feedback Manager │
│ store_generated_ │
│ statement() │
│ │
│ → Statement ID: │
│ uuid-123 │
└─────────┬───────────┘
│
▼
📤 RESPONSE TO CLIENT
┌─────────────────────┐
│ Excel File + │
│ RLHF Headers: │
│ │
│ X-Statement-ID │
│ X-Quality-Score │
│ X-Confidence │
└─────────┬───────────┘
│
▼
👥 HUMAN REVIEW CYCLE
┌─────────────────────┐
│ Human Reviews │
│ Statement │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ POST /rlhf/feedback │
│ │
│ Statement-ID: 123 │
│ Ratings: 1-5 scale │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Feedback Manager │
│ store_feedback() │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Reward Model │
│ trigger_retraining_ │
│ if_needed() │
└─────────────────────┘
```
## 🛠️ **Component Interaction Flow**
```
📡 API LAYER
┌─────────────┐ ┌─────────────┐
│ FastAPI │ │ RLHF │
│ App │ │ Routes │
└──────┬──────┘ └──────┬──────┘
│ │
▼ ▼
🔄 WORKFLOW LAYER
┌─────────────┐ ┌─────────────┐
│ LangGraph │ │ RLHF │
│ Workflows │ │ Workflows │
└──────┬──────┘ └──────┬──────┘
│ │
└─────┬────────────┘
▼
🛠️ TOOLS LAYER
┌─────────────┐
│ Simple │
│ Tools │
└──────┬──────┘
│
┌──┴────────────────┬────────────────┬──────────────┐
▼ ▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ BS │ │ PnL │ │ CF │ │ Notes │
│ Tool │ │ Tool │ │ Tool │ │ Tool │
└────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘
│ │ │ │
└──────┬───────┴──────┬───────┴──────┬───────┘
▼ ▼ ▼
⚙️ PROCESSING LAYER
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Data │ │ Data │ │ Excel │
│ Extraction │ │ Processing │ │ Formatting │
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
│ │ │
└─────┬────────────┴─────┬────────────┘
▼ ▼
🤖 RLHF LAYER
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Feedback │ │ Reward │ │ RLHF │
│ Manager │ │ Model │ │ Trainer │
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
│ │ │
└─────┬────────────┴─────┬────────────┘
▼ ▼
💾 STORAGE LAYER
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ JSON │ │ Excel │ │ Feedback │ │ Model │
│ Storage │ │ Output │ │ Database │ │ Storage │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
```
## 📈 **RLHF Training & Improvement Cycle**
```
🎯 START
┌─────────────────────┐
│ Statement Generation│
│ Request │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ RLHF Enabled │
│ (?) │
└─────┬─────────┬─────┘
│ │
NO│ │YES
▼ ▼
┌──────────┐ ┌──────────┐
│ Standard │ │ RLHF │
│ Workflow │ │ Workflow │
└─────┬────┘ └─────┬────┘
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│ Generate │ │Generate 3│
│ Single │ │Candidates│
│Statement │ │ │
└─────┬────┘ └─────┬────┘
│ │
└─────┬──────┘
▼
┌─────────────────────┐
│ Extract Features │
│ for Each Candidate │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Predict Quality │
│ Scores │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Select Best │
│ Candidate │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Store for Feedback │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Excel Output │
└─────────┬───────────┘
│
▼
👥 HUMAN FEEDBACK
┌─────────────────────┐
│ Human Review │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Feedback Form │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Collect Ratings │
│ (1-5 scale) │
└─────────┬───────────┘
│
▼
💾 FEEDBACK STORAGE
┌─────────────────────┐
│ Feedback Database │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Enough Samples │
│ (?) │
└─────┬─────────┬─────┘
│ │
NO│ │YES
▼ ▼
┌──────────┐ ┌──────────┐
│ Wait │ │ Retrain │
│ for │ │ Reward │
│ More │ │ Model │
│Feedback │ │ │
└─────┬────┘ └─────┬────┘
│ │
└─────┐ ▼
│ ┌──────────┐
│ │ Update │
│ │ Model │
│ │ Weights │
│ └─────┬────┘
│ │
│ ▼
│ ┌──────────┐
│ │ Improved │
│ │Predictions│
│ └─────┬────┘
│ │
└───────┼─────────┐
│ │
┌─────────┘ │
▼ │
(Back to Human) │
│
┌───────────────┘
▼
(Back to RLHF Start)
```
## 🎯 **Statement Type Processing Flows**
```
📝 NOTES GENERATION
┌─────────────────────┐
│ Trial Balance Excel │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│extract_trial_ │
│balance_data │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│analyze_and_save_ │
│results │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ process_json │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ LLM Notes Generation│
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ JSON Normalization │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ json_to_xlsx │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ final_output.xlsx │
└─────────────────────┘
📊 BALANCE SHEET
┌─────────────────────┐
│ Trial Balance Excel │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ BS Data Extractor │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│clean_financial_ │
│data_bs.json │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ BS Generator │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Excel Formatting │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ balance_sheet.xlsx │
└─────────────────────┘
💰 P&L STATEMENT
┌─────────────────────┐
│ Trial Balance Excel │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ PnL Data Processing │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ PnL Calculations │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Excel Export │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ pnl_statement.xlsx │
└─────────────────────┘
💧 CASH FLOW
┌─────────────────────┐
│ Trial Balance Excel │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ CF Data Processor │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│extracted_cfs_ │
│data.json │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│CF Statement │
│Generator │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Excel Formatting │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│cash_flow_statements.│
│xlsx │
└─────────────────────┘
```
**generator-validator**
┌─────────────────┐
│ API Request │
│ POST /notes-llm│
│ with file │
└─────────┬───────┘
│
▼
┌─────────────────┐
│ create_notes_ │
│ pipeline() │
│ │
│ → LLMNotesGen │
│ → NotesValidator│
└─────────┬───────┘
│
▼
┌─────────────────┐ ┌─────────────────┐
│Generator-Validator│◄───│ Max 3 Attempts │
│ Pipeline │ │ │
│ │ │ │
│ ┌─────────────┐ │ └─────────────────┘
│ │ Attempt │ │ │ No
│ │ Counter=0 │ │ ▼
│ └─────────────┘ │ ┌─────────────────┐
└─────────┬───────┘ │ Return Best │
│ │ Result │
▼ └─────────────────┘
┌─────────────────┐
│ Generate │
│ (LLM) │
│ │
│ - Call langgraph│
│ - Use RLHF if │
│ requested │
│ - Track attempt │
└─────────┬───────┘
│
▼
┌─────────────────┐
│ Validate │
│ Quality │
│ │
│ - File exists │
│ - Size >1KB │
│ - Metadata OK │
│ - RLHF quality │
│ - Score 0.0-1.0 │
└─────────┬───────┘
│
┌─────┴─────┐
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ Valid? │ │ Invalid │
│ Score │ │ Score │
│ ≥0.6 │ │ <0.6 │
└─────┬───┘ └─────┬───┘
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ Return │ │ Refine │
│ Success │ │ & Retry │
│ with │ │ │
│ Metadata │ │ - Use │
│ Headers │ │ feedback│
└─────────┘ └─────┬───┘
│
▼
┌─────────┐
│Increment │
│ Attempt │
│ Counter │
└─────┬───┘
│
└─────────────┐
▼
┌─────────────────┐
│ Continue to │
│ Next Attempt │
└─────────────────┘
**refine and retry**
┌─────────────────┐
│ Attempt 1 │
│ Generate │
│ → Validate │
│ Score: 0.4 │ ❌ FAIL (< 0.6)
└─────────┬───────┘
│
▼
┌─────────────────┐
│ Refinement │
│ Analysis │
│ │
│ Feedback: │
│ - "Low quality" │
│ - "Small file" │
└─────────┬───────┘
│
┌─────┴─────┐
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│Quality │ │Other │
│Issue? │ │Issue? │
│ │ │ │
│"quality" │ │File size │
│in feedback│ │Metadata │
└─────┬───┘ └─────┬───┘
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│Switch to │ │Simple │
│RLHF │ │Retry │
│Mode │ │ │
│ │ │Use same │
│use_rlhf= │ │config │
│true │ │ │
└─────┬───┘ └─────┬───┘
│ │
└─────┬─────┘
│
▼
┌─────────────────┐
│ Attempt 2 │
│ Generate │
│ (Improved) │
│ → Validate │
│ Score: 0.8 │ ✅ SUCCESS (≥ 0.6)
└─────────────────┘ |