File size: 107,587 Bytes
7c42403 54f60e1 c3dca85 e0b088f 7c42403 650a052 7c42403 a06a9ed 650a052 7c42403 e0b088f 0a55939 e0b088f 0a55939 a06a9ed 0a55939 0d2984f e0b088f 6a2aca3 bcdf604 5120d94 bcdf604 54f60e1 7c42403 164c0fc 7c42403 164c0fc 7c42403 567e4e6 aa2f2ec 7c42403 aa2f2ec 7c42403 347f438 f456ecb 2029fc2 164c0fc 54f60e1 650a052 7c42403 54f60e1 29f4610 54f60e1 29f4610 7c42403 de6cf4c 7c42403 de6cf4c 7c42403 de6cf4c 7c42403 de6cf4c 34bec84 de6cf4c 7c42403 3d26c4f 7c42403 3d26c4f 164c0fc 3d26c4f 164c0fc 53d06aa 164c0fc 53d06aa 164c0fc 3d26c4f 9441294 54f60e1 3d26c4f 9441294 3d26c4f 53d06aa aa2f2ec 7d5c7d3 6a2aca3 53d06aa 7c42403 84c0568 b99c2aa 84c0568 b99c2aa 84c0568 b99c2aa 84c0568 b99c2aa 84c0568 b99c2aa 84c0568 7c42403 0d2984f 7c42403 de6cf4c 7c42403 de6cf4c 7c42403 de6cf4c 7c42403 164c0fc 658bf69 7c42403 658bf69 164c0fc 7c42403 567e4e6 7c42403 de6cf4c 7c42403 650a052 7c42403 567e4e6 650a052 7c42403 567e4e6 7c42403 650a052 164c0fc 567e4e6 7c42403 745b4f3 7c42403 745b4f3 7c42403 e0b088f 7c42403 b5700e0 7c42403 b5700e0 7c42403 b5700e0 7c42403 567e4e6 7c42403 567e4e6 7c42403 567e4e6 7c42403 650a052 54f60e1 650a052 7c42403 650a052 7c42403 650a052 7c42403 2029fc2 7c42403 53d06aa b5700e0 7c42403 b5700e0 7c42403 b5700e0 7c42403 f456ecb 347f438 7c42403 347f438 aa2f2ec 29f4610 54f60e1 7c42403 aa2f2ec 54f60e1 aa2f2ec 2029fc2 54f60e1 2029fc2 347f438 2029fc2 7c42403 2029fc2 7c42403 54f60e1 7c42403 2029fc2 5e695e8 2029fc2 48eee7a 347f438 29f4610 48eee7a 347f438 48eee7a 347f438 f456ecb 7c42403 2029fc2 7c42403 29f4610 7c42403 aa2f2ec 7eb5299 1898de3 72ecb17 1898de3 b7d680c a06a9ed 0a55939 7eb5299 0a55939 e0b088f 0a55939 e0b088f 9441294 e0b088f 0a55939 e0b088f 0a55939 e0b088f 0a55939 e0b088f 0a55939 e0b088f 0a55939 a06a9ed 70a4296 a06a9ed 0a55939 a06a9ed 0a55939 a06a9ed 0a55939 a06a9ed 0a55939 b8ec270 0a55939 b8ec270 0a55939 a06a9ed b92e866 0a55939 78afc38 0a55939 78afc38 0a55939 b92e866 0a55939 a06a9ed 40ba4ba 72ecb17 0a55939 b8ec270 72ecb17 0a55939 72ecb17 a06a9ed 0a55939 72ecb17 a06a9ed 40ba4ba 72ecb17 a06a9ed 72ecb17 0a55939 a06a9ed 9441294 a06a9ed 0a55939 a06a9ed 9441294 a06a9ed 72ecb17 40ba4ba a06a9ed 40ba4ba 70a4296 0a55939 9441294 0a55939 40ba4ba b8ec270 40ba4ba b8ec270 40ba4ba b8ec270 0a55939 b8ec270 0a55939 b8ec270 0a55939 b8ec270 40ba4ba a06a9ed 40ba4ba b8ec270 72ecb17 40ba4ba 0a55939 b8ec270 40ba4ba b8ec270 40ba4ba 0a55939 e0b088f 0a55939 b8ec270 9441294 b8ec270 40ba4ba a06a9ed b8ec270 9441294 a06a9ed 0a55939 b8ec270 a06a9ed 9441294 a06a9ed 40ba4ba b8ec270 40ba4ba b8ec270 40ba4ba 0a55939 40ba4ba 0a55939 b8ec270 40ba4ba b8ec270 9441294 b8ec270 9441294 b8ec270 9441294 b8ec270 9441294 40ba4ba 7eb5299 72ecb17 7eb5299 72ecb17 7c42403 de6cf4c 7c42403 b5700e0 7c42403 164c0fc 9de7f08 b99c2aa 567e4e6 7c42403 b5700e0 7c42403 b5700e0 7c42403 b5700e0 c3dca85 b5700e0 3d26c4f b5700e0 3d26c4f 7c42403 bcdf604 84c0568 bcdf604 b99c2aa bcdf604 3d26c4f 7c42403 c3dca85 7c42403 3d26c4f 7c42403 c3dca85 0d2984f 34bec84 b99c2aa 34bec84 c3dca85 b5700e0 7c42403 b5700e0 164c0fc 3d26c4f 7c42403 de6cf4c 7c42403 34bec84 c3dca85 164c0fc 34bec84 0d2984f 164c0fc c3dca85 164c0fc 34bec84 c3dca85 164c0fc 567e4e6 34bec84 567e4e6 34bec84 c3dca85 34bec84 c3dca85 7c42403 164c0fc 7c42403 8fb9069 7c42403 2029fc2 b7d680c a06a9ed 7eb5299 7c42403 567e4e6 7c42403 164c0fc 567e4e6 9de7f08 feeb9a4 e4a366f feeb9a4 e0b088f 0a55939 e0b088f e4a366f e0b088f e4a366f e0b088f b5700e0 7c42403 b5700e0 7c42403 164c0fc 9441294 164c0fc 9441294 164c0fc 9441294 164c0fc 9441294 164c0fc 7c42403 3d26c4f 7c42403 164c0fc c3dca85 164c0fc c3dca85 8fb9069 164c0fc b5700e0 164c0fc 8fb9069 b99c2aa 8fb9069 54f60e1 aa2f2ec 8fb9069 567e4e6 bcdf604 567e4e6 53d06aa 567e4e6 bcdf604 53d06aa 567e4e6 a0a2b84 aa2f2ec a0a2b84 aa2f2ec a0a2b84 8fb9069 b99c2aa aa2f2ec b99c2aa 567e4e6 aa2f2ec 567e4e6 b99c2aa 53d06aa b99c2aa aa2f2ec 84c0568 aa2f2ec 84c0568 b99c2aa 78afc38 b92e866 78afc38 c55a615 0d2984f 347f438 78afc38 b92e866 78afc38 347f438 7eb5299 78afc38 347f438 78afc38 54f60e1 8fb9069 aa2f2ec 6a2aca3 8fb9069 567e4e6 164c0fc c3dca85 8fb9069 164c0fc 7c42403 164c0fc e0b088f aa2f2ec e0b088f aa2f2ec e0b088f 164c0fc 567e4e6 7d5c7d3 72ecb17 7d5c7d3 2613038 7d5c7d3 40ba4ba 72ecb17 7d5c7d3 0a55939 e0b088f 7d5c7d3 72ecb17 7d5c7d3 b7d680c 7d5c7d3 b7d680c 7eb5299 0a55939 7eb5299 b7d680c 7eb5299 b7d680c 72ecb17 0a55939 7d5c7d3 0a55939 7eb5299 b7d680c 2613038 7d5c7d3 2613038 b7d680c e0b088f c0860b7 0a55939 e0b088f 0a55939 e0b088f 0a55939 e0b088f 0a55939 e0b088f b8ec270 c0860b7 b8ec270 0a55939 e0b088f b8ec270 c0860b7 b8ec270 e0b088f b8ec270 c0860b7 b8ec270 c0860b7 0a55939 c0860b7 e0b088f 0a55939 c0860b7 e0b088f c0860b7 0a55939 e0b088f c0860b7 0a55939 c0860b7 e0b088f 0a55939 e0b088f c0860b7 e0b088f c0860b7 e0b088f 29f4610 e0b088f 29f4610 e0b088f 29f4610 e0b088f 29f4610 1898de3 7eb5299 1898de3 29f4610 1898de3 29f4610 1898de3 29f4610 1898de3 29f4610 c0860b7 6a2aca3 aa2f2ec 6a2aca3 aa2f2ec 1898de3 aa2f2ec 6a2aca3 aa2f2ec 6a2aca3 1898de3 6a2aca3 7c42403 6a2aca3 aa2f2ec 6a2aca3 7c42403 6a2aca3 aa2f2ec 6a2aca3 164c0fc 3d26c4f 6a2aca3 de6cf4c b99c2aa 567e4e6 de6cf4c 6a2aca3 3d26c4f 164c0fc 9de7f08 b99c2aa 567e4e6 164c0fc 3d26c4f 164c0fc 3d26c4f 164c0fc 7c42403 b5700e0 164c0fc 7c42403 3d26c4f 6a2aca3 7c42403 650a052 de6cf4c 164c0fc 9de7f08 8fb9069 7eb5299 b99c2aa 567e4e6 7c42403 c55a615 7eb5299 c55a615 48eee7a 7eb5299 48eee7a 7c42403 b5700e0 7c42403 8fb9069 7c42403 48eee7a 7c42403 54f60e1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 | #!/usr/bin/env python3
"""
Balochi Latin translation API server for Syáhag.
Priority (understanding → English → target):
1) dataset.xlsx / dataset.csv — sentence search
2) words_balochi_to_english.xlsx — word senses
3) Balochi_grammar_rules.pdf — grammar understanding
4) AI (Groq) Balochi→English grounded by (1)–(3)
5) AI / Google Translate English→selected language
TranslateGemma adapter is NOT used.
"""
from __future__ import annotations
import asyncio
import os
import re
import time
import unicodedata
from collections import Counter, defaultdict
from heapq import nlargest
from pathlib import Path
import pandas as pd
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from google import genai
from google.genai import types
from google.genai.errors import APIError
from pydantic import BaseModel, Field
from pypdf import PdfReader
# Understanding stack — no TranslateGemma adapter.
GENAI_SUPPORT = [
"dataset",
"lexicon",
"grammar",
]
PRIORITIES = list(GENAI_SUPPORT)
DEFAULT_GROQ_MODEL = os.getenv("GROQ_MODEL", "openai/gpt-oss-120b")
GROQ_SUPPORT = [
f"groq:{DEFAULT_GROQ_MODEL}",
"google_translate",
]
# Adapter disabled for labzank pipeline (dataset/lexicon/grammar → EN → AI).
os.environ.setdefault("ENABLE_TRANSLATEGEMMA", "0")
# Skip LoRA drafts above these limits (long labzank posts time out on ZeroGPU).
TG_MAX_CHARS = int(os.getenv("TRANSLATEGEMMA_MAX_CHARS", "1200"))
TG_MAX_WORDS = int(os.getenv("TRANSLATEGEMMA_MAX_WORDS", "180"))
TG_MAX_LINES = int(os.getenv("TRANSLATEGEMMA_MAX_LINES", "6"))
# Chunk Gemini only for very long essays (many sequential calls can exceed 90s).
GENAI_CHUNK_CHARS = int(os.getenv("GENAI_CHUNK_CHARS", "5000"))
GENAI_CHUNK_PARAS = int(os.getenv("GENAI_CHUNK_PARAS", "10"))
TRANSLATEGEMMA_ADAPTER_DIR = os.getenv(
"TRANSLATEGEMMA_ADAPTER",
"Aynkader/translategemma-balochi-adapter",
)
TRANSLATEGEMMA_HUB = os.getenv(
"TRANSLATEGEMMA_HUB",
"Aynkader/translategemma-balochi-adapter",
)
# Optional .env support
try:
from dotenv import load_dotenv
load_dotenv(Path(__file__).resolve().parent / ".env")
except Exception:
pass
ROOT = Path(__file__).resolve().parent
STATIC_DIR = ROOT / "static"
GRAMMAR_PDF = ROOT / "Balochi_grammar_rules.pdf"
GRAMMAR_CACHE = ROOT / "Balochi_grammar_rules.cache.txt"
DATASET_CSV = ROOT / "Datasets" / "dataset.csv"
DATASET_CSV_ROOT = ROOT / "dataset.csv"
DATASET_XLSX = ROOT / "dataset.xlsx"
WORDS_CSV = ROOT / "Datasets" / "words_balochi_to_english.csv"
WORDS_CSV_ROOT = ROOT / "words_balochi_to_english.csv"
WORDS_XLSX = ROOT / "words_balochi_to_english.xlsx"
app = FastAPI(title="Balochi Latin Translation API", version="2.0")
_cors_origins = [
o.strip()
for o in os.getenv(
"CORS_ORIGINS",
"https://gwachen.com,https://www.gwachen.com,https://api.gwachen.com,https://labzank.com,https://www.labzank.com,http://127.0.0.1:8000,http://localhost:8000",
).split(",")
if o.strip()
]
app.add_middleware(
CORSMiddleware,
allow_origins=_cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
PATH_PREFIX = os.getenv("PATH_PREFIX", "").rstrip("/")
if PATH_PREFIX:
@app.middleware("http")
async def _strip_path_prefix(request, call_next):
path = request.scope.get("path", "")
if path == PATH_PREFIX or path.startswith(PATH_PREFIX + "/"):
new_path = path[len(PATH_PREFIX) :] or "/"
request.scope["path"] = new_path
raw = request.scope.get("raw_path")
if isinstance(raw, (bytes, bytearray)):
prefix_b = PATH_PREFIX.encode("utf-8")
if raw == prefix_b or raw.startswith(prefix_b + b"/"):
request.scope["raw_path"] = raw[len(prefix_b) :] or b"/"
return await call_next(request)
# Model configuration — match gwaschen production; override via .env
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-3.5-flash-lite")
GEMINI_FALLBACKS = [
m.strip()
for m in os.getenv(
"GEMINI_FALLBACKS",
"gemini-2.5-flash-lite,gemini-3.5-flash,gemini-3.6-flash,gemini-2.5-flash",
).split(",")
if m.strip()
]
GEMINI_RETRIES = int(os.getenv("GEMINI_RETRIES", "2"))
GEMINI_RETRY_BASE_SEC = float(os.getenv("GEMINI_RETRY_BASE_SEC", "1.5"))
GEMINI_THINKING_BUDGET = int(os.getenv("GEMINI_THINKING_BUDGET", "0"))
MAX_FEWSHOT = int(os.getenv("MAX_FEWSHOT", "12"))
MAX_LEXICON_HITS = int(os.getenv("MAX_LEXICON_HITS", "30"))
CORPUS_DIRECT_SCORE = float(os.getenv("CORPUS_DIRECT_SCORE", "0.92"))
GRAMMAR_CHARS = int(os.getenv("GRAMMAR_CHARS", "0"))
FEWSHOT_CANDIDATE_CAP = int(os.getenv("FEWSHOT_CANDIDATE_CAP", "400"))
# Lazy Gemini client initialization to prevent top-level import crashes
_gemini_client: genai.Client | None = None
# After free-tier 429, skip Gemini until this monotonic deadline (unix time).
_gemini_quota_until: float = 0.0
def get_gemini_client() -> genai.Client | None:
global _gemini_client
if _gemini_client is not None:
return _gemini_client
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print("[WARNING] GEMINI_API_KEY is not set. Gemini fallbacks will fail if invoked.")
return None
try:
_gemini_client = genai.Client(api_key=api_key)
return _gemini_client
except Exception as err:
print(f"[ERROR] Failed to initialize Gemini client: {err}")
return None
def _mark_gemini_quota(err: Exception | None = None) -> None:
global _gemini_quota_until
wait = _quota_retry_seconds(err) if err else 60.0
_gemini_quota_until = max(_gemini_quota_until, time.time() + wait)
print(f"[gemini] quota cooldown until +{wait:.0f}s")
def gemini_allowed() -> bool:
return time.time() >= _gemini_quota_until
BAL_FUNCTION = {
"ant": "are",
"ent": "is",
"at": "was",
"o": "and",
"ke": "that",
"pa": "for",
"cha": "from",
"man": "I",
"taw": "you",
"má": "we",
"shomá": "you (pl)",
"é": "this",
"á": "that/he/she",
}
CORE_ORTHOGRAPHY = """
You MUST follow Balóchiay Látini Syáhag (Balochi_grammar_rules.pdf) for ALL Balochi output.
Alphabet (only these): A Á B Ch D Dh E É G H I J K L M N O Ó P R S Sh T Th U W Y Z Zh
- Digraphs (jotkáb): Ch, Dh, Sh, Th, Zh
- Never invent letters f, q, v, x (map f→p, q→k, v→w, x→ks)
- Digits: Western 0-9 only (never Arabic-Indic / Persian digit shapes)
- Never output Arabic/Perso-Arabic script for Balochi
Vowels (kassháb): short a e o · long á é i ó u
- Long-vowel shortening before suffixes: Ráh+Band→Rahband, Sháh+Zádag→Shahzádag
Grammar (mandatory for Balochi Latin):
- Word order often SOV; copula ent / ant / at…
- Verb endings: man→án, má→én, taw→ay, shomá→et, á sg→t/it/ent, á pl→ant
- Nonpast = present/future (Man rawán = I go / I will go)
- Imperative: be-/bo- affirmative, ma- prohibitive
- Possessive clitics: -on/-en/-et/-o/-y/-esh; adjectives often -én (sharrén)
- Gemination (dhakk): poll/porr; digraph gemination drops first h (Bachch→Bacch)
Output rules:
- Reply with the translation ONLY (no quotes, no commentary)
- Preserve structure: one source line → one output line; blank lines stay blank
- SUPPORT ORDER: (1) dataset.xlsx sentence/word search
(2) words_balochi_to_english.xlsx lexicon glosses
(3) Balochi_grammar_rules.pdf for understanding
(4) Groq English, then Google Translate / Groq for the target language
- Match dataset.xlsx Latin Syáhag style (á/é/ó, digraphs, e.g. Thám)
"""
GRAMMAR_MUST_FOLLOW = """
MUST FOLLOW Balochi_grammar_rules.pdf for Balochi Latin output:
1) Latin Syáhag alphabet only (A Á B Ch D Dh E É G H I J K L M N O Ó P R S Sh T Th U W Y Z Zh)
2) No Arabic/Perso-Arabic letters; no f/q/v/x; Western digits 0-9
3) Prefer SOV; use correct endings án/én/ay/et/t|it|ent/ant and copula ent/ant/at
4) Imperatives with be-/bo- or prohibitive ma-
5) Prefer wording from dataset.xlsx examples when they match meaning
If a draft violates these rules, rewrite it so it obeys the PDF.
"""
ARABIC_SCRIPT_TARGETS = {
"urdu",
"arabic",
"persian",
"farsi",
"persian (farsi)",
"pashto",
"sindhi",
"dari",
"kurdish",
"sorani",
}
def norm(text: str) -> str:
text = unicodedata.normalize("NFC", str(text)).strip()
return re.sub(r"\s+", " ", text)
def norm_preserve_structure(text: str) -> str:
text = unicodedata.normalize("NFC", str(text)).replace("\r\n", "\n").replace("\r", "\n")
lines = [re.sub(r"[ \t]+", " ", ln).strip() for ln in text.split("\n")]
while lines and not lines[0]:
lines.pop(0)
while lines and not lines[-1]:
lines.pop()
return "\n".join(lines)
def structure_hint(text: str, text_format: str | None = None) -> str:
fmt = (text_format or "").strip().casefold()
lines = text.split("\n")
n = len(lines)
blank = sum(1 for ln in lines if not ln.strip())
if fmt == "poetry":
return (
"FORMAT: POETRY / VERSE. "
f"Source has {n} lines ({blank} blank stanza breaks). "
f"Your translation MUST have exactly {n} lines with the same blank-line pattern. "
"Keep poem layout: one source line → one output line; blank lines stay blank between stanzas. "
"Do NOT rewrite as a prose paragraph. Do NOT merge couplets into one sentence. "
"Do NOT translate only the first line — translate EVERY non-blank line."
)
if fmt == "story":
return (
"FORMAT: STORY / PROSE. "
"Translate as natural narrative paragraphs. "
"Use a blank line between paragraphs where the source has paragraph breaks. "
"Do NOT force a line-per-line verse layout."
)
if n <= 1:
return "The source is a single line or paragraph — keep it as one unit."
return (
f"The source has {n} lines ({blank} blank). "
f"Your translation MUST have exactly {n} lines with the same blank-line pattern. "
"Translate line-by-line; do not merge lines."
)
def wants_arabic_script(route: dict) -> bool:
if route["key"] != "bal-any":
return False
label = f"{route.get('target', '')} {route.get('target_label', '')}".casefold()
return any(name in label for name in ARABIC_SCRIPT_TARGETS)
def arabic_script_ratio(text: str) -> float:
letters = [c for c in text if c.isalpha()]
if not letters:
return 1.0
arabic = sum(1 for c in letters if "\u0600" <= c <= "\u06FF" or "\u0750" <= c <= "\u077F")
return arabic / len(letters)
def align_line_structure(source: str, translated: str) -> str:
src_lines = source.split("\n")
out = translated.replace("\r\n", "\n").replace("\r", "\n").strip()
out_lines = out.split("\n")
if len(out_lines) == len(src_lines):
aligned = "\n".join(ln.strip() for ln in out_lines)
return preserve_untranslated(source, aligned)
non_empty_src = [ln for ln in src_lines if ln.strip()]
if len(src_lines) == len(non_empty_src) and len(out_lines) == 1 and len(non_empty_src) > 1:
parts = re.split(r"(?<=[.!?۔؟؛])\s+", out)
parts = [p.strip() for p in parts if p.strip()]
if len(parts) == len(non_empty_src):
return preserve_untranslated(source, "\n".join(parts))
return preserve_untranslated(source, out)
def preserve_untranslated(source: str, translated: str | None) -> str:
"""Keep original Balochi for lines that failed or were left empty.
Aligns non-empty lines; blank lines stay blank. If the model returned fewer
lines, or a line looks like a refusal / empty, copy the source line as-is.
"""
src = (source or "").replace("\r\n", "\n").replace("\r", "\n")
dst = (translated or "").replace("\r\n", "\n").replace("\r", "\n")
if not src.strip():
return (dst or "").strip()
if not dst.strip():
return src
src_lines = src.split("\n")
dst_lines = dst.split("\n")
# Fast path: same shape — only replace empty / refusal lines.
if len(dst_lines) == len(src_lines):
fixed: list[str] = []
for s, d in zip(src_lines, dst_lines):
if not s.strip():
fixed.append("")
continue
d_st = d.strip()
if not d_st or _looks_like_mt_refusal(d_st):
fixed.append(s)
else:
fixed.append(d_st)
return "\n".join(fixed)
# Map non-empty → non-empty; rebuild with original blanks.
src_ne = [ln for ln in src_lines if ln.strip()]
dst_ne = [ln.strip() for ln in dst_lines if ln.strip()]
mapped: list[str] = []
for i, s in enumerate(src_ne):
if i < len(dst_ne):
d = dst_ne[i]
if not d or _looks_like_mt_refusal(d):
mapped.append(s)
else:
mapped.append(d)
else:
mapped.append(s) # no translation for this line → keep original
out: list[str] = []
mi = 0
for ln in src_lines:
if not ln.strip():
out.append("")
else:
out.append(mapped[mi] if mi < len(mapped) else ln)
mi += 1
return "\n".join(out)
def non_empty_line_count(text: str) -> int:
return sum(1 for ln in text.replace("\r\n", "\n").replace("\r", "\n").split("\n") if ln.strip())
def should_run_translategemma(text: str, text_format: str | None = None) -> bool:
"""TranslateGemma adapter is disabled — Groq + dataset/lexicon/grammar only."""
return False
def story_paragraphs(text: str) -> list[str]:
parts = re.split(r"\n\s*\n+", (text or "").strip())
return [p.strip() for p in parts if p.strip()]
def should_chunk_genai(text: str, text_format: str | None) -> bool:
if (text_format or "") == "poetry":
return False
paras = story_paragraphs(text)
return len(text) >= GENAI_CHUNK_CHARS or len(paras) >= GENAI_CHUNK_PARAS
def draft_covers_source(source: str, draft: str) -> bool:
"""Reject short one-liners when the source is a multi-line poem/story."""
draft = (draft or "").strip()
if not draft:
return False
src_n = non_empty_line_count(source)
if src_n <= 1:
return True
dst_n = non_empty_line_count(draft)
src_words = max(1, len(source.split()))
dst_words = len(draft.split())
# Need most lines, or enough words that it is clearly a full rendering.
min_lines = max(2, int(src_n * 0.6 + 0.999))
if dst_n >= min_lines:
return True
if dst_words >= max(12, int(src_words * 0.45)):
return True
return False
def norm_key(text: str) -> str:
return norm(text).casefold()
def tokenize(text: str) -> set[str]:
return {
t
for t in re.findall(
r"[A-Za-zÁÉÓŪĪáéóūīōŌZhzhChchDhShThdhshth]+",
text.casefold(),
)
if len(t) > 1
}
def fold_accents(text: str) -> str:
"""á/é/ó/ū/ī/ō → a/e/o/u/i/o for false-friend key matching."""
table = str.maketrans(
{
"á": "a",
"à": "a",
"ä": "a",
"é": "e",
"è": "e",
"ë": "e",
"í": "i",
"ì": "i",
"ī": "i",
"ó": "o",
"ò": "o",
"ö": "o",
"ō": "o",
"ú": "u",
"ù": "u",
"ü": "u",
"ū": "u",
}
)
return (text or "").casefold().translate(table)
# LoRA false friends: folded Balochi lemma → English strings that mean the WRONG sense.
LEXICON_FALSE_FRIENDS: dict[str, list[str]] = {
"hajjam": [
"pilgrim",
"pilgrims",
"kaaba",
"ka'ba",
"ka'bah",
"mecca",
"makkah",
"hajj",
"pilgrimage",
],
}
def lexicon_draft_conflicts(source: str, draft: str, route_key: str) -> list[str]:
"""If the LoRA draft contradicts clear lexicon senses, list conflicts."""
if route_key != "bal-en" or not draft:
return []
draft_l = f" {draft.casefold()} "
conflicts: list[str] = []
hits = lexicon_hits(source, "bal-en", limit=50)
hit_by_fold = {fold_accents(tok): (tok, meaning) for tok, meaning in hits}
# Also catch source tokens that map via folded false-friend keys
# (lexicon stores Hajjám → key "hajjám"; false friends use "hajjam").
for tok in tokenize(source):
folded = fold_accents(tok)
wrongs = LEXICON_FALSE_FRIENDS.get(folded, [])
if not wrongs:
continue
meaning = hit_by_fold.get(folded, (tok, "Barber"))[1]
if folded in hit_by_fold:
tok, meaning = hit_by_fold[folded]
elif fold_accents("hajjám") == folded:
meaning = RESOURCES["bal_lex"].get("hajjám") or "Barber"
tok = "hajjám"
for w in wrongs:
needle = w.casefold().strip()
if not needle:
continue
# Word-ish match so "hajj" does not require trailing space.
if re.search(rf"(?<![a-z]){re.escape(needle)}(?![a-z])", draft_l):
conflicts.append(
f"{tok} means '{meaning}' (not '{needle}')"
)
break
return conflicts
def draft_misses_lexicon_senses(source: str, draft: str, route_key: str) -> bool:
"""True when the draft ignores clear lexicon glosses from the source (hallucination)."""
if route_key != "bal-en" or not draft:
return False
hits = lexicon_hits(source, "bal-en", limit=20)
if len(hits) < 2:
return False
draft_l = draft.casefold()
matched = 0
for _tok, meaning in hits:
head = re.split(r"[.;/]", meaning.casefold())[0].strip()
words = [w for w in re.findall(r"[a-z]{4,}", head)]
if any(w in draft_l for w in words):
matched += 1
return matched == 0
def load_grammar_text() -> str:
if GRAMMAR_PDF.exists():
reader = PdfReader(str(GRAMMAR_PDF))
parts: list[str] = []
for page in reader.pages:
try:
parts.append(page.extract_text() or "")
except Exception:
continue
text = re.sub(r"\n{3,}", "\n\n", "\n".join(parts)).strip()
if text:
GRAMMAR_CACHE.write_text(text, encoding="utf-8")
return text
if GRAMMAR_CACHE.exists():
return GRAMMAR_CACHE.read_text(encoding="utf-8", errors="ignore")
return ""
def extract_grammar_rules(full: str, max_chars: int = 0) -> str:
if not full:
return "(grammar PDF unavailable)"
start = 0
m = re.search(r"A\s*Á\s*B\s*Ch", full)
if m:
start = m.start()
else:
for needle in ("Balóchiay Áb", "Kassháb", "Kasshab", "Jotkáb"):
i = full.find(needle)
if i >= 0:
start = i
break
body = full[start:].strip()
if not body:
body = full.strip()
if max_chars and max_chars > 0:
body = body[:max_chars]
return body
def violates_syahag(text: str) -> list[str]:
issues: list[str] = []
if re.search(r"[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]", text):
issues.append("contains_arabic_script")
if re.search(r"[fqvxFQVX]", text):
issues.append("forbidden_letters_fqvx")
if re.search(r"[\u0660-\u0669\u06F0-\u06F9]", text):
issues.append("non_western_digits")
return issues
def _first_existing(*paths: Path) -> Path | None:
for pth in paths:
if pth.exists():
return pth
return None
def _read_table(*candidates: Path):
path = _first_existing(*candidates)
if path is None:
names = ", ".join(pth.name for pth in candidates)
raise FileNotFoundError(f"Missing table among: {names}")
if path.suffix.lower() == ".csv":
return pd.read_csv(path), path
return pd.read_excel(path), path
def load_resources():
dataset_df, dataset_path = _read_table(DATASET_CSV, DATASET_CSV_ROOT, DATASET_XLSX)
pairs: list[dict] = []
en_to_bal: dict[str, str] = {}
bal_to_en: dict[str, str] = {}
for en, bal in zip(dataset_df["English_sentences"], dataset_df["Balochi_sentences"]):
if pd.isna(en) or pd.isna(bal):
continue
en_s, bal_s = norm(en), norm(bal)
if not en_s or not bal_s:
continue
pairs.append({"en": en_s, "bal": bal_s, "en_toks": tokenize(en_s), "bal_toks": tokenize(bal_s)})
en_to_bal.setdefault(norm_key(en_s), bal_s)
bal_to_en.setdefault(norm_key(bal_s), en_s)
# Lexicon (helps gwachen quality on CPU when TranslateGemma is unavailable).
bal_lex: dict[str, str] = {}
en_lex: dict[str, str] = {}
words_path = None
try:
words_df, words_path = _read_table(WORDS_CSV, WORDS_CSV_ROOT, WORDS_XLSX)
for bal, meaning in zip(words_df["balochi_word"], words_df["english_meaning"]):
if pd.isna(bal) or pd.isna(meaning):
continue
bal_s, en_s = norm(bal), norm(meaning)
if not bal_s or not en_s:
continue
bal_lex.setdefault(norm_key(bal_s), en_s)
en_lex.setdefault(norm_key(en_s), bal_s)
except FileNotFoundError:
words_path = None
grammar = load_grammar_text()
grammar_excerpt = extract_grammar_rules(grammar, GRAMMAR_CHARS)
bal_index: dict[str, list[int]] = defaultdict(list)
en_index: dict[str, list[int]] = defaultdict(list)
for i, pair in enumerate(pairs):
for tok in pair["bal_toks"]:
bal_index[tok].append(i)
for tok in pair["en_toks"]:
en_index[tok].append(i)
print(
f"[startup] dataset={dataset_path} words={words_path} "
f"pairs={len(pairs):,} lexicon={len(bal_lex):,} "
f"grammar_chars={len(grammar_excerpt):,} model={GEMINI_MODEL}"
)
return {
"pairs": pairs,
"en_to_bal": en_to_bal,
"bal_to_en": bal_to_en,
"bal_lex": bal_lex,
"en_lex": en_lex,
"grammar_excerpt": grammar_excerpt,
"bal_index": bal_index,
"en_index": en_index,
"dataset_path": str(dataset_path),
"words_path": str(words_path) if words_path else None,
}
RESOURCES = load_resources()
BAL_ALIASES = {
"balochi",
"baluchi",
"bal",
"bal_latn",
"balochi-latin",
"latin-balochi",
"balochi latin",
}
EN_ALIASES = {"english", "en", "eng"}
TARGET_LANGUAGES = [
("english", "English"),
("urdu", "Urdu"),
("persian", "Persian (Farsi)"),
("arabic", "Arabic"),
("hindi", "Hindi"),
("pashto", "Pashto"),
("sindhi", "Sindhi"),
("brahui", "Brahui"),
("turkish", "Turkish"),
("spanish", "Spanish"),
("french", "French"),
("german", "German"),
("portuguese", "Portuguese"),
("russian", "Russian"),
("chinese", "Chinese"),
("japanese", "Japanese"),
("korean", "Korean"),
("italian", "Italian"),
("dutch", "Dutch"),
("swedish", "Swedish"),
("indonesian", "Indonesian"),
("bengali", "Bengali"),
("punjabi", "Punjabi"),
("gujarati", "Gujarati"),
("marathi", "Marathi"),
("tamil", "Tamil"),
("telugu", "Telugu"),
("malayalam", "Malayalam"),
("thai", "Thai"),
("vietnamese", "Vietnamese"),
("polish", "Polish"),
("ukrainian", "Ukrainian"),
("greek", "Greek"),
("hebrew", "Hebrew"),
("swahili", "Swahili"),
]
LANG_BY_NAME = {name: code for code, name in TARGET_LANGUAGES}
LANG_BY_NAME.update({code: code for code, _name in TARGET_LANGUAGES})
LANG_BY_NAME["Persian (Farsi)"] = "persian"
LANG_BY_NAME["Farsi"] = "persian"
def is_balochi(lang: str) -> bool:
return lang.strip().casefold() in BAL_ALIASES
def is_english(lang: str) -> bool:
return lang.strip().casefold() in EN_ALIASES
def parse_route(source_lang: str, target_lang: str) -> dict:
src = source_lang.strip()
tgt = target_lang.strip()
if not src or not tgt:
raise HTTPException(status_code=400, detail="source_lang and target_lang are required")
if is_balochi(tgt):
if is_english(src):
return {
"key": "en-bal",
"source": "english",
"target": "balochi",
"target_label": "Balochi Latin Syáhag",
"source_label": "English",
}
if is_balochi(src):
raise HTTPException(
status_code=400,
detail="Source and target cannot both be Balochi. Choose another source language.",
)
label = src
for code, name in TARGET_LANGUAGES:
if src.casefold() in {code, name.casefold()}:
label = name
break
return {
"key": "any-bal",
"source": src.casefold(),
"target": "balochi",
"target_label": "Balochi Latin Syáhag",
"source_label": label,
}
if is_balochi(src) and is_english(tgt):
return {
"key": "bal-en",
"source": "balochi",
"target": "english",
"target_label": "English",
}
if is_balochi(src):
label = tgt
for code, name in TARGET_LANGUAGES:
if tgt.casefold() in {code, name.casefold()}:
label = name
break
return {
"key": "bal-any",
"source": "balochi",
"target": tgt.casefold(),
"target_label": label,
}
raise HTTPException(
status_code=400,
detail=(
"Supported: Balochi → any language, or any language → Balochi. "
f"Got source={source_lang!r} target={target_lang!r}"
),
)
def exact_lookup(text: str, route_key: str) -> str | None:
key = norm_key(text)
if route_key == "en-bal":
return RESOURCES["en_to_bal"].get(key) or RESOURCES["en_lex"].get(key)
if route_key in {"bal-en", "bal-any"}:
return RESOURCES["bal_to_en"].get(key) or RESOURCES["bal_lex"].get(key)
return None
def lexicon_hits(text: str, route_key: str, limit: int = MAX_LEXICON_HITS) -> list[tuple[str, str]]:
toks = tokenize(text)
if not toks:
return []
lex = RESOURCES["en_lex"] if route_key == "en-bal" else RESOURCES["bal_lex"]
out: list[tuple[str, str]] = []
seen: set[str] = set()
for tok in toks:
k = norm_key(tok)
if k in seen:
continue
hit = lex.get(k)
if hit:
seen.add(k)
out.append((tok, hit))
if len(out) >= limit:
break
return out
def retrieve_fewshot(text: str, route_key: str, k: int = MAX_FEWSHOT) -> list[tuple[float, dict]]:
q_toks = tokenize(text)
if not q_toks:
return []
index = RESOURCES["en_index"] if route_key == "en-bal" else RESOURCES["bal_index"]
pairs = RESOURCES["pairs"]
hit_counts: dict[int, int] = defaultdict(int)
for tok in sorted(q_toks, key=lambda t: len(index.get(t, []))):
postings = index.get(tok)
if not postings:
continue
for idx in postings:
hit_counts[idx] += 1
if len(hit_counts) >= FEWSHOT_CANDIDATE_CAP * 3:
break
if not hit_counts:
return []
cand_ids = [
idx
for idx, _ in nlargest(
FEWSHOT_CANDIDATE_CAP,
hit_counts.items(),
key=lambda item: item[1],
)
]
scored: list[tuple[float, dict]] = []
for idx in cand_ids:
pair = pairs[idx]
ref = pair["en_toks"] if route_key == "en-bal" else pair["bal_toks"]
if not ref:
continue
inter = len(q_toks & ref)
if inter == 0:
continue
union = len(q_toks | ref)
score = inter / union
src = pair["en"] if route_key == "en-bal" else pair["bal"]
length_penalty = abs(len(src) - len(text)) / max(len(text), 1)
score = score - 0.05 * min(length_penalty, 2.0)
if score >= 0.08:
scored.append((score, pair))
scored.sort(key=lambda x: x[0], reverse=True)
out: list[tuple[float, dict]] = []
seen = set()
for score, pair in scored:
sig = (pair["en"], pair["bal"])
if sig in seen:
continue
seen.add(sig)
out.append((score, pair))
if len(out) >= k:
break
return out
def offline_fallback(
text: str,
route_key: str,
fewshot: list[tuple[float, dict]],
target_label: str,
) -> tuple[str, dict] | None:
if route_key == "bal-any":
return None
# Never replace a multi-line poem/story with a single dataset sentence.
if non_empty_line_count(text) > 1:
return None
to_bal = route_key in {"en-bal", "any-bal"}
if fewshot:
score, pair = fewshot[0]
if score >= 0.22:
tgt = pair["bal"] if to_bal else pair["en"]
return tgt, {
"reason": "closest_dataset_example",
"score": round(score, 3),
"example": {"en": pair["en"], "bal": pair["bal"]},
}
if fewshot:
score, pair = fewshot[0]
tgt = pair["bal"] if to_bal else pair["en"]
return tgt, {
"reason": "weak_dataset_example",
"score": round(score, 3),
"example": {"en": pair["en"], "bal": pair["bal"]},
}
return None
def _is_retryable(err: Exception) -> bool:
msg = str(err).casefold()
needles = (
"high demand",
"resource_exhausted",
"unavailable",
"try again",
"rate",
"quota",
"429",
"503",
"overloaded",
"temporarily",
"spikes in demand",
"capacity",
"server error",
"internal error",
"deadline",
"timeout",
"timed out",
"free_tier",
)
return any(n in msg for n in needles)
def _is_quota_exhausted(err: Exception) -> bool:
msg = str(err).casefold()
return any(
n in msg
for n in (
"exceeded your current quota",
"free_tier",
"quota exceeded",
"resource_exhausted",
"rate limit",
)
)
def _quota_retry_seconds(err: Exception) -> float:
"""Parse 'Please retry in 30.29s' from Gemini quota errors."""
msg = str(err)
m = re.search(r"retry in\s+([0-9]+(?:\.[0-9]+)?)\s*s", msg, flags=re.I)
if m:
return min(45.0, max(5.0, float(m.group(1)) + 1.0))
return 20.0
def generate_with_retries(prompt: str, *, light: bool = False) -> tuple[str, str]:
if not gemini_allowed():
raise RuntimeError(
f"Gemini free-tier cooldown active; retry in "
f"{max(1, int(_gemini_quota_until - time.time()))}s"
)
client = get_gemini_client()
if client is None:
raise RuntimeError("GEMINI_API_KEY is not configured on the server.")
models = [GEMINI_MODEL] + [m for m in GEMINI_FALLBACKS if m != GEMINI_MODEL]
last_err: Exception | None = None
system = (
"You are a careful literary translator. "
"Reply with the translation ONLY — no preface."
if light
else build_system_prompt()
)
config_kwargs: dict = {"system_instruction": system, "temperature": 0.1}
if (not light) and GEMINI_THINKING_BUDGET >= 0:
try:
config_kwargs["thinking_config"] = types.ThinkingConfig(
thinking_budget=GEMINI_THINKING_BUDGET
)
except Exception:
pass
gen_config = types.GenerateContentConfig(**config_kwargs)
quota_hits = 0
for model in models:
for attempt in range(1, GEMINI_RETRIES + 1):
try:
response = client.models.generate_content(
model=model,
contents=prompt,
config=gen_config,
)
text = (response.text or "").strip()
if not text:
raise RuntimeError("Empty model response")
return text, model
except Exception as err:
last_err = err
msg = str(err).casefold()
bad_model = any(
n in msg
for n in (
"not found",
"not_found",
"invalid",
"404",
"unsupported",
"no longer available",
"update your code",
"deprecated",
)
)
# Free-tier quota: do NOT sleep 30–45s here (client already waits).
# One quick retry only — cascading models burns the 20/min limit.
if _is_quota_exhausted(err):
quota_hits += 1
_mark_gemini_quota(err)
print(f"[gemini] quota on {model} (hit={quota_hits})")
if quota_hits >= 2:
raise err
time.sleep(1.5)
continue
high_demand = any(
n in msg
for n in (
"high demand",
"spikes in demand",
"overloaded",
"429",
"503",
)
)
if high_demand:
time.sleep(min(12.0, GEMINI_RETRY_BASE_SEC * (2 ** (attempt - 1))))
if attempt >= 2:
break
continue
if attempt < GEMINI_RETRIES and _is_retryable(err):
time.sleep(GEMINI_RETRY_BASE_SEC * attempt)
continue
if _is_retryable(err) or bad_model:
break
raise
assert last_err is not None
if _is_quota_exhausted(last_err):
_mark_gemini_quota(last_err)
raise last_err
def build_pivot_prompt(
english: str,
target_label: str,
*,
text_format: str | None,
arabic_script: bool,
) -> str:
"""Small EN→target prompt (avoids huge Balochi grammar system prompt)."""
parts = [
f"Translate from English to {target_label}.",
"Keep meaning faithful. Reply with the translation ONLY.",
structure_hint(english, text_format),
]
if arabic_script:
parts.append(
f"CRITICAL: Write the entire {target_label} translation in Arabic / "
"Perso-Arabic script only. No Latin transliteration."
)
else:
parts.append(f"Use the standard writing system for {target_label}.")
parts.append(f"English source:\n{english}\n\n{target_label} translation:")
return "\n\n".join(parts)
# Free EN→target when Gemini quota is dead (Google Translate + Groq + HF).
GOOGLE_TL = {
"english": "en",
"urdu": "ur",
"arabic": "ar",
"persian": "fa",
"farsi": "fa",
"hindi": "hi",
"pashto": "ps",
"sindhi": "sd",
"turkish": "tr",
"spanish": "es",
"french": "fr",
"german": "de",
"portuguese": "pt",
"russian": "ru",
"chinese": "zh-CN",
"japanese": "ja",
"korean": "ko",
"italian": "it",
"dutch": "nl",
"swedish": "sv",
"indonesian": "id",
"bengali": "bn",
"punjabi": "pa",
"gujarati": "gu",
"marathi": "mr",
"tamil": "ta",
"telugu": "te",
"malayalam": "ml",
"thai": "th",
"vietnamese": "vi",
"polish": "pl",
"ukrainian": "uk",
"greek": "el",
"hebrew": "he",
"swahili": "sw",
}
NLLB_TARGET = {
"urdu": "urd_Arab",
"arabic": "arb_Arab",
"persian": "pes_Arab",
"farsi": "pes_Arab",
"hindi": "hin_Deva",
"pashto": "pbt_Arab",
"sindhi": "snd_Arab",
"turkish": "tur_Latn",
"spanish": "spa_Latn",
"french": "fra_Latn",
"german": "deu_Latn",
"portuguese": "por_Latn",
"russian": "rus_Cyrl",
"chinese": "zho_Hans",
"japanese": "jpn_Jpan",
"korean": "kor_Hang",
"italian": "ita_Latn",
"dutch": "nld_Latn",
"swedish": "swe_Latn",
"indonesian": "ind_Latn",
"bengali": "ben_Beng",
"punjabi": "pan_Guru",
"gujarati": "guj_Gujr",
"marathi": "mar_Deva",
"tamil": "tam_Taml",
"telugu": "tel_Telu",
"malayalam": "mal_Mlym",
"thai": "tha_Thai",
"vietnamese": "vie_Latn",
"polish": "pol_Latn",
"ukrainian": "ukr_Cyrl",
"greek": "ell_Grek",
"hebrew": "heb_Hebr",
"swahili": "swh_Latn",
}
OPUS_EN_TARGET = {
"urdu": "Helsinki-NLP/opus-mt-en-ur",
"arabic": "Helsinki-NLP/opus-mt-en-ar",
"hindi": "Helsinki-NLP/opus-mt-en-hi",
"french": "Helsinki-NLP/opus-mt-en-fr",
"german": "Helsinki-NLP/opus-mt-en-de",
"spanish": "Helsinki-NLP/opus-mt-en-es",
"russian": "Helsinki-NLP/opus-mt-en-ru",
"chinese": "Helsinki-NLP/opus-mt-en-zh",
}
def _hf_token() -> str:
return (
os.getenv("HF_TOKEN")
or os.getenv("HUGGING_FACE_HUB_TOKEN")
or os.getenv("HUGGINGFACE_HUB_TOKEN")
or ""
).strip()
def _hf_en_to_target(english: str, target_code: str, target_label: str) -> tuple[str, str] | None:
"""EN→target via Hugging Face Inference (opus-mt / NLLB)."""
english = (english or "").strip()
if not english:
return None
code = (target_code or "").casefold().strip()
token = _hf_token()
if not token:
return None
try:
from huggingface_hub import InferenceClient
client = InferenceClient(token=token)
opus = OPUS_EN_TARGET.get(code)
if opus:
try:
out = client.translation(english, model=opus)
if isinstance(out, list):
out = out[0] if out else ""
if isinstance(out, dict):
out = out.get("translation_text") or out.get("generated_text") or ""
text = str(out).strip()
if text:
return text, opus
except Exception as err:
print(f"[free_mt] opus {opus} failed: {err}")
nllb_tgt = NLLB_TARGET.get(code)
if nllb_tgt:
try:
out = client.translation(
english,
model="facebook/nllb-200-distilled-600M",
src_lang="eng_Latn",
tgt_lang=nllb_tgt,
)
if isinstance(out, list):
out = out[0] if out else ""
if isinstance(out, dict):
out = out.get("translation_text") or out.get("generated_text") or ""
text = str(out).strip()
if text:
return text, "facebook/nllb-200-distilled-600M"
except Exception as err:
print(f"[free_mt] nllb failed: {err}")
except Exception as err:
print(f"[free_mt] hf client failed: {err}")
return None
def groq_configured() -> bool:
return bool((os.getenv("GROQ_API_KEY") or "").strip())
def groq_model_id() -> str:
return (os.getenv("GROQ_MODEL") or DEFAULT_GROQ_MODEL).strip() or DEFAULT_GROQ_MODEL
def groq_support_stack() -> list[str]:
return list(GROQ_SUPPORT)
def mt_support_stack() -> list[str]:
"""Public support labels: adapter/dataset/grammar/lexicon first, then Groq/Google."""
return list(PRIORITIES) + ([f"groq:{groq_model_id()}", "google_translate"] if groq_configured() else [])
def build_mt_grounding(text: str, route_key: str) -> str:
"""Binding senses from dataset + lexicon + grammar (no adapter)."""
rk = "en-bal" if route_key in {"en-bal", "any-bal"} else "bal-en"
parts: list[str] = [
"UNDERSTANDING PIPELINE (binding order):",
"1) Search dataset.xlsx for matching Balochi sentences → English parallels",
"2) Look up words in words_balochi_to_english.xlsx",
"3) Apply Balochi_grammar_rules.pdf for morphology/syntax sense",
"4) Produce faithful translation from the Balochi source using (1)–(3)",
"KEEP-ORIGINAL RULE (mandatory): If a word, phrase, or whole line has no "
"clear sense from dataset/lexicon/grammar and you cannot translate it "
"confidently, copy the original Balochi Latin text unchanged into that "
"place. Do NOT invent, skip, omit, or leave a blank for that part.",
"Do NOT invent unrelated themes. Do NOT use TranslateGemma adapter drafts.",
]
# Exact / near sentence hits first
few = retrieve_fewshot(text, rk, k=8)
if few:
parts.append("BINDING dataset.xlsx sentence matches:")
for score, pair in few:
parts.append(
f"[score={score:.2f}]\nBalochi: {pair['bal']}\nEnglish: {pair['en']}"
)
# Also search each non-empty line as its own query
line_hits = 0
for ln in (text or "").split("\n"):
raw = ln.strip()
if not raw or line_hits >= 6:
continue
exact = exact_lookup(raw, "bal-en" if rk == "bal-en" else "en-bal")
if exact:
parts.append(f"EXACT dataset/lexicon line:\nBalochi: {raw}\nEnglish: {exact}")
line_hits += 1
continue
sub = retrieve_fewshot(raw, rk, k=1)
if sub and sub[0][0] >= 0.2:
score, pair = sub[0]
parts.append(
f"LINE dataset match [score={score:.2f}]:\n"
f"Balochi: {pair['bal']}\nEnglish: {pair['en']}"
)
line_hits += 1
lex = lexicon_hits(text, rk, limit=40)
if lex:
parts.append("BINDING lexicon (words_balochi_to_english.xlsx):")
for bal, en in lex:
parts.append(f"- {bal} = {en}")
grammar = (RESOURCES.get("grammar_excerpt") or "").strip()
if grammar:
parts.append(
"BINDING grammar (Balochi_grammar_rules.pdf) — use for understanding "
"the Balochi Latin source before writing English:\n"
+ grammar[:1800]
)
return "\n".join(parts)
def _looks_like_mt_refusal(text: str) -> bool:
"""True if the model refused instead of translating."""
t = (text or "").casefold()
if len(t) > 280:
return False
needles = (
"cannot translate",
"can't translate",
"do not know how to translate",
"don't know how to translate",
"i'm unable to translate",
"i am unable to translate",
"unable to translate",
"as an ai",
"ترجمہ کیسے",
"ترجمہ نہیں",
"نہیں جانتا",
)
return any(n in t for n in needles)
def _looks_like_hallucinated_mt(source: str, translated: str) -> bool:
"""Catch generic invented poems / repeated lines that ignore the source."""
src_n = non_empty_line_count(source)
dst_n = non_empty_line_count(translated)
if src_n >= 2 and dst_n < max(1, int(src_n * 0.7 + 0.001)):
return True
lines = [ln.strip() for ln in (translated or "").split("\n") if ln.strip()]
if len(lines) >= 3:
top_n = Counter(lines).most_common(1)[0][1]
if top_n >= 2 and top_n >= len(lines) // 2:
return True
# Generic English filler often produced when the model ignores Balochi.
en = (translated or "").casefold()
generic = (
"purpose of life",
"wandering soul",
"according to this account",
"restless and tormented",
"night spreads its darkness",
)
if src_n >= 2 and any(g in en for g in generic):
# Allow only if source somehow matches — these almost always mean hallucination.
return True
return False
def _english_misses_lexicon(
source: str, english: str, *, strict: bool = False
) -> bool:
"""True when binding lexicon glosses are absent from the English draft."""
hits = lexicon_hits(source, "bal-en", limit=24)
if len(hits) < 3:
return False
en_fold = fold_accents(english or "").casefold()
matched = 0
stop = {
"the", "and", "for", "with", "from", "that", "this", "into", "onto",
"over", "under", "have", "been", "were", "will", "shall", "also",
}
for _bal, gloss in hits:
words = [
w
for w in re.findall(r"[A-Za-z]{4,}", gloss or "")
if w.casefold() not in stop
]
if any(w.casefold() in en_fold for w in words):
matched += 1
if strict:
need = max(2, int(len(hits) * 0.35 + 0.999))
return matched < need
# Soft: only reject total misses (used for Groq output filter).
return matched == 0
def _number_source_lines(text: str) -> str:
"""Number non-empty lines so the model must return the same count."""
out: list[str] = []
n = 0
for ln in (text or "").split("\n"):
if not ln.strip():
out.append("")
continue
n += 1
out.append(f"{n}. {ln.strip()}")
return "\n".join(out)
def _strip_line_numbers(text: str) -> str:
return re.sub(r"(?m)^\s*\d+\s*[.)\-:]\s*", "", text or "")
def _groq_extract_content(data: dict) -> tuple[str, str, dict]:
"""Return (text, finish_reason, usage) from a Groq chat completion."""
choice = (data.get("choices") or [{}])[0]
msg = choice.get("message") or {}
text = (msg.get("content") or "").strip().strip('"').strip("'")
finish = str(choice.get("finish_reason") or "")
usage = data.get("usage") or {}
return text, finish, usage if isinstance(usage, dict) else {}
def groq_chat_translate(
source_text: str,
*,
source_label: str,
target_code: str,
target_label: str,
grounding: str | None = None,
route_key: str | None = None,
reject_filters: bool = True,
) -> tuple[str, str] | None:
"""Direct Groq chat translation (Balochi or English → target), optionally grounded."""
groq_key = (os.getenv("GROQ_API_KEY") or "").strip()
source_text = (source_text or "").strip()
if not groq_key or not source_text:
return None
code = (target_code or "").casefold().strip()
label = (target_label or code).strip()
arabic = code in {
"urdu",
"arabic",
"persian",
"farsi",
"pashto",
"sindhi",
"hebrew",
}
src_lines = non_empty_line_count(source_text)
rk = route_key or (
"bal-en"
if "balochi" in source_label.casefold()
else ("en-bal" if code.startswith("bal") else "bal-en")
)
ground = (grounding or "").strip() or build_mt_grounding(source_text, rk)
try:
import httpx
model = groq_model_id()
max_out = int(os.getenv("GROQ_MAX_COMPLETION_TOKENS", "8192"))
script = (
f"Write {label} in Arabic/Perso-Arabic script only (not Latin/Roman). "
if arabic
else f"Use the standard writing system for {label}. "
)
numbered = _number_source_lines(source_text)
prompt = (
f"{ground}\n\n"
f"Faithfully translate from {source_label} to {label}.\n"
f"{script}\n"
f"There are {src_lines} non-empty source lines. "
f"Return exactly {src_lines} non-empty output lines "
"(same blank-line stanza breaks). "
"Use BINDING lexicon/dataset senses above. "
"Translate each numbered line's meaning — do NOT invent a new poem, "
"do NOT summarize, do NOT repeat lines, do NOT transliterate "
"letter-by-letter. "
"KEEP-ORIGINAL: if a word/phrase/line cannot be translated from the "
"bindings, leave that Balochi Latin text as-is in the output. "
"Never refuse. Reply with the translation ONLY "
"(you may keep or drop the numbers).\n\n"
f"{source_label}:\n{numbered}\n\n{label}:"
)
body: dict = {
"model": model,
"temperature": 0.05,
"max_completion_tokens": max_out,
"messages": [
{
"role": "system",
"content": (
"You are a careful literary translator for Balochi Latin "
"(Syáhag). Obey lexicon + dataset.xlsx + grammar rules. "
"Translate meaning line-by-line. Never invent themes. "
"If you cannot translate a word or line, keep the original "
"Balochi Latin text in that place. "
"Never refuse. Reply with the translation only."
),
},
{"role": "user", "content": prompt},
],
}
if "gpt-oss" in model.casefold():
body["reasoning_effort"] = os.getenv("GROQ_REASONING_EFFORT", "low")
filters_on = reject_filters
for attempt in range(3):
r = httpx.post(
"https://api.groq.com/openai/v1/chat/completions",
headers={
"Authorization": f"Bearer {groq_key}",
"Content-Type": "application/json",
},
json=body,
timeout=90.0,
)
if r.status_code == 429:
print(f"[groq] HTTP 429: {r.text[:300]}")
time.sleep(2.5 * (attempt + 1))
continue
if r.status_code >= 300:
print(f"[groq] HTTP {r.status_code}: {r.text[:300]}")
return None
data = r.json()
text_out, finish, usage = _groq_extract_content(data)
text_out = _strip_line_numbers(text_out)
if text_out and _looks_like_mt_refusal(text_out):
print(f"[groq] refusal-like output: {text_out[:160]!r}")
text_out = ""
if filters_on and text_out and _looks_like_hallucinated_mt(source_text, text_out):
print(
f"[groq] hallucinated/short output rejected ({src_lines} src lines)"
)
text_out = ""
if (
filters_on
and text_out
and rk == "bal-en"
and label.casefold().startswith("english")
and _english_misses_lexicon(source_text, text_out)
):
print("[groq] rejected: English misses binding lexicon glosses")
text_out = ""
if text_out:
return text_out, f"groq:{model}"
details = usage.get("completion_tokens_details") or {}
reasoning_n = details.get("reasoning_tokens")
print(
f"[groq] empty/bad content finish={finish!r} "
f"reasoning_tokens={reasoning_n} usage={usage}"
)
if finish == "length" and attempt == 0:
body["max_completion_tokens"] = min(max_out * 2, 32768)
continue
# Drop quality filters so labzank never hard-fails empty.
if filters_on:
filters_on = False
continue
return None
except Exception as err:
print(f"[groq] failed: {err}")
return None
def groq_translate_chunked(
source_text: str,
*,
source_label: str,
target_code: str,
target_label: str,
text_format: str | None = None,
route_key: str | None = None,
adapter_draft: str | None = None,
strict_quality: bool = True,
) -> tuple[str, str] | None:
"""Translate with dataset/lexicon/grammar grounding; always try a best-effort fallback."""
source_text = (source_text or "").strip()
if not source_text:
return None
rk = route_key or (
"bal-en"
if "balochi" in source_label.casefold()
else ("en-bal" if (target_code or "").casefold().startswith("bal") else "bal-en")
)
ground = build_mt_grounding(source_text, rk)
# adapter_draft ignored — TranslateGemma is disabled for this pipeline.
hit = groq_chat_translate(
source_text,
source_label=source_label,
target_code=target_code,
target_label=target_label,
grounding=ground,
route_key=rk,
reject_filters=strict_quality,
)
if hit:
return preserve_untranslated(source_text, hit[0]), hit[1]
parts = story_paragraphs(source_text)
src_lines = non_empty_line_count(source_text)
should_chunk = len(parts) >= 3 or src_lines >= 8 or (text_format or "") == "poetry"
if not should_chunk or len(parts) < 2:
fallback = groq_chat_translate(
source_text,
source_label=source_label,
target_code=target_code,
target_label=target_label,
grounding=ground,
route_key=rk,
reject_filters=False,
)
if fallback:
return preserve_untranslated(source_text, fallback[0]), fallback[1]
# Nothing translated — keep original Balochi in place.
return source_text, f"groq:{groq_model_id()}:keep_original"
group_size = 1 if (text_format or "") == "poetry" or len(parts) >= 4 else 2
out_parts: list[str] = []
model_id = ""
failed = 0
n_groups = 0
for i in range(0, len(parts), group_size):
n_groups += 1
chunk = "\n\n".join(parts[i : i + group_size])
chunk_ground = build_mt_grounding(chunk, rk)
piece = groq_chat_translate(
chunk,
source_label=source_label,
target_code=target_code,
target_label=target_label,
grounding=chunk_ground,
route_key=rk,
reject_filters=strict_quality,
)
if not piece:
piece = groq_chat_translate(
chunk,
source_label=source_label,
target_code=target_code,
target_label=target_label,
grounding=chunk_ground,
route_key=rk,
reject_filters=False,
)
if not piece:
failed += 1
out_parts.append(chunk) # keep original Balochi for this chunk
continue
text_part, model_id = piece
out_parts.append(preserve_untranslated(chunk, text_part.strip()))
time.sleep(0.2)
joined = "\n\n".join(out_parts).strip()
if not joined:
return source_text, f"groq:{groq_model_id()}:keep_original"
if failed >= n_groups:
# All chunks kept original — still return original rather than inventing.
return preserve_untranslated(source_text, joined), model_id or (
f"groq:{groq_model_id()}:keep_original"
)
return (
preserve_untranslated(source_text, joined),
model_id or f"groq:{groq_model_id()}",
)
def google_translate_configured() -> bool:
# Official Cloud key optional; free gtx client always available as fallback.
return True
def google_tl_code(target_code: str) -> str | None:
code = (target_code or "").casefold().strip()
if code in GOOGLE_TL:
return GOOGLE_TL[code]
if len(code) in {2, 3, 5, 7} and code.replace("-", "").isalpha():
return code
return None
def _google_translate_cloud(
text: str, *, source_tl: str, target_tl: str, api_key: str
) -> str | None:
import httpx
r = httpx.post(
"https://translation.googleapis.com/language/translate/v2",
params={"key": api_key},
json={
"q": text,
"source": source_tl,
"target": target_tl,
"format": "text",
},
timeout=60.0,
)
if r.status_code >= 300:
print(f"[google_translate] cloud HTTP {r.status_code}: {r.text[:240]}")
return None
data = r.json() or {}
translations = ((data.get("data") or {}).get("translations")) or []
if not translations:
return None
out = (translations[0].get("translatedText") or "").strip()
return out or None
def _google_translate_gtx(
text: str, *, source_tl: str, target_tl: str
) -> str | None:
"""Unofficial translate.googleapis.com client=gtx (no key). Chunk long text."""
import httpx
def split_parts(src: str, limit: int = 1400) -> list[str]:
lines = src.split("\n")
parts: list[str] = []
buf = ""
for ln in lines:
candidate = f"{buf}\n{ln}" if buf else ln
if len(candidate) > limit and buf:
parts.append(buf)
buf = ln
else:
buf = candidate
if buf:
parts.append(buf)
return parts or [src]
out_chunks: list[str] = []
for part in split_parts(text):
if not part.strip():
out_chunks.append(part)
continue
r = httpx.get(
"https://translate.googleapis.com/translate_a/single",
params={
"client": "gtx",
"sl": source_tl,
"tl": target_tl,
"dt": "t",
"q": part,
},
timeout=60.0,
)
if r.status_code >= 300:
print(f"[google_translate] gtx HTTP {r.status_code}: {r.text[:240]}")
return None
data = r.json()
segs: list[str] = []
try:
for row in data[0] or []:
if row and isinstance(row[0], str):
segs.append(row[0])
except (TypeError, IndexError, KeyError) as err:
print(f"[google_translate] gtx parse failed: {err}")
return None
piece = "".join(segs).strip()
if not piece:
return None
out_chunks.append(piece)
return "\n".join(out_chunks).strip() or None
def google_translate_text(
text: str,
*,
target_code: str,
source_code: str = "english",
) -> tuple[str, str] | None:
"""EN (or other) → target via Google Cloud or free gtx endpoint."""
text = (text or "").strip()
if not text:
return None
target_tl = google_tl_code(target_code)
source_tl = google_tl_code(source_code) or "auto"
if not target_tl:
return None
if source_tl == target_tl:
return text, "google_translate:identity"
api_key = (
os.getenv("GOOGLE_TRANSLATE_API_KEY")
or os.getenv("GOOGLE_API_KEY")
or ""
).strip()
try:
if api_key:
out = _google_translate_cloud(
text, source_tl=source_tl, target_tl=target_tl, api_key=api_key
)
if out:
return out, "google_translate:cloud"
out = _google_translate_gtx(text, source_tl=source_tl, target_tl=target_tl)
if out:
return out, "google_translate:gtx"
except Exception as err:
print(f"[google_translate] failed: {err}")
return None
def free_en_to_target(english: str, target_code: str, target_label: str) -> tuple[str, str] | None:
"""EN→target: Google Translate first, then Groq, then HF opus/NLLB."""
english = (english or "").strip()
if not english:
return None
hit = google_translate_text(
english, target_code=target_code, source_code="english"
)
if hit:
return hit
hit = groq_chat_translate(
english,
source_label="English",
target_code=target_code,
target_label=target_label,
)
if hit:
return hit
return _hf_en_to_target(english, target_code, target_label)
def build_system_prompt() -> str:
return (
"PRIMARY LAW: Obey Balochi_grammar_rules.pdf for every Balochi Latin string.\n\n"
+ CORE_ORTHOGRAPHY
+ "\n\n--- Full rules from Balochi_grammar_rules.pdf (binding) ---\n"
+ RESOURCES["grammar_excerpt"]
)
def style_fewshot_en_bal(k: int = MAX_FEWSHOT) -> list[tuple[float, dict]]:
out: list[tuple[float, dict]] = []
for pair in RESOURCES["pairs"]:
if len(pair["en"]) > 90 or len(pair["bal"]) > 90:
continue
out.append((0.45, pair))
if len(out) >= k:
break
return out
def build_user_prompt(
text: str,
route: dict,
fewshot: list[tuple[float, dict]],
english_pivot: str | None = None,
text_format: str | None = None,
support_draft: str | None = None,
support_draft_rejected: bool = False,
lex_hits: list[tuple[str, str]] | None = None,
) -> str:
route_key = route["key"]
target_label = route["target_label"]
to_balochi = route_key in {"en-bal", "any-bal"}
if route_key in {"en-bal", "any-bal"}:
src_l = route.get("source_label") or (
"English" if route_key == "en-bal" else route.get("source", "source language")
)
tgt_l = "Balochi Latin Syáhag"
pair_lines = [f"EN: {p['en']}\nBAL: {p['bal']}" for _, p in fewshot]
if route_key == "any-bal" and not is_english(route.get("source", "")):
bridge = (
f"The source is {src_l} (not Balochi). "
"Understand the meaning, then write natural Balochi Latin Syáhag. "
"Use English↔Balochi dataset pairs as style/sense guides. "
"Apply Balochi_grammar_rules.pdf strictly (SOV, endings, orthography)."
)
else:
bridge = ""
script_rule = (
"CRITICAL for Balochi output: Latin Syáhag ONLY (never Arabic/Perso-Arabic). "
"Alphabet A Á B Ch D Dh E É G H I J K L M N O Ó P R S Sh T Th U W Y Z Zh. "
"Never use f, q, v, x. Follow Balochi grammar (SOV, ent/ant, verb endings)."
)
elif route_key == "bal-en":
src_l, tgt_l = "Balochi Latin Syáhag", "English"
pair_lines = [f"BAL: {p['bal']}\nEN: {p['en']}" for _, p in fewshot]
bridge = (
"FIDELITY RULES (mandatory):\n"
"- Translate ONLY the Balochi source meaning — nothing else.\n"
"- Do NOT invent scenes, religions, places, or English idioms "
"that are not in the source (e.g. no Hajj/Kaaba/pilgrims/'early bird' "
"unless those ideas are actually present in the Balochi).\n"
"- CRITICAL: Balochi 'hajjám' means BARBER (not pilgrim). "
"Do not confuse it with Hajj.\n"
"- Do NOT replace the text with an unrelated story.\n"
"- Keep the same paragraph / line structure as the source.\n"
"- Lexicon glosses are BINDING for those words (hajjám = Barber)."
)
script_rule = "Write natural English in Latin script."
else:
src_l, tgt_l = "Balochi Latin Syáhag", target_label
pair_lines = [f"BAL: {p['bal']}\nEN (sense): {p['en']}" for _, p in fewshot]
bridge = (
f"The source is Balochi in Latin Syáhag script (not Arabic script). "
f"Use dataset English glosses only as meaning support, then write natural {target_label}."
)
if wants_arabic_script(route):
script_rule = (
f"CRITICAL: Write the entire {target_label} translation in Arabic / Perso-Arabic script only. "
"Do NOT use Latin transliteration (no Roman Urdu / Roman Persian)."
)
else:
script_rule = f"Use the standard writing system for {target_label}."
priority = (
"SUPPORT STACK (mandatory order — matches genai_support):\n"
"1) dataset.xlsx / dataset.csv — search words & sentences (BINDING sense)\n"
"2) words_balochi_to_english.xlsx — word senses (BINDING; "
"hajjám = Barber — NEVER pilgrim/Kaaba/Hajj)\n"
"3) Balochi_grammar_rules.pdf — understand Balochi morphology/syntax\n"
"4) Produce faithful English from the Balochi source using (1)–(3)\n"
"5) Then render that English meaning into the selected target language.\n"
"Do NOT use TranslateGemma / LoRA adapter drafts."
)
sections = [
f"Translate from {src_l} to {tgt_l}.",
"Keep meaning faithful. Reply with the translation ONLY.",
priority,
structure_hint(text, text_format),
script_rule,
]
if to_balochi:
sections.append(GRAMMAR_MUST_FOLLOW)
if bridge:
sections.append(bridge)
# support_draft ignored — TranslateGemma adapter is disabled.
_ = (support_draft, support_draft_rejected)
if pair_lines:
sections.append(
"SUPPORT 1 — parallel examples from dataset.xlsx / dataset.csv "
"(BINDING sense guides):\n"
+ "\n\n".join(pair_lines)
)
if english_pivot:
sections.append(
"SUPPORT 1b — exact/near dataset English sense:\n"
f"{english_pivot}"
)
if lex_hits:
gloss = "\n".join(f"{a} → {b}" for a, b in lex_hits)
sections.append(
"SUPPORT 2 — BINDING lexicon glosses from words_balochi_to_english.xlsx:\n"
f"{gloss}"
)
if to_balochi:
sections.append(
"SUPPORT 3 — BINDING text from Balochi_grammar_rules.pdf "
"(follow these rules exactly):\n"
+ RESOURCES["grammar_excerpt"][:12000]
)
elif route_key in {"bal-en", "bal-any"}:
sections.append(
"SUPPORT 3 — Understand Balochi Latin Syáhag with Balochi_grammar_rules.pdf "
"(Latin orthography, SOV, endings) before writing the translation."
)
sections.append(
"SUPPORT 4 — Produce the final translation using supports (1)–(3) only.\n"
f"Source text:\n{text}\n\nTranslation:"
)
return "\n\n".join(sections)
def postprocess_balochi(text: str) -> str:
text = text.strip().strip('"').strip("'")
text = re.sub(r"[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]+", "", text)
repl = {"f": "p", "F": "P", "q": "k", "Q": "K", "v": "w", "V": "W", "x": "ks", "X": "Ks"}
text = "".join(repl.get(ch, ch) for ch in text)
return re.sub(r"\s+", " ", text).strip()
class TranslationRequest(BaseModel):
text: str
source_lang: str
target_lang: str
text_format: str | None = Field(
default=None,
description="Optional: poetry | story — controls output layout",
)
class TranslationResponse(BaseModel):
original_text: str
translated_text: str
source_lang: str
target_lang: str
method: str = Field(
description="translategemma | exact_dataset | corpus_near_exact | grounded_model"
)
support: dict = Field(default_factory=dict)
@app.get("/health")
def health():
return {
"ok": True,
"model": GEMINI_MODEL,
"fallbacks": GEMINI_FALLBACKS,
"thinking_budget": GEMINI_THINKING_BUDGET,
"gemini_key_set": bool(os.getenv("GEMINI_API_KEY")),
"groq_key_set": groq_configured(),
"groq_model": groq_model_id(),
"google_translate": True,
"google_translate_cloud_key_set": bool(
(os.getenv("GOOGLE_TRANSLATE_API_KEY") or os.getenv("GOOGLE_API_KEY") or "").strip()
),
"pairs": len(RESOURCES["pairs"]),
"lexicon": len(RESOURCES["bal_lex"]),
"grammar_chars": len(RESOURCES["grammar_excerpt"]),
"dataset_source": RESOURCES.get("dataset_path"),
"words_source": RESOURCES.get("words_path"),
"grammar_source": (
GRAMMAR_CACHE.name if GRAMMAR_CACHE.exists() else GRAMMAR_PDF.name
),
"supporting_model": {
"enabled": False,
"ready": False,
"note": "disabled — Groq uses dataset + lexicon + grammar only",
},
"priority": PRIORITIES,
"groq_support": groq_support_stack() if groq_configured() else [],
"genai_support": GENAI_SUPPORT,
"pipeline": [
"dataset.xlsx sentence/word search",
"words_balochi_to_english.xlsx word senses",
"Balochi_grammar_rules.pdf understanding",
"groq Balochi→selected language",
],
"priority_adapter": None,
"translategemma_hub": None,
"routes": ["balochi→any", "any→balochi", "english→balochi"],
}
@app.get("/languages")
def languages():
return {
"source_default": "balochi",
"targets": [{"code": code, "name": name} for code, name in TARGET_LANGUAGES],
"also_supported": (
"Balochi → any language as target_lang; "
"any language as source_lang → balochi (grammar rules applied)"
),
}
@app.get("/")
def ui():
index = STATIC_DIR / "index.html"
if not index.exists():
raise HTTPException(status_code=404, detail="UI missing: static/index.html")
return FileResponse(index)
if STATIC_DIR.exists():
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
def corpus_compose_lines(text: str, route_key: str) -> tuple[str, dict] | None:
"""Line-compose from dataset; unmatched lines keep the original text."""
if route_key not in {"bal-en", "en-bal"}:
return None
lines = text.split("\n")
out: list[str] = []
matched = 0
kept_original = 0
sources: list[str] = []
for line in lines:
raw = line.strip()
if not raw:
out.append("")
continue
hit = exact_lookup(raw, route_key)
if hit is None:
out.append(raw) # keep original Balochi / English
kept_original += 1
sources.append(raw)
continue
matched += 1
out.append(hit)
sources.append(raw)
if matched == 0:
return None
return "\n".join(out), {
"matched_lines": matched,
"kept_original_lines": kept_original,
"sources": sources,
"note": "unmatched_lines_kept_as_original",
}
@app.post("/translate", response_model=TranslationResponse)
async def translate_text(request: TranslationRequest):
text_clean = norm_preserve_structure(request.text)
if not text_clean:
raise HTTPException(status_code=400, detail="text is empty")
route = parse_route(request.source_lang, request.target_lang)
route_key = route["key"]
exact_en_or_bal = exact_lookup(text_clean, route_key) if "\n" not in text_clean else None
if exact_en_or_bal is not None and route_key in {"en-bal", "bal-en"}:
return TranslationResponse(
original_text=text_clean,
translated_text=exact_en_or_bal,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="exact_dataset",
support={
"matched": text_clean,
"dataset_source": RESOURCES.get("dataset_path"),
"grammar_source": GRAMMAR_CACHE.name if GRAMMAR_CACHE.exists() else GRAMMAR_PDF.name,
"priority": PRIORITIES,
},
)
if route_key == "any-bal":
fewshot = retrieve_fewshot(text_clean, "en-bal") or style_fewshot_en_bal()
elif route_key == "bal-any":
fewshot = retrieve_fewshot(text_clean, "bal-en")
else:
fewshot = retrieve_fewshot(text_clean, route_key)
text_format = (request.text_format or "").strip().casefold() or None
if text_format not in {None, "poetry", "story"}:
text_format = None
support_draft = None
support_draft_rejected = False
support_draft_meta: dict = {}
# TranslateGemma draft for short en↔bal AND bal→any (English pivot for Urdu etc.).
# Long labzank essays skip LoRA to avoid ZeroGPU + 90s timeouts.
tg_route = (
"bal-en"
if route_key == "bal-any"
else (route_key if route_key in {"en-bal", "bal-en"} else None)
)
if tg_route and should_run_translategemma(text_clean, text_format):
try:
from support_translategemma import draft_translation, translategemma_status
def _tg_max_tokens(src: str) -> int:
base = int(os.getenv("TRANSLATEGEMMA_MAX_NEW_TOKENS", "256"))
words = max(1, len(src.split()))
lines = max(1, src.count("\n") + 1)
return min(1024, max(base, words * 2, lines * 32))
def _tg_draft_primary(src: str, rk: str) -> str | None:
"""Line-by-line LoRA first for multi-line posts; else whole-text."""
def _from_lines(s: str) -> str | None:
out_lines: list[str] = []
for line in s.split("\n"):
raw = line.strip()
if not raw:
out_lines.append("")
continue
part = draft_translation(raw, rk, _tg_max_tokens(raw))
if not part or not part.strip():
return None
out_lines.append(part.strip().strip('"').strip("'"))
draft = "\n".join(out_lines).strip()
if draft and draft_covers_source(s, draft):
return draft
return None
if "\n" in src:
lined = _from_lines(src)
if lined:
return lined
max_tokens = _tg_max_tokens(src)
whole = draft_translation(src, rk, max_tokens)
if whole and whole.strip() and draft_covers_source(src, whole):
return whole.strip().strip('"').strip("'")
if "\n" not in src and whole and whole.strip():
return whole.strip().strip('"').strip("'")
return None
on_zerogpu = os.getenv("SPACES_ZERO_GPU", "").strip().lower() in {
"1",
"true",
"yes",
}
if on_zerogpu:
support_draft = _tg_draft_primary(text_clean, tg_route)
else:
support_draft = await asyncio.to_thread(
_tg_draft_primary, text_clean, tg_route
)
support_draft_meta = translategemma_status()
if support_draft:
translated = support_draft
if tg_route == "en-bal":
translated = postprocess_balochi(translated)
support_draft = translated
support_draft_meta = {
**support_draft_meta,
"draft": support_draft,
"adapter_dir": str(
Path(
os.getenv(
"TRANSLATEGEMMA_ADAPTER",
str(ROOT / "translategemma_balochi_adapter"),
)
)
),
"priority_role": (
"english_pivot_for_bal_any"
if route_key == "bal-any"
else "draft_for_genai"
),
}
if not draft_covers_source(text_clean, translated):
support_draft_meta = {
**support_draft_meta,
"rejected_short_draft": True,
"draft_lines": non_empty_line_count(translated),
"source_lines": non_empty_line_count(text_clean),
}
support_draft_rejected = True
if tg_route == "bal-en":
conflicts = lexicon_draft_conflicts(
text_clean, translated, "bal-en"
)
if conflicts:
support_draft_meta = {
**support_draft_meta,
"rejected_lexicon_conflict": conflicts,
}
support_draft_rejected = True
elif _looks_like_hallucinated_mt(text_clean, translated) or (
_english_misses_lexicon(text_clean, translated, strict=True)
or draft_misses_lexicon_senses(
text_clean, translated, "bal-en"
)
):
support_draft_meta = {
**support_draft_meta,
"rejected_unrelated_draft": True,
}
support_draft_rejected = True
# bal→en / en→bal: accept a clean LoRA draft WITHOUT Gemini.
if (
route_key in {"bal-en", "en-bal"}
and not support_draft_rejected
and support_draft
and draft_covers_source(text_clean, support_draft)
and not _looks_like_hallucinated_mt(text_clean, support_draft)
and (
route_key == "en-bal"
or not _english_misses_lexicon(
text_clean, support_draft, strict=True
)
)
and (
route_key == "bal-en"
or not violates_syahag(support_draft)
)
):
return TranslationResponse(
original_text=text_clean,
translated_text=support_draft,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="translategemma",
support={
"supporting_model": {
**support_draft_meta,
"priority_role": "accepted_without_genai",
},
"dataset_source": RESOURCES.get("dataset_path"),
"grammar_source": (
GRAMMAR_CACHE.name
if GRAMMAR_CACHE.exists()
else GRAMMAR_PDF.name
),
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
"note": "adapter_ok_dataset_lexicon_checked",
"syahag_ok": (
not bool(violates_syahag(support_draft))
if route_key == "en-bal"
else None
),
},
)
# Do NOT soft-accept weak LoRA poems — fall through to
# dataset / grounded Groq instead of shipping invented lines.
except Exception as err:
support_draft_meta = {"ready": False, "error": str(err)}
elif route_key in {"en-bal", "bal-en", "bal-any"}:
try:
from support_translategemma import translategemma_status
support_draft_meta = {
**translategemma_status(),
"skipped_long_text": True,
"chars": len(text_clean),
"words": len(text_clean.split()),
"lines": non_empty_line_count(text_clean),
"priority_role": "skipped_for_latency",
}
except Exception:
support_draft_meta = {"skipped_long_text": True}
composed = corpus_compose_lines(text_clean, route_key)
if composed is not None:
translated, meta = composed
if route_key in {"en-bal", "any-bal"}:
translated = postprocess_balochi(translated)
return TranslationResponse(
original_text=text_clean,
translated_text=translated,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="exact_dataset_lines",
support={
**meta,
"dataset_source": RESOURCES.get("dataset_path"),
"grammar_source": GRAMMAR_CACHE.name if GRAMMAR_CACHE.exists() else GRAMMAR_PDF.name,
"supporting_model": support_draft_meta,
"priority": PRIORITIES,
},
)
if (
route_key in {"bal-en", "en-bal"}
and "\n" not in text_clean
and fewshot
and fewshot[0][0] >= CORPUS_DIRECT_SCORE
):
score, pair = fewshot[0]
translated = pair["bal"] if route_key == "en-bal" else pair["en"]
if route_key == "en-bal":
translated = postprocess_balochi(translated)
return TranslationResponse(
original_text=text_clean,
translated_text=translated,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="corpus_near_exact",
support={
"score": round(score, 3),
"example": {"en": pair["en"], "bal": pair["bal"]},
"dataset_source": RESOURCES.get("dataset_path"),
"grammar_source": GRAMMAR_CACHE.name if GRAMMAR_CACHE.exists() else GRAMMAR_PDF.name,
"supporting_model": support_draft_meta,
"priority": PRIORITIES,
},
)
english_pivot = exact_en_or_bal if route_key == "bal-any" else None
if english_pivot is None and route_key == "bal-any" and fewshot and fewshot[0][0] >= 0.55:
english_pivot = fewshot[0][1]["en"]
# Prefer dataset English pivot for bal→Urdu/etc. (no adapter).
if (
route_key == "bal-any"
and fewshot
and fewshot[0][0] >= 0.45
and draft_covers_source(text_clean, fewshot[0][1]["en"])
):
english_pivot = fewshot[0][1]["en"]
lex_route = "en-bal" if route_key in {"en-bal", "any-bal"} else "bal-en"
lex_hits = lexicon_hits(text_clean, lex_route)
# bal→Urdu/etc.: Groq reads dataset+lexicon+grammar, then translates
# Balochi → target itself (no TranslateGemma; Google only as last resort).
if route_key == "bal-any" and groq_configured():
tgt_code = route.get("target", "")
tgt_label = route.get("target_label", "")
fmt = text_format or "poetry"
free = groq_translate_chunked(
text_clean,
source_label="Balochi Latin Syáhag",
target_code=tgt_code,
target_label=tgt_label,
text_format=fmt,
route_key="bal-en",
adapter_draft=None,
strict_quality=True,
)
note = "groq_grounded_balochi_to_target"
pivot = None
if not free:
# Retry without strict filters
free = groq_translate_chunked(
text_clean,
source_label="Balochi Latin Syáhag",
target_code=tgt_code,
target_label=tgt_label,
text_format=fmt,
route_key="bal-en",
adapter_draft=None,
strict_quality=False,
)
if not free:
# Last resort: grounded English then Google / Groq EN→target
pivot = english_pivot
if (
not pivot
or non_empty_line_count(text_clean) > 6
or _looks_like_hallucinated_mt(text_clean, pivot or "")
or _english_misses_lexicon(text_clean, pivot or "")
):
en_hit = groq_translate_chunked(
text_clean,
source_label="Balochi Latin Syáhag",
target_code="english",
target_label="English",
text_format=fmt,
route_key="bal-en",
adapter_draft=None,
)
if en_hit:
pivot = en_hit[0]
if pivot:
free = free_en_to_target(pivot, tgt_code, tgt_label)
if free and str(free[1]).startswith("google_translate"):
note = "fallback_google_via_grounded_english"
elif free:
note = "fallback_groq_via_grounded_english"
if free:
text, model_id = free
if fmt != "story":
text = align_line_structure(text_clean, text)
method = (
"google_translate"
if str(model_id).startswith("google_translate")
else "groq_grounded"
)
return TranslationResponse(
original_text=text_clean,
translated_text=text,
source_lang=request.source_lang,
target_lang=request.target_lang,
method=method,
support={
"model": model_id,
"english_pivot": pivot,
"note": note,
"lexicon_hits": lex_hits[:12],
"supporting_model": {
"enabled": False,
"ready": False,
"note": "translategemma_disabled",
},
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
"groq_support": groq_support_stack(),
"google_translate": str(model_id).startswith("google_translate"),
"routes": ["balochi→any", "any→balochi", "english→balochi"],
},
)
raise HTTPException(
status_code=502,
detail=(
"Translation failed after dataset/lexicon/grammar/Groq "
"(no TranslateGemma). Retry in a minute."
),
)
# bal→English: dataset + lexicon + grammar → Groq English. No adapter / Gemini.
if route_key == "bal-en" and groq_configured():
lex_hits_be = lexicon_hits(text_clean, "bal-en")
# 1) Strong dataset near-match / line compose
if fewshot and fewshot[0][0] >= 0.35:
score, pair = fewshot[0]
if draft_covers_source(text_clean, pair["en"]) or non_empty_line_count(text_clean) <= 2:
return TranslationResponse(
original_text=text_clean,
translated_text=pair["en"],
source_lang=request.source_lang,
target_lang=request.target_lang,
method="corpus_near_exact",
support={
"score": round(score, 3),
"example": {"en": pair["en"], "bal": pair["bal"]},
"lexicon_hits": lex_hits_be[:12],
"note": "dataset_sentence_search",
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
},
)
composed = corpus_compose_lines(text_clean, "bal-en")
if composed:
text, meta = composed
return TranslationResponse(
original_text=text_clean,
translated_text=text,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="exact_dataset_lines",
support={
**meta,
"lexicon_hits": lex_hits_be[:12],
"note": "dataset_line_compose",
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
},
)
# 2) AI English grounded by dataset + lexicon + grammar
fmt = text_format
if not fmt:
lines_n = non_empty_line_count(text_clean)
avg = len(text_clean) / max(lines_n, 1)
fmt = "poetry" if lines_n >= 4 and avg < 90 else "story"
free = groq_translate_chunked(
text_clean,
source_label="Balochi Latin Syáhag",
target_code="english",
target_label="English",
text_format=fmt,
route_key="bal-en",
adapter_draft=None,
strict_quality=True,
)
if not free:
free = groq_translate_chunked(
text_clean,
source_label="Balochi Latin Syáhag",
target_code="english",
target_label="English",
text_format=fmt,
route_key="bal-en",
adapter_draft=None,
strict_quality=False,
)
if free:
text, model_id = free
if fmt != "story":
text = align_line_structure(text_clean, text)
return TranslationResponse(
original_text=text_clean,
translated_text=text,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="groq_grounded",
support={
"model": model_id,
"note": "dataset_lexicon_grammar_then_groq_english",
"lexicon_hits": lex_hits_be[:12],
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
"groq_support": groq_support_stack(),
},
)
raise HTTPException(
status_code=502,
detail=(
"Balochi→English failed after dataset/lexicon/grammar/Groq "
"(TranslateGemma off; Gemini skipped). Retry in a minute."
),
)
if route_key == "en-bal" and groq_configured():
free = groq_translate_chunked(
text_clean,
source_label="English",
target_code="balochi",
target_label="Balochi Latin Syáhag",
text_format=text_format or "story",
route_key="en-bal",
adapter_draft=None,
)
if free:
text, model_id = free
text = postprocess_balochi(text)
return TranslationResponse(
original_text=text_clean,
translated_text=text,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="groq_grounded",
support={
"model": model_id,
"note": "dataset_lexicon_grammar_then_groq_balochi",
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
"groq_support": groq_support_stack(),
},
)
raise HTTPException(
status_code=502,
detail=(
"English→Balochi failed after dataset/lexicon/grammar/Groq "
"(TranslateGemma off; Gemini skipped). Retry in a minute."
),
)
# Short bal↔en without Groq: dataset only (no adapter / Gemini).
if route_key in {"bal-en", "en-bal"} and not groq_configured():
if fewshot and fewshot[0][0] >= 0.22:
score, pair = fewshot[0]
translated = pair["bal"] if route_key == "en-bal" else pair["en"]
if route_key == "en-bal":
translated = postprocess_balochi(translated)
return TranslationResponse(
original_text=text_clean,
translated_text=translated,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="corpus_near_exact",
support={
"score": round(score, 3),
"example": {"en": pair["en"], "bal": pair["bal"]},
"note": "dataset_only_no_groq_key",
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
},
)
raise HTTPException(
status_code=503,
detail=(
"Set GROQ_API_KEY for AI English/target MT after dataset+lexicon+grammar. "
"TranslateGemma adapter is disabled."
),
)
# Cooldown: do not call Gemini at all while free-tier is exhausted.
if not gemini_allowed():
wait = max(1, int(_gemini_quota_until - time.time()))
if route_key == "bal-any" and (english_pivot or support_draft):
pivot = english_pivot or support_draft or ""
free = free_en_to_target(
pivot, route.get("target", ""), route.get("target_label", "")
)
if free:
text, model_id = free
if text_format != "story":
text = align_line_structure(text_clean, text)
return TranslationResponse(
original_text=text_clean,
translated_text=text,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="free_mt",
support={
"model": model_id,
"note": "gemini_quota_used_free_hf_or_groq",
"english_pivot": pivot,
"retry_after_sec": wait,
"supporting_model": support_draft_meta,
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
},
)
return TranslationResponse(
original_text=text_clean,
translated_text=pivot,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="english_sense_quota_fallback",
support={
"warning": (
f"Gemini cooldown (~{wait}s). Free HF/Groq MT unavailable; "
f"showing English sense. Set GROQ_API_KEY or wait / enable billing."
),
"retry_after_sec": wait,
"supporting_model": support_draft_meta,
"priority": PRIORITIES,
},
)
raise HTTPException(
status_code=429,
detail=(
f"Gemini free-tier cooldown — retry in {wait}s, use English, "
"or enable billing on GEMINI_API_KEY / set GROQ_API_KEY."
),
)
# Hard stop: with GROQ_API_KEY set, never call Gemini for labzank routes.
if groq_configured() and route_key in {"bal-en", "en-bal", "bal-any", "any-bal"}:
raise HTTPException(
status_code=502,
detail=(
"Groq/Google path unavailable; Gemini skipped because "
"GROQ_API_KEY is set. Retry in a minute."
),
)
async def _genai_one(
chunk: str,
*,
draft: str | None,
draft_rejected: bool,
fmt: str | None,
pivot: str | None = None,
) -> tuple[str, str]:
# Fast path: EN→target with a tiny prompt when we already have English.
if route_key == "bal-any" and pivot and chunk == text_clean:
try:
pivot_prompt = build_pivot_prompt(
pivot,
route["target_label"],
text_format=fmt,
arabic_script=wants_arabic_script(route),
)
out, model = await asyncio.to_thread(
generate_with_retries, pivot_prompt, light=True
)
return out.strip().strip('"').strip("'"), model
except Exception as err:
if _is_quota_exhausted(err) or not gemini_allowed():
free = free_en_to_target(
pivot, route.get("target", ""), route.get("target_label", "")
)
if free:
return free
raise
chunk_few = retrieve_fewshot(
chunk,
"en-bal"
if route_key == "any-bal"
else ("bal-en" if route_key == "bal-any" else route_key),
)
if route_key == "any-bal" and not chunk_few:
chunk_few = fewshot
chunk_lex = lexicon_hits(chunk, lex_route)
prompt = build_user_prompt(
chunk,
route,
chunk_few or fewshot,
english_pivot=pivot if chunk == text_clean else None,
text_format=fmt,
support_draft=draft if chunk == text_clean else None,
support_draft_rejected=draft_rejected if chunk == text_clean else False,
lex_hits=chunk_lex or lex_hits,
)
try:
out, model = await asyncio.to_thread(generate_with_retries, prompt)
except Exception as err:
if (
route_key == "bal-any"
and pivot
and (_is_quota_exhausted(err) or not gemini_allowed())
):
free = free_en_to_target(
pivot, route.get("target", ""), route.get("target_label", "")
)
if free:
return free
raise
if route_key in {"en-bal", "any-bal"}:
out = postprocess_balochi(out)
else:
out = out.strip().strip('"').strip("'")
return out, model
try:
used_model = GEMINI_MODEL
chunked = False
if should_chunk_genai(text_clean, text_format) and route_key != "bal-any":
paras = story_paragraphs(text_clean)
# Merge tiny paragraphs so we don't fire dozens of Gemini calls.
batches: list[str] = []
buf = ""
for p in paras:
if not buf:
buf = p
elif len(buf) + len(p) + 2 <= GENAI_CHUNK_CHARS:
buf = f"{buf}\n\n{p}"
else:
batches.append(buf)
buf = p
if buf:
batches.append(buf)
outs: list[str] = []
for batch in batches:
part, used_model = await _genai_one(
batch,
draft=None,
draft_rejected=False,
fmt=text_format or "story",
)
outs.append(part)
translated = "\n\n".join(outs)
chunked = True
else:
translated, used_model = await _genai_one(
text_clean,
draft=support_draft,
draft_rejected=support_draft_rejected,
fmt=text_format,
pivot=english_pivot,
)
if text_format != "story" and not chunked:
translated = align_line_structure(text_clean, translated)
if route_key in {"en-bal", "any-bal"} and not chunked:
issues = violates_syahag(translated)
if issues:
repair_prompt = (
build_user_prompt(
text_clean,
route,
fewshot,
english_pivot=english_pivot,
text_format=text_format,
support_draft=support_draft,
support_draft_rejected=support_draft_rejected,
lex_hits=lex_hits,
)
+ "\n\nYour previous Balochi draft VIOLATES Balochi_grammar_rules.pdf "
f"({', '.join(issues)}).\n"
"Rewrite the FULL translation in correct Balochi Latin Syáhag only, "
"obeying the PDF rules. Reply with the corrected translation only:\n"
f"{translated}\n\nCorrected translation:"
)
translated, used_model = await asyncio.to_thread(
generate_with_retries, repair_prompt
)
translated = postprocess_balochi(translated)
if text_format != "story":
translated = align_line_structure(text_clean, translated)
if (
wants_arabic_script(route)
and arabic_script_ratio(translated) < 0.55
and not chunked
):
fix_prompt = (
build_user_prompt(
text_clean,
route,
fewshot,
english_pivot=english_pivot,
text_format=text_format,
support_draft=support_draft,
support_draft_rejected=support_draft_rejected,
lex_hits=lex_hits,
)
+ "\n\nYour previous draft used Latin letters. "
f"Rewrite the full translation in Arabic script for {route['target_label']} only, "
"preserving the same layout:\n"
f"{translated}\n\nCorrected translation:"
)
translated, used_model = await asyncio.to_thread(generate_with_retries, fix_prompt)
translated = translated.strip().strip('"').strip("'")
if text_format != "story":
translated = align_line_structure(text_clean, translated)
return TranslationResponse(
original_text=text_clean,
translated_text=translated,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="grounded_model",
support={
"model": used_model,
"route": route_key,
"target_label": route["target_label"],
"source_label": route.get("source_label"),
"text_format": text_format or "auto",
"english_pivot": english_pivot,
"arabic_script_required": wants_arabic_script(route),
"source_lines": len(text_clean.split("\n")),
"output_lines": len(translated.split("\n")),
"chunked": chunked,
"fewshot": [{"score": round(s, 3), "en": p["en"], "bal": p["bal"]} for s, p in fewshot],
"grammar_source": GRAMMAR_CACHE.name if GRAMMAR_CACHE.exists() else GRAMMAR_PDF.name,
"grammar_chars": len(RESOURCES["grammar_excerpt"]),
"syahag_ok": not bool(violates_syahag(translated))
if route_key in {"en-bal", "any-bal"}
else None,
"dataset_source": RESOURCES.get("dataset_path"),
"supporting_model": support_draft_meta,
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
"priority_adapter": TRANSLATEGEMMA_ADAPTER_DIR,
"translategemma_hub": TRANSLATEGEMMA_HUB,
"lexicon": len(RESOURCES["bal_lex"]),
},
)
except HTTPException:
raise
except Exception as err:
# bal→en: never die on Gemini quota if we already have a LoRA draft.
if (
route_key == "bal-en"
and support_draft
and _is_quota_exhausted(err)
and not support_draft_meta.get("rejected_lexicon_conflict")
):
return TranslationResponse(
original_text=text_clean,
translated_text=support_draft,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="translategemma",
support={
"warning": "Gemini quota hit; returning adapter draft.",
"gemini_error": str(getattr(err, "message", err)),
"supporting_model": {
**support_draft_meta,
"priority_role": "lora_after_gemini_quota",
},
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
},
)
# bal→Urdu/etc.: if Gemini free quota is dead but we have English, return it.
if (
route_key == "bal-any"
and english_pivot
and _is_quota_exhausted(err)
):
return TranslationResponse(
original_text=text_clean,
translated_text=english_pivot,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="english_sense_quota_fallback",
support={
"warning": (
f"{route['target_label']} needs Gemini; free-tier quota "
"exceeded. Showing English sense from the adapter/dataset. "
"Wait ~1 min or enable billing on GEMINI_API_KEY."
),
"gemini_error": str(getattr(err, "message", err)),
"retry_after_sec": _quota_retry_seconds(err),
"route": route_key,
"target_label": route["target_label"],
"english_pivot": english_pivot,
"supporting_model": support_draft_meta,
"priority": PRIORITIES,
"genai_support": GENAI_SUPPORT,
},
)
offline = offline_fallback(
text_clean,
route_key,
fewshot,
route["target_label"],
)
if offline is not None:
translated, meta = offline
if route_key in {"en-bal", "any-bal"}:
translated = postprocess_balochi(translated)
return TranslationResponse(
original_text=text_clean,
translated_text=translated,
source_lang=request.source_lang,
target_lang=request.target_lang,
method="offline_fallback",
support={
"gemini_error": str(getattr(err, "message", err)),
"route": route_key,
"target_label": route["target_label"],
**meta,
"priority": PRIORITIES,
},
)
detail = getattr(err, "message", None) or str(err)
if _is_quota_exhausted(err):
raise HTTPException(
status_code=429,
detail=(
f"Gemini free-tier quota exceeded. Retry in "
f"{int(_quota_retry_seconds(err))}s, use English, "
f"or enable billing on GEMINI_API_KEY. ({detail})"
),
)
if isinstance(err, APIError):
raise HTTPException(status_code=502, detail=f"Gemini API Error: {err.message}")
raise HTTPException(status_code=502, detail=f"Gemini API Error: {err}")
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "8000"))
uvicorn.run(app, host="0.0.0.0", port=port) |