Spaces:
Running
Running
File size: 113,308 Bytes
9df4cc0 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 20,
"id": "f4ff218d-f4eb-4e4b-9f4c-205351349fd2",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import re\n",
"import os\n",
"import random\n",
"import datasets\n",
"import pandas as pd\n",
"from glob import glob\n",
"from tqdm.notebook import tqdm\n",
"from datasets import load_dataset, load_from_disk, Dataset, DatasetDict\n",
"from transformers import AutoTokenizer, AutoConfig"
]
},
{
"cell_type": "markdown",
"id": "dc6368fa-4fcd-4336-a548-f37bda3a5d2f",
"metadata": {
"tags": []
},
"source": [
"# Sentiment Datasets (following FinGPT v3)"
]
},
{
"cell_type": "markdown",
"id": "90c1b2b0-550c-47a7-8486-c7f5601bc0a1",
"metadata": {},
"source": [
"### 1. FPB"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0d79a0e4-f8f8-4e20-811e-42256b0362df",
"metadata": {},
"outputs": [],
"source": [
"dic = {\n",
" 0:\"negative\",\n",
" 1:'neutral',\n",
" 2:'positive',\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "85a3d5c8-6a06-40e5-8ae7-06d1fd93f479",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 3634\n",
"})"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# fpb_datasets = load_dataset(\"financial_phrasebank\", \"sentences_50agree\")\n",
"fpb_datasets = load_from_disk('../data/financial_phrasebank-sentences_50agree/')\n",
"fpb_datasets = fpb_datasets[\"train\"]\n",
"fpb_datasets = fpb_datasets.to_pandas()\n",
"fpb_datasets.columns = [\"input\", \"output\"]\n",
"fpb_datasets[\"output\"] = fpb_datasets[\"output\"].apply(lambda x:dic[x])\n",
"fpb_datasets[\"instruction\"] = \"What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}.\"\n",
"fpb_datasets = datasets.Dataset.from_pandas(fpb_datasets)\n",
"fpb_datasets = fpb_datasets.train_test_split(seed=42)['train']\n",
"fpb_datasets"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8d8d28e4-f380-423b-8a1b-51bfcb0b7004",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 21804\n",
"})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_dataset = datasets.concatenate_datasets([fpb_datasets]*6) # we want each data source have similar number of samples\n",
"train_dataset"
]
},
{
"cell_type": "markdown",
"id": "9097f1f3-598c-47d3-8b96-a08254da4c4e",
"metadata": {},
"source": [
"### 2. FiQA SA"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "55d618cc-9e05-4053-b354-e7b159c6906b",
"metadata": {},
"outputs": [],
"source": [
"def make_label(x):\n",
" if x < - 0.1:\n",
" return \"negative\"\n",
" elif -0.1 <= x < 0.1:\n",
" return \"neutral\"\n",
" else:\n",
" return \"positive\"\n",
"\n",
"def add_instructions(x):\n",
" if x == \"post\":\n",
" return \"What is the sentiment of this tweet? Please choose an answer from {negative/neutral/positive}.\"\n",
" else:\n",
" return \"What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}.\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4b81fb40-1275-4205-9d42-dbfc425dd4a4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 938\n",
"})"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# dataset = load_dataset('pauri32/fiqa-2018')\n",
"dataset = load_from_disk('../data/fiqa-2018/')\n",
"dataset = datasets.concatenate_datasets([dataset[\"train\"], dataset[\"validation\"] ,dataset[\"test\"] ])\n",
"dataset = dataset.to_pandas()\n",
"dataset[\"output\"] = dataset.sentiment_score.apply(make_label)\n",
"dataset[\"instruction\"] = dataset.format.apply(add_instructions)\n",
"dataset = dataset[['sentence', 'output', \"instruction\"]]\n",
"dataset.columns = [\"input\", \"output\", \"instruction\"]\n",
"dataset = datasets.Dataset.from_pandas(dataset)\n",
"dataset = dataset.train_test_split(0.226, seed=42)['train']\n",
"dataset"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "80349c7a-3c0c-4c99-b2c3-f1b2dd377e64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"19698\n"
]
},
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 41502\n",
"})"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp_dataset = datasets.concatenate_datasets([dataset]*21)\n",
"train_dataset = datasets.concatenate_datasets([train_dataset, tmp_dataset]) \n",
"print(tmp_dataset.num_rows)\n",
"train_dataset"
]
},
{
"cell_type": "markdown",
"id": "a4887301-4f6a-49b0-9e42-aa5bdcd19856",
"metadata": {},
"source": [
"### 3. TFNS"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e707ffbf-7f92-4b45-8570-a6d63bc64bc7",
"metadata": {},
"outputs": [],
"source": [
"dic = {\n",
" 0:\"negative\",\n",
" 1:'positive',\n",
" 2:'neutral',\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "41388d35-c6ae-4da8-b92d-13fb0b1db21e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 9543\n",
"})"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# social_media_dataset = load_dataset('zeroshot/twitter-financial-news-sentiment')\n",
"social_media_dataset = load_from_disk('../data/twitter-financial-news-sentiment')\n",
"social_media_dataset = social_media_dataset['train']\n",
"social_media_dataset = social_media_dataset.to_pandas()\n",
"social_media_dataset['label'] = social_media_dataset['label'].apply(lambda x:dic[x])\n",
"social_media_dataset['instruction'] = 'What is the sentiment of this tweet? Please choose an answer from {negative/neutral/positive}.'\n",
"social_media_dataset.columns = ['input', 'output', 'instruction']\n",
"social_media_dataset = datasets.Dataset.from_pandas(social_media_dataset)\n",
"social_media_dataset"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9f1ef9b7-af0c-407b-b270-7c4b2180036c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"19086\n"
]
},
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 60588\n",
"})"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp_dataset = datasets.concatenate_datasets([social_media_dataset]*2)\n",
"train_dataset = datasets.concatenate_datasets([train_dataset,tmp_dataset]) \n",
"print(tmp_dataset.num_rows)\n",
"train_dataset"
]
},
{
"cell_type": "markdown",
"id": "aee85f1d-7c0e-4642-adbd-f145adb23ef1",
"metadata": {},
"source": [
"### 4. NWGI"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "642fcaf0-b909-4562-a913-877557d829bb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 16184\n",
"})"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# finance_dataset = load_dataset('oliverwang15/news_with_gpt_instructions')\n",
"finance_dataset = load_from_disk('../data/news_with_gpt_instructions/')\n",
"finance_dataset = finance_dataset['train'].to_pandas()\n",
"finance_dataset['output'] = finance_dataset['label']\n",
"finance_dataset[\"input\"] = finance_dataset[\"news\"]\n",
"finance_dataset[\"instruction\"] = 'What is the sentiment of this news? Please choose an answer from {strong negative/moderately negative/mildly negative/neutral/mildly positive/moderately positive/strong positive}.'\n",
"finance_dataset = finance_dataset[['input', 'output', 'instruction']]\n",
"finance_dataset = datasets.Dataset.from_pandas(finance_dataset)\n",
"finance_dataset"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "98e24280-119d-4748-8c1d-709d7d3cf50d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(76772, 3)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_dataset = datasets.concatenate_datasets([train_dataset, finance_dataset])\n",
"all_dataset = train_dataset.shuffle(seed=42)\n",
"all_dataset.shape"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a8f4d2a8-c0ac-49ce-8933-cce0de14f99f",
"metadata": {},
"outputs": [],
"source": [
"# from huggingface_hub import notebook_login\n",
"# notebook_login()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "4509a524-d694-4bcf-813c-4a28551bed39",
"metadata": {},
"outputs": [],
"source": [
"# all_dataset.push_to_hub(\"fingpt_chatglm2_sentiment_instruction_lora_ft_dataset\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "b7774a58-e4e6-49ac-81fb-ef72dc46446e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"76772"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"21804 + 19698 + 19086 + 16184"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1c740a64-d41d-43de-b20b-7826f01e9421",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004731893539428711,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 76772,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "24c8597611564c9a8f2aa8055b126bf7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/76772 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"all_dataset.save_to_disk('fingpt-sentiment-train')"
]
},
{
"cell_type": "markdown",
"id": "26ff88aa-2c9e-4627-8e5f-4517809bc192",
"metadata": {},
"source": [
"## CLS Ver. For Zero-shot Assessment"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "363744e9-cecb-45b8-b2ea-df22ff786e18",
"metadata": {},
"outputs": [],
"source": [
"chatgpt_templates = [\n",
" \"What is the sentiment of the input {type} from financial perspective?\",\n",
" \"Assign a sentiment category to this {type} related to finance.\",\n",
" \"Categorize the input {type}'s emotional tone into one of three groups.\",\n",
" \"Determine the sentiment expressed in the {type} from financial perspective.\",\n",
" \"Characterize the {type}'s sentiment using the following options.\",\n",
"]\n",
"\n",
"with open('../benchmarks/sentiment_templates.txt', 'w') as f:\n",
" f.writelines([l + '\\n' for l in chatgpt_templates])\n",
"\n",
"\n",
"def option_list(ops_str):\n",
" options = ops_str.split('/')\n",
" random.shuffle(options)\n",
" return \", \".join(options)\n",
"\n",
"\n",
"def map_func(feature):\n",
" t = re.search(r'tweet|news', feature['instruction']).group()\n",
" ops = option_list(\"negative/positive\")\n",
" if 'positive' in feature['output']:\n",
" output = 'positive'\n",
" elif 'negative' in feature['output']:\n",
" output = 'negative' \n",
" else:\n",
" output = 'neutral'\n",
" return {\"instruction\": random.choice(chatgpt_templates).format(type=t) + \"\\nOptions: \" + ops, \"output\": output}\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "aca0c743-68b1-4d4d-b75b-5e51d36c2318",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004678487777709961,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Map",
"rate": null,
"total": 76772,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "2798b09bd1ff4167bd432f92d26503f0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/76772 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004431486129760742,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Filter",
"rate": null,
"total": 76772,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "c41f028e5dd24a1ca7eaa920f3a480ab",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Filter: 0%| | 0/76772 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 47557\n",
"})"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.seed(0)\n",
"all_dataset_instruct = all_dataset.map(map_func)\n",
"all_dataset_instruct = all_dataset_instruct.filter(lambda x: x['output'] != 'neutral')\n",
"all_dataset_instruct"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "39a9f66a-e9e4-4392-8e7e-1f3348dc0be3",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004517316818237305,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 47557,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "651b706e66bd43b2a2184b663ec0aa21",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/47557 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"['Determine the sentiment expressed in the news from financial perspective.\\nOptions: negative, positive',\n",
" 'Determine the sentiment expressed in the tweet from financial perspective.\\nOptions: negative, positive',\n",
" \"Characterize the news's sentiment using the following options.\\nOptions: negative, positive\",\n",
" \"Characterize the news's sentiment using the following options.\\nOptions: negative, positive\",\n",
" 'What is the sentiment of the input tweet from financial perspective?\\nOptions: positive, negative',\n",
" 'Determine the sentiment expressed in the tweet from financial perspective.\\nOptions: negative, positive',\n",
" \"Categorize the input tweet's emotional tone into one of three groups.\\nOptions: negative, positive\",\n",
" \"Characterize the news's sentiment using the following options.\\nOptions: positive, negative\",\n",
" 'Determine the sentiment expressed in the news from financial perspective.\\nOptions: negative, positive',\n",
" 'What is the sentiment of the input tweet from financial perspective?\\nOptions: negative, positive']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_dataset_instruct.save_to_disk('fingpt-sentiment-cls-instruct')\n",
"all_dataset_instruct['instruction'][:10]"
]
},
{
"cell_type": "markdown",
"id": "a5a9357d-7b48-46f8-9ebe-cfcb36dec1d6",
"metadata": {},
"source": [
"# Headline"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "2b71a977-3ed7-459d-97bf-878d086da1f8",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Dates</th>\n",
" <th>URL</th>\n",
" <th>News</th>\n",
" <th>Price or Not</th>\n",
" <th>Price Direction Up</th>\n",
" <th>Price Direction Constant</th>\n",
" <th>Price Direction Down</th>\n",
" <th>PastPrice</th>\n",
" <th>FuturePrice</th>\n",
" <th>PastNews</th>\n",
" <th>FutureNews</th>\n",
" <th>Asset Comparision</th>\n",
" <th>Price Sentiment</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>28-01-2016</td>\n",
" <td>http://www.marketwatch.com/story/april-gold-do...</td>\n",
" <td>april gold down 20 cents to settle at $1,116.1...</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>negative</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>13-09-2017</td>\n",
" <td>http://www.marketwatch.com/story/gold-prices-s...</td>\n",
" <td>gold suffers third straight daily decline</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>negative</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>26-07-2016</td>\n",
" <td>http://www.marketwatch.com/story/gold-futures-...</td>\n",
" <td>Gold futures edge up after two-session decline</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>positive</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>28-02-2018</td>\n",
" <td>https://www.metalsdaily.com/link/277199/dent-r...</td>\n",
" <td>dent research : is gold's day in the sun comin...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>none</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>06-09-2017</td>\n",
" <td>http://www.marketwatch.com/story/gold-steadies...</td>\n",
" <td>Gold snaps three-day rally as Trump, lawmakers...</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>positive</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Dates URL \\\n",
"0 28-01-2016 http://www.marketwatch.com/story/april-gold-do... \n",
"1 13-09-2017 http://www.marketwatch.com/story/gold-prices-s... \n",
"2 26-07-2016 http://www.marketwatch.com/story/gold-futures-... \n",
"3 28-02-2018 https://www.metalsdaily.com/link/277199/dent-r... \n",
"4 06-09-2017 http://www.marketwatch.com/story/gold-steadies... \n",
"\n",
" News Price or Not \\\n",
"0 april gold down 20 cents to settle at $1,116.1... 1 \n",
"1 gold suffers third straight daily decline 1 \n",
"2 Gold futures edge up after two-session decline 1 \n",
"3 dent research : is gold's day in the sun comin... 0 \n",
"4 Gold snaps three-day rally as Trump, lawmakers... 1 \n",
"\n",
" Price Direction Up Price Direction Constant Price Direction Down \\\n",
"0 0 0 1 \n",
"1 0 0 1 \n",
"2 1 0 0 \n",
"3 0 0 0 \n",
"4 1 0 0 \n",
"\n",
" PastPrice FuturePrice PastNews FutureNews Asset Comparision \\\n",
"0 1 0 0 0 0 \n",
"1 1 0 0 0 0 \n",
"2 1 0 0 0 0 \n",
"3 0 0 0 1 0 \n",
"4 1 0 0 0 0 \n",
"\n",
" Price Sentiment \n",
"0 negative \n",
"1 negative \n",
"2 positive \n",
"3 none \n",
"4 positive "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('gold-dataset-sinha-khandait.csv')\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "2e848299-c202-4163-b46a-336af9b98495",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9129\n",
"2283\n"
]
}
],
"source": [
"train_dataset, test_dataset = {}, {}\n",
"inputs, outputs, instructions = [], [], []\n",
"for index, row in df.iterrows():\n",
" \n",
" if index + 1 >= len(df) * 0.8 and not train_dataset:\n",
" train_dataset['input'] = inputs\n",
" train_dataset['output'] = outputs\n",
" train_dataset['instruction'] = instructions\n",
" inputs, outputs, instructions = [], [], []\n",
" \n",
" inputs.extend([row['News']] * 9)\n",
" # price or not\n",
" instructions.append('Does the news headline talk about price? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['Price Direction Constant'] else 'No')\n",
" # price up\n",
" instructions.append('Does the news headline talk about price going up? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['Price Direction Up'] else 'No')\n",
" # price stable\n",
" instructions.append('Does the news headline talk about price staying constant? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['Price Direction Constant'] else 'No')\n",
" # price down\n",
" instructions.append('Does the news headline talk about price going down? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['Price Direction Down'] else 'No')\n",
" # past price\n",
" instructions.append('Does the news headline talk about price in the past? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['PastPrice'] else 'No')\n",
" # future price\n",
" instructions.append('Does the news headline talk about price in the future? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['FuturePrice'] else 'No')\n",
" # past general\n",
" instructions.append('Does the news headline talk about a general event (apart from prices) in the past? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['PastNews'] else 'No')\n",
" # future general\n",
" instructions.append('Does the news headline talk about a general event (apart from prices) in the future? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['FutureNews'] else 'No')\n",
" # asset comparison\n",
" instructions.append('Does the news headline compare gold with any other asset? Please choose an answer from {Yes/No}.')\n",
" outputs.append('Yes' if row['Asset Comparision'] else 'No')\n",
" \n",
"test_dataset['input'] = inputs\n",
"test_dataset['output'] = outputs\n",
"test_dataset['instruction'] = instructions\n",
"\n",
"print(len(train_dataset['input']) // 9)\n",
"print(len(test_dataset['input']) // 9)\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "6ae82176-fd34-43b9-a3ad-7c85bf35ee08",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004788875579833984,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 82161,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "670ca6e61e0141cfbe00f348caa2bdf5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/82161 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004403829574584961,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 20547,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "33fd0bb8a0d4403eb67aa4c002a5ffc3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/20547 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 82161\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 20547\n",
" })\n",
"})"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"headline_dataset = DatasetDict({\n",
" 'train': Dataset.from_dict(train_dataset),\n",
" 'test': Dataset.from_dict(test_dataset)\n",
"})\n",
"headline_dataset.save_to_disk('fingpt-headline')\n",
"headline_dataset"
]
},
{
"cell_type": "markdown",
"id": "64d9cfc3-02d6-4e46-8dfb-93d43b1bc8d5",
"metadata": {},
"source": [
"## CLS Ver. for Zero-shot Training"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "b72efdd8-e920-416d-9a25-8aa0c65769e5",
"metadata": {},
"outputs": [],
"source": [
"chatgpt_templates = [\n",
" \"Does the news headline talk about {subject}? \",\n",
" \"Please determine if the news headline addresses {subject}.\",\n",
" \"In the context of the news headline, is {subject} discussed?\",\n",
" \"Is the news headline related to {subject}?\",\n",
" \"Let me know if the news headline talks about {subject}.\",\n",
" \"Consider the news headline - does it concern {subject}?\",\n",
" \"Examine the news headline and decide if it includes {subject}.\",\n",
" \"Assess if the news headline touches on {subject}.\",\n",
" \"Review the news headline and determine if it relates to {subject}.\",\n",
" \"Analyze the news headline for any mention of {subject}.\",\n",
" \"Interpret the news headline to see if it mentions {subject}.\"\n",
"]\n",
"\n",
"def yes_no():\n",
" options = ['Yes', 'No']\n",
" random.shuffle(options)\n",
" return \", \".join(options)\n",
"\n",
"def map_func(feature):\n",
" match_res = re.search(r'talk about (.*)\\? Please', feature['instruction'])\n",
" subject = 'comparing gold with any other asset' if match_res is None else match_res.group(1)\n",
" return {\"instruction\": random.choice(chatgpt_templates).format(subject=subject) + \"\\nOptions: \" + yes_no()}\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "50c93371-343c-458f-8500-89e418c02969",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004721164703369141,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Map",
"rate": null,
"total": 82161,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "a421304eaaad44dfac5e9e3da95a9791",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/82161 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.0043904781341552734,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Map",
"rate": null,
"total": 20547,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "adab61cca62342e487bf14b46f017214",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/20547 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004418373107910156,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 82161,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "2ff2661ab7294006861eb7f827297b7e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/82161 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004082918167114258,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 20547,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "11a9114fba2f4d3e85c1a4034e8e755c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/20547 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"random.seed(0)\n",
"headline_dataset_instruct = headline_dataset.map(map_func)\n",
"headline_dataset_instruct.save_to_disk('fingpt-headline-cls-instruct')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "00916647-6764-464e-b0f3-2299675da1f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Examine the news headline and decide if it includes price.\\nOptions: Yes, No',\n",
" 'Does the news headline talk about price going up? \\nOptions: Yes, No',\n",
" 'Review the news headline and determine if it relates to price staying constant.\\nOptions: Yes, No',\n",
" 'Examine the news headline and decide if it includes price going down.\\nOptions: Yes, No',\n",
" 'Assess if the news headline touches on price in the past.\\nOptions: Yes, No',\n",
" 'Analyze the news headline for any mention of price in the future.\\nOptions: No, Yes',\n",
" 'Review the news headline and determine if it relates to a general event (apart from prices) in the past.\\nOptions: No, Yes',\n",
" 'Let me know if the news headline talks about a general event (apart from prices) in the future.\\nOptions: No, Yes',\n",
" 'Please determine if the news headline addresses comparing gold with any other asset.\\nOptions: Yes, No',\n",
" 'Review the news headline and determine if it relates to price.\\nOptions: No, Yes']"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"headline_dataset_instruct['train']['instruction'][:10]"
]
},
{
"cell_type": "markdown",
"id": "add5ca03-de6f-47be-9ebe-9c63e194b120",
"metadata": {
"tags": []
},
"source": [
"# NER"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "95459177-9eff-40e3-946d-035043e22650",
"metadata": {},
"outputs": [],
"source": [
"# Read and parse the CoNLL-2003 formatted dataset\n",
"\n",
"ent_dict = {\n",
" 'PER': 'person',\n",
" 'ORG': 'organization',\n",
" 'LOC': 'location',\n",
"}\n",
"\n",
"def read_conll_file(file_path):\n",
" sentences, tokens, labels = [], [], []\n",
" with open(file_path, 'r') as f:\n",
" sentence = []\n",
" for line in f:\n",
" if line.strip() == '':\n",
" if sentence:\n",
" sentences.append(sentence)\n",
" sentence = []\n",
" else:\n",
" parts = line.strip().split()\n",
" token, label = parts[0], parts[-1]\n",
" tokens.append(token)\n",
" labels.append(label)\n",
" sentence.append((token, label))\n",
" \n",
" return sentences\n",
" \n",
"\n",
"def get_ner_dataset(sentences):\n",
" \n",
" inputs, outputs, instructions = [], [], []\n",
" count = {'person': 0, 'organization': 0, 'location': 0}\n",
" for sentence in sentences:\n",
" is_entity = [tup[1] != 'O' and not tup[1].endswith('MISC') for tup in sentence]\n",
" if sum(is_entity) == 0:\n",
" continue\n",
" instructions.append('Please extract entities and their types from the input sentence, entity types should be chosen from {person/organization/location}.')\n",
" inputs.append(' '.join([tup[0] for tup in sentence]))\n",
" outputs.append('')\n",
" tmp_tup_list = []\n",
" for i, tup in enumerate(sentence):\n",
" if tmp_tup_list and (not is_entity[i] or tmp_tup_list[-1][1] != tup[1] or i + 1 == len(sentence)):\n",
" entity = ' '.join([t[0] for t in tmp_tup_list])\n",
" assert tmp_tup_list[0][1] == tmp_tup_list[-1][1], tmp_tup_list\n",
" entity_type = ent_dict[tmp_tup_list[-1][1].split('-')[-1]]\n",
" a = 'an' if entity_type == 'organization' else 'a'\n",
" outputs[-1] += f'{entity} is {a} {entity_type}, ' \n",
" tmp_tup_list = [] if not is_entity[i] else [tup]\n",
" count[entity_type] += 1\n",
" elif is_entity[i]:\n",
" tmp_tup_list.append(tup)\n",
" else:\n",
" pass\n",
" outputs[-1] = outputs[-1].strip(', ') + '.'\n",
" \n",
" print(len(instructions))\n",
" print(count)\n",
" \n",
" return {\"input\": inputs, \"output\": outputs, \"instruction\": instructions}"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "9aeec446-04ce-44d5-abea-38b65aece991",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"511\n",
"{'person': 745, 'organization': 243, 'location': 168}\n",
"98\n",
"{'person': 216, 'organization': 56, 'location': 39}\n"
]
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.00464177131652832,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 511,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "6de323eafdf649b8af9b322dc33d0fcb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/511 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004297494888305664,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 98,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "834f28cf02724a729c9421e7b29349c5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/98 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 511\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 98\n",
" })\n",
"})"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_data = read_conll_file('./SEC-filings/CONLL-format/data/train/FIN5.txt')\n",
"test_data = read_conll_file('./SEC-filings/CONLL-format/data/test/FIN3.txt')\n",
"\n",
"train_data = get_ner_dataset(train_data)\n",
"test_data = get_ner_dataset(test_data)\n",
"\n",
"ner_dataset = DatasetDict({\n",
" 'train': Dataset.from_dict(train_data),\n",
" 'test': Dataset.from_dict(test_data)\n",
"})\n",
"ner_dataset.save_to_disk('fingpt-ner')\n",
"ner_dataset"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "32c6d6c2-3d15-4382-bc0a-5c1d5b17fc00",
"metadata": {},
"outputs": [],
"source": [
"# def get_ner_dataset_1(sentences):\n",
" \n",
"# inputs, outputs, instructions = [], [], []\n",
"# count = {'person': 0, 'organization': 0, 'location': 0}\n",
"# for sentence in sentences:\n",
"# is_entity = [tup[1] != 'O' and not tup[1].endswith('MISC') for tup in sentence]\n",
"# if sum(is_entity) == 0:\n",
"# continue\n",
"# instructions.append('Please list all entities in the input text that fit the following entity types: \"person\", \"organization\", \"location\". Output format is \"type1: entity1; type2: entity2\"')\n",
"# instructions.append('Given options of entity types, please find all the entities associated with them in the input text. answer with format \"entity1: type1; entity2: type2\".\\nOptions: \"person\", \"organization\", \"location\".')\n",
"# inputs.append(' '.join([tup[0] for tup in sentence]))\n",
"# inputs.append(' '.join([tup[0] for tup in sentence]))\n",
"# outputs.append('')\n",
"# outputs.append('')\n",
"# tmp_tup_list = []\n",
"# for i, tup in enumerate(sentence):\n",
"# if tmp_tup_list and (not is_entity[i] or tmp_tup_list[-1][1] != tup[1] or i + 1 == len(sentence)):\n",
"# entity = ' '.join([t[0] for t in tmp_tup_list])\n",
"# assert tmp_tup_list[0][1] == tmp_tup_list[-1][1], tmp_tup_list\n",
"# entity_type = ent_dict[tmp_tup_list[-1][1].split('-')[-1]]\n",
"# outputs[-2] += f'{entity_type}: {entity}; ' \n",
"# outputs[-1] += f'{entity}: {entity_type}; ' \n",
"# tmp_tup_list = [] if not is_entity[i] else [tup]\n",
"# count[entity_type] += 1\n",
"# elif is_entity[i]:\n",
"# tmp_tup_list.append(tup)\n",
"# else:\n",
"# pass\n",
"# outputs[-1] = outputs[-1].strip('; ')\n",
"# outputs[-2] = outputs[-2].strip('; ')\n",
" \n",
"# print(len(instructions))\n",
"# print(count)\n",
" \n",
"# return {\"input\": inputs, \"output\": outputs, \"instruction\": instructions}\n",
"\n",
"\n",
"# def get_ner_dataset_2(sentences):\n",
" \n",
"# inputs, outputs, instructions = [], [], []\n",
"# count = {'person': 0, 'organization': 0, 'location': 0}\n",
"# templates = [\n",
"# 'Does the input text include any entity of type \"{entity_type}\" ? If so, list them all, and answer with format \"entity1; entity2\". If not, Answer \"No\".',\n",
"# 'Please tell me all the entities in the text that belong to the given category \"{entity_type}\". Output format is \"entity1; entity2\". If no such entity can be found, answer \"No\".',\n",
"# ]\n",
"# for sentence in sentences:\n",
"# is_entity = [tup[1] != 'O' and not tup[1].endswith('MISC') for tup in sentence]\n",
"# if sum(is_entity) == 0:\n",
"# continue\n",
"# for template in templates:\n",
"# for tgt_entity_type in ['person', 'location', 'organization']:\n",
"# instructions.append(template.format(entity_type=tgt_entity_type))\n",
"# inputs.append(' '.join([tup[0] for tup in sentence]))\n",
"# outputs.append('')\n",
"# tmp_tup_list = []\n",
"# for i, tup in enumerate(sentence):\n",
"# if tmp_tup_list and (not is_entity[i] or tmp_tup_list[-1][1] != tup[1] or i + 1 == len(sentence)):\n",
"# entity = ' '.join([t[0] for t in tmp_tup_list])\n",
"# assert tmp_tup_list[0][1] == tmp_tup_list[-1][1], tmp_tup_list\n",
"# entity_type = ent_dict[tmp_tup_list[-1][1].split('-')[-1]]\n",
"# tmp_tup_list = [] if not is_entity[i] else [tup]\n",
"# if entity_type == tgt_entity_type:\n",
"# outputs[-1] += f'{entity}; ' \n",
"# count[entity_type] += 1\n",
"# elif is_entity[i]:\n",
"# tmp_tup_list.append(tup)\n",
"# else:\n",
"# pass\n",
"# outputs[-1] = 'No' if not outputs[-1] else outputs[-1].strip('; ')\n",
" \n",
"# print(len(instructions))\n",
"# print(count)\n",
" \n",
"# return {\"input\": inputs, \"output\": outputs, \"instruction\": instructions}\n",
"\n",
"\n",
"# def get_ner_dataset_3(sentences):\n",
" \n",
"# inputs, outputs, instructions = [], [], []\n",
"# count = {'person': 0, 'organization': 0, 'location': 0}\n",
"# for sentence in sentences:\n",
"# is_entity = [tup[1] != 'O' and not tup[1].endswith('MISC') for tup in sentence]\n",
"# if sum(is_entity) == 0:\n",
"# continue\n",
"# prev_count = count.copy()\n",
"# instructions.append('Please find all entities in the input text that fit the following entity types: \"person\", \"organization\", \"location\", then answer with the number counted for each type. Output format is \"type1: number1; type2: number2\"')\n",
"# instructions.append('Given entity types as options, please find the number of occurrence for each entity type in the input text. answer with format \"type1: number1; type2: number2\".\\nOptions: \"person\", \"organization\", \"location\".')\n",
"# inputs.extend([' '.join([tup[0] for tup in sentence])] * 2)\n",
"# tmp_tup_list = []\n",
"# for i, tup in enumerate(sentence):\n",
"# if tmp_tup_list and (not is_entity[i] or tmp_tup_list[-1][1] != tup[1] or i + 1 == len(sentence)):\n",
"# entity = ' '.join([t[0] for t in tmp_tup_list])\n",
"# assert tmp_tup_list[0][1] == tmp_tup_list[-1][1], tmp_tup_list\n",
"# entity_type = ent_dict[tmp_tup_list[-1][1].split('-')[-1]]\n",
"# tmp_tup_list = [] if not is_entity[i] else [tup]\n",
"# count[entity_type] += 1\n",
"# elif is_entity[i]:\n",
"# tmp_tup_list.append(tup)\n",
"# else:\n",
"# pass\n",
" \n",
"# per_cnt = count['person'] - prev_count['person']\n",
"# loc_cnt = count['location'] - prev_count['location']\n",
"# org_cnt = count['organization'] - prev_count['organization']\n",
"# output_str = f'person: {per_cnt}; location: {loc_cnt}; organization: {org_cnt}'\n",
"# outputs.extend([output_str] * 2)\n",
" \n",
"# print(len(instructions))\n",
"# print(count)\n",
" \n",
"# return {\"input\": inputs, \"output\": outputs, \"instruction\": instructions}\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "b00bbd11-7867-4a8e-8d9c-877d2b827160",
"metadata": {},
"outputs": [],
"source": [
"# train_data = read_conll_file('./SEC-filings/CONLL-format/data/train/FIN5.txt')\n",
"# test_data = read_conll_file('./SEC-filings/CONLL-format/data/test/FIN3.txt')\n",
"\n",
"# train_data_0 = get_ner_dataset(train_data)\n",
"# train_data_1 = get_ner_dataset_1(train_data)\n",
"# train_data_2 = get_ner_dataset_2(train_data)\n",
"# train_data_3 = get_ner_dataset_3(train_data)\n",
"\n",
"# test_data_0 = get_ner_dataset(test_data)\n",
"# test_data_1 = get_ner_dataset_1(test_data)\n",
"# test_data_2 = get_ner_dataset_2(test_data)\n",
"# test_data_3 = get_ner_dataset_3(test_data)\n",
"\n",
"# train_data = {k: train_data_0[k] + train_data_1[k] + train_data_2[k] + train_data_3[k] for k in train_data_0.keys()} \n",
"# test_data = {k: test_data_0[k] + test_data_1[k] + test_data_2[k] + test_data_3[k] for k in test_data_0.keys()} \n",
"# ner_dataset = DatasetDict({\n",
"# 'train': Dataset.from_dict(train_data),\n",
"# 'test': Dataset.from_dict(test_data)\n",
"# })\n",
"# ner_dataset.save_to_disk('fingpt-ner-full')\n",
"# ner_dataset"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "b4559b61-c3eb-4a72-8a5c-aaa7bfd9d209",
"metadata": {},
"outputs": [],
"source": [
"# chatgpt_templates = {\n",
"# 'Please list all entities in the input text that fit the following entity types: \"person\", \"organization\", \"location\". Output format is \"type1: entity1; type2: entity2\"':[\n",
"# \"Identify and compile all entities of types 'person,' 'organization,' and 'location' from the input text. Format the output as 'type1: entity1; type2: entity2.'\",\n",
"# \"Gather all entities falling under the categories 'person,' 'organization,' and 'location' within the input text. Please use the 'type1: entity1; type2: entity2' format for the output.\",\n",
"# \"Extract entities categorized as 'person,' 'organization,' or 'location' from the input text. Organize the results in the format 'type1: entity1; type2: entity2.'\",\n",
"# \"Compile a list of entities matching 'person,' 'organization,' and 'location' entity types from the input text. Ensure the output format remains 'type1: entity1; type2: entity2.'\",\n",
"# \"Detect and list all entities that fall under the types 'person,' 'organization,' and 'location' in the input text. Format the output as 'type1: entity1; type2: entity2.'\",\n",
"# \"Enumerate entities of types 'person,' 'organization,' and 'location' from the input text. Use the output format 'type1: entity1; type2: entity2.'\",\n",
"# \"Identify and present entities categorized as 'person,' 'organization,' and 'location' from the input text. Keep the output in the format 'type1: entity1; type2: entity2.'\",\n",
"# \"Spot and document entities falling into the 'person,' 'organization,' and 'location' types within the input text. Keep the 'type1: entity1; type2: entity2' output format.\",\n",
"# \"Extract entities matching 'person,' 'organization,' and 'location' types from the input text. Ensure the output follows the 'type1: entity1; type2: entity2' format.\",\n",
"# \"Enumerate all entities that correspond to 'person,' 'organization,' and 'location' entity types within the input text. Format the output as 'type1: entity1; type2: entity2.'\"\n",
"# ],\n",
"# 'Given options of entity types, please find all the entities associated with them in the input text. answer with format \"entity1: type1; entity2: type2\".\\nOptions: \"person\", \"organization\", \"location\".': [\n",
"# \"Using the provided options of entity types, locate all corresponding entities within the input text. Present the results in the format 'entity1: type1; entity2: type2.'\",\n",
"# \"Find entities related to the given options of entity types within the input text. Respond with the format 'entity1: type1; entity2: type2.'\",\n",
"# \"Identify entities associated with the provided entity type options in the input text. Format your response as 'entity1: type1; entity2: type2.'\",\n",
"# \"Discover entities linked to the entity types listed in the options within the input text. Provide the output in the format 'entity1: type1; entity2: type2.'\",\n",
"# \"Locate all entities corresponding to the provided entity type options in the input text. Present your findings as 'entity1: type1; entity2: type2.'\",\n",
"# \"Search for entities related to the entity types listed in the options within the input text. Share the results in the 'entity1: type1; entity2: type2' format.\",\n",
"# \"Identify entities associated with the given entity type options in the input text. Use the format 'entity1: type1; entity2: type2' for your response.\",\n",
"# \"Retrieve entities connected to the entity types mentioned in the options from the input text. Format your answer as 'entity1: type1; entity2: type2.'\",\n",
"# \"Examine the input text for entities that match the provided entity type options. Respond using the 'entity1: type1; entity2: type2' format.\",\n",
"# \"Scour the input text for entities corresponding to the entity types in the options. Present the results in the 'entity1: type1; entity2: type2' format.\",\n",
"# ],\n",
"# 'Does the input text include any entity of type \"{entity_type}\" ? If so, list them all, and answer with format \"entity1; entity2\". If not, Answer \"No\".': [\n",
"# \"Check if the input text contains any entities of type '{entity_type}'. If found, list them all in the format 'entity1; entity2.' Otherwise, respond with 'No.'\",\n",
"# \"Examine the input text for the presence of entities belonging to the '{entity_type}' type. If any are found, provide a list in the format 'entity1; entity2.' If none are found, answer 'No.'\",\n",
"# \"Verify whether the input text contains entities of the type '{entity_type}.' If so, enumerate them in the format 'entity1; entity2.' If not, reply with 'No.'\",\n",
"# \"Determine if the input text includes any entities categorized as '{entity_type}.' If it does, present them in the format 'entity1; entity2.' In case of none, respond with 'No.'\",\n",
"# \"Assess whether the input text encompasses entities of the '{entity_type}' type. If there are any, list them as 'entity1; entity2.' If there are none, respond with 'No.'\",\n",
"# \"Inspect the input text to find out if it contains any entities labeled as '{entity_type}.' If it does, provide a list in the format 'entity1; entity2.' If not, reply with 'No.'\",\n",
"# \"Ascertain whether the input text possesses entities falling under the '{entity_type}' category. If it does, compile them in the format 'entity1; entity2.' If it doesn't, answer 'No.'\",\n",
"# \"Confirm whether the input text harbors entities of type '{entity_type}.' If it does, list them all as 'entity1; entity2.' If it doesn't, respond with 'No.'\",\n",
"# \"Investigate whether the input text features any entities of the '{entity_type}' variety. If any are present, document them in the format 'entity1; entity2.' Otherwise, answer 'No.'\",\n",
"# \"Scrutinize the input text to determine the presence of entities belonging to the '{entity_type}' category. If any are found, report them as 'entity1; entity2.' If none are found, respond with 'No.'\"\n",
"# ],\n",
"# 'Please tell me all the entities in the text that belong to the given category \"{entity_type}\". Output format is \"entity1; entity2\". If no such entity can be found, answer \"No\".': [\n",
"# \"Identify and provide all entities within the text that fall under the specified category '{entity_type}'. Format the output as 'entity1; entity2.' If no such entity exists, respond with 'No.'\",\n",
"# \"Locate and list all entities in the text that are categorized as '{entity_type}'. Use the output format 'entity1; entity2.' If there are no entities of this type, answer 'No.'\",\n",
"# \"Find and present all entities within the text that belong to the designated category '{entity_type}'. Ensure the output maintains the format 'entity1; entity2.' If there are none, reply with 'No.'\",\n",
"# \"Discover and enumerate all entities in the text that are classified as '{entity_type}'. Format the output as 'entity1; entity2.' In the absence of such entities, respond with 'No.'\",\n",
"# \"Search for and compile all entities in the text that pertain to the specified category '{entity_type}'. Use the 'entity1; entity2' format for the output. If none are found, answer 'No.'\",\n",
"# \"Identify and gather all entities within the text that fit the given category '{entity_type}'. Present the results in the format 'entity1; entity2.' If there are no entities of this type, reply with 'No.'\",\n",
"# \"Detect and list all entities in the text that are associated with the provided category '{entity_type}'. Maintain the output format as 'entity1; entity2.' If none are found, respond with 'No.'\",\n",
"# \"Spot and document all entities within the text that are aligned with the specified category '{entity_type}'. Use the format 'entity1; entity2.' In the event of no such entities, answer 'No.'\",\n",
"# \"Examine and compile all entities in the text that fall under the given category '{entity_type}'. Keep the output in the 'entity1; entity2' format. If there are no entities of this type, reply with 'No.'\",\n",
"# \"Check and provide all entities in the text that match the category '{entity_type}'. Format the output as 'entity1; entity2.' If there are no entities of this type, answer 'No.'\",\n",
"# ],\n",
"# 'Please find all entities in the input text that fit the following entity types: \"person\", \"organization\", \"location\", then answer with the number counted for each type. Output format is \"type1: number1; type2: number2\"': [\n",
"# \"Identify all entities within the input text falling under the specified entity types: 'person,' 'organization,' and 'location.' Report the counts for each type in the format 'type1: number1; type2: number2.'\",\n",
"# \"Detect entities of the 'person,' 'organization,' and 'location' types in the input text. Provide the respective counts for each type as 'type1: number1; type2: number2.'\",\n",
"# \"Locate entities categorized as 'person,' 'organization,' and 'location' within the input text. Deliver the counts for each type in the 'type1: number1; type2: number2' format.\",\n",
"# \"Search for entities that match the entity types 'person,' 'organization,' and 'location' in the input text. Present the counts for each type in the format 'type1: number1; type2: number2.'\",\n",
"# \"Gather entities belonging to the specified types 'person,' 'organization,' and 'location' from the input text. Respond with the counts for each type in the 'type1: number1; type2: number2' format.\",\n",
"# \"Examine the input text for entities of the 'person,' 'organization,' and 'location' categories. Report the respective counts for each type as 'type1: number1; type2: number2.'\",\n",
"# \"Check for entities falling under 'person,' 'organization,' and 'location' types in the input text. Provide the counts for each type in the format 'type1: number1; type2: number2.'\",\n",
"# \"Assess the input text for entities of types 'person,' 'organization,' and 'location.' Share the counts for each type in the 'type1: number1; type2: number2' format.\",\n",
"# \"Inspect the input text to find entities categorized as 'person,' 'organization,' and 'location.' Communicate the counts for each type using the format 'type1: number1; type2: number2.'\",\n",
"# \"Identify entities matching 'person,' 'organization,' and 'location' types within the input text. Provide the counts for each type in the 'type1: number1; type2: number2' format.\"\n",
"# ],\n",
"# 'Given entity types as options, please find the number of occurrence for each entity type in the input text. answer with format \"type1: number1; type2: number2\".\\nOptions: \"person\", \"organization\", \"location\".': [\n",
"# \"Using the provided entity type options, determine the count of each entity type within the input text. Respond in the format 'type1: number1; type2: number2.'\",\n",
"# \"Calculate the occurrences of each entity type listed in the options within the input text. Format the answer as 'type1: number1; type2: number2.'\",\n",
"# \"Count the instances of each entity type mentioned in the options within the input text. Provide the counts in the format 'type1: number1; type2: number2.'\",\n",
"# \"Find and tally the number of occurrences for each entity type from the given options in the input text. Answer using the format 'type1: number1; type2: number2.'\",\n",
"# \"Examine the input text and enumerate the occurrences of each entity type specified in the options. Report the counts in the format 'type1: number1; type2: number2.'\",\n",
"# \"Search for and calculate the occurrences of each entity type included in the options within the input text. Present the results in the format 'type1: number1; type2: number2.'\",\n",
"# \"Identify and count the number of times each entity type from the provided options appears in the input text. Share the counts using the format 'type1: number1; type2: number2.'\",\n",
"# \"Inspect the input text for occurrences of each entity type mentioned in the options. Report the counts in the 'type1: number1; type2: number2' format.\",\n",
"# \"Confirm the occurrences of each entity type listed in the options within the input text. Present the counts as 'type1: number1; type2: number2.'\",\n",
"# \"Gather the occurrences of each entity type specified in the options within the input text. Format the response as 'type1: number1; type2: number2.'\",\n",
"# ],\n",
"# }"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "c8821ba4-0a0d-4446-814a-047bd17696f5",
"metadata": {},
"outputs": [],
"source": [
"# def get_augmented_dataset(data):\n",
"# instructions, inputs, outputs = [], [], []\n",
"# for i, ins in enumerate(data['instruction']):\n",
"# if ins not in chatgpt_templates:\n",
"# entity_type = re.search(r'location|organization|person', ins).group()\n",
"# ins = ins.replace(entity_type, '{entity_type}')\n",
"# assert ins in chatgpt_templates\n",
"# for ins_template in chatgpt_templates[ins]:\n",
"# if '{entity_type}' in ins_template:\n",
"# ins_template = ins_template.replace('{entity_type}', entity_type)\n",
"# instructions.append(ins_template)\n",
"# inputs.append(data['input'][i])\n",
"# outputs.append(data['output'][i])\n",
"# return {\n",
"# 'instruction': data['instruction'] + instructions,\n",
"# 'input': data['input'] + inputs,\n",
"# 'output': data['output'] + outputs\n",
"# }\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "f9ce43c9-8f22-4a79-b5c9-042df702da2c",
"metadata": {},
"outputs": [],
"source": [
"# train_data_aug_1 = get_augmented_dataset(train_data_1)\n",
"# train_data_aug_2 = get_augmented_dataset(train_data_2)\n",
"# train_data_aug_3 = get_augmented_dataset(train_data_3)\n",
"\n",
"# test_data_aug_1 = get_augmented_dataset(test_data_1)\n",
"# test_data_aug_2 = get_augmented_dataset(test_data_2)\n",
"# test_data_aug_3 = get_augmented_dataset(test_data_3)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "a915673a-3f3a-4b68-bdfb-e7e7a49dbb86",
"metadata": {},
"outputs": [],
"source": [
"# train_instruct_data = {k: train_data_aug_1[k] + train_data_aug_2[k] + train_data_aug_3[k] for k in train_data_aug_1.keys()} \n",
"# test_instruct_data = {k: test_data_aug_1[k] + test_data_aug_2[k] + test_data_aug_3[k] for k in test_data_aug_1.keys()} \n",
"\n",
"# ner_instruct_dataset = DatasetDict({\n",
"# 'train': Dataset.from_dict(train_instruct_data),\n",
"# 'test': Dataset.from_dict(test_instruct_data)\n",
"# })\n",
"# ner_instruct_dataset.save_to_disk('fingpt-ner-full-instruct')\n",
"# ner_instruct_dataset\n",
"# # train_data_aug_2['instruction'][-30:]"
]
},
{
"cell_type": "markdown",
"id": "cbae408e-509b-4b74-80ff-d09e12f24f59",
"metadata": {
"tags": []
},
"source": [
"## CLS Ver. for Zero-shot Training"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "dd3613bf-cd27-47ac-a72d-b5a90d5ecf55",
"metadata": {},
"outputs": [],
"source": [
"chatgpt_templates = [\n",
" \"What is the entity type of '{entity}' in the input sentence.\",\n",
" \"With the input text as context, identify the entity type of '{entity}'.\",\n",
" \"Using the input sentence as a reference, analyze and specify the entity type of '{entity}'.\",\n",
" \"In the context of the input sentence, examine and categorize the entity type of '{entity}'.\",\n",
" \"Utilize the input text as context to explore and ascertain the entity type of '{entity}'.\",\n",
" \"Leverage the input sentence to evaluate and define the entity type for '{entity}'.\",\n",
" \"Considering the input sentence as context, inspect and classify the entity type of '{entity}'.\",\n",
" \"With the input sentence as a backdrop, scrutinize and determine the entity type of '{entity}'.\",\n",
" \"Interpreting the input sentence as context, specify the entity type for '{entity}'.\",\n",
" \"Assessing the input sentence as context, label the entity type of '{entity}'.\",\n",
" \"In the input sentence, determine the entity type for '{entity}'.\",\n",
" \"Within the input text, identify the entity type of '{entity}'.\",\n",
" \"Analyze the input sentence to find the entity type of '{entity}'.\",\n",
" \"Check the input sentence for the entity type associated with '{entity}'.\",\n",
" \"Explore the input sentence to ascertain the entity type of '{entity}'.\",\n",
" \"Examine the input text to classify the entity type of '{entity}'.\",\n",
" \"Scrutinize the input sentence to define the entity type of '{entity}'.\"\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "e8b3d4fe-fa0e-44cb-917e-6677b331c98c",
"metadata": {},
"outputs": [],
"source": [
"def entities():\n",
" options = ['location', 'organization', 'person']\n",
" random.shuffle(options)\n",
" return \", \".join(options)\n",
"\n",
"\n",
"def get_ner_dataset_4(sentences):\n",
" \n",
" inputs, outputs, instructions = [], [], []\n",
" for sentence in sentences:\n",
" is_entity = [tup[1] != 'O' and not tup[1].endswith('MISC') for tup in sentence]\n",
" if sum(is_entity) == 0:\n",
" continue\n",
" tmp_tup_list = []\n",
" entity_dict = {}\n",
" for i, tup in enumerate(sentence):\n",
" if tmp_tup_list and (not is_entity[i] or tmp_tup_list[-1][1] != tup[1] or i + 1 == len(sentence)):\n",
" entity = ' '.join([t[0] for t in tmp_tup_list])\n",
" assert tmp_tup_list[0][1] == tmp_tup_list[-1][1], tmp_tup_list\n",
" entity_type = ent_dict[tmp_tup_list[-1][1].split('-')[-1]]\n",
" tmp_tup_list = [] if not is_entity[i] else [tup]\n",
" if entity not in entity_dict:\n",
" entity_dict[entity] = entity_type\n",
" elif entity_dict[entity] != entity_type:\n",
" entity_dict[entity] = \"\"\n",
" else:\n",
" pass\n",
" elif is_entity[i]:\n",
" tmp_tup_list.append(tup)\n",
" else:\n",
" pass\n",
" # print(entity_dict)\n",
" for k, v in entity_dict.items():\n",
" # instructions.extend([t.format(entity=entity, options=entities()) for t in chatgpt_templates])\n",
" instructions.extend([t.format(entity=entity) + '\\nOptions: ' + entities() for t in chatgpt_templates])\n",
" inputs.extend([' '.join([tup[0] for tup in sentence])] * len(chatgpt_templates))\n",
" outputs.extend([entity_type] * len(chatgpt_templates))\n",
" \n",
" print(len(instructions))\n",
" \n",
" return {\"input\": inputs, \"output\": outputs, \"instruction\": instructions}\n"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "421389f7-0174-44de-8e93-374ced33e949",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13549\n",
"3502\n"
]
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004518270492553711,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 13549,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "2d89aaa59b74485aa4c33dc64ba2637d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/13549 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.0043218135833740234,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 3502,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "de5e4a165e5e4d44a61247db629bbc1f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/3502 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 13549\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 3502\n",
" })\n",
"})"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_data = read_conll_file('./SEC-filings/CONLL-format/data/train/FIN5.txt')\n",
"test_data = read_conll_file('./SEC-filings/CONLL-format/data/test/FIN3.txt')\n",
"\n",
"train_data_cls = get_ner_dataset_4(train_data)\n",
"test_data_cls = get_ner_dataset_4(test_data)\n",
"\n",
"ner_cls_instruct_dataset = DatasetDict({\n",
" 'train': Dataset.from_dict(train_data_cls),\n",
" 'test': Dataset.from_dict(test_data_cls)\n",
"})\n",
"ner_cls_instruct_dataset.save_to_disk('fingpt-ner-cls-instruct')\n",
"ner_cls_instruct_dataset\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "e5178c4a-5add-4828-bca5-549b0edb1090",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"What is the entity type of '40 William St' in the input sentence.\\nOptions: person, location, organization\",\n",
" \"With the input text as context, identify the entity type of '40 William St'.\\nOptions: organization, person, location\",\n",
" \"Using the input sentence as a reference, analyze and specify the entity type of '40 William St'.\\nOptions: organization, location, person\",\n",
" \"In the context of the input sentence, examine and categorize the entity type of '40 William St'.\\nOptions: location, organization, person\",\n",
" \"Utilize the input text as context to explore and ascertain the entity type of '40 William St'.\\nOptions: organization, person, location\",\n",
" \"Leverage the input sentence to evaluate and define the entity type for '40 William St'.\\nOptions: person, organization, location\",\n",
" \"Considering the input sentence as context, inspect and classify the entity type of '40 William St'.\\nOptions: organization, location, person\",\n",
" \"With the input sentence as a backdrop, scrutinize and determine the entity type of '40 William St'.\\nOptions: organization, location, person\",\n",
" \"Interpreting the input sentence as context, specify the entity type for '40 William St'.\\nOptions: person, location, organization\",\n",
" \"Assessing the input sentence as context, label the entity type of '40 William St'.\\nOptions: organization, location, person\"]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ner_cls_instruct_dataset['train']['instruction'][:10]"
]
},
{
"cell_type": "markdown",
"id": "9f829608-6cfd-488c-ad8f-053013034e97",
"metadata": {},
"source": [
"# FinRED"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "13db75e7-fc60-4ff6-9806-a27d53c1c19a",
"metadata": {},
"outputs": [],
"source": [
"with open('FinRED/relations.txt') as f:\n",
" relations = [r.strip() for r in f.readlines()]\n",
"\n",
" \n",
"def get_instruction(sent, tuples, with_orig=True, with_cls=False):\n",
" \n",
" instructions, inputs, outputs = [], [], []\n",
" if with_orig:\n",
" instructions.append(f\"Given phrases that describe the relationship between two words/phrases as options, extract the word/phrase pair and the corresponding lexical relationship between them from the input text. The output format should be \\\"relation1: word1, word2; relation2: word3, word4\\\". Options: {', '.join(relations)}.\")\n",
" instructions.append(f\"Given the input sentence, please extract the subject and object containing a certain relation in the sentence according to the following relation types, in the format of \\\"relation1: word1, word2; relation2: word3, word4\\\". Relations include: {'; '.join(relations)}.\")\n",
" inputs.extend([sent] * 2)\n",
" outputs.extend([\"; \".join([f\"{tup[-1]}: {tup[0]}, {tup[1]}\" for tup in tuples])] * 2)\n",
" \n",
" if with_cls:\n",
" for tup in tuples:\n",
" instructions.append(f\"Utilize the input text as a context reference, choose the right relationship between {tup[0]} and {tup[1]} from the options. Options: {', '.join(relations)}.\")\n",
" instructions.append(f\"What is the relationship between {tup[0]} and {tup[1]} in the context of the input sentence. Choose an answer from: {'; '.join(relations)}.\")\n",
" inputs.extend([sent] * 2)\n",
" outputs.extend([tup[-1]] * 2)\n",
" \n",
" return instructions, inputs, outputs\n",
"\n",
"\n",
"def get_finred_dataset(sent_file, tup_file, with_orig, with_cls):\n",
"\n",
" instructions, inputs, outputs = [], [], []\n",
"\n",
" with open(sent_file) as f:\n",
" sentences = [s.strip() for s in f.readlines()]\n",
" with open(tup_file) as f:\n",
" tuples_list = [s.split(' | ') for s in f.readlines()]\n",
" \n",
" for sent, tuples in zip(sentences, tuples_list):\n",
" tuples = [[e.strip() for e in tup.split(' ; ')] for tup in tuples]\n",
" \n",
" ins, i, o = get_instruction(sent, tuples, with_orig, with_cls)\n",
" \n",
" instructions.extend(ins)\n",
" inputs.extend(i)\n",
" outputs.extend(o)\n",
" \n",
" return Dataset.from_dict({\n",
" 'input': inputs,\n",
" 'output': outputs,\n",
" 'instruction': instructions\n",
" })\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "fc080217-5907-4050-b8fc-143729b43c04",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.0045130252838134766,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 27558,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/27558 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004259586334228516,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 5112,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/5112 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 27558\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 5112\n",
" })\n",
"})"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_dataset = get_finred_dataset('FinRED/train.sent', 'FinRED/train.tup', with_orig=True, with_cls=True)\n",
"test_dataset = get_finred_dataset('FinRED/test.sent', 'FinRED/test.tup', with_orig=True, with_cls=True)\n",
"\n",
"finred_dataset = DatasetDict({\n",
" 'train': train_dataset,\n",
" 'test': test_dataset\n",
"})\n",
"\n",
"finred_dataset.save_to_disk('fingpt-finred')\n",
"finred_dataset"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "b81ad3e1-6e6f-465c-be23-559b561be89d",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.00446009635925293,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 11400,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/11400 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004523754119873047,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 2136,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/2136 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 11400\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 2136\n",
" })\n",
"})"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_dataset = get_finred_dataset('FinRED/train.sent', 'FinRED/train.tup', with_orig=True, with_cls=False)\n",
"test_dataset = get_finred_dataset('FinRED/test.sent', 'FinRED/test.tup', with_orig=True, with_cls=False)\n",
"\n",
"finred_dataset = DatasetDict({\n",
" 'train': train_dataset,\n",
" 'test': test_dataset\n",
"})\n",
"\n",
"finred_dataset.save_to_disk('fingpt-finred-re')\n",
"finred_dataset"
]
},
{
"cell_type": "markdown",
"id": "700987c5-5561-407d-a87f-ff3ef8a343a0",
"metadata": {},
"source": [
"## CLS Ver. for Zero-shot Training"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "12b63879-8b4a-4f6e-8e2a-ea769cc5ace4",
"metadata": {},
"outputs": [],
"source": [
"with open('FinRED/relations.txt') as f:\n",
" all_relations = [r.strip() for r in f.readlines()]\n",
"\n",
"\n",
"def get_instruction(sent, tuples):\n",
" \n",
" instructions, inputs, outputs = [], [], []\n",
" for tup in tuples: \n",
" output = tup[-1].replace('_', ' ').replace(' / ', '/').replace(' or ', '/')\n",
" relations = all_relations.copy()\n",
" relations.remove(output)\n",
" random.shuffle(relations)\n",
" relations = relations[:3] + [output]\n",
" random.shuffle(relations)\n",
" instructions.append(f\"Utilize the input text as a context reference, choose the right relationship between '{tup[0]}' and '{tup[1]}' from the options.\\nOptions: {', '.join(relations)}\")\n",
" random.shuffle(relations)\n",
" instructions.append(f\"Refer to the input text as context and select the correct relationship between '{tup[0]}' and '{tup[1]}' from the available options.\\nOptions: {', '.join(relations)}\")\n",
" random.shuffle(relations)\n",
" instructions.append(f\"Take context from the input text and decide on the accurate relationship between '{tup[0]}' and '{tup[1]}' from the options provided.\\nOptions: {', '.join(relations)}\")\n",
" random.shuffle(relations)\n",
" instructions.append(f\"What is the relationship between '{tup[0]}' and '{tup[1]}' in the context of the input sentence.\\nOptions: {', '.join(relations)}\")\n",
" random.shuffle(relations)\n",
" instructions.append(f\"In the context of the input sentence, determine the relationship between '{tup[0]}' and '{tup[1]}'.\\nOptions: {', '.join(relations)}\")\n",
" random.shuffle(relations)\n",
" instructions.append(f\"Analyze the relationship between '{tup[0]}' and '{tup[1]}' within the context of the input sentence.\\nOptions: {', '.join(relations)}\")\n",
" inputs.extend([sent] * 6)\n",
" outputs.extend([output] * 6)\n",
" \n",
" return instructions, inputs, outputs\n",
"\n",
"\n",
"def get_finred_dataset(sent_file, tup_file):\n",
" \n",
" random.seed(0)\n",
" instructions, inputs, outputs = [], [], []\n",
"\n",
" with open(sent_file) as f:\n",
" sentences = [s.strip() for s in f.readlines()]\n",
" with open(tup_file) as f:\n",
" tuples_list = [s.split(' | ') for s in f.readlines()]\n",
" \n",
" for sent, tuples in zip(sentences, tuples_list):\n",
" tuples = [[e.strip() for e in tup.split(' ; ')] for tup in tuples]\n",
" \n",
" ins, i, o = get_instruction(sent, tuples)\n",
" \n",
" instructions.extend(ins)\n",
" inputs.extend(i)\n",
" outputs.extend(o)\n",
" \n",
" return Dataset.from_dict({\n",
" 'input': inputs,\n",
" 'output': outputs,\n",
" 'instruction': instructions\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "22eb8f60-3060-4e83-a785-d3a66c7f60f9",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004596710205078125,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 48474,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/48474 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004204511642456055,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 8928,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/8928 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 48474\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 8928\n",
" })\n",
"})"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_dataset_instruct = get_finred_dataset('FinRED/train.sent', 'FinRED/train.tup')\n",
"test_dataset_instruct = get_finred_dataset('FinRED/test.sent', 'FinRED/test.tup')\n",
"\n",
"finred_dataset_instruct = DatasetDict({\n",
" 'train': train_dataset_instruct,\n",
" 'test': test_dataset_instruct\n",
"})\n",
"\n",
"finred_dataset_instruct.save_to_disk('fingpt-finred-cls-instruct')\n",
"finred_dataset_instruct"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "ac71c924-4372-4b72-95d8-92a063cc9fdb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"Utilize the input text as a context reference, choose the right relationship between 'Apple Inc' and 'Steve Jobs' from the options.\\nOptions: industry, founded by, owner of, currency\",\n",
" \"Refer to the input text as context and select the correct relationship between 'Apple Inc' and 'Steve Jobs' from the available options.\\nOptions: industry, currency, owner of, founded by\",\n",
" \"Take context from the input text and decide on the accurate relationship between 'Apple Inc' and 'Steve Jobs' from the options provided.\\nOptions: industry, currency, owner of, founded by\",\n",
" \"What is the relationship between 'Apple Inc' and 'Steve Jobs' in the context of the input sentence.\\nOptions: currency, founded by, owner of, industry\",\n",
" \"In the context of the input sentence, determine the relationship between 'Apple Inc' and 'Steve Jobs'.\\nOptions: industry, founded by, owner of, currency\",\n",
" \"Analyze the relationship between 'Apple Inc' and 'Steve Jobs' within the context of the input sentence.\\nOptions: currency, founded by, owner of, industry\",\n",
" \"Utilize the input text as a context reference, choose the right relationship between 'Apple Inc' and 'Steve Jobs' from the options.\\nOptions: chief executive officer, headquarters location, subsidiary, industry\",\n",
" \"Refer to the input text as context and select the correct relationship between 'Apple Inc' and 'Steve Jobs' from the available options.\\nOptions: subsidiary, chief executive officer, headquarters location, industry\",\n",
" \"Take context from the input text and decide on the accurate relationship between 'Apple Inc' and 'Steve Jobs' from the options provided.\\nOptions: chief executive officer, industry, subsidiary, headquarters location\",\n",
" \"What is the relationship between 'Apple Inc' and 'Steve Jobs' in the context of the input sentence.\\nOptions: subsidiary, headquarters location, chief executive officer, industry\"]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"finred_dataset_instruct['train']['instruction'][:10]"
]
},
{
"cell_type": "markdown",
"id": "eb84d245-daef-483d-bf77-8bde1dcc4481",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# ConvFinQA"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "964e11a4-6f54-42de-bde4-ed8aac19a010",
"metadata": {},
"outputs": [],
"source": [
"def get_confinqa_dataset(json_file):\n",
"\n",
" instructions, inputs, outputs = [], [], []\n",
"\n",
" instruction = 'Read the following texts and table with financial data from an S&P 500 earnings report carefully.' \\\n",
" 'Based on the question-answer history (if provided), answer the last question. ' \\\n",
" 'The answer may require mathematical calculation based on the data provided.\\n'\n",
"\n",
" samples = json.load(open(json_file))\n",
" for sample in samples:\n",
" annos = sample['annotation']\n",
" pre_text, post_text = annos['amt_pre_text'], annos['amt_post_text']\n",
" table = annos['amt_table'].replace('<td></td>', '<td>-</td>')\n",
" context = f'{pre_text} {table} {post_text}\\n'\n",
" questions, answers, turn_ind = annos['dialogue_break'], annos['exe_ans_list'], annos['turn_ind']\n",
" for i in range(turn_ind):\n",
" context += f'Question: {questions[i]}\\n'\n",
" context += f'Answer: {answers[i]}\\n'\n",
" context += f'Question: {questions[turn_ind]}\\n'\n",
" outputs.append(str(answers[turn_ind]))\n",
" instructions.append(instruction)\n",
" inputs.append(context)\n",
" \n",
" return Dataset.from_dict({\n",
" 'input': inputs,\n",
" 'output': outputs,\n",
" 'instruction': instructions\n",
" })\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "e817bc45-9825-4af6-bd34-94a6307c15fc",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004643678665161133,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 11104,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "4bebad1197d14912ba83461801e19b9a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/11104 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004342794418334961,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 1490,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "eeba81a2788b4b9b87f47671a9d6a579",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/1490 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 11104\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 1490\n",
" })\n",
"})"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_file = 'ConvFinQA/data/train_turn.json'\n",
"test_file = 'ConvFinQA/data/dev_turn.json'\n",
"\n",
"train_dataset = get_confinqa_dataset(train_file)\n",
"test_dataset = get_confinqa_dataset(test_file)\n",
"\n",
"convfinqa_dataset = DatasetDict({\n",
" 'train': train_dataset,\n",
" 'test': test_dataset\n",
"})\n",
"\n",
"convfinqa_dataset.save_to_disk('fingpt-convfinqa')\n",
"convfinqa_dataset"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "babd6ee9-3d28-4ca6-bdc9-4d13cb76c7be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'input': 'in a new business model such as the retail segment is inherently risky , particularly in light of the significant investment involved , the current economic climate , and the fixed nature of a substantial portion of the retail segment\\'s operating expenses . results for this segment are dependent upon a number of risks and uncertainties , some of which are discussed below under the heading \"factors that may affect future results and financial condition.\" backlog in the company\\'s experience , the actual amount of product backlog at any particular time is not a meaningful indication of its future business prospects . in particular , backlog often increases in anticipation of or immediately following new product introductions because of over- ordering by dealers anticipating shortages . backlog often is reduced once dealers and customers believe they can obtain sufficient supply . because of the foregoing , backlog cannot be considered a reliable indicator of the company\\'s ability to achieve any particular level of revenue or financial performance . further information regarding the company\\'s backlog may be found below under the heading \"factors that may affect future results and financial condition.\" gross margin gross margin for the three fiscal years ended september 28 , 2002 are as follows ( in millions , except gross margin percentages ) : gross margin increased to 28% ( 28 % ) of net sales in 2002 from 23% ( 23 % ) in 2001 . as discussed below , gross margin in 2001 was unusually low resulting from negative gross margin of 2% ( 2 % ) experienced in the first quarter of 2001 . as a percentage of net sales , the company\\'s quarterly gross margins declined during fiscal 2002 from 31% ( 31 % ) in the first quarter down to 26% ( 26 % ) in the fourth quarter . this decline resulted from several factors including a rise in component costs as the year progressed and aggressive pricing by the company across its products lines instituted as a result of continued pricing pressures in the personal computer industry . the company anticipates that its gross margin and the gross margin of the overall personal computer industry will remain under pressure throughout fiscal 2003 in light of weak economic conditions , flat demand for personal computers in general , and the resulting pressure on prices . the foregoing statements regarding anticipated gross margin in 2003 and the general demand for personal computers during 2003 are forward- looking . gross margin could differ from anticipated levels because of several factors , including certain of those set forth below in the subsection entitled \"factors that may affect future results and financial condition.\" there can be no assurance that current gross margins will be maintained , targeted gross margin levels will be achieved , or current margins on existing individual products will be maintained . in general , gross margins and margins on individual products will remain under significant downward pressure due to a variety of factors , including continued industry wide global pricing pressures , increased competition , compressed product life cycles , potential increases in the cost and availability of raw material and outside manufacturing services , and potential changes to the company\\'s product mix , including higher unit sales of consumer products with lower average selling prices and lower gross margins . in response to these downward pressures , the company expects it will continue to take pricing actions with respect to its products . gross margins could also be affected by the company\\'s ability to effectively manage quality problems and warranty costs and to stimulate demand for certain of its products . the company\\'s operating strategy and pricing take into account anticipated changes in foreign currency exchange rates over time ; however , the company\\'s results of operations can be significantly affected in the short-term by fluctuations in exchange rates . the company orders components for its products and builds inventory in advance of product shipments . because the company\\'s markets are volatile and subject to rapid technology and price changes , there is a risk the company will forecast incorrectly and produce or order from third parties excess or insufficient inventories of particular products or components . the company\\'s operating results and financial condition have been in the past and may in the future be materially adversely affected by the company\\'s ability to manage its inventory levels and outstanding purchase commitments and to respond to short-term shifts in customer demand patterns . gross margin declined to 23% ( 23 % ) of net sales in 2001 from 27% ( 27 % ) in 2000 . this decline resulted primarily from gross margin of negative 2% ( 2 % ) experienced during the first quarter of 2001 compared to 26% ( 26 % ) gross margin for the same quarter in 2000 . in addition to lower than normal net . <table class=\\'wikitable\\'><tr><td>1</td><td>-</td><td>2002</td><td>2001</td><td>2000</td></tr><tr><td>2</td><td>net sales</td><td>$ 5742</td><td>$ 5363</td><td>$ 7983</td></tr><tr><td>3</td><td>cost of sales</td><td>4139</td><td>4128</td><td>5817</td></tr><tr><td>4</td><td>gross margin</td><td>$ 1603</td><td>$ 1235</td><td>$ 2166</td></tr><tr><td>5</td><td>gross margin percentage</td><td>28% ( 28 % )</td><td>23% ( 23 % )</td><td>27% ( 27 % )</td></tr></table> .\\nQuestion: what was the total of net sales in 2001?\\nAnswer: 5363.0\\nQuestion: and what was that in 2000?\\n',\n",
" 'output': '7983.0',\n",
" 'instruction': 'Read the following texts and table with financial data from an S&P 500 earnings report carefully.Based on the question-answer history (if provided), answer the last question. The answer may require mathematical calculation based on the data provided.\\n'}"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convfinqa_dataset['train'][9]"
]
},
{
"cell_type": "markdown",
"id": "44f2a7ea-b832-462f-8103-eca6ee4046bb",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# FinEval"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "7f42f8eb-4174-46e7-b2fe-7c8a6487f084",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.004954338073730469,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 1056,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "3784d06ad3f94b04a2e185d139aa9f33",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/1056 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.0040547847747802734,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 265,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "ae0d31a357e74c6d825773a7c8f81cb1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/265 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 1056\n",
" })\n",
" test: Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 265\n",
" })\n",
"})"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"csv_list = glob('FinEval/val/*.csv') + glob('FinEval/dev/*.csv')\n",
"subject_mapping = json.load(open('FinEval/subject_mapping.json'))\n",
"\n",
"instructions, inputs, outputs = [], [], []\n",
"\n",
"for csv_file in csv_list:\n",
" subject = subject_mapping[csv_file.split('/')[-1][:-8]][1]\n",
" df = pd.read_csv(csv_file)\n",
" for index, row in df.iterrows():\n",
" instructions.append(f'以下是中国关于{subject}考试的单项选择题,请选出其中的正确答案。')\n",
" inputs.append(f\"{row['question']}\\nA. {row['A']}\\nB. {row['B']}\\nC. {row['C']}\\nD. {row['D']}\\n\")\n",
" outputs.append(f\"{row['answer']}. {row[row['answer']]}\")\n",
" \n",
"fineval_dataset = Dataset.from_dict({\n",
" 'input': inputs,\n",
" 'output': outputs,\n",
" 'instruction': instructions\n",
"})\n",
"fineval_dataset = fineval_dataset.train_test_split(0.2, seed=42)\n",
"fineval_dataset.save_to_disk('fingpt-fineval')\n",
"fineval_dataset\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "cd766190-4cdc-4f4d-93d1-d2bfd0627093",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'input': '研究社会资本再生产的出发点是____。\\nA. 货币资本\\nB. 生产资本\\nC. 流通资本\\nD. 社会总产品\\n',\n",
" 'output': 'D. 社会总产品',\n",
" 'instruction': '以下是中国关于政治经济学考试的单项选择题,请选出其中的正确答案。'}"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fineval_dataset['train'][3]"
]
},
{
"cell_type": "markdown",
"id": "0c87615b-95e9-4dfd-b2a4-fa5ed900c2ba",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# FiQA QA"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "a410166c-265f-4843-afba-35feb14b6063",
"metadata": {},
"outputs": [],
"source": [
"docs = pd.read_csv('FiQA_train/FiQA_train_doc_final.tsv', sep='\\t')\n",
"questions = pd.read_csv('FiQA_train/FiQA_train_question_final.tsv', sep='\\t')\n",
"qa_pairs = pd.read_csv('FiQA_train/FiQA_train_question_doc_final.tsv', sep='\\t')"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "96230dce-bac3-43f1-ba19-48eeeb03a347",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.005600690841674805,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "Saving the dataset (0/1 shards)",
"rate": null,
"total": 17110,
"unit": " examples",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "8b1cce16e3de4d49ae7ac66647e093a0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/17110 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['input', 'output', 'instruction'],\n",
" num_rows: 17110\n",
"})"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"doc_dict, question_dict = {}, {}\n",
"for i, row in docs.iterrows():\n",
" doc_dict[row['docid']] = row['doc']\n",
"for i, row in questions.iterrows():\n",
" question_dict[row['qid']] = row['question']\n",
" \n",
"instruction_templates = [\n",
" \"Utilize your financial knowledge, give your answer or opinion to the input question or subject . Answer format is not limited.\",\n",
" \"Offer your insights or judgment on the input financial query or topic using your financial expertise. Reply as normal question answering\",\n",
" \"Based on your financial expertise, provide your response or viewpoint on the given financial question or topic. The response format is open.\",\n",
" \"Share your insights or perspective on the financial matter presented in the input.\",\n",
" \"Offer your thoughts or opinion on the input financial query or topic using your financial background.\"\n",
"]\n",
"\n",
"inputs, outputs, instructions = [], [], []\n",
"for i, row in qa_pairs.iterrows():\n",
" qid, docid = row['qid'], row['docid']\n",
" q = str(question_dict[qid])\n",
" doc = str(doc_dict[docid])\n",
" inputs.append(q)\n",
" outputs.append(doc)\n",
" instructions.append(instruction_templates[i%5])\n",
"\n",
"fiqa_qa_dataset = Dataset.from_dict({\n",
" 'input': inputs,\n",
" 'output': outputs,\n",
" 'instruction': instructions\n",
"})\n",
"fiqa_qa_dataset.save_to_disk('fingpt-fiqa_qa')\n",
"fiqa_qa_dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25a8558d-97a9-4b90-8835-cbfa32e28125",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b07cfc13-4b1e-4672-ab76-64f90c9d03e1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|