Spaces:
Running
Running
File size: 144,840 Bytes
1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 309d907 1e2f54d c3a2666 1e2f54d c3a2666 05e5fac ba69ad8 05e5fac ba69ad8 1e2f54d 0c212bd 1e2f54d c3a2666 ff4573f c3a2666 ff4573f c3a2666 ff4573f c3a2666 ff4573f c3a2666 ff4573f c3a2666 ff4573f c3a2666 ff4573f ba69ad8 1e2f54d c3a2666 1e2f54d 9eefd88 1e2f54d ef18338 c3a2666 1e2f54d 4bacc26 c3a2666 1e2f54d c3a2666 ef18338 c3a2666 1e2f54d 05e5fac 1e2f54d c3a2666 1e2f54d ce4ad6d 1e2f54d 05e5fac 1e2f54d ef18338 0c212bd 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d c3a2666 1e2f54d c3a2666 ba69ad8 c3a2666 ba69ad8 c3a2666 ba69ad8 c3a2666 ba69ad8 c3a2666 ba69ad8 c3a2666 ba69ad8 c3a2666 ba69ad8 c3a2666 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d 05e5fac 1e2f54d 9eefd88 1e2f54d a4f6f1d 1e2f54d 05e5fac 0c212bd 1e2f54d 0c212bd 444f117 0c212bd 07c774d 9bb2916 444f117 0c212bd 444f117 0c212bd 444f117 07c774d 9bb2916 444f117 07c774d 9bb2916 444f117 0c212bd 444f117 05e5fac 444f117 05e5fac 444f117 05e5fac 444f117 07c774d 9bb2916 444f117 05e5fac 444f117 07c774d 9bb2916 444f117 07c774d 0c212bd 05e5fac 0c212bd 1e2f54d 0c212bd 05e5fac 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 444f117 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 05e5fac 1e2f54d 0c212bd 1e2f54d c3a2666 1e2f54d 0c212bd 1e2f54d c3a2666 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d c3a2666 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d ce4ad6d 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 0c212bd 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d c3a2666 05e5fac c3a2666 05e5fac c3a2666 05e5fac c3a2666 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d ba69ad8 1e2f54d c3a2666 1e2f54d 309d907 1e2f54d c3a2666 1e2f54d 0c212bd 1e2f54d c3a2666 1e2f54d c3a2666 1e2f54d c3a2666 1e2f54d c3a2666 1e2f54d ef18338 1e2f54d ef18338 1e2f54d c3a2666 ef18338 c3a2666 9eefd88 c3a2666 ef18338 c3a2666 1e2f54d 05e5fac 1e2f54d 05e5fac 1e2f54d 9ead96a 05e5fac 9ead96a 05e5fac 1e2f54d f29399f ad6b406 0b8f68a f29399f 1e2f54d ad17203 1e2f54d 309d907 1e2f54d 41176a4 0895a00 1e2f54d c3a2666 |
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 |
import os
from transformers import file_utils
print(file_utils.default_cache_path)
import pandas as pd
from tqdm import tqdm
from gliner import GLiNER
import logging
import time
import sys
from transformers import pipeline, AutoTokenizer, AutoModel
from transformers.pipelines.pt_utils import KeyDataset
from sentence_transformers.util import cos_sim
from typing import Dict
from concurrent.futures import ThreadPoolExecutor, as_completed
from collections import Counter
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Device: {device}...")
if device.type == "cuda":
print("GPU number:", torch.cuda.current_device())
import datasets
import argparse
import json
import random
import numpy as np
import requests
from langchain.text_splitter import TokenTextSplitter
from virtuosoQueryRest import sparqlQuery
import gradio as gr
import re
from common import strtobool, split_camel_case, chunk_tokens, update_nested_dict, cleanInputText, token_counter, encoding_getter, extract_words, all_words_in_list, row_to_dict_string, strip_quotes, rescale_exponential_to_logarithmic
examples = [
["He said the disease was 1st detected to have spread to Malaysia from Africa in 1997. " , None],
["He said the disease which was 1st detected in Johor had spread to Negeri Sembilan, Melaka, Perak, Selangor and the latest Kedah. He said the disease was 1st detected to have spread to Malaysia from Africa in 1997. ", None],
["The Health Ministry has detected about 4000 suspected chikungunya cases nationwide this year [2008], Minister Datuk Liow Tiong Lai said Wednesday [17 Dec 2008]. ", None],
["The Health Ministry has detected about 4000 suspected chikungunya cases nationwide this year [2008], Minister Datuk Liow Tiong Lai said Wednesday [17 Dec 2008]. He said the disease which was 1st detected in Johor had spread to Negeri Sembilan, Melaka, Perak, Selangor and the latest Kedah. \"So far, the chikungunya disease is still under control nationwide,\" he told reporters after visiting Sultanah Nur Zahirah Hospital here. Present was Terengganu Health Director Dr. Nordiyanah Hassan. Liow said that so far, there is no specific medicine to treat the chikungunya fever disease spread by _Aedes_ mosquito. \"So, I would like to call on the public to be careful particularly during the wet season now because _Aedes_ mosquito is easy to breed,\" he said. To contain the spread of the disease, he said, the ministry had taken several measures including intensifying the campaign to rid of _Aedes_ mosquito and holding lectures on the outbreak. He said the disease was 1st detected to have spread to Malaysia from Africa in 1997. Meanwhile, he said 63 health projects costing RM458 million [USD 131 230 211] had been approved for implementation in Terengganu under the Ninth Malaysia Plan and some had started.", None],
["Carcinoma", None],
["The doctor diagnosed the patient with basal cell carcinoma, a common type of skin cancer.", None],
["West Nile virus", None],
["Legionellosis", None],
["Eight years ago I started with Fosamax for 3-4 years and then took Actonel. In March, I decided not to take Actonel any longer. I had been on it for too long and was fearful of esophageal cancer and bone breakage. Now my doctor wants me to take the Prolia injections, which I am not going to do. I am not going to continue with any drugs. My bone density recently done was in the minuses. I do work with a personal trainer and execise daily. I am searching for alternative ways to deal with this problem.", None],
["Does Chicago have any stores and does Joe live here?", None],
["Cholera has been reported every week since November 1994. By 5 November 1995 at total of 12,344 with 245 deaths have been notified. Of these, 879 cases with 4 deaths were reported for the period 9 October to 5 November 1995. Control efforts have not succeeded in preventing the spread of the epidemic and when cases were detected on Sao Nicolau and Sal Islands in the period 9 October to 5 November all nine inhabited islands of Cap Verde had become infected. The last cholera epidemic in Cap Verde occurred in 1979. (See also Weekly Epidemiological Record No. 44, 3 November 1995) Côte d'Ivoire: A cholera outbreak which started in September 1995 caused 2,027 cases and 150 deaths up to 12 November 1995. The first cases were reported in Department de l'Ouest on 18 September 1995. Cases were subsequently reported in Department de Nord and most recently in Department du Centre and Department de Sud. The WHO Representative assisted in the organization of a team to visit the area and evaluate the situation as well as arranging for medical supplies. (1.12.95) Iran, Islamic Republic of,: Kordestan Province has been declared free of cholera. (1.12.95) Iraq: An outbreak of cholera reported from Sulaimaniyah Governorate in Northern Iraq has resulted in 519 cases, 264 of which have been confirmed, and 3 deaths to date. Vibrio cholerae O1 serotype Ogawa has been isolated. At the request of the Iraqi Ministry of Health, a WHO consultant has been sent to the area to assess and monitor the situation, provide guidance to the health authorities, and coordinate inputs by non-governmental organizations. WHO has also made available essential treatment supplies. An intensive media campaign to raise public awareness about essential preventive measures has been successful in containing the spread of the outbreak. (1.12.95) Senegal: Despite the fact that cholera has been endemic in countries bordering Senegal for the past two years, no cases were reported from Senegal until mid- August 1995. Between 15 August and 17 November 1995, 852 case and 43 deaths were notified. A further 731 cases with 37 deaths have been reported for the period 1 September to 12 November. Most cases were in the Departments of Dakar and Pikine in the Dakar Region and recently also Departments of Mbacke and Touba in Diourbel Region. ", None],
]
#models_List = ["FacebookAI/xlm-roberta-large-finetuned-conll03-english", "Babelscape/wikineural-multilingual-ner", "blaze999/Medical-NER", "urchade/gliner_large-v2.1", "urchade/gliner_large_bio-v0.1", "NCBO/BioPortal" ] # "urchade/gliner_large-v2.1", "knowledgator/gliner-multitask-large-v0.5"
models_List = ["Babelscape/wikineural-multilingual-ner", "urchade/gliner_large-v2.1", "NCBO/BioPortal" ] # "urchade/gliner_large-v2.1", "knowledgator/gliner-multitask-large-v0.5"
#models_List = ["NCBO/BioPortal" ]
categories_List = ["MED","LOC","PER","ORG","DATE","MISC"]
modelGliner=None
modelGlinerBio=None
num_cores_Gliner_forDemo = 0 # 0 means use the GPU for Gliner !
tokenizerGliner = AutoTokenizer.from_pretrained('microsoft/deberta-v3-large')
POSSIBLE_KGchoices_List = ["AEO", "BFO", "BIM", "BCGO", "CL", "CHIRO", "CHEBI", "DCM", "FMA", "GO", "GENO",
"GeoSPARQL", "HL7", "DOID", "HP", "HP_O", "IDO", "IAO", "ICD10", "LOINC", "MESH",
"MONDO", "NCIT", "NCBITAXON", "NCBITaxon_", "NIFCELL", "NIFSTD", "GML", "OBCS", "OCHV", "OHPI",
"OPB", "TRANS", "PLOSTHES", "RADLEX", "RO", "STY", "SO", "SNOMED", "STATO",
"SYMP", "FoodOn", "UBERON", "VO", "OGMS", "EuroSciVoc"]
ONLY_Ontologies_OnBIOPORTAL = ["AEO", "BCGO", "BFO", "BIM", "CHEBI", "CHIRO", "CL", "DCM", "DOID", "FMA", "FOODON", "GENO", "GML", "GO", "GEOSPARQL", "HL7", "HP", "HP_O", "IAO", "ICD10", "IDO", "LOINC", "MESH", "MONDO", "NCBITAXON", "NCIT", "NIFCELL", "NIFSTD", "OBCS", "OCHV", "OHPI", "OPB", "PLOSTHES", "RADLEX", "OBOREL", "SNOMEDCT", "SO", "STATO", "STY", "SYMP", "PTRANS", "UBERON", "VO", "OGMS"]
encod = encoding_getter('microsoft/deberta-v3-large')
text_splitter = TokenTextSplitter(
# separators=separators,
encoding_name=encod.name,
chunk_size=80000,
chunk_overlap=50,
length_function=len,
add_start_index=True,
)
pipe_dict = {}
for modelName in models_List:
tsk = "token-classification"
if (("/gliner" in modelName) == False) and (("NCBO" in modelName) == False):
pipe = pipeline(
tsk,
model=modelName,
aggregation_strategy="simple",
device=device,
)
pipe_dict[modelName] = pipe
elif ("/gliner" in modelName):
if not tokenizerGliner:
tokenizerGliner = AutoTokenizer.from_pretrained('microsoft/deberta-v3-large')
if "_bio-" in modelName:
if num_cores_Gliner_forDemo > 0:
modelGlinerBio = GLiNER.from_pretrained(modelName) # "urchade/gliner_large_bio-v0.1")
else:
modelGlinerBio = GLiNER.from_pretrained(modelName, map_location=device)
else:
if num_cores_Gliner_forDemo > 0:
modelGliner = GLiNER.from_pretrained(
modelName) # "knowledgator/gliner-multitask-large-v0.5" - "urchade/gliner_large-v2.1"
else:
modelGliner = GLiNER.from_pretrained(modelName, map_location=device)
# 1. Load the model and tokenizer
model_id_Retriever = 'mixedbread-ai/mxbai-embed-large-v1'
tokenizer_Retriever = AutoTokenizer.from_pretrained(model_id_Retriever)
modelRetriever = AutoModel.from_pretrained(model_id_Retriever)
def RAG_retrieval_Base(queryText, passages, min_threshold=0.0, max_num_passages=None):
similarities = retrievePassageSimilarities(queryText, passages)
# Create a DataFrame
df = pd.DataFrame({
'Passage': passages,
'Similarity': similarities.flatten() # Flatten the similarity tensor/array to ensure compatibility
})
# Filter the DataFrame based on the similarity threshold
df_filtered = df[df['Similarity'] >= min_threshold]
# If max_num_passages is specified, limit the number of passages returned
if max_num_passages is not None:
df_filtered = df_filtered.nlargest(max_num_passages, 'Similarity')
df_filtered = df_filtered.sort_values(by='Similarity', ascending=False)
# Return the filtered DataFrame
return df_filtered
def RAG_retrieval_Percentile(queryText, passages, percentile=90, max_num_passages=None, min_threshold=0.5):
# Encoding and similarity computation remains the same
similarities = retrievePassageSimilarities(queryText, passages)
# Determine threshold based on percentile
threshold = np.percentile(similarities.flatten(), percentile)
# Create a DataFrame
df = pd.DataFrame({
'Passage': passages,
'Similarity': similarities.flatten()
})
# Filter using percentile threshold
df_filtered = df[df['Similarity'] >= threshold]
if min_threshold:
# Filter the DataFrame also on min similarity threshold
df_filtered = df[df['Similarity'] >= min_threshold]
# If max_num_passages is specified, limit the number of passages returned
if max_num_passages is not None:
df_filtered = df_filtered.nlargest(max_num_passages, 'Similarity')
# Sort by similarity
df_filtered = df_filtered.sort_values(by='Similarity', ascending=False)
return df_filtered
def RAG_retrieval_TopK(queryText, passages, top_fraction=0.1, max_num_passages=None, min_threshold=0.5):
# Encoding and similarity computation remains the same
similarities = retrievePassageSimilarities(queryText, passages)
# Calculate the number of passages to select based on top fraction
num_passages_TopFraction = max(1, int(top_fraction * len(passages)))
# Create a DataFrame
df = pd.DataFrame({
'Passage': passages,
'Similarity': similarities.flatten()
})
# Select the top passages dynamically
df_filtered = df.nlargest(num_passages_TopFraction, 'Similarity')
if min_threshold:
# Filter the DataFrame also on min similarity threshold
df_filtered = df[df['Similarity'] >= min_threshold]
# If max_num_passages is specified, limit the number of passages returned
if max_num_passages is not None:
df_filtered = df_filtered.nlargest(max_num_passages, 'Similarity')
# Sort by similarity
df_filtered = df_filtered.sort_values(by='Similarity', ascending=False)
return df_filtered
# Define the transform_query function
def transform_query(queryText: str) -> str:
"""For retrieval, add the prompt for queryText (not for documents)."""
return f'Represent this sentence for searching relevant passages: {queryText}'
# Define the pooling function
def pooling(outputs: torch.Tensor, inputs: Dict, strategy: str = 'cls') -> np.ndarray:
if strategy == 'cls':
outputs = outputs[:, 0]
elif strategy == 'mean':
outputs = torch.sum(
outputs * inputs["attention_mask"][:, :, None], dim=1
) / torch.sum(inputs["attention_mask"], dim=1, keepdim=True)
else:
raise NotImplementedError
return outputs.detach().cpu().numpy()
def retrievePassageSimilarities(queryText, passages):
# Create the docs list by adding the transformed queryText and then the passages
docs = [transform_query(queryText)] + passages
# 2. Encode the inputs
inputs = tokenizer_Retriever(docs, padding=True, return_tensors='pt')
# Move inputs to the right device using accelerator
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = modelRetriever(**inputs).last_hidden_state
embeddings = pooling(outputs, inputs, 'cls')
similarities = cos_sim(embeddings[0], embeddings[1:])
# print('similarities:', similarities)
return similarities
def process_row_Gliner(args, tokenizerGliner, modelGlinerBio, modelGliner, glinerlabels, row):
context_to_annotate = row[args.source_column]
tokens = tokenizerGliner.tokenize(context_to_annotate)
entities = []
offset = 0
if "gliner_large_bio" in args.model_id:
max_chunk_length = modelGlinerBio.config.max_len
else:
max_chunk_length = modelGliner.config.max_len
for chunk in chunk_tokens(tokens, (max_chunk_length - 1)):
chunk_text = tokenizerGliner.convert_tokens_to_string(chunk)
if "gliner_large_bio" in args.model_id:
chunk_entities = modelGlinerBio.predict_entities(chunk_text, glinerlabels,
threshold=args.entities_filter_threshold)
else:
chunk_entities = modelGliner.predict_entities(chunk_text, glinerlabels,
threshold=args.entities_filter_threshold)
adjusted_entities = []
for entity in chunk_entities:
adjusted_entity = {
'text': entity['text'],
'score': entity['score'],
'start': entity['start'] + offset,
'end': entity['end'] + offset,
'label': entity['label']
}
adjusted_entities.append(adjusted_entity)
entities.extend(adjusted_entities)
offset += len(chunk_text)
if entities and isinstance(entities, list):
for d in entities:
d['entity_group'] = d.pop('label')
d['word'] = d.pop('text')
d['entity_group'] = d['entity_group'].upper()
return row.name, entities
def parallel_process_df_Gliner(args, df, tokenizerGliner, modelGlinerBio, modelGliner, glinerlabels):
results = []
if args.num_cores_Gliner > 0:
with ThreadPoolExecutor(max_workers=args.num_cores_Gliner) as executor:
futures = [
executor.submit(
process_row_Gliner, args, tokenizerGliner, modelGlinerBio, modelGliner, glinerlabels, row
)
for _, row in df.iterrows()
]
for future in tqdm(futures):
drm_idx, entities = future.result()
df.at[drm_idx, 'annotation'] = entities
else:
# Apply the function to each row and extract only the entities part
df['annotation'] = df.apply(
lambda row: process_row_Gliner(args, tokenizerGliner, modelGlinerBio, modelGliner, glinerlabels, row)[1],
axis=1
)
return df
def process_row_BioPortal_api(args, key_bioportal, row):
#context_to_annotate = row[args.source_column]
if isinstance(row, list) or isinstance(row, pd.Series):
context_to_annotate = row[args.source_column]
elif isinstance(row, str):
context_to_annotate = row
else:
raise ValueError("Unsupported type for row. Expected list or string.")
url=""
if getattr(args, 'KG_restriction', None):
# api call
if strtobool(args.debug):
print("--- BIOPORTAL: " + context_to_annotate)
# args.KG_restriction exists and is not empty
if strtobool(args.debug):
print("KG_restriction is provided and not empty:", args.KG_restriction)
onto_clauses = ""
for choice in args.KG_restriction:
if choice == "SNOMED":
choice="SNOMEDCT"
elif choice == "RO":
choice = "OBOREL"
elif choice == "TRANS":
choice = "PTRANS"
elif choice == "FoodOn":
choice = "FOODON"
elif choice == "GeoSPARQL":
choice = "GEOSPARQL"
# elif choice == "NCBITAXON":
# choice = "NCBITAXON,NCBITaxon_"
elif choice == "NCBITaxon_":
choice = "NCBITAXON"
if choice in ONLY_Ontologies_OnBIOPORTAL:
onto_clauses=onto_clauses+choice+","
if onto_clauses and onto_clauses[-1] == ",":
onto_clauses=onto_clauses[:-1]
url = f"https://services.data.bioontology.org/annotatorplus/?text={context_to_annotate}&ontologies={onto_clauses}&longest_only=true&exclude_numbers=true&whole_word_only=true&exclude_synonyms=false&negation=false&experiencer=false&temporality=false&score_threshold=0&confidence_threshold=0&display_links=false&display_context=false&score=cvalue&apikey={key_bioportal}"
else:
# args.KG_restriction does not exist or is empty
if strtobool(args.debug):
print("--- BIOPORTAL: " + context_to_annotate)
print("KG_restriction is not provided or empty - Consider all the KGs")
url = f"https://services.data.bioontology.org/annotatorplus/?text={context_to_annotate}&ontologies=AEO,BFO,BIM,BCGO,CL,CHIRO,CHEBI,DCM,FMA,GO,GENO,GEOSPARQL,HL7,DOID,HP,HP_O,IDO,IAO,ICD10,LOINC,MESH,MONDO,NCIT,NCBITAXON,NIFCELL,NIFSTD,GML,OBCS,OCHV,OHPI,OPB,PTRANS,PLOSTHES,RADLEX,OBOREL,STY,SO,SNOMEDCT,STATO,SYMP,FOODON,UBERON,VO&longest_only=true&exclude_numbers=true&whole_word_only=true&exclude_synonyms=false&negation=false&experiencer=false&temporality=false&score_threshold=0&confidence_threshold=0&display_links=false&display_context=false&score=cvalue&apikey={key_bioportal}"
response = requests.get(url)
try:
data = response.json()
if not data:
# nothing found from Bioportal
return pd.DataFrame()
dff = pd.DataFrame(data)
dff = dff.drop(columns=['hierarchy', 'mappings'])
# If the columns are dictionary-like, use pd.json_normalize:
expanded_annotated_class = pd.json_normalize(dff['annotatedClass'])
expanded_annotations = pd.DataFrame(dff['annotations'].tolist(), index=dff.index)
expanded_annotations = pd.json_normalize(expanded_annotations[0])
# Join the expanded columns back to the original DataFrame
df_expanded = dff.drop(columns=['annotatedClass', 'annotations']).join(expanded_annotated_class).join(
expanded_annotations)
# Snomed id replacement because in our internal knolwedgebase we have this base uri
df_expanded['@id'] = df_expanded['@id'].str.replace(
"http://purl.bioontology.org/ontology/SNOMEDCT/",
"http://snomed.info/id/"
)
return df_expanded
except Exception as err:
logging.error(
f'ERROR ON BioPortal Annotator API Call\n\tError: {err}\n TextToAnnotate: {context_to_annotate}\n Have a check...')
return pd.DataFrame() # empty dataframe
def annotate(df, args, pipeInner, tokenizerGliner, modelGliner, modelGlinerBio, device="cpu"):
if strtobool(args.debug):
print("\nAnnotate using " + args.model_id)
print("device=" + str(device))
startAnnotate = time.time()
if "gliner" in args.model_id:
df['model'] = args.model_id
df['annotation'] = None
glinerlabels = ["location", "disease", "date", "numerical value", "number"]
# Parallel CPU computation for Gliner:
df = parallel_process_df_Gliner(args, df, tokenizerGliner, modelGlinerBio, modelGliner, glinerlabels)
# for drm_idx, row in tqdm(df.iterrows()):
# context_to_annotate = row[args.source_column]
#
# # Tokenize the text
# tokens = tokenizerGliner.tokenize(context_to_annotate)
#
# # Process each chunk and predict entities
# entities = []
# offset = 0 # Initialize the offset
#
# if "gliner_large_bio" in args.model_id:
# maxchunckslen=modelGlinerBio.config.max_len
# else:
# maxchunckslen = modelGliner.config.max_len
# for chunk in chunk_tokens(tokens, (maxchunckslen - 1)):
# # Convert tokens back to text for the chunk
# chunk_text = tokenizerGliner.convert_tokens_to_string(chunk)
# # Predict entities for the chunk
# if "gliner_large_bio" in args.model_id:
# chunk_entities = modelGlinerBio.predict_entities(chunk_text, glinerlabels, threshold=args.entities_filter_threshold)
# else:
# chunk_entities = modelGliner.predict_entities(chunk_text, glinerlabels, threshold=args.entities_filter_threshold)
#
# # Adjust the start and end positions of entities to reflect their positions in the original text
# adjusted_entities = []
# for entity in chunk_entities:
# adjusted_entity = {
# 'text': entity['text'],
# 'score': entity['score'],
# 'start': entity['start'] + offset,
# 'end': entity['end'] + offset,
# 'label': entity['label']
# }
# adjusted_entities.append(adjusted_entity)
#
# # Append adjusted entities to all_entities
# entities.extend(adjusted_entities)
#
# # Update the offset for the next chunk by adding the length of the current chunk
# offset += len(chunk_text)
#
# # Now `all entities` contains all entities with adjusted positions
# if entities and isinstance(entities, list):
#
# # if strtobool(args.debug):
# # for entity in entities:
# # print(entity["text"], "=>", entity["label"])
#
# for d in entities:
# d['entity_group'] = d.pop('label') # Change 'label' to 'entity_group'
# d['word'] = d.pop('text') # Change 'text' to 'word'
# d['entity_group'] = d['entity_group'].upper()
#
# df.at[drm_idx, 'annotation'] = entities
df_annot = df.explode('annotation').dropna(subset=['annotation']).reset_index(drop=True)
elif "NCBO" in args.model_id: #NCBO/BioPortal" annotator
#https://data.bioontology.org/documentation#nav_annotator
#https://bioportal.bioontology.org/annotatorplus
#key_bioportal = ""
#if args.bioportalkey_filename:
# fkeyname = args.bioportalkey_filename
# with open(fkeyname) as f:
# key_bioportal = f.read()
key_bioportal = os.environ['key_bioportal']
df_annot = pd.DataFrame()
for drm_idx, row in tqdm(df.iterrows()):
df_BioPortalAnnotation=process_row_BioPortal_api(args, key_bioportal, row)
if not df_BioPortalAnnotation.empty:
df_BioPortalAnnotation = df_BioPortalAnnotation.sort_values(
by=['from', 'text', 'score', 'matchType'], ascending=[True, True, False, False])
df_biop_minimised = df_BioPortalAnnotation.copy()
# Group the dataframe by "from" and "to" columns
grouped_biop = df_biop_minimised.groupby(['from', 'to'])
# Get the index of the row with the maximum score in each group
idx_biop = grouped_biop['score'].idxmax()
# Filter the dataframe using these indices
df_max_score_biop = df_biop_minimised.loc[idx_biop]
# Optional: Reset index if you want a clean index
df_max_score_biop = df_max_score_biop.reset_index(drop=True)
# Create the "pippo" column by grouping and collecting "@id" values
pippo_lists = grouped_biop['@id'].apply(list).reset_index()
# Merge the lists back into the df_max_score_biop by 'from' and 'to'
df_max_score_biop = df_max_score_biop.merge(pippo_lists, on=['from', 'to'])
# Rename the merged column to "pippo"
df_max_score_biop = df_max_score_biop.rename(columns={'@id_x': '@id'})
df_max_score_biop = df_max_score_biop.rename(columns={'@id_y': 'ALLURIScontextFromNCBO'})
# The df_max_score_biop now contains the "pippo" column with lists of "@id" values.
# Filter the dataframe to keep only rows where the score is greater than 3.0
df_max_score_biop = df_max_score_biop[df_max_score_biop['score'] > 3.0]
if "semantic_groups" not in df_max_score_biop.columns:
# Drop the '@id' column
df_max_score_biop["semantic_groups"] = None
# Specify the columns you want to keep
columns_to_keep = ["score", "from", "to", "prefLabel", "text", "semantic_groups", "@id", "ALLURIScontextFromNCBO"]
# Subset the dataframe to keep only the specified columns
df_max_score_biop = df_max_score_biop[columns_to_keep]
# Rename the specified columns
df_max_score_biop = df_max_score_biop.rename(columns={"from": "start", "to": "end", "text": "word", "semantic_groups": "entity_group"})
# Optional: Reset index if you want a clean index after filtering
df_max_score_biop = df_max_score_biop.reset_index(drop=True)
df_max_score_biop['score'] = df_max_score_biop['score'].round(2)
# Each row of "semantic_groups" --> 'entity_group' is a list of string ... keep only the first part if not NaN
# df_max_score_biop['entity_group'] = df_max_score_biop['entity_group'].apply(lambda x: x[0] if pd.notna(x).all() else x)
df_max_score_biop['entity_group'] = df_max_score_biop['entity_group'].apply(lambda x: x[0] if isinstance(x, list) and len(x) > 0 else (np.nan if x is None or (isinstance(x, float) and pd.isna(x)) else x))
#df_max_score_biop = df_max_score_biop.dropna(subset=['entity_group'])
all_empty_or_nan_or_empty_string = df_max_score_biop['entity_group'].replace('', pd.NA).isna().all()
if not all_empty_or_nan_or_empty_string:
#print("Is the 'entity_group' column completely NaN, None, or empty strings?", all_empty_or_nan_or_empty_string)
# Identify the minimum score
min_score_biop = df_max_score_biop['score'].min()
# Apply filter to find rows where entity_group is None and score is the minimum one
conditionBiop = (df_max_score_biop['entity_group'].isna()) & (df_max_score_biop['score'] == min_score_biop)
df_max_score_biop = df_max_score_biop[~conditionBiop]
# Replace all NaN values in 'entity_group' with 'BIOP'
df_max_score_biop['entity_group'] = df_max_score_biop['entity_group'].fillna('BIOP')
# To delete the columns "start" and "end"
#df_max_score_biop = df_max_score_biop.drop(columns=['start', 'end', 'word', 'entity_group', 'score', 'prefLabel'])
if not df_max_score_biop.empty:
row_df = pd.DataFrame([row] * len(df_max_score_biop), columns=row.index)
row_df['model'] = args.model_id #'NCBO/BioPortal'
df_max_score_biop = pd.concat([row_df.reset_index(drop=True), df_max_score_biop.reset_index(drop=True)],
axis=1)
df_annot = pd.concat([df_annot, df_max_score_biop], ignore_index=True)
#here I finish the for cycle
if not df_annot.empty:
mmax_score = df_annot['score'].max()
mmin_score = df_annot['score'].min()
if mmax_score == mmin_score:
df_annot['score'] = 0.3
df_annot.loc[df_annot['score'].notnull(), 'score'] = 0.7
else:
# Apply the transformation function
#df_annot = rescale_exponential_to_linear(df_annot, 'score', new_min=0.5, new_max=1.0)
df_annot = rescale_exponential_to_logarithmic(df_annot, 'score', new_min=0.7, new_max=1.0)
columnsDict = ['start', 'end', 'word', 'entity_group', 'score', 'prefLabel']
df_annot['annotation'] = df_annot.apply(row_to_dict_string, axis=1, columnsDict=columnsDict)
# Convert JSON string to a dictionary, if needed
df_annot['annotation'] = df_annot['annotation'].apply(
lambda x: json.loads(x) if isinstance(x, str) else x
)
df_annot = df_annot.drop(columns=columnsDict)
else:
HF_dataset = datasets.Dataset.from_pandas(pd.DataFrame(data=df))
# tsk = "token-classification"
#
# pipe = pipeline(
# tsk,
# model=args.model_id,
# aggregation_strategy="simple",
# device=device,
# )
if strtobool(args.debug):
print('Annotating texts...')
annotated_texts = [
# out for out in tqdm(pipe(KeyDataset(HF_dataset, args.source_column), batch_size=args.batch_size))
# out for out in tqdm(ner_pipeline(KeyDataset(HF_dataset, args.source_column), batch_size=args.batch_size))
out for out in tqdm(pipeInner(KeyDataset(HF_dataset, args.source_column), batch_size=args.batch_size))
]
if strtobool(args.debug):
print('looping annotations...')
df['model'] = args.model_id
df['annotation'] = annotated_texts
df_annot = df.explode('annotation').dropna(subset=['annotation']).reset_index(drop=True)
# # Initialize an empty dataframe with the same columns as df
# df_annot = pd.DataFrame(columns=df.columns.tolist()) #+ ['model', 'annotation'])
#
# for drm_idx, txt_ents in enumerate(tqdm(annotated_texts)):
#
# filtered_entities = get_filtered_entities(txt_ents)
#
# for x_ent in filtered_entities:
# # Create a new row with the annotation
# new_row = df.iloc[drm_idx].copy()
# new_row['model'] = args.model_id
# new_row['annotation'] = x_ent
# df_annot = pd.concat([df_annot, new_row.to_frame().T], ignore_index=True) # Append the new row to the dataframe
if strtobool(args.debug):
endAnnotate = time.time()
hours, rem = divmod(endAnnotate - startAnnotate, 3600)
minutes, seconds = divmod(rem, 60)
print("...end annotation - Time... {:0>2}:{:0>2}:{:05.2f}\n".format(int(hours), int(minutes), seconds))
print('\n')
return df_annot
def default_serializer(obj):
if isinstance(obj, np.floating):
return float(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
def is_json(myjson):
try:
# json.loads(myjson, default=default_serializer)
json.dumps(myjson, default=default_serializer)
except ValueError as e:
return False
return True
def is_cross_inside(df_sorted, args, valuecutCross=0.75):
df_sorted['IsCrossInside'] = 0
df_sorted = df_sorted.reset_index(drop=True) # I reset the index so that I can loop on inner loop inside
# this works but it is absolutely too heavy!!! Got: mask1 = (SentenceRef_array[:, None] == SentenceRef_array) & \
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# numpy.core._exceptions._ArrayMemoryError: Unable to allocate 73.4 TiB for an array with shape (8984940, 8984940) and data type bool
#
# # Assuming df_sorted is your DataFrame
# df_sorted['IsCrossInside'] = 0
#
# # Convert necessary columns to numpy arrays
# SentenceRef_array = df_sorted['SentenceRef'].to_numpy()
# start_array = df_sorted['start'].to_numpy()
# end_array = df_sorted['end'].to_numpy()
# score_array = df_sorted['score'].to_numpy()
# ToLink_array = df_sorted['ToLink'].to_numpy()
#
# # Identify NaN or empty strings in ToLink array
# toLink_nan_or_empty = pd.isna(ToLink_array) | (ToLink_array == '')
#
# # Create the mask for the first set of conditions
# mask1 = (SentenceRef_array[:, None] == SentenceRef_array) & \
# (start_array[:, None] <= start_array) & \
# (end_array[:, None] > start_array) & \
# (end_array[:, None] <= end_array) & \
# (score_array[:, None] < score_array) & \
# toLink_nan_or_empty[:, None]
#
# # Create the mask for the second set of conditions
# mask2 = (SentenceRef_array[:, None] == SentenceRef_array) & \
# (end_array[:, None] >= end_array) & \
# (start_array[:, None] < end_array) & \
# (start_array[:, None] >= start_array) & \
# (score_array[:, None] < score_array) & \
# toLink_nan_or_empty[:, None]
#
# # Combine the masks
# mask = mask1 | mask2
#
# # Aggregate the masks and set 'IsCrossInside' where the condition is True
# df_sorted.loc[mask.any(axis=1), 'IsCrossInside'] = 1
# df_sorted['IsCrossInside'] = 0
#
# #questo for e' altamente inefficiente...guardare per alternative piu' ottimizzate!!!
# for index, row in df_sorted.iterrows():
#
# print(row)
#
# mask = (df_sorted['SentenceRef'] == row['SentenceRef']) & (df_sorted['start'] <= row['start']) & (df_sorted['end'] > row['start']) & (
# df_sorted['end'] <= row['end']) & (df_sorted['score'] < row['score']) & (df_sorted['ToLink'].isnull() | (df_sorted['ToLink'] == ''))
# mask |= (df_sorted['SentenceRef'] == row['SentenceRef']) & (df_sorted['end'] >= row['end']) & (df_sorted['start'] < row['end']) & (
# df_sorted['start'] >= row['start']) & (df_sorted['score'] < row['score']) & (df_sorted['ToLink'].isnull() | (df_sorted['ToLink'] == ''))
#
# df_sorted.loc[mask, 'IsCrossInside'] = 1
# Iterate over the DataFrame with an outer loop - I know that df_sorted is ordered by 'SentenceRef' and 'start'
indexes_list = []
scores_list = []
IsToLinkContained = []
for i, row_outer in tqdm(df_sorted.iterrows()):
#print(row_outer)
# if (i>0) and (df_sorted.iloc[i-1]['SentenceRef'] == row_outer['SentenceRef']): # if the previous row is of the same SenteceRef, it has been alredy computed and I skip
# continue
if len(indexes_list)==0:
scores_list.append(row_outer['score'])
indexes_list.append(i)
if (pd.isnull(row_outer['ToLink']==False) and len(row_outer['ToLink'])>0):
IsToLinkContained.append(True)
else:
IsToLinkContained.append(False)
# if (pd.isnull(row_outer['ToLink']) or row_outer['ToLink'] == ''):
# IsToLinkContained.append(False)
# elif len(row_outer['ToLink'])>0:
# IsToLinkContained.append(True)
else:
if i in indexes_list:
if (i == indexes_list[-1]):
# indexes_list = []
# scores_list = []
# IsToLinkContained = []
indexes_list = [indexes_list[-1]]
scores_list = [scores_list[-1]]
IsToLinkContained = [IsToLinkContained[-1]]
else:
continue
# Inner loop only needs to consider rows starting from the current position
for j in range(i + 1, len(df_sorted)):
#for j in range(0, len(df_sorted)):
# if i==j: continue
#print(j)
row_inner = df_sorted.iloc[j]
# Break the inner loop if SentenceRef changes (due to sorting)
if row_inner['SentenceRef'] != row_outer['SentenceRef']:
break
elif row_inner['start'] >= row_outer['end']:
break
else:
scores_list.append(row_inner['score'])
indexes_list.append(j)
if (pd.isnull(row_inner['ToLink'] == False) and len(row_inner['ToLink']) > 0):
IsToLinkContained.append(True)
else:
IsToLinkContained.append(False)
if len(indexes_list)>1:
first_true_index = -1
try:
first_true_index = IsToLinkContained.index(True)
#print(f"The first index with value True is: {first_true_index}")
except ValueError:
first_true_index = -1
#print("No True value found in the list")
topinlist=-1
if first_true_index >=0:
topinlist = first_true_index
else:
topinlist = scores_list.index(max(scores_list))
#print(f"The position with the maximum score is: {topinlist}")
if topinlist >= 0:
for xx in range(0, len(indexes_list)):
if xx == topinlist:
continue
df_sorted.at[indexes_list[xx], 'IsCrossInside'] = 1
else:
indexes_list = []
scores_list = []
IsToLinkContained = []
#Now I want to delete all the words that are contained within another . For example I want to drop "osis" from Legionellosis
if not df_sorted.empty:
# punctuation_and_space = set(string.punctuation + ' ')
# condition = df_sorted.apply(lambda row:
# (row['IsCrossInside'] == 0) or
# ((row['IsCrossInside'] == 1) and
# ((row[args.source_column][row['start'] - 1] in punctuation_and_space) if row[
# 'start'] - 1 >= 0 else True) and
# ((row[args.source_column][row['end']] in punctuation_and_space) if row['end'] <= len(
# row[args.source_column]) else True)),
# axis=1)
condition = df_sorted.apply(lambda row:
(row['IsCrossInside'] == 0) or
( (row['IsCrossInside'] == 1) and (row['score'] >=valuecutCross )),
axis=1)
# Filter the DataFrame to keep only rows where the condition is False
df_sorted = df_sorted[condition]
return df_sorted
def entitiesFusion(df_annotated, args):
if strtobool(args.debug):
print("\nStart entities fusion and filtering ...")
areJson = df_annotated["annotation"].apply(is_json)
if False in areJson.unique():
for idxr, rr in df_annotated.iterrows():
# for idxr in range(df_annotated["annotation"].shape[0]):
if areJson[idxr] == False:
print("PROBLEM WITH JSON AT INDEX " + str(idxr) + ":\n" + df_annotated["annotation"][idxr])
replacement_empty_myjson = '{\"entity_group\": \"\", \"score\": \"\", "word": \"\", \"start\": \"\", \"end\": \"\"}' # {'entity_group': 'ORG', 'score': 0.9999951, 'word': 'Health Ministry', 'start': 4, 'end': 19}
df_annotated.at[idxr, "annotation"] = replacement_empty_myjson
print(" ...... Then replacing it with empty JSON --> " + df_annotated["annotation"][idxr])
try:
# df_extract = df_annotated.apply(lambda x: pd.Series(
# json.loads(x['annotation'], default=default_serializer).values(),
# index=json.loads(x['annotation'], default=default_serializer).keys()), axis=1)
df_extract = df_annotated.apply(lambda x: pd.Series(x['annotation'].values(),
index=x['annotation'].keys()), axis=1)
# Check if '@id' column exists in df_Extract
if '@id' in df_extract.columns:
# Drop the '@id' column
df_extract = df_extract.drop(columns='@id')
df_annotated = pd.merge(df_annotated, df_extract, left_index=True, right_index=True)
except Exception as err:
logging.error(
f'FAILED to extract json results\n\tError: {err}\nLeaving it as a single column then and not decompressing! Have a check...')
#
# Delete all the rows where EXACT MATCHING NOT MET:
# Apply the conditions
condition_to_delete = (
df_annotated[args.source_column].str.startswith('"') &
df_annotated[args.source_column].str.endswith('"') &
(df_annotated[args.source_column].apply(strip_quotes).str.lower() != df_annotated['word'].str.lower())
)
# Now Filter out the rows where condition_to_delete is True
df_annotated = df_annotated[~condition_to_delete].copy()
#
#delete all the rows with score smaller than entities_filter_threshold:
if args.entities_filter_threshold > 0:
# df_annotated = df_annotated[df_annotated['score'] >= args.entities_filter_threshold]
df_annotated = df_annotated[df_annotated['score'] > args.entities_filter_threshold]
if df_annotated.empty:
return df_annotated
# #delete all the rows where the concept that was asked to link, in the column "ToLink", is present and it is different from the column "word", that was recognised
# df_annotated = df_annotated[(df_annotated['ToLink'] == df_annotated['word']) | df_annotated['ToLink'].isna()]
# in all the rows having a value not null for the column "ToLink", compare this value to that of the column "word". If they are different, set the value in "ToLink" to None
# df_annotated.loc[
# (~df_annotated['ToLink'].isnull()) & (df_annotated['ToLink'] != df_annotated['word']), 'ToLink'] = None
df_annotated.loc[
(~df_annotated['ToLink'].isnull()) & (
df_annotated['ToLink'].str.casefold() != df_annotated['word'].str.casefold()), 'ToLink'] = None
# now fill all the values of the column "toLink" that are empty with the values of the row "word":
# df_annotated['ToLink'] = df_annotated['ToLink'].fillna(df_annotated['word'])
if "IsGeo" not in df_annotated.columns:
#df_annotated["IsGeo"] = None # 0
df_annotated.loc[:, "IsGeo"] = None
if "IsBio" not in df_annotated.columns:
#df_annotated["IsBio"] = None # 0
df_annotated.loc[:, "IsBio"] = None
df_annotated.loc[df_annotated['entity_group'] == 'LOCATION', 'entity_group'] = "LOC"
df_annotated.loc[df_annotated['entity_group'] == 'LOC', 'IsGeo'] = 1
#df_annotated.loc[df_annotated['entity_group'] == 'DISEASE', 'IsBio'] = 1
df_annotated.loc[df_annotated['entity_group'].str.lower().str.contains('disease'), 'IsBio'] = 1
df_annotated.loc[(df_annotated['model'].str.contains('Medical-NER')) & (
df_annotated['entity_group'].isin(['LOC', 'DATE', 'PER', 'ORG', 'DOSAGE', 'LAB_VALUE', 'DURATION']) == False), 'IsBio'] = 1
df_annotated.loc[(df_annotated['model'].str.contains('NCBO')) & (
df_annotated['entity_group'].isin(['CONC']) == False), 'IsBio'] = 1
# !!! THIS CHECK HAS BEEN PLACED HERE BECAUSE NCBO Bioportal put Start+1 !!! SO I NEED TO DROP A ONE TO DROP DUPLICATES AND MAX SCORES
df_annotated.loc[df_annotated['model'].str.lower().str.contains('ncbo'), 'start'] -= 1
# !!! THIS CHECK HAS BEEN PLACED HERE BECAUSE MEDICAL-NER PUT A VALUE OF START WHICH IS THE REAL ONE MINUS ONE, IN THE CASE THAT THE IDENTIFIED ENTITY STARTS WITH A SPACE!!! SO I NEED TO ADD A ONE TO DROP DUPLICATES AND MAX SCORES
df_annotated.loc[(df_annotated['model'] == 'blaze999/Medical-NER') &
df_annotated.apply(lambda row: row[args.source_column][row['start']] == ' ',
axis=1), 'start'] += 1
# !!! THIS CHECK HAS BEEN PLACED HERE BECAUSE GLINER MODELS PUT A VALUE OF START and of END WHICH IS THE REAL ONE MINUS ONE, IN THE CASE THAT THE IDENTIFIED ENTITY STARTS WITH A SPACE!!! SO I NEED TO ADD A ONE to START and END TO DROP DUPLICATES AND MAX SCORES
df_annotated.loc[df_annotated['model'].str.lower().str.contains('gliner') &
df_annotated.apply(lambda row: row[args.source_column][row['start']] == ' ',
axis=1), 'end'] += 1
df_annotated.loc[df_annotated['model'].str.lower().str.contains('gliner') &
df_annotated.apply(lambda row: row[args.source_column][row['start']] == ' ',
axis=1), 'start'] += 1
#### Here I drop all the identified NER words which are strictly contained in other words
# Apply first the function to the "args.source_column" to create lists of words
df_annotated['extracted_words'] = df_annotated[args.source_column].apply(extract_words,putInLower=True)
# Filter the DataFrame
df_annotated = df_annotated[df_annotated.apply(lambda row: all_words_in_list(row['word'], row['extracted_words'], putInLower=True), axis=1)]
# drop the 'extracted_words' column afterwards:
df_annotated = df_annotated.drop(columns=['extracted_words'])
#####
# This operation sorts the DataFrame by the "ToLink" column in descending order, with null values at the end, and then uses the drop_duplicates method to drop all duplicate rows,
# except the first one, based on all columns except "ToLink".
# This way, it will keep the rows with the non-null value in "ToLink" if there are multiple rows with the same values in all columns except "ToLink".
df_annotated = df_annotated.sort_values(by='ToLink', ascending=False, na_position='last')
for col in df_annotated.columns:
if df_annotated[col].apply(lambda x: isinstance(x, dict)).any():
if strtobool(args.debug):
print(
f"Column '{col}' contains dictionaries...converting it to strings otherwise it will not work the concat etc..")
df_annotated[col] = df_annotated[col].apply(lambda x: str(x))
df_annotated = df_annotated.drop_duplicates(subset=[col for col in df_annotated.columns if
col != 'ToLink' and col != 'ALLURIScontextFromNCBO' and not df_annotated[col].apply(
lambda x: isinstance(x, dict)).any()], keep='first')
# df_annotated = df_annotated.loc[df_annotated.groupby(['SentenceRef', 'ToLink', args.source_column, 'end', 'start', df_annotated['word'].str.lower()])['score'].idxmax()]
# df_annotated = df_annotated.loc[df_annotated.groupby(['SentenceRef', args.source_column, 'end', 'start', df_annotated['word'].str.lower()])['score'].idxmax()]
df_annotated_Geo = df_annotated.loc[df_annotated.groupby(
['SentenceRef', args.source_column, 'end', 'start', df_annotated['word'].str.lower(), 'IsGeo'])[
'score'].idxmax()]
df_annotated_Bio = df_annotated.loc[df_annotated.groupby(
['SentenceRef', args.source_column, 'end', 'start', df_annotated['word'].str.lower(), 'IsBio'])[
'score'].idxmax()]
df_annotated_all = df_annotated.loc[
df_annotated.groupby(['SentenceRef', args.source_column, 'end', 'start', df_annotated['word'].str.lower()])[
'score'].idxmax()]
# now you can concat
df_annotated_combined = pd.concat([df_annotated_Geo, df_annotated_Bio, df_annotated_all])
df_annotated_combined = df_annotated_combined.drop_duplicates(subset=[col for col in df_annotated_combined.columns if
col != 'ToLink' and col != 'ALLURIScontextFromNCBO' and not df_annotated_combined[col].apply(
lambda x: isinstance(x, dict)).any()], keep='first')
# df_annotated_combined['IsBioGeo'] = df_annotated_combined['IsGeo'].fillna(0) + df_annotated_combined['IsBio'].fillna(0)
# df_annotated_combined['IsBioGeo'] = pd.to_numeric(df_annotated_combined['IsGeo'], errors='coerce').fillna(0) + pd.to_numeric(df_annotated_combined['IsBio'], errors='coerce').fillna(0)
# df_annotated_combined['IsBioGeo'] = df_annotated_combined['IsGeo'].infer_objects(copy=False).fillna(0) + df_annotated_combined['IsBio'].infer_objects(copy=False).fillna(0)
df_annotated_combined.loc[:, "IsBioGeo"] = df_annotated_combined.loc[:, 'IsGeo'].infer_objects(copy=False).fillna(0) + df_annotated_combined.loc[:, 'IsBio'].infer_objects(copy=False).fillna(0)
df_annotated_combined = df_annotated_combined.loc[df_annotated_combined.groupby(
['SentenceRef', args.source_column, 'end', 'start', df_annotated['word'].str.lower()])['IsBioGeo'].idxmax()]
df_annotated_combined = df_annotated_combined.loc[
df_annotated_combined.groupby(
['SentenceRef', args.source_column, 'end', 'start', df_annotated['word'].str.lower(), 'IsBioGeo'])[
'score'].idxmax()]
df_annotated_combined = df_annotated_combined.drop('IsBioGeo', axis=1)
df_annotated_combined.loc[df_annotated_combined['IsBio'] == 0, 'IsBio'] = None
df_annotated_combined.loc[df_annotated_combined['IsGeo'] == 0, 'IsGeo'] = None
df_annotated_combined = df_annotated_combined.sort_values(by=['SentenceRef', 'start', 'ToLink', 'word', 'score'], ascending=[True, True, True, True, False])
#df_annotated_combined = df_annotated_combined.reindex(range(len(df_annotated_combined)))
return df_annotated_combined
def geonames_api_call(word, args, key_geonames, cache_map_geonames):
context = ""
singleContext = None
globalContext = None
singleTriples = None
globalTriples = None
if cache_map_geonames is not None:
if word in cache_map_geonames:
if context in cache_map_geonames[word]:
url_text = cache_map_geonames[word][context]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", word, " => ", url_text, "\n")
return url_text, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames
# url = f"http://api.geonames.org/search?name_equals={word}&maxRows=1&featureClass=A&featureCode=ADM1&featureCode=ADM2&featureCode=ADM3&featureCode=ADM4&featureCode=ADM5&type=json&username={key_geonames}"
url = f"http://api.geonames.org/search?name_equals={word}&maxRows=1&type=json&username={key_geonames}"
response = requests.get(url)
try:
data = response.json()
if data['geonames']:
# geoname = data['geonames'][0]['name']
geonameId = data['geonames'][0]['geonameId']
geonameUrl = "https://sws.geonames.org/" + str(geonameId) + "/"
if cache_map_geonames is not None:
if not word in cache_map_geonames:
cache_map_geonames[word] = {}
cache_map_geonames[word][context] = geonameUrl
return geonameUrl, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames
else:
if cache_map_geonames is not None:
if not word in cache_map_geonames:
cache_map_geonames[word] = {}
cache_map_geonames[word][context] = None
return None, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames
except Exception as err:
# if cache_map_geonames is not None:
# if not word in cache_map_geonames:
# cache_map_geonames[word] = {}
# cache_map_geonames[word][context] = None
return None, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames
def getUrlBioAndAllOtherBioConcepts(word, args, key_virtuoso, cache_map_virtuoso, endpoint, VirtuosoUsername, contextWordVirtuoso, UseBioportalForLinking=False, questionText="" ):
#UseBioportalForLinking = False #trial to del
if strtobool(args.debug):
print("--- start getUrlBioAndAllOtherBioConcepts for " + word.lower())
entityBioeUrl = None
ALLURIScontext = []
#key_bioportal = ""
#if args.bioportalkey_filename:
# fkeyname = args.bioportalkey_filename
# with open(fkeyname) as f:
# key_bioportal = f.read()
key_bioportal = os.environ['key_bioportal']
# Check if args.KG_restriction exists and is not empty
if getattr(args, 'KG_restriction', None):
# api call
if strtobool(args.debug):
print("--- " + word.lower())
# args.KG_restriction exists and is not empty
if strtobool(args.debug):
print("KG_restriction is provided and not empty:", args.KG_restriction)
from_clauses = ' '.join([f"FROM <{choice}>" for choice in args.KG_restriction])
# Construct the full SPARQL query
query = f"""
prefix skosxl: <http://www.w3.org/2008/05/skos-xl#>
SELECT ?concept ?label (COUNT(?edge) AS ?score)
{from_clauses}
WHERE {{
?concept skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?label .
FILTER (LCASE(STR(?label)) = "{word.lower()}")
?concept ?edge ?o .
}}
GROUP BY ?concept ?label
ORDER BY DESC(?score)
"""
### this is for Bioportal url api:
onto_clauses = ""
# for choice in args.KG_restriction:
# if choice == "SNOMEDCT":
# choice = "SNOMED"
# elif choice == "OBOREL":
# choice = "RO"
# elif choice == "PTRANS":
# choice = "TRANS"
# elif choice == "FOODON":
# choice = "FoodOn"
# elif choice == "GEOSPARQL":
# choice = "GeoSPARQL"
# elif choice == "NCBITAXON":
# choice = "NCBITAXON,NCBITaxon_"
# onto_clauses = onto_clauses + choice + ","
for choice in args.KG_restriction:
if choice == "SNOMED":
choice="SNOMEDCT"
elif choice == "RO":
choice = "OBOREL"
elif choice == "TRANS":
choice = "PTRANS"
elif choice == "FoodOn":
choice = "FOODON"
elif choice == "GeoSPARQL":
choice = "GEOSPARQL"
# elif choice == "NCBITAXON":
# choice = "NCBITAXON,NCBITaxon_"
elif choice == "NCBITaxon_":
choice = "NCBITAXON"
if choice in ONLY_Ontologies_OnBIOPORTAL:
onto_clauses=onto_clauses+choice+","
if onto_clauses and onto_clauses[-1] == ",":
onto_clauses = onto_clauses[:-1]
url = f"https://services.data.bioontology.org/annotatorplus/?text={word.lower()}&ontologies={onto_clauses}&longest_only=true&exclude_numbers=true&whole_word_only=true&exclude_synonyms=false&negation=false&experiencer=false&temporality=false&score_threshold=0&confidence_threshold=0&display_links=false&display_context=false&score=cvalue&apikey={key_bioportal}"
else:
# args.KG_restriction does not exist or is empty
if strtobool(args.debug):
print("--- " + word.lower())
print("KG_restriction is not provided or empty - Consider all the KGs in the virtuoso endpoint")
query = f"""
prefix skosxl: <http://www.w3.org/2008/05/skos-xl#>
SELECT ?concept ?label (COUNT(?edge) AS ?score)
WHERE {{
?concept skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?label .
FILTER (LCASE(STR(?label)) = "{word.lower()}")
?concept ?edge ?o .
}}
GROUP BY ?concept ?label
ORDER BY DESC(?score)
"""
### this is for Bioportal url api:
url = f"https://services.data.bioontology.org/annotatorplus/?text={word.lower()}&ontologies=AEO,BFO,BIM,BCGO,CL,CHIRO,CHEBI,DCM,FMA,GO,GENO,GEOSPARQL,HL7,DOID,HP,HP_O,IDO,IAO,ICD10,LOINC,MESH,MONDO,NCIT,NCBITAXON,NIFCELL,NIFSTD,GML,OBCS,OCHV,OHPI,OPB,PTRANS,PLOSTHES,RADLEX,OBOREL,STY,SO,SNOMEDCT,STATO,SYMP,FOODON,UBERON,VO&longest_only=true&exclude_numbers=true&whole_word_only=true&exclude_synonyms=false&negation=false&experiencer=false&temporality=false&score_threshold=0&confidence_threshold=0&display_links=false&display_context=false&score=cvalue&apikey={key_bioportal}"
try:
if UseBioportalForLinking == False:
if strtobool(args.debug):
print("Use Virtuoso Sparql endpoint for linking ... " + word.lower())
responseText = sparqlQuery(endpoint, questionText, VirtuosoUsername, key_virtuoso, strtobool(args.USE_CACHE))
# Parse the response as JSON
results = json.loads(responseText)
if len(results) > 0 and results['results']['bindings']:
entityBioeUrl = str(results['results']['bindings'][0]['concept']['value'])
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][contextWordVirtuoso] = entityBioeUrl
# # loop the results
for result in results['results']['bindings']:
# print(result)
contextConcept = result['concept']['value']
if contextConcept not in ALLURIScontext:
ALLURIScontext.append(contextConcept)
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][contextConcept] = None
if ALLURIScontext and isinstance(ALLURIScontext, list):
ALLURIScontext = list(set(ALLURIScontext))
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word]['ALLURIScontext'] = ALLURIScontext
else:
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][contextWordVirtuoso] = None
cache_map_virtuoso[word]['ALLURIScontext'] = []
else: #this is instead using Bioportal API for linking
if strtobool(args.debug):
print("Use Bioportal for linking ... " + word.lower())
response = requests.get(url)
try:
data = response.json()
if not data:
# nothing found from Bioportal
return None, None, None, None, None, cache_map_virtuoso
dff = pd.DataFrame(data)
dff = dff.drop(columns=['hierarchy', 'mappings'])
# If the columns are dictionary-like, use pd.json_normalize:
expanded_annotated_class = pd.json_normalize(dff['annotatedClass'])
expanded_annotations = pd.DataFrame(dff['annotations'].tolist(), index=dff.index)
expanded_annotations = pd.json_normalize(expanded_annotations[0])
# Join the expanded columns back to the original DataFrame
df_expanded = dff.drop(columns=['annotatedClass', 'annotations']).join(expanded_annotated_class).join(
expanded_annotations)
# Snomed id replacement because in our internal knolwedgebase we have this base uri
df_expanded['@id'] = df_expanded['@id'].str.replace(
"http://purl.bioontology.org/ontology/SNOMEDCT/",
"http://snomed.info/id/"
)
if not df_expanded.empty:
df_expanded = df_expanded.sort_values(
by=['from', 'text', 'score', 'matchType'], ascending=[True, True, False, False])
df_expanded = df_expanded.drop_duplicates(subset=['@id'])
# Filter rows where 'prefLabel' is exactly equal to 'word.lower()' or 'word.lower()' is in 'synonym'
# filtered_df = df_expanded[
# df_expanded['prefLabel'].str.lower() == word.lower() |
# df_expanded['synonym'].apply(
# lambda x: True if isinstance(x, list) and len(x) > 0 and word.lower() in [item.lower() for
# item in x] else (
# np.nan if x is None or (isinstance(x, float) and pd.isna(x)) else x))
# ]
df_expanded = df_expanded[
df_expanded['prefLabel'].apply(
lambda x: isinstance(x, str) and x.lower() == word.lower()
) |
df_expanded['synonym'].apply(
lambda x: isinstance(x, list) and any(item.lower() == word.lower() for item in x)
)
]
if df_expanded.empty:
# nothing found from Bioportal
return None, None, None, None, None, cache_map_virtuoso
# Specify the columns you want to keep
columns_to_keep = ["score", "from", "to", "prefLabel", "text", "@id"]
# Subset the dataframe to keep only the specified columns
df_expanded = df_expanded[columns_to_keep]
# Rename the specified columns
df_expanded = df_expanded.rename(
columns={"from": "start", "to": "end", "text": "word"})
# Optional: Reset index if you want a clean index after filtering
df_expanded = df_expanded.reset_index(drop=True)
df_expanded['score'] = df_expanded['score'].round(2)
# Find the index of the row with the maximum 'score'
max_score_index = df_expanded['score'].idxmax()
max_score_row = df_expanded.loc[df_expanded['score'].idxmax()]
entityBioeUrl = str(max_score_row['@id'])
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][contextWordVirtuoso] = entityBioeUrl
# Drop the row with the maximum 'score'
#df_expanded = df_expanded.drop(max_score_index)
# Reset the index if desired (optional)
df_expanded.reset_index(drop=True, inplace=True)
# Iterate over each row in the DataFrame
for index, row in df_expanded.iterrows():
# Append the '@id' value to the list
if row['@id'] is not None and pd.notna(row['@id']):
contextConcept=row['@id']
ALLURIScontext.append(contextConcept)
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][contextConcept] = None
if ALLURIScontext and isinstance(ALLURIScontext, list):
ALLURIScontext = list(set(ALLURIScontext))
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word]['ALLURIScontext'] = ALLURIScontext
return entityBioeUrl, ALLURIScontext, cache_map_virtuoso
else:
#nothing found from Bioportal
return None, None, None, None, None, cache_map_virtuoso
except Exception as err:
logging.error(
f'ERROR ON BioPortal Annotator API Call\n\tError: {err}\n TextToAnnotate: {word.lower()}\n Have a check...')
return None, None, None, None, None, cache_map_virtuoso
except Exception as err:
# if cache_map_virtuoso is not None:
# if not word in cache_map_virtuoso:
# cache_map_virtuoso[word] = {}
# cache_map_virtuoso[word][contextWordVirtuoso] = None
return None, None, None, None, None, cache_map_virtuoso
return entityBioeUrl, ALLURIScontext, cache_map_virtuoso
def getLinearTextualContextFromTriples(word,labelTriplesLIST, text_splitter, args, map_query_input_output, cleanInput=True, questionText=""):
# trial
#return None, map_query_input_output
word = word.lower()
word = word.capitalize()
if (strtobool(args.UseRetrieverForContextCreation)==True):
labelTriples = ""
passages = []
nn = 200
if len(labelTriplesLIST)<=nn:
passages = []
for i, triple in enumerate(labelTriplesLIST, start=1):
# for triple in labelTriplesLIST:
TriplesString = (" ".join(str(element).capitalize() for element in triple))
passages.append(TriplesString)
df_retrieved = RAG_retrieval_TopK(questionText, passages, top_fraction=0.1, max_num_passages=20,
min_threshold=0.7)
if not df_retrieved.empty:
#labelTriplesLIST_RAGGED = df_retrieved.to_records(index=False).tolist()
labelTriplesLIST_RAGGED = df_retrieved['Passage'].apply(lambda x: (x,)).tolist()
labelTriplesAPP = ". ".join(
" ".join(str(element).capitalize() for element in triple) for triple in labelTriplesLIST_RAGGED)
if not labelTriples:
labelTriples = labelTriplesAPP
else:
labelTriples = labelTriples + ". " + labelTriplesAPP
else:
OverallListRAGtriples = labelTriplesLIST.copy()
while len(OverallListRAGtriples)>nn:
Oinnerlistiterative=[]
for i, triple in enumerate(OverallListRAGtriples, start=1):
# for triple in labelTriplesLIST:
TriplesString = (" ".join(str(element).capitalize() for element in triple))
passages.append(TriplesString)
# Check if the current index is a multiple of nn
if i % nn == 0:
# print("elaborate RAG triples")
# df_retrieved_Base = RAG_retrieval_Base(questionText, passages, min_threshold=0.7, max_num_passages=20)
# df_retrievedZscore = RAG_retrieval_Z_scores(questionText, passages, z_threshold=1.0, max_num_passages=20, min_threshold=0.7)
# df_retrievedPercentile = RAG_retrieval_Percentile(questionText, passages, percentile=90, max_num_passages=20, min_threshold=0.7)
df_retrievedtopk = RAG_retrieval_TopK(questionText, passages, top_fraction=0.1, max_num_passages=20,
min_threshold=0.7)
passages = []
df_retrieved = df_retrievedtopk.copy()
if not df_retrieved.empty:
#labelTriplesLIST_RAGGED = df_retrieved.to_records(index=False).tolist()
labelTriplesLIST_RAGGED = df_retrieved['Passage'].apply(lambda x: (x,)).tolist()
if not Oinnerlistiterative:
Oinnerlistiterative=labelTriplesLIST_RAGGED
else:
Oinnerlistiterative.extend(labelTriplesLIST_RAGGED)
if passages:
df_retrievedtopk = RAG_retrieval_TopK(questionText, passages, top_fraction=0.1, max_num_passages=20,
min_threshold=0.7)
df_retrieved = df_retrievedtopk.copy()
if not df_retrieved.empty:
#labelTriplesLIST_RAGGED = df_retrieved.to_records(index=False).tolist()
labelTriplesLIST_RAGGED = df_retrieved['Passage'].apply(lambda x: (x,)).tolist()
if not Oinnerlistiterative:
Oinnerlistiterative = labelTriplesLIST_RAGGED
else:
Oinnerlistiterative.extend(labelTriplesLIST_RAGGED)
OverallListRAGtriples = Oinnerlistiterative.copy()
if OverallListRAGtriples:
labelTriplesAPP = ". ".join(" ".join(str(element).capitalize() for element in triple) for triple in OverallListRAGtriples)
if not labelTriples:
labelTriples = labelTriplesAPP
else:
labelTriples = labelTriples + ". " + labelTriplesAPP
labelTriples = labelTriples.strip().replace("..", ".").strip()
# labelTriples = ""
# passages = []
# nn=200
# for i, triple in enumerate(labelTriplesLIST, start=1):
# #for triple in labelTriplesLIST:
# TriplesString = (" ".join(str(element).capitalize() for element in triple))
# passages.append(TriplesString)
# # Check if the current index is a multiple of nn
# if i % nn == 0:
# #print("elaborate RAG triples")
#
# #df_retrieved_Base = RAG_retrieval_Base(questionText, passages, min_threshold=0.7, max_num_passages=20)
# #df_retrievedZscore = RAG_retrieval_Z_scores(questionText, passages, z_threshold=1.0, max_num_passages=20, min_threshold=0.7)
# #df_retrievedPercentile = RAG_retrieval_Percentile(questionText, passages, percentile=90, max_num_passages=20, min_threshold=0.7)
# df_retrievedtopk = RAG_retrieval_TopK(questionText, passages, top_fraction=0.1, max_num_passages=20, min_threshold=0.7)
#
# passages = []
#
# df_retrieved = df_retrievedtopk.copy()
# if not df_retrieved.empty:
# #labelTriplesLIST_RAGGED = df_retrieved.to_records(index=False).tolist()
# labelTriplesLIST_RAGGED = df_retrieved['Passage'].apply(lambda x: (x,)).tolist()
# labelTriplesAPP = ". ".join(" ".join(str(element).capitalize() for element in triple) for triple in labelTriplesLIST_RAGGED)
#
# if not labelTriples:
# labelTriples =labelTriplesAPP
# else:
# labelTriples = labelTriples + ". " + labelTriplesAPP
#
# if passages:
# df_retrievedtopk = RAG_retrieval_TopK(questionText, passages, top_fraction=0.1, max_num_passages=20, min_threshold=0.7)
#
# df_retrieved = df_retrievedtopk.copy()
# if not df_retrieved.empty:
# #labelTriplesLIST_RAGGED = df_retrieved.to_records(index=False).tolist()
# labelTriplesLIST_RAGGED = df_retrieved['Passage'].apply(lambda x: (x,)).tolist()
# labelTriplesAPP = ". ".join(" ".join(str(element).capitalize() for element in triple) for triple in labelTriplesLIST_RAGGED)
# if not labelTriples:
# labelTriples = labelTriplesAPP
# else:
# labelTriples = labelTriples + ". " + labelTriplesAPP
#
# if labelTriples:
# labelTriples.strip().replace("..",".").strip()
else: # NO RAG on triples
labelTriples = ". ".join(" ".join(str(element).capitalize() for element in triple) for triple in labelTriplesLIST)
if not(labelTriples) or labelTriples.strip=="":
logging.warning("getLinearTextualContextFromTriples - No text or prompt supplied! No relevant contextual triples retrieved...Skypping it! Word: "+str(word))
return "", map_query_input_output
if token_counter(labelTriples, args.model_name) > args.tokens_max: # THE CONTEXT IS TOO BIG, BIGGER THAN tokens_max, I need to split
texts = text_splitter.create_documents([labelTriples])
labelTriples = texts[0].page_content
if not (labelTriples) or labelTriples.strip == "":
logging.warning("after splitting ...No text or prompt supplied! Skypping it! Word: "+str(word))
return "", map_query_input_output
contextText = ""
if (strtobool(args.UseRetrieverForContextCreation) == True):
contextText = labelTriples
else: #USE the LLM for summarise the triples
# Can you elaborate and express better the following notes, delimited by triple backticks, about "{word}"?
# Don't add explanations for your answer. Do not invent. Don't use a structure or indenting. Be concise. Don't discard relevant information.
# made of RDF-like statements,
# myPromt = f"""
# Can you elaborate and express better the given notes below, delimited by triple backticks, about "{word}"?
# Don't add explanations for your answer.
# Do not invent.
# Don't use a structure or indenting.
# Be concise but exhaustive. Don't discard information reported in the notes.
# """
myPromt = f"""
Can you reformulate the following notes, provided between triple backticks, into clear and complete sentences about "{word}"?
Ensure the rewriting is human-readable and easily interpretable. Maintain conciseness and exhaustiveness, including all information from the notes.
Avoid using note formats or lists, and refrain from inventing additional information.
"""
myDelimiter = "```"
if cleanInput==True:
labelTriples = cleanInputText(labelTriples)
# try to read cache
if map_query_input_output is not None:
key = args.model_name + "__" + str(args.temperature) + "__" + myPromt
if key in map_query_input_output:
if labelTriples in map_query_input_output[key]:
output = map_query_input_output[key][labelTriples]
# if input_text.strip() == "":
# print("here")
# if handler == api_call_dglc:
# output = clean_gpt_out(output) #clean output
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", myPromt, "\n", myDelimiter, word, myDelimiter, "\n=>\n", output, "\n")
return output, map_query_input_output
# call
try:
contextText = ""
if args.service_provider == "gptjrc":
contextText = call_model(input_text=labelTriples, prompt=myPromt, model=args.model_name,
temperature=args.temperature, delimiter=myDelimiter,
InContextExamples=[],
handler=api_call_gptjrc,
verbose=True, args=args)
elif args.service_provider == "HFonPremises":
contextText = call_model(input_text=labelTriples, prompt=myPromt, model=args.model_name,
temperature=args.temperature, delimiter=myDelimiter,
InContextExamples=[],
handler=api_call_HFonPremises,
verbose=True, args=args)
if contextText:
if not isinstance(contextText, str):
contextText = contextText['choices'][0]['message']['content']
if map_query_input_output is not None:
if not key in map_query_input_output:
map_query_input_output[key] = {}
if contextText:
if contextText != "":
map_query_input_output[key][labelTriples] = contextText
except Exception as err:
return None, map_query_input_output
return contextText, map_query_input_output
#@mem.cache
def virtuoso_api_call(word, text_splitter, args, key_virtuoso, cache_map_virtuoso, load_map_query_input_output, id=None, iALLURIScontextFromNCBO=None,UseBioportalForLinking=True,questionText=""):
if strtobool(args.debug):
print(f"\n----- Starting virtuoso_api_call for {word}")
word = word.lower()
word = strip_quotes(word)
endpoint = 'https://api-vast.jrc.service.ec.europa.eu/sparql'
VirtuosoUsername = 'dba'
if getattr(args, 'KG_restriction', None):
contextWordVirtuoso = ', '.join(sorted(args.KG_restriction))
else:
contextWordVirtuoso = ""
singleContext = None
globalContext = None
sssingleTriples = None
ggglobalTriples = None
unique_listLabelTriples = []
unique_listGlobalTriples = []
ALLURIScontext = []
url_text = None
if id:
url_text = id
if iALLURIScontextFromNCBO and isinstance(iALLURIScontextFromNCBO, list):
ALLURIScontext=iALLURIScontextFromNCBO
ALLURIScontext = list(set(ALLURIScontext))
if (cache_map_virtuoso is not None) and (not url_text):
if word in cache_map_virtuoso:
if contextWordVirtuoso in cache_map_virtuoso[word]:
url_text = cache_map_virtuoso[word][contextWordVirtuoso]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", word, " => ", url_text, "\n")
if not url_text:
return None, None, None, None, None, None, cache_map_virtuoso, load_map_query_input_output
if url_text and not ALLURIScontext:
if cache_map_virtuoso is not None:
if word in cache_map_virtuoso:
if 'ALLURIScontext' in cache_map_virtuoso[word]:
ALLURIScontext = cache_map_virtuoso[word]['ALLURIScontext']
entityBioeUrl = None
if url_text and ALLURIScontext:
entityBioeUrl = url_text
else:
try:
entityBioeUrl, ALLURIScontext, cache_map_virtuoso = getUrlBioAndAllOtherBioConcepts(word, args, key_virtuoso, cache_map_virtuoso, endpoint, VirtuosoUsername, contextWordVirtuoso, UseBioportalForLinking=UseBioportalForLinking, questionText=questionText )
if ALLURIScontext and isinstance(ALLURIScontext, list):
ALLURIScontext = list(set(ALLURIScontext))
except Exception as err:
# if cache_map_virtuoso is not None:
# if not word in cache_map_virtuoso:
# cache_map_virtuoso[word] = {}
# cache_map_virtuoso[word][contextWordVirtuoso] = None
return None, None, None, None, None, None, cache_map_virtuoso, load_map_query_input_output
if entityBioeUrl:
if strtobool(args.computeEntityContext):
if strtobool(args.debug):
print("START computeEntityContext")
unique_listLabelTriples = []
singleContext = None
if cache_map_virtuoso is not None:
if entityBioeUrl in cache_map_virtuoso:
if "LabelTriples" in cache_map_virtuoso[entityBioeUrl]:
unique_listLabelTriples = cache_map_virtuoso[entityBioeUrl]["LabelTriples"]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", entityBioeUrl, " => ", "LabelTriples", "\n")
if ("SingleContext" in cache_map_virtuoso[entityBioeUrl]) and (strtobool(args.UseRetrieverForContextCreation)==False):
singleContext = cache_map_virtuoso[entityBioeUrl]["SingleContext"]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", entityBioeUrl, " => ", "SingleContext", "\n")
if not singleContext:
if unique_listLabelTriples:
singleContext, load_map_query_input_output = getLinearTextualContextFromTriples(word, unique_listLabelTriples,
text_splitter, args,
load_map_query_input_output,cleanInput=True,questionText=questionText)
else:
query = f"""
prefix skosxl: <http://www.w3.org/2008/05/skos-xl#>
SELECT DISTINCT ?labelS ?labelP ?labelO
WHERE {{
{{
<{entityBioeUrl}> ?p ?o.
<{entityBioeUrl}> skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelS .
?p skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelP .
?o skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelO .
}}
UNION
{{
SELECT ?labelS ?labelP ?labelO
WHERE {{
<{entityBioeUrl}> ?p ?labelO .
<{entityBioeUrl}> skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelS .
?p skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelP .
FILTER (isLiteral(?labelO))
}}
}}
UNION
{{
SELECT DISTINCT ?labelS ?labelP ?labelO
WHERE {{
<{entityBioeUrl}> ?ppp ?ooo .
?ooo rdf:type owl:Restriction .
?ooo owl:onProperty ?p .
?ooo owl:someValuesFrom ?o .
<{entityBioeUrl}> skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelS .
?p skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelP .
?o skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelO .
}}
}}
}}
"""
try:
responseText = sparqlQuery(endpoint, query, VirtuosoUsername, key_virtuoso, strtobool(args.USE_CACHE))
# Parse the response as JSON
results = json.loads(responseText)
if len(results) > 0 and results['results']['bindings']:
# word = "subClassOf"
# split_word = split_camel_case(word)
# # loop the results
labelTriples=""
listLabelTriples = []
pattern = r'\^\^<http:.*?>'
for result in results['results']['bindings']:
#print(result)
ss = str(result['labelS']['value']).strip().replace("..",".").replace("@en","")
ss = re.sub(pattern, '', ss)
pp = split_camel_case(str(result['labelP']['value'])).replace("_"," ").strip().replace("..",".").replace("@en","")
pp = re.sub(pattern, '', pp)
oo = str(result['labelO']['value']).replace("_"," ").strip().replace("..",".").replace("@en","")
oo = re.sub(pattern, '', oo)
listLabelTriples.append([ss, pp, oo])
# Remove duplicates while preserving order
unique_listLabelTriples = list(dict.fromkeys(tuple(triple) for triple in listLabelTriples))
# # If you need the result back in list of lists format
# unique_listLabelTriples = [list(triple) for triple in unique_listLabelTriples]
if unique_listLabelTriples:
if cache_map_virtuoso is not None:
if not entityBioeUrl in cache_map_virtuoso:
cache_map_virtuoso[entityBioeUrl] = {}
cache_map_virtuoso[entityBioeUrl]["LabelTriples"] = unique_listLabelTriples
singleContext, load_map_query_input_output = getLinearTextualContextFromTriples(word, unique_listLabelTriples, text_splitter, args, load_map_query_input_output,cleanInput=True,questionText=questionText)
except Exception as err:
singleContext = None
if singleContext and (strtobool(args.UseRetrieverForContextCreation)==False):
if cache_map_virtuoso is not None:
if not entityBioeUrl in cache_map_virtuoso:
cache_map_virtuoso[entityBioeUrl] = {}
cache_map_virtuoso[entityBioeUrl]["SingleContext"] = singleContext
if strtobool(args.computeEntityGlobalContext):
if strtobool(args.debug):
print("START computeEntityGlobalContext")
unique_listGlobalTriples = []
globalContext = None
if cache_map_virtuoso is not None:
if word in cache_map_virtuoso:
if ("GlobalTriples"+" "+contextWordVirtuoso).strip() in cache_map_virtuoso[word]:
unique_listGlobalTriples = cache_map_virtuoso[word][("GlobalTriples"+" "+contextWordVirtuoso).strip()]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", word, " => ", ("GlobalTriples"+" "+contextWordVirtuoso).strip(), "\n")
if (("GlobalContext"+" "+contextWordVirtuoso).strip() in cache_map_virtuoso[word]) and (strtobool(args.UseRetrieverForContextCreation)==False):
globalContext = cache_map_virtuoso[word][("GlobalContext"+" "+contextWordVirtuoso).strip()]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", word, " => ", ("GlobalContext"+" "+contextWordVirtuoso).strip(), "\n")
if not globalContext:
if unique_listGlobalTriples:
globalContext, load_map_query_input_output = getLinearTextualContextFromTriples(word, unique_listGlobalTriples,
text_splitter, args,
load_map_query_input_output,cleanInput=True,questionText=questionText)
else:
if not ALLURIScontext:
if cache_map_virtuoso is not None:
if word in cache_map_virtuoso:
ALLURIScontext = list(cache_map_virtuoso[word].keys())
ALLURIScontext = [element for element in ALLURIScontext if element and ("GlobalTriples" in element == False) and ("GlobalContext" in element == False) and "http" in element ]
if not ALLURIScontext:
# THIS CASE SHOULD BE VERY DIFFICULT TO HAPPEN...IT WILL HAPPEN IN CASE IT COMES FROM AN INITIAL BIOPORTAL ANNOTATION; WHICH ALREADY RECOGNISED THE FIRST URL
# # Print the error message to stderr
# print("THIS CASE SHOULD NEVER HAPPEN!!!! Check what's happening...exiting now...")
# # Exit the program with a non-zero status code (commonly used to indicate an error)
# sys.exit(1)
try:
entityBioeUrl, ALLURIScontext, cache_map_virtuoso = getUrlBioAndAllOtherBioConcepts(word,
args,
key_virtuoso,
cache_map_virtuoso,
endpoint,
VirtuosoUsername,
contextWordVirtuoso,
UseBioportalForLinking=UseBioportalForLinking,
questionText=questionText)
if ALLURIScontext and isinstance(ALLURIScontext, list):
ALLURIScontext = list(set(ALLURIScontext))
except Exception as err:
# if cache_map_virtuoso is not None:
# if not word in cache_map_virtuoso:
# cache_map_virtuoso[word] = {}
# cache_map_virtuoso[word][contextWordVirtuoso] = None
return None, None, None, None, None, None, cache_map_virtuoso, load_map_query_input_output
if not ALLURIScontext:
# Print the error message to stderr
print("THIS CASE SHOULD NEVER HAPPEN NOW!!!! Check what's happening...exiting now...")
# Exit the program with a non-zero status code (commonly used to indicate an error)
sys.exit(1)
else:
for xxUrl in ALLURIScontext:
unique_listLabelTriples = []
# singleContext = None
if cache_map_virtuoso is not None:
if xxUrl in cache_map_virtuoso:
if "LabelTriples" in cache_map_virtuoso[xxUrl]:
unique_listLabelTriples = cache_map_virtuoso[xxUrl]["LabelTriples"]
if strtobool(args.debug):
print("RETRIEVED CACHED RESULT FOR:\n", xxUrl, " => ",
"LabelTriples", "\n")
# if "SingleContext" in cache_map_virtuoso[xxUrl] and (strtobool(args.UseRetrieverForContextCreation)==False):
# singleContext = cache_map_virtuoso[xxUrl]["SingleContext"]
# if strtobool(args.debug):
# print("RETRIEVED CACHED RESULT FOR:\n", xxUrl, " => ",
# "SingleContext", "\n")
# if not singleContext:
# if unique_listLabelTriples:
# singleContext, load_map_query_input_output = getLinearTextualContextFromTriples(
# word, unique_listLabelTriples,
# text_splitter, args,
# load_map_query_input_output, cleanInput=True, questionText=questionText)
# else:
if not unique_listLabelTriples:
query = f"""
prefix skosxl: <http://www.w3.org/2008/05/skos-xl#>
SELECT DISTINCT ?labelS ?labelP ?labelO
WHERE {{
{{
<{xxUrl}> ?p ?o.
<{xxUrl}> skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelS .
?p skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelP .
?o skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelO .
}}
UNION
{{
SELECT ?labelS ?labelP ?labelO
WHERE {{
<{xxUrl}> ?p ?labelO .
<{xxUrl}> skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelS .
?p skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelP .
FILTER (isLiteral(?labelO))
}}
}}
UNION
{{
SELECT DISTINCT ?labelS ?labelP ?labelO
WHERE {{
<{xxUrl}> ?ppp ?ooo .
?ooo rdf:type owl:Restriction .
?ooo owl:onProperty ?p .
?ooo owl:someValuesFrom ?o .
<{xxUrl}> skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelS .
?p skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelP .
?o skos:prefLabel|rdfs:label|skos:altLabel|skosxl:literalForm|obo:hasRelatedSynonym ?labelO .
}}
}}
}}
"""
try:
responseText = sparqlQuery(endpoint, query, VirtuosoUsername, key_virtuoso, strtobool(args.USE_CACHE))
# Parse the response as JSON
results = json.loads(responseText)
if len(results) > 0 and results['results']['bindings']:
# word = "subClassOf"
# split_word = split_camel_case(word)
# # loop the results
labelTriples = ""
listLabelTriples = []
pattern = r'\^\^<http:.*?>'
for result in results['results']['bindings']:
# print(result)
ss = str(result['labelS']['value']).strip().replace("..", ".").replace("@en","")
ss = re.sub(pattern, '', ss)
pp = split_camel_case(str(result['labelP']['value'])).replace("_"," ").strip().replace("..", ".").replace("@en","")
pp = re.sub(pattern, '', pp)
oo = str(result['labelO']['value']).replace("_"," ").strip().replace("..", ".").replace("@en","")
oo = re.sub(pattern, '', oo)
listLabelTriples.append([ss, pp, oo])
# Remove duplicates while preserving order
unique_listLabelTriples = list(
dict.fromkeys(tuple(triple) for triple in listLabelTriples))
# # If you need the result back in list of lists format
# unique_listLabelTriples = [list(triple) for triple in unique_listLabelTriples]
if unique_listLabelTriples:
if cache_map_virtuoso is not None:
if not xxUrl in cache_map_virtuoso:
cache_map_virtuoso[xxUrl] = {}
cache_map_virtuoso[xxUrl][
"LabelTriples"] = unique_listLabelTriples
# singleContext, load_map_query_input_output = getLinearTextualContextFromTriples(
# word, unique_listLabelTriples, text_splitter, args, load_map_query_input_output, cleanInput=True, questionText=questionText)
#
# if singleContext and (strtobool(args.UseRetrieverForContextCreation)==False):
# if cache_map_virtuoso is not None:
# if not xxUrl in cache_map_virtuoso:
# cache_map_virtuoso[xxUrl] = {}
# cache_map_virtuoso[xxUrl][
# "SingleContext"] = singleContext
except Exception as err:
unique_listLabelTriples = []
if unique_listLabelTriples:
unique_listGlobalTriples.extend(unique_listLabelTriples)
# If I want to speed up, I can break here, but in this case I will not store the triples for the other uris in the cache, which maybe useful in the future
# if token_counter(str(unique_listGlobalTriples),args.model_name) > args.tokens_max:
# break # BREAK THE FOR LOOP IF THE GLOBAL CONTEXT IS ALREADY TOO BIG, BIGGER THAN tokens_max
if unique_listGlobalTriples:
# Remove duplicates while preserving order
unique_listGlobalTriples = list(
dict.fromkeys(tuple(triple) for triple in unique_listGlobalTriples))
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][("GlobalTriples"+" "+contextWordVirtuoso).strip()] = unique_listGlobalTriples
globalContext, load_map_query_input_output = getLinearTextualContextFromTriples(word,
unique_listGlobalTriples,
text_splitter, args,
load_map_query_input_output, cleanInput=True, questionText=questionText)
if globalContext and (strtobool(args.UseRetrieverForContextCreation)==False):
if cache_map_virtuoso is not None:
if not word in cache_map_virtuoso:
cache_map_virtuoso[word] = {}
cache_map_virtuoso[word][("GlobalContext"+" "+contextWordVirtuoso).strip()] = globalContext
if unique_listLabelTriples:
sssingleTriples = " ,., ".join(
" ,,, ".join(str(element).capitalize() for element in triple) for triple in unique_listLabelTriples)
while "\\n" in sssingleTriples:
sssingleTriples = sssingleTriples.replace("\\n", " ")
sssingleTriples = sssingleTriples.strip()
while "\t" in sssingleTriples:
sssingleTriples = sssingleTriples.replace("\t", " ")
sssingleTriples = sssingleTriples.strip()
if unique_listGlobalTriples:
ggglobalTriples = " ,., ".join(
" ,,, ".join(str(element).capitalize() for element in triple) for triple in unique_listGlobalTriples)
while "\\n" in ggglobalTriples:
ggglobalTriples = ggglobalTriples.replace("\\n", " ")
ggglobalTriples = ggglobalTriples.strip()
while "\t" in ggglobalTriples:
ggglobalTriples = ggglobalTriples.replace("\t", " ")
ggglobalTriples = ggglobalTriples.strip()
return entityBioeUrl, ALLURIScontext, singleContext, globalContext, sssingleTriples, ggglobalTriples, cache_map_virtuoso, load_map_query_input_output
def process_row4Linking(row, text_splitter, args, key_geonames, cache_map_geonames, key_virtuoso, cache_map_virtuoso, load_map_query_input_output):
result = "" #None
singleContext = "" #None
globalContext = "" #None
singleTriples = "" #None
globalTriples = "" #None
ALLURIScontext = []
try:
if row.empty:
return result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames, cache_map_virtuoso, load_map_query_input_output, row.name
InRagMode=False
if hasattr(args, 'useBioKgRAG') and (strtobool(args.useBioKgRAG)==True):
InRagMode = True
if (InRagMode==False):
if row['IsGeo'] == 1:
if strtobool(args.debug):
print(f"\n----- IsGeo ... COMPUTING {row['word']} IN THE TEXT:")
print(row[args.source_column])
result, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames = geonames_api_call(row['word'], args, key_geonames, cache_map_geonames)
elif row['IsBio'] == 1:
# Check if '@id' column exists in df_Extract
iiid = None
# Check if the '@id' exists in the Series
if '@id' in row:
# Check if the value is not None or NaN
if row['@id'] is not None and not pd.isna(row['@id']):
# Assign the value to the variable iiid
iiid = row['@id']
iiiALLURIScontextFromNCBO = None
if 'ALLURIScontextFromNCBO' in row:
if row['ALLURIScontextFromNCBO'] is not None and isinstance(row['ALLURIScontextFromNCBO'], list): #and not pd.isna(row['ALLURIScontextFromNCBO']):
iiiALLURIScontextFromNCBO=row['ALLURIScontextFromNCBO']
iiiALLURIScontextFromNCBO = list(set(iiiALLURIScontextFromNCBO))
if strtobool(args.debug):
print(f"\n----- isBio COMPUTING ... {row['word']} IN THE TEXT:")
print(row[args.source_column])
result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_virtuoso, load_map_query_input_output = virtuoso_api_call(row['word'], text_splitter, args, key_virtuoso, cache_map_virtuoso, load_map_query_input_output, id=iiid, iALLURIScontextFromNCBO=iiiALLURIScontextFromNCBO, UseBioportalForLinking=True, questionText=row[args.source_column])
else:
if row['model'] == "Forced":
# Check if '@id' column exists in df_Extract
iiid = None
# Check if the '@id' exists in the Series
if '@id' in row:
# Check if the value is not None or NaN
if row['@id'] is not None and not pd.isna(row['@id']):
# Assign the value to the variable iiid
iiid = row['@id']
iiiALLURIScontextFromNCBO = None
if 'ALLURIScontextFromNCBO' in row:
if row['ALLURIScontextFromNCBO'] is not None and isinstance(row['ALLURIScontextFromNCBO'],
list): # and not pd.isna(row['ALLURIScontextFromNCBO']):
iiiALLURIScontextFromNCBO = row['ALLURIScontextFromNCBO']
iiiALLURIScontextFromNCBO = list(set(iiiALLURIScontextFromNCBO))
if strtobool(args.debug):
print(f"\n----- isForced COMPUTING ... {row['word']} IN THE TEXT:")
print(row[args.source_column])
result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_virtuoso, load_map_query_input_output = virtuoso_api_call(
row['word'], text_splitter, args, key_virtuoso, cache_map_virtuoso, load_map_query_input_output,
id=iiid, iALLURIScontextFromNCBO=iiiALLURIScontextFromNCBO,UseBioportalForLinking=True,questionText=row[args.source_column])
if not result: #try annotation without bioportal
result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_virtuoso, load_map_query_input_output = virtuoso_api_call(
row['word'], text_splitter, args, key_virtuoso, cache_map_virtuoso, load_map_query_input_output,
id=iiid, iALLURIScontextFromNCBO=iiiALLURIScontextFromNCBO, UseBioportalForLinking=False,questionText=row[args.source_column])
else:
if (row['IsBio'] == 1) or ( (pd.isnull(row["IsBio"]) or row["IsBio"] == '' or row['IsBio'] == 0 or row["IsBio"] is None) and (row['entity_group'] == "MISC") ):
if strtobool(args.debug):
print(f"\n----- InRagMode ...COMPUTING ... {row['word']} IN THE TEXT:")
print(row[args.source_column])
# Check if '@id' column exists in df_Extract
iiid = None
# Check if the '@id' exists in the Series
if '@id' in row:
# Check if the value is not None or NaN
if row['@id'] is not None and not pd.isna(row['@id']):
# Assign the value to the variable iiid
iiid = row['@id']
iiiALLURIScontextFromNCBO = None
if 'ALLURIScontextFromNCBO' in row:
if row['ALLURIScontextFromNCBO'] is not None and isinstance(row['ALLURIScontextFromNCBO'], list):
iiiALLURIScontextFromNCBO = row['ALLURIScontextFromNCBO']
iiiALLURIScontextFromNCBO = list(set(iiiALLURIScontextFromNCBO))
result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_virtuoso, load_map_query_input_output = virtuoso_api_call(
row['word'], text_splitter, args, key_virtuoso, cache_map_virtuoso, load_map_query_input_output, id=iiid, iALLURIScontextFromNCBO=iiiALLURIScontextFromNCBO,UseBioportalForLinking=True,questionText=row[args.source_column])
return result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames, cache_map_virtuoso, load_map_query_input_output, row.name
except Exception as e:
#print(f"Error occurred: {e}")
return result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames, cache_map_virtuoso, load_map_query_input_output, row.name
def parallel_process_Row4Linking(df, text_splitter, args, key_geonames, cache_map_geonames, key_virtuoso, cache_map_virtuoso, load_map_query_input_output):
results = []
with ThreadPoolExecutor(max_workers=args.num_cores_eLinking) as executor:
# Submit tasks to ThreadPoolExecutor
futures = [executor.submit(process_row4Linking, row, text_splitter, args, key_geonames, cache_map_geonames, key_virtuoso, cache_map_virtuoso, load_map_query_input_output)
for _, row in df.iterrows()]
# Collect results
for future in as_completed(futures):
try:
result, ALLURIScontext, singleContext, globalContext, singleTriples, globalTriples, cache_map_geonames_Inner, cache_map_virtuoso_Inner, load_map_query_input_output_Inner, drm_idx = future.result()
df.at[drm_idx,'namedEntity'] = result
df.at[drm_idx, 'ALLURIScontext'] = ALLURIScontext
df.at[drm_idx,'Context'] = singleContext
df.at[drm_idx,'ContextGlobal'] = globalContext
df.at[drm_idx, 'Triples'] = singleTriples
df.at[drm_idx, 'TriplesGlobal'] = globalTriples
# Recursively update cache_map files with values from _Inner --> actually, this will never happen, because when you pass a mutable object like a dictionary to a function,
# changes made to that object within the function are reflected outside the function as well. This is because the function receives a reference to the original object,
# rather than a copy of it.
# cache_map_geonames, cache_map_virtuoso, load_map_query_input_output are like global variables, which is very good for the parallelization!
if (cache_map_geonames == cache_map_geonames_Inner)==False:
update_nested_dict(cache_map_geonames, cache_map_geonames_Inner)
if (cache_map_virtuoso == cache_map_virtuoso_Inner) == False:
update_nested_dict(cache_map_virtuoso, cache_map_virtuoso_Inner)
if (load_map_query_input_output == load_map_query_input_output_Inner) == False:
update_nested_dict(load_map_query_input_output, load_map_query_input_output_Inner)
except Exception as e:
print(f"Error occurred: {e}")
return df, cache_map_geonames, cache_map_virtuoso, load_map_query_input_output
def elinking(df_annotated_combined, text_splitter, args, key_geonames, cache_map_geonames, key_virtuoso, cache_map_virtuoso, load_map_query_input_output, device):
if "ALLURIScontext" not in df_annotated_combined.columns:
df_annotated_combined["ALLURIScontext"] = None
if args.num_cores_eLinking>1:
# parallel elinking process
#result
df_annotated_combined, cache_map_geonames_AFTER, cache_map_virtuoso_AFTER, load_map_query_input_output_AFTER = parallel_process_Row4Linking(df_annotated_combined, text_splitter, args, key_geonames, cache_map_geonames, key_virtuoso, cache_map_virtuoso, load_map_query_input_output)
#if isinstance(result, list):
# result=pd.Series(result) # I need this after the parallel processing
else:
# single processing
result = df_annotated_combined.apply(lambda row: process_row4Linking(row, text_splitter, args, key_geonames, cache_map_geonames, key_virtuoso, cache_map_virtuoso, load_map_query_input_output), axis=1)
#
try:
df_annotated_combined['namedEntity'] = result.str[0]
df_annotated_combined['ALLURIScontext'] = result.str[1]
df_annotated_combined['Context'] = result.str[2]
df_annotated_combined['ContextGlobal'] = result.str[3]
df_annotated_combined['Triples'] = result.str[4]
df_annotated_combined['TriplesGlobal'] = result.str[5]
cache_map_geonames_AFTER = result.str[6].iloc[-1]
cache_map_virtuoso_AFTER = result.str[7].iloc[-1]
load_map_query_input_output_AFTER = result.str[8].iloc[-1] #
except Exception as e:
# print(f"Error occurred: {e}")
df_annotated_combined['namedEntity'] = ""
df_annotated_combined['ALLURIScontext'] = ""
df_annotated_combined['Context'] = ""
df_annotated_combined['ContextGlobal'] = ""
df_annotated_combined['Triples'] = ""
df_annotated_combined['TriplesGlobal'] = ""
cache_map_geonames_AFTER = cache_map_geonames
cache_map_virtuoso_AFTER = cache_map_virtuoso
load_map_query_input_output_AFTER = load_map_query_input_output
if args.num_cores_eLinking>1:
# if parallel elinking process I need to sort again, because rows in late will arrive later
df_annotated_combined = df_annotated_combined.sort_values(by=['SentenceRef', 'start', 'ToLink', 'word', 'score'],
ascending=[True, True, True, True, False])
return df_annotated_combined, cache_map_geonames_AFTER, cache_map_virtuoso_AFTER, load_map_query_input_output_AFTER
def nerBio(text, ModelsSelection, CategoriesSelection, ScoreFilt, EntityLinking, KGchoices, history_dict: dict):
if EntityLinking:
EnableNEL="True"
else:
EnableNEL="False"
if not text:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": []}, html_output, dict()
df_annotated = pd.DataFrame()
parser = argparse.ArgumentParser()
parser.add_argument("--model_id", type=str, default=models_List[0], help="model to use")
parser.add_argument("--debug", type=str, default="True", help="set debug mode")
parser.add_argument("--source_column", type=str, default="ContextToAnnotate")
parser.add_argument("--entities_filter_threshold", type=int, default=ScoreFilt)
parser.add_argument("--SEED", type=int, default=41)
parser.add_argument("--batch_size", type=int, default=1) # 4 - 8 - 16
parser.add_argument("--num_cores_Gliner", type=int, default=num_cores_Gliner_forDemo, help="parallel processing for Gliner annotation") # 0 means use the GPU for Gliner !
parser.add_argument("--entity_linking", type=str, default=EnableNEL, help="whether to make entities linking or not")
parser.add_argument("--geonameskey_filename", type=str, default="GEONAMES-API.key", help="file location where it is stored the geonames api key")
parser.add_argument("--virtuosokey_filename", type=str, default="VIRTUOSO-dba.key", help="file location where it is stored the virtuoso endpoint dba pwd")
parser.add_argument("--bioportalkey_filename", type=str, default="NCBO-BioPortal.key", help="file location where it is stored the NCBO BioPortal api key")
# consose 20250205:
# KGchoices = None
# KGchoices = ['SNOMED', 'LOINC', 'ICD10', 'NCIT']
# KGchoices = ['SNOMED', 'LOINC', 'ICD10', 'MESH', 'NCIT'] # restricts the input to these values only
if KGchoices:
KGchoices.sort()
parser.add_argument("--KG_restriction", nargs='+', choices=KGchoices, default=KGchoices,
help="List of ontologies to which restrict the entity linking task.")
# # consose 20250502:
# if Counter(KGchoices) == Counter(POSSIBLE_KGchoices_List):
# parser.add_argument("--USE_CACHE", type=str, default="True",
# help="whether to use cache for the NER and NEL tasks or not")
# else:
# # print("Lists do not have the same elements")
# parser.add_argument("--USE_CACHE", type=str, default="False",
# help="whether to use cache for the NER and NEL tasks or not")
parser.add_argument("--USE_CACHE", type=str, default="False", help="whether to use cache for the NER and NEL tasks or not")
parser.add_argument("--num_cores_eLinking", type=int, default=1, help="parallel processing for the entity linking process")
parser.add_argument("--computeEntityContext", type=str, default="False",
help="whether to extract a readable context from the extracted triples for the concept")
parser.add_argument("--computeEntityGlobalContext", type=str, default="False",
help="whether to extract a readable context from the extracted triples of all the entities extracted from the endpoint for the concept")
parser.add_argument("--UseRetrieverForContextCreation", type=str, default="True",
help="whether to use a retriever for the creation of the context of the entities from the triples coming from the KGs")
parser.add_argument("--service_provider", type=str, default="no", help="llm service provider")
parser.add_argument("--model_name", type=str, default="no", help="llm to use")
parser.add_argument("--tokens_max", type=int, default=80000, help="max number of tokens to supply to the llm")
parser.add_argument("--temperature", type=int, default=0.01)
args = parser.parse_args()
df_ToAnnotate = pd.DataFrame()
#print("Are all models in any row of the 'model' column, case-insensitively?", all_models_in_any_row)
#if (not history_dict) or (history_dict[args.source_column][0] != text) or (all_models_in_any_row == False):
if (not history_dict) or (history_dict[args.source_column][0] != text):
for model_id in models_List: # always do all the annotations, only filter them afterwards
#for model_id in ModelsSelection:
# if history_dict and (history_dict[args.source_column][0] == text):
# if model_id in hhist['model'].unique():
# continue
parser.set_defaults(model_id=model_id)
args = parser.parse_args()
print("ARGS:")
print(args)
# %% n machine learning tasks, particularly when dealing with models that have stochasticity involved (like text generation), it's important to set seeds for random number generators to ensure reproducibility of results. In the case of using models from the transformers library, you need to set seeds for both Python's random module, NumPy, and PyTorch to ensure that the results are the same every time you run the code.
# Before you create the pipeline and run the text generation, set the seeds like this:
random.seed(args.SEED)
np.random.seed(args.SEED)
torch.manual_seed(args.SEED)
torch.cuda.manual_seed_all(args.SEED)
###
df_ToAnnotate = pd.DataFrame({ "ToLink": [None], args.source_column: [text]})
if "SentenceRef" not in df_ToAnnotate.columns:
df_ToAnnotate["SentenceRef"] = None
df_ToAnnotate = df_ToAnnotate[['SentenceRef'] + [col for col in df_ToAnnotate.columns if
col != 'SentenceRef']] # this moves it to the first position
df_ToAnnotate['SentenceRef'] = df_ToAnnotate.index + 1
df_ToAnnotate['SentenceRef'] = df_ToAnnotate['SentenceRef'].argsort().groupby(df_ToAnnotate[args.source_column]).transform('min').astype(int)
df_ToAnnotate['SentenceRef'] = df_ToAnnotate['SentenceRef'].rank(method='dense').astype(int)
pipeToUse = None
if (("gliner" in args.model_id) == False) and (("NCBO" in args.model_id)== False) :
pipeToUse = pipe_dict[args.model_id]
new_annotations = annotate(df_ToAnnotate, args, pipeToUse, tokenizerGliner, modelGliner, modelGlinerBio, device)
if not new_annotations.empty:
if df_annotated.empty:
# If df_annotated is empty, just assign new_annotations to it
df_annotated = new_annotations
else:
# If df_annotated is not empty, concatenate new_annotations to it
df_annotated = pd.concat([df_annotated, new_annotations], ignore_index=True)
history = df_annotated.copy()
else:
print("ARGS:")
print(args)
# %% n machine learning tasks, particularly when dealing with models that have stochasticity involved (like text generation), it's important to set seeds for random number generators to ensure reproducibility of results. In the case of using models from the transformers library, you need to set seeds for both Python's random module, NumPy, and PyTorch to ensure that the results are the same every time you run the code.
# Before you create the pipeline and run the text generation, set the seeds like this:
random.seed(args.SEED)
np.random.seed(args.SEED)
torch.manual_seed(args.SEED)
torch.cuda.manual_seed_all(args.SEED)
###
history = pd.DataFrame(history_dict)
df_annotated = history.copy()
quoted_text = text.startswith('"') & text.endswith('"')
if (not df_annotated.empty) or quoted_text:
if (not df_annotated.empty):
# filter now per models selection
df_annotated = df_annotated[df_annotated['model'].str.lower().isin([model.lower() for model in ModelsSelection])]
if df_annotated.empty and quoted_text==False:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": []}, html_output, history.to_dict()
df_annotated_combined = pd.DataFrame()
if (not df_annotated.empty):
df_annotated_combined = entitiesFusion(df_annotated,args)
if df_annotated_combined.empty and quoted_text==False:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": []}, html_output, history.to_dict()
else:
df_annotated_combined = is_cross_inside(df_annotated_combined, args, 0.999) #I cut all the cross inside with the 0.99. to avoid the linking
cache_prefix_fp = "LLMQUERYNER"
cache_nameLLMs = cache_prefix_fp + "___" + "__".join(
[args.service_provider, args.model_name, str(args.temperature)]).replace(
" ", "_") + ".json"
load_map_query_input_output = None
if strtobool(args.USE_CACHE):
if os.path.exists(cache_nameLLMs):
with open(cache_nameLLMs) as f:
load_map_query_input_output = json.load(f)
else:
load_map_query_input_output = {}
### entity linking part:
if strtobool(args.entity_linking):
cache_map_geonames = None
if strtobool(args.USE_CACHE):
cache_filename = "CACHE_geonames.json"
if os.path.exists(cache_filename):
with open(cache_filename) as f:
cache_map_geonames = json.load(f)
else:
cache_map_geonames = {}
#key_geonames = ""
#if args.geonameskey_filename:
# fkeyname = args.geonameskey_filename
# with open(fkeyname) as f:
# key_geonames = f.read()
key_geonames = os.environ['key_geonames']
cache_map_virtuoso = None
if strtobool(args.USE_CACHE):
cacheVirtuoso_filename = "CACHE_virtuoso.json"
if os.path.exists(cacheVirtuoso_filename):
with open(cacheVirtuoso_filename) as f:
cache_map_virtuoso = json.load(f)
else:
cache_map_virtuoso = {}
#key_virtuoso = ""
#if args.virtuosokey_filename:
# fkeyname = args.virtuosokey_filename
# with open(fkeyname) as f:
# key_virtuoso = f.read()
key_virtuoso = os.environ['key_virtuoso']
# Here for the EXACT MATCHING "" - if the desired term has not been identified in the NER, add to the dataframe:
if df_ToAnnotate.empty:
df_ToAnnotate = pd.DataFrame({"ToLink": [None], args.source_column: [text]})
if "SentenceRef" not in df_ToAnnotate.columns:
df_ToAnnotate["SentenceRef"] = None
df_ToAnnotate = df_ToAnnotate[['SentenceRef'] + [col for col in df_ToAnnotate.columns if
col != 'SentenceRef']] # this moves it to the first position
df_ToAnnotate['SentenceRef'] = df_ToAnnotate.index + 1
df_ToAnnotate['SentenceRef'] = df_ToAnnotate['SentenceRef'].argsort().groupby(
df_ToAnnotate[args.source_column]).transform('min').astype(int)
df_ToAnnotate['SentenceRef'] = df_ToAnnotate['SentenceRef'].rank(method='dense').astype(int)
# Define the condition to find missing SentenceRefs
missing_sentence_refs = ~df_ToAnnotate['SentenceRef'].isin(df_annotated_combined['SentenceRef'])
# Define the condition to check if ContextToAnnotate starts and ends with quotes
quoted_context = df_ToAnnotate[args.source_column].str.startswith('"') & df_ToAnnotate[
args.source_column].str.endswith('"')
# Combine both conditions
condition = missing_sentence_refs & quoted_context
# Select rows from df_ToAnnotate that meet the condition
rows_to_add = df_ToAnnotate[condition].copy()
rows_to_add['model'] = "Forced"
rows_to_add['entity_group'] = "MISC"
rows_to_add['word'] = rows_to_add[args.source_column]
rows_to_add['word'] = rows_to_add[args.source_column].apply(strip_quotes)
rows_to_add['score'] = 1.0
rows_to_add['start'] = int(1)
rows_to_add['end'] = rows_to_add['word'].apply(len) + int(1)
rows_to_add['IsGeo'] = None
rows_to_add['IsBio'] = None
rows_to_add['IsCrossInside'] = 0.0
if df_annotated_combined.empty:
df_annotated_combined = pd.DataFrame(columns=df_ToAnnotate.columns)
# Append these rows to df_annotated_combined
df_annotated_combined = pd.concat([df_annotated_combined, rows_to_add], ignore_index=True)
df_annotated_combined['start'] = df_annotated_combined['start'].astype(int)
df_annotated_combined['end'] = df_annotated_combined['end'].astype(int)
df_annotated_combined = df_annotated_combined.sort_values(
by=['SentenceRef', 'start', 'ToLink', 'word', 'score'],
ascending=[True, True, True, True, False])
# Now df_annotated_combined contains the additional rows
df_annotated_combined, cache_map_geonames_AFTER, cache_map_virtuoso_AFTER, load_map_query_input_output_AFTER = elinking(df_annotated_combined,
text_splitter, args, key_geonames,
cache_map_geonames,
key_virtuoso,
cache_map_virtuoso,
load_map_query_input_output,
device)
if strtobool(args.USE_CACHE):
if cache_map_geonames_AFTER is not None:
with open(cache_filename, "w") as f:
json.dump(cache_map_geonames_AFTER, f)
if cache_map_virtuoso_AFTER is not None:
with open(cacheVirtuoso_filename, "w") as f:
json.dump(cache_map_virtuoso_AFTER, f)
if load_map_query_input_output_AFTER is not None:
with open(cache_nameLLMs, "w") as f:
json.dump(load_map_query_input_output_AFTER, f)
### end entity linking part
### filter by selected category only
# #df_annotated_combined = df_annotated_combined[df_annotated_combined['entity_group'].str.lower().isin([cat.lower() for cat in CategoriesSelection])]
# if "MED" in CategoriesSelection:
# filter_mask = df_annotated_combined['entity_group'].str.lower().isin(
# [cat.lower() for cat in CategoriesSelection]) | (df_annotated_combined['IsBio'] == 1)
# else:
# filter_mask = df_annotated_combined['entity_group'].str.lower().isin(
# [cat.lower() for cat in CategoriesSelection])
# df_annotated_combined = df_annotated_combined[filter_mask]
#
# if "MED" in CategoriesSelection:
# filter_mask = df_annotated_combined['entity_group'].str.lower().isin(
# [cat.lower() for cat in CategoriesSelection]) | (df_annotated_combined['IsBio'] == 1)
# elif "OTHER" in CategoriesSelection:
# filter_mask = ~(
# df_annotated_combined['entity_group'].str.lower().isin([cat.lower() for cat in categories_List]))
# else:
# filter_mask = df_annotated_combined['entity_group'].str.lower().isin(
# [cat.lower() for cat in CategoriesSelection])
filter_mask = df_annotated_combined['entity_group'].str.lower().isin(
[cat.lower() for cat in CategoriesSelection])
if "MED" in CategoriesSelection:
filter_mask |= df_annotated_combined['entity_group'].str.lower().isin(
[cat.lower() for cat in CategoriesSelection]) | (df_annotated_combined['IsBio'] == 1)
if "MISC" in CategoriesSelection:
#filter_mask |= ~(df_annotated_combined['entity_group'].str.lower().isin([cat.lower() for cat in categories_List]))
filter_mask |= ~(df_annotated_combined['entity_group'].str.lower().isin([cat.lower() for cat in categories_List])) & ~(df_annotated_combined['IsBio'] == 1) # with this cluase, I'm including not only the categories labelled as MISC, but also the other that are not MED, PER, ORG, LOC
df_annotated_combined = df_annotated_combined[filter_mask]
if df_annotated_combined.empty:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": []}, html_output, history.to_dict()
###
#df_annotated_combined = is_cross_inside(df_annotated_combined, args)
if 'IsCrossInside' in df_annotated_combined.columns:
df_annotated_combined = df_annotated_combined[df_annotated_combined['IsCrossInside'] != 1]
if df_annotated_combined.empty:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": []}, html_output, history.to_dict()
dict_annotated_combined_NER = df_annotated_combined[["end", "entity_group", "score", "start", "word"]].to_dict(orient="records")
### continue linking part:
if strtobool(args.entity_linking):
# ##### this is to pass the links:
# Create a new column for the entities with links
# #df_annotated_combined['entity_with_link'] = df_annotated_combined.apply(lambda row: f"<a href='{row['namedEntity']}' target='_blank'>{row['word']}</a>", axis=1)
# df_annotated_combined['entity_with_link'] = df_annotated_combined.apply(
# lambda row: f"<a href='{row['namedEntity']}' target='_blank'>{row['word']}</a>" if pd.notnull(row['namedEntity']) else row[
# 'word'], axis=1)
#include the expl-rel prefix:
#df_annotated_combined['entity_with_link'] = df_annotated_combined.apply(
# lambda row: f"<a href='https://expl-rels-dev-vast.apps.ocpt.jrc.ec.europa.eu/?concept={row['namedEntity']}' target='_blank'>{row['word']}</a>" if pd.notnull(row['namedEntity']) else row[
# 'word'], axis=1)
# df_annotated_combined['entity_with_link'] = df_annotated_combined.apply(
# lambda
# row: f"<a href='https://api-vast.jrc.service.ec.europa.eu/describe//?url={row['namedEntity']}' target='_blank'>{row['word']}</a>" if pd.notnull(
# row['namedEntity']) else row[
# 'word'], axis=1)
df_annotated_combined['entity_with_link'] = df_annotated_combined.apply(
lambda
row: f"<a href='https://api-vast.jrc.service.ec.europa.eu/describe//?url={row['namedEntity']}' target='_blank'>{row['word']}</a>" if row['namedEntity'] not in [None, '', 'NaN', 'nan'] and pd.notnull(row['namedEntity']) else row[
'word'], axis=1)
# Create a new dictionary with the entity information and the link
dict_annotated_combined_NEL = df_annotated_combined[
["end", "entity_group", "score", "start", "entity_with_link"]].to_dict(orient="records")
# Sort the entities by their start index
dict_annotated_combined_NEL.sort(key=lambda x: x['start'])
# Create a dictionary to map entity groups to colors
entity_colors = {
"MED": "#E6E6E6",
"PER": "#FFC0CB",
"ORG": "#C6F4D6",
"LOC": "#FFFFCC",
"MISC": "#F5DEB3"
}
text_with_links = text
offset = 0
for entity in dict_annotated_combined_NEL:
start = entity["start"] + offset
end = entity["end"] + offset
entity_text = entity["entity_with_link"]
text_with_links = text_with_links[:start] + entity_text + text_with_links[end:]
offset += len(entity_text) - (end - start)
# # Create the text with entities highlighted and linked
# text_with_links = text
# offset = 0
# for entity in dict_annotated_combined_NEL:
# start = entity["start"] + offset
# end = entity["end"] + offset
# entity_text = entity["entity_with_link"]
# entity_group = entity["entity_group"]
#
# color = entity_colors.get(entity_group, "#dbeafe") # Default
# darker_color = "#008080"
#
# if "https:" in entity_text:
# text_with_links = text_with_links[
# :start] + f'<span style="background-color: {color}; border-radius: 2px; padding: 2px 4px"><a style="color: {darker_color}" href="{entity_text.split(">")[1].split("<")[0]}">{entity_text.split(">")[1].split("<")[0]}</a> <span style="color: {darker_color}; font-size: 0.8em">{entity_group}</span></span>' + text_with_links[
# end:]
# offset += len(
# f'<span style="background-color: {color}; border-radius: 2px; padding: 2px 4px"><a style="color: {darker_color}" href="{entity_text.split(">")[1].split("<")[0]}">{entity_text.split(">")[1].split("<")[0]}</a> <span style="color: {darker_color}; font-size: 0.8em">{entity_group}</span></span>') - (
# end - start)
# # text_with_links = text_with_links[:start] + f'<span style="background-color: {color}"><a href="{entity_text.split(">")[1].split("<")[0]}">{entity_text.split(">")[1].split("<")[0]}</a></span>' + text_with_links[end:]
# # offset += len(
# # f'<span style="background-color: {color}"><a href="{entity_text.split(">")[1].split("<")[0]}">{entity_text.split(">")[1].split("<")[0]}</a></span>') - (
# # end - start)
# #
# # text_with_links = text_with_links[:start] + entity_text + text_with_links[end:]
# # offset += len(entity_text) - (end - start)
# else:
# text_with_links = text_with_links[
# :start] + f'<span style="background-color: {color}; border-radius: 2px; padding: 2px 4px">{entity_text} <span style="color: {darker_color}; font-size: 0.8em">{entity_group}</span></span>' + text_with_links[end:]
# offset += len(
# f'<span style="background-color: {color}; border-radius: 2px; padding: 2px 4px">{entity_text} <span style="color: {darker_color}; font-size: 0.8em">{entity_group}</span></span>') - (
# end - start)
# # text_with_links = text_with_links[
# # :start] + f'<span style="background-color: {color}">{entity_text}</span>' + text_with_links[
# # end:]
# # offset += len(
# # f'<span style="background-color: {color}">{entity_text}</span>') - (end - start)
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text_with_links}</div>"
return {"text": text, "entities": dict_annotated_combined_NER}, html_output, history.to_dict()
else:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": dict_annotated_combined_NER}, html_output, history.to_dict()
else:
html_output = f"<div class='gr-textbox' style='white-space: pre-wrap; overflow-wrap: break-word; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; font-size: 12px; line-height: 24px;'>{text}</div>"
return {"text": text, "entities": []}, html_output, history.to_dict()
demo = gr.Interface(
fn=nerBio,
inputs=[
gr.Textbox(label= "Input text", placeholder="Enter text here..."),
gr.CheckboxGroup(models_List, label="ModelsSelection", value=models_List),
gr.CheckboxGroup(categories_List, label="CategoriesSelection", value=categories_List),
gr.Slider(minimum=0, maximum=1.0, step=0.1, label="Score", value=0.7),
gr.Checkbox(label="Enable Named-Entity Linking (NEL)", value=False), #True False
#gr.CheckboxGroup(POSSIBLE_KGchoices_List, label="KGchoices Selection", value=POSSIBLE_KGchoices_List, visible=True),
gr.Dropdown(POSSIBLE_KGchoices_List, multiselect=True, label="KGchoices Selection", value=POSSIBLE_KGchoices_List),
gr.State(value={})
],
outputs=[
gr.HighlightedText(label="Annotated Text"),
gr.HTML(label="Linked Text", show_label=True, visible=True), # use gr.HTML to render the annotated text with links , visible
gr.State()
],
live=True,
title="BioAnnotator: Biomedical Named-Entity Recognition (NER) and Linking (NEL)",
description="Select one or more NER models and enter some text to get it processed. Please select also the entity categories you want to extract, as well as the score to use as a threshold for the NER extraction. Finally, select whether you want to perform Named-Entity Linking (NEL) and if you want to enable the filtering to some specific biomedical ontologies only (acronyms description at: https://bioportal.bioontology.org/ontologies. See also: https://citnet.tech.ec.europa.eu/CITnet/confluence/display/DIGHEALTH/Inventory+of+existing+KGs+related+to+the+Digital+Health+domain). ",
examples=examples,
cache_examples=False
)
demo.launch()
#demo.launch(share=True) # Share your demo with just 1 extra parameter |