Spaces:
Running
on
Zero
Running
on
Zero
File size: 74,066 Bytes
6ea41ec |
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 |
import gradio as gr
import markdown
import threading
import time
import logging
from pathlib import Path
from src.core.converter import convert_file, set_cancellation_flag, is_conversion_in_progress
from src.parsers.parser_registry import ParserRegistry
from src.core.config import config
from src.core.exceptions import (
DocumentProcessingError,
UnsupportedFileTypeError,
FileSizeLimitError,
ConfigurationError
)
from src.core.logging_config import get_logger
from src.rag import rag_chat_service, document_ingestion_service
from src.rag.vector_store import vector_store_manager
from src.services.data_clearing_service import data_clearing_service
# Use centralized logging
logger = get_logger(__name__)
# Import MarkItDown to check if it's available
try:
from markitdown import MarkItDown
HAS_MARKITDOWN = True
logger.info("MarkItDown is available for use")
except ImportError:
HAS_MARKITDOWN = False
logger.warning("MarkItDown is not available")
# Add a global variable to track cancellation state
conversion_cancelled = threading.Event()
# Pass the cancellation flag to the converter module
set_cancellation_flag(conversion_cancelled)
# Add a background thread to monitor cancellation
def monitor_cancellation():
"""Background thread to monitor cancellation and update UI if needed"""
logger.info("Starting cancellation monitor thread")
while is_conversion_in_progress():
if conversion_cancelled.is_set():
logger.info("Cancellation detected by monitor thread")
time.sleep(0.1) # Check every 100ms
logger.info("Cancellation monitor thread ending")
def update_ui_for_file_count(files):
"""Update UI components based on the number of files uploaded."""
if not files or len(files) == 0:
return (
gr.update(visible=False), # processing_type_selector
"<div style='color: #666; font-style: italic;'>Upload documents to begin</div>" # file_status_text
)
if len(files) == 1:
file_name = files[0].name if hasattr(files[0], 'name') else str(files[0])
return (
gr.update(visible=False), # processing_type_selector (hidden for single file)
f"<div style='color: #2563eb; font-weight: 500;'>π Single document: {file_name}</div>"
)
else:
# Calculate total size for validation display
total_size = 0
try:
for file in files:
if hasattr(file, 'size'):
total_size += file.size
elif hasattr(file, 'name'):
# For file paths, get size from filesystem
total_size += Path(file.name).stat().st_size
except:
pass # Size calculation is optional for display
size_display = f" ({total_size / (1024*1024):.1f}MB)" if total_size > 0 else ""
# Check if within limits
if len(files) > 5:
status_color = "#dc2626" # red
status_text = f"β οΈ Too many files: {len(files)}/5 (max 5 files allowed)"
elif total_size > 20 * 1024 * 1024: # 20MB
status_color = "#dc2626" # red
status_text = f"β οΈ Files too large{size_display} (max 20MB combined)"
else:
status_color = "#059669" # green
status_text = f"π Batch mode: {len(files)} files{size_display}"
return (
gr.update(visible=True), # processing_type_selector (visible for multiple files)
f"<div style='color: {status_color}; font-weight: 500;'>{status_text}</div>"
)
def validate_file_for_parser(file_path, parser_name):
"""Validate if the file type is supported by the selected parser."""
if not file_path:
return True, "" # No file selected yet
try:
file_path_obj = Path(file_path)
file_ext = file_path_obj.suffix.lower()
# Check file size
if file_path_obj.exists():
file_size = file_path_obj.stat().st_size
if file_size > config.app.max_file_size:
size_mb = file_size / (1024 * 1024)
max_mb = config.app.max_file_size / (1024 * 1024)
return False, f"File size ({size_mb:.1f}MB) exceeds maximum allowed size ({max_mb:.1f}MB)"
# Check file extension
if file_ext not in config.app.allowed_extensions:
return False, f"File type '{file_ext}' is not supported. Allowed types: {', '.join(config.app.allowed_extensions)}"
# Parser-specific validation
if "GOT-OCR" in parser_name:
if file_ext not in ['.jpg', '.jpeg', '.png']:
return False, "GOT-OCR only supports JPG and PNG formats."
return True, ""
except Exception as e:
logger.error(f"Error validating file: {e}")
return False, f"Error validating file: {e}"
def format_markdown_content(content):
if not content:
return content
# Convert the content to HTML using markdown library
html_content = markdown.markdown(str(content), extensions=['tables'])
return html_content
def render_latex_to_html(latex_content):
"""Convert LaTeX content to HTML using Mathpix Markdown like GOT-OCR demo."""
import json
# Clean up the content similar to GOT-OCR demo
content = latex_content.strip()
if content.endswith("<|im_end|>"):
content = content[:-len("<|im_end|>")]
# Fix unbalanced delimiters exactly like GOT-OCR demo
right_num = content.count("\\right")
left_num = content.count("\\left")
if right_num != left_num:
content = (
content.replace("\\left(", "(")
.replace("\\right)", ")")
.replace("\\left[", "[")
.replace("\\right]", "]")
.replace("\\left{", "{")
.replace("\\right}", "}")
.replace("\\left|", "|")
.replace("\\right|", "|")
.replace("\\left.", ".")
.replace("\\right.", ".")
)
# Process content like GOT-OCR demo: remove $ signs and replace quotes
content = content.replace('"', "``").replace("$", "")
# Split into lines and create JavaScript string like GOT-OCR demo
outputs_list = content.split("\n")
js_text_parts = []
for line in outputs_list:
# Escape backslashes and add line break
escaped_line = line.replace("\\", "\\\\")
js_text_parts.append(f'"{escaped_line}\\n"')
# Join with + like in GOT-OCR demo
js_text = " + ".join(js_text_parts)
# Create HTML using Mathpix Markdown like GOT-OCR demo
html_content = f"""<!DOCTYPE html>
<html lang="en" data-lt-installed="true">
<head>
<meta charset="UTF-8">
<title>LaTeX Content</title>
<script>
const text = {js_text};
</script>
<style>
#content {{
max-width: 800px;
margin: auto;
padding: 20px;
}}
body {{
font-family: 'Times New Roman', serif;
line-height: 1.6;
background-color: #ffffff;
color: #333;
}}
table {{
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}}
td, th {{
border: 1px solid #333;
padding: 8px 12px;
text-align: center;
vertical-align: middle;
}}
</style>
<script>
let script = document.createElement('script');
script.src = "https://cdn.jsdelivr.net/npm/mathpix-markdown-it@1.3.6/es5/bundle.js";
document.head.append(script);
script.onload = function() {{
const isLoaded = window.loadMathJax();
if (isLoaded) {{
console.log('Styles loaded!')
}}
const el = window.document.getElementById('content-text');
if (el) {{
const options = {{
htmlTags: true
}};
const html = window.render(text, options);
el.outerHTML = html;
}}
}};
</script>
</head>
<body>
<div id="content">
<div id="content-text"></div>
</div>
</body>
</html>"""
return html_content
def format_latex_content(content):
"""Format LaTeX content for display in UI using MathJax rendering like GOT-OCR demo."""
if not content:
return content
try:
# Generate rendered HTML
rendered_html = render_latex_to_html(content)
# Encode for iframe display (similar to GOT-OCR demo)
import base64
encoded_html = base64.b64encode(rendered_html.encode("utf-8")).decode("utf-8")
iframe_src = f"data:text/html;base64,{encoded_html}"
# Create the display with both rendered and raw views
formatted_content = f"""
<div style="background-color: #f8f9fa; border-radius: 8px; border: 1px solid #e9ecef; margin: 10px 0;">
<div style="background-color: #e9ecef; padding: 10px; border-radius: 8px 8px 0 0; font-weight: bold; color: #495057;">
π LaTeX Content (Rendered with MathJax)
</div>
<div style="padding: 0;">
<iframe src="{iframe_src}" width="100%" height="500px" style="border: none; border-radius: 0 0 8px 8px;"></iframe>
</div>
<div style="background-color: #e9ecef; padding: 8px 15px; border-radius: 0; font-size: 12px; color: #6c757d; border-top: 1px solid #dee2e6;">
π‘ LaTeX content rendered with MathJax. Tables and formulas are displayed as they would appear in a LaTeX document.
</div>
<details style="margin: 0; border-top: 1px solid #dee2e6;">
<summary style="padding: 8px 15px; background-color: #e9ecef; cursor: pointer; font-size: 12px; color: #6c757d;">
π View Raw LaTeX Source
</summary>
<div style="padding: 15px; background-color: #f8f9fa;">
<pre style="background-color: transparent; margin: 0; padding: 0;
font-family: 'Courier New', monospace; font-size: 12px; line-height: 1.4;
white-space: pre-wrap; word-wrap: break-word; color: #2c3e50; max-height: 200px; overflow-y: auto;">
{content}
</pre>
</div>
</details>
</div>
"""
except Exception as e:
# Fallback to simple formatting if rendering fails
import html
escaped_content = html.escape(str(content))
formatted_content = f"""
<div style="background-color: #f8f9fa; border-radius: 8px; border: 1px solid #e9ecef; margin: 10px 0;">
<div style="background-color: #e9ecef; padding: 10px; border-radius: 8px 8px 0 0; font-weight: bold; color: #495057;">
π LaTeX Content (Fallback View)
</div>
<div style="padding: 15px;">
<pre style="background-color: transparent; margin: 0; padding: 0;
font-family: 'Courier New', monospace; font-size: 14px; line-height: 1.4;
white-space: pre-wrap; word-wrap: break-word; color: #2c3e50;">
{escaped_content}
</pre>
</div>
<div style="background-color: #e9ecef; padding: 8px 15px; border-radius: 0 0 8px 8px; font-size: 12px; color: #6c757d;">
β οΈ Rendering failed, showing raw LaTeX. Error: {str(e)}
</div>
</div>
"""
return formatted_content
# Function to run conversion in a separate thread
def run_conversion_thread(file_path, parser_name, ocr_method_name, output_format):
"""Run the conversion in a separate thread and return the thread object"""
global conversion_cancelled
# Reset the cancellation flag
conversion_cancelled.clear()
# Create a container for the results
results = {"content": None, "download_file": None, "error": None}
def conversion_worker():
try:
content, download_file = convert_file(file_path, parser_name, ocr_method_name, output_format)
results["content"] = content
results["download_file"] = download_file
except Exception as e:
logger.error(f"Error during conversion: {str(e)}")
results["error"] = str(e)
# Create and start the thread
thread = threading.Thread(target=conversion_worker)
thread.daemon = True
thread.start()
return thread, results
def run_conversion_thread_multi(file_paths, parser_name, ocr_method_name, output_format, processing_type):
"""Run the conversion in a separate thread for multiple files."""
import threading
from src.services.document_service import DocumentService
# Results will be shared between threads
results = {"content": None, "download_file": None, "error": None}
def conversion_worker():
try:
logger.info(f"Starting multi-file conversion thread for {len(file_paths)} files")
# Use the new document service unified method
document_service = DocumentService()
document_service.set_cancellation_flag(conversion_cancelled)
# Call the unified convert_documents method
content, output_file = document_service.convert_documents(
file_paths=file_paths,
parser_name=parser_name,
ocr_method_name=ocr_method_name,
output_format=output_format,
processing_type=processing_type
)
logger.info(f"Multi-file conversion completed successfully for {len(file_paths)} files")
results["content"] = content
results["download_file"] = output_file
except Exception as e:
logger.error(f"Error during multi-file conversion: {str(e)}")
results["error"] = str(e)
# Create and start the thread
thread = threading.Thread(target=conversion_worker)
thread.daemon = True
thread.start()
return thread, results
def handle_convert(files, parser_name, ocr_method_name, output_format, processing_type, is_cancelled):
"""Handle file conversion for single or multiple files."""
global conversion_cancelled
# Check if we should cancel before starting
if is_cancelled:
logger.info("Conversion cancelled before starting")
return "Conversion cancelled.", None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
# Validate files input
if not files or len(files) == 0:
error_msg = "No files uploaded. Please upload at least one document."
logger.error(error_msg)
return f"Error: {error_msg}", None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
# Convert Gradio file objects to file paths
file_paths = []
for file in files:
if hasattr(file, 'name'):
file_paths.append(file.name)
else:
file_paths.append(str(file))
# Validate file types for the selected parser
for file_path in file_paths:
is_valid, error_msg = validate_file_for_parser(file_path, parser_name)
if not is_valid:
logger.error(f"File validation error: {error_msg}")
return f"Error: {error_msg}", None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
logger.info(f"Starting conversion of {len(file_paths)} file(s) with cancellation flag cleared")
# Start the conversion in a separate thread
thread, results = run_conversion_thread_multi(file_paths, parser_name, ocr_method_name, output_format, processing_type)
# Start the monitoring thread
monitor_thread = threading.Thread(target=monitor_cancellation)
monitor_thread.daemon = True
monitor_thread.start()
# Wait for the thread to complete or be cancelled
while thread.is_alive():
# Check if cancellation was requested
if conversion_cancelled.is_set():
logger.info("Cancellation detected, waiting for thread to finish")
# Give the thread a chance to clean up
thread.join(timeout=0.5)
if thread.is_alive():
logger.warning("Thread did not finish within timeout")
return "Conversion cancelled.", None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
# Sleep briefly to avoid busy waiting
time.sleep(0.1)
# Thread has completed, check results
if results["error"]:
return f"Error: {results['error']}", None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
content = results["content"]
download_file = results["download_file"]
# If conversion returned a cancellation message
if content == "Conversion cancelled.":
logger.info("Converter returned cancellation message")
return content, None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
# Format the content based on parser type
if "GOT-OCR" in parser_name:
# For GOT-OCR, display as LaTeX
formatted_content = format_latex_content(str(content))
html_output = f"<div class='output-container'>{formatted_content}</div>"
else:
# For other parsers, display as Markdown
formatted_content = format_markdown_content(str(content))
html_output = f"<div class='output-container'>{formatted_content}</div>"
logger.info("Conversion completed successfully")
# Auto-ingest the converted document for RAG
try:
# Read original file content for proper deduplication hashing
original_file_content = None
if file_path and Path(file_path).exists():
try:
with open(file_path, 'rb') as f:
original_file_content = f.read().decode('utf-8', errors='ignore')
except Exception as e:
logger.warning(f"Could not read original file content: {e}")
conversion_result = {
"markdown_content": content,
"original_filename": Path(file_path).name if file_path else "unknown",
"conversion_method": parser_name,
"file_size": Path(file_path).stat().st_size if file_path and Path(file_path).exists() else 0,
"conversion_time": 0, # Could be tracked if needed
"original_file_content": original_file_content
}
success, ingestion_msg, stats = document_ingestion_service.ingest_from_conversion_result(conversion_result)
if success:
logger.info(f"Document auto-ingested for RAG: {ingestion_msg}")
else:
logger.warning(f"Document ingestion failed: {ingestion_msg}")
except Exception as e:
logger.error(f"Error during auto-ingestion: {e}")
return html_output, download_file, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
def handle_chat_message(message, history):
"""Handle a new chat message with streaming response."""
if not message or not message.strip():
return "", history, gr.update()
try:
# Add user message to history
history = history or []
history.append({"role": "user", "content": message})
# Add assistant message placeholder
history.append({"role": "assistant", "content": ""})
# Get response from RAG service
response_text = ""
for chunk in rag_chat_service.chat_stream(message):
response_text += chunk
# Update the last message in history with the current response
history[-1]["content"] = response_text
# Update status in real-time during streaming
updated_status = get_chat_status()
yield "", history, updated_status
logger.info(f"Chat response completed for message: {message[:50]}...")
# Final status update after message completion
final_status = get_chat_status()
yield "", history, final_status
except Exception as e:
error_msg = f"Error generating response: {str(e)}"
logger.error(error_msg)
if history and len(history) > 0:
history[-1]["content"] = f"β {error_msg}"
else:
history = [
{"role": "user", "content": message},
{"role": "assistant", "content": f"β {error_msg}"}
]
# Update status even on error
error_status = get_chat_status()
yield "", history, error_status
def start_new_chat_session():
"""Start a new chat session."""
try:
session_id = rag_chat_service.start_new_session()
logger.info(f"Started new chat session: {session_id}")
return [], f"β
New chat session started: {session_id}"
except Exception as e:
error_msg = f"Error starting new session: {str(e)}"
logger.error(error_msg)
return [], f"β {error_msg}"
def handle_clear_all_data():
"""Handle clearing all RAG data (vector store + chat history)."""
try:
# Clear all data using the data clearing service
success, message, stats = data_clearing_service.clear_all_data()
if success:
# Reset chat session after clearing data
session_id = rag_chat_service.start_new_session()
# Get updated status
updated_status = get_chat_status()
# Create success message with stats
if stats.get("total_cleared_documents", 0) > 0 or stats.get("total_cleared_files", 0) > 0:
clear_msg = f"β
{message}"
session_msg = f"π Started new session: {session_id}"
combined_msg = f'{clear_msg}<br/><div class="session-info">{session_msg}</div>'
else:
combined_msg = f'βΉοΈ {message}<br/><div class="session-info">π Started new session: {session_id}</div>'
logger.info(f"Data cleared successfully: {message}")
return [], combined_msg, updated_status
else:
error_msg = f"β {message}"
logger.error(f"Data clearing failed: {message}")
# Still get updated status even on error
updated_status = get_chat_status()
return None, f'<div class="session-info">{error_msg}</div>', updated_status
except Exception as e:
error_msg = f"Error clearing data: {str(e)}"
logger.error(error_msg)
# Get current status
current_status = get_chat_status()
return None, f'<div class="session-info">β {error_msg}</div>', current_status
def handle_query_search(query, method, k_value):
"""Handle query search and return formatted results."""
if not query or not query.strip():
return """
<div class="ranker-container">
<div class="ranker-placeholder">
<h3>π Query Ranker</h3>
<p>Enter a search query to find relevant document chunks with similarity scores.</p>
</div>
</div>
"""
try:
logger.info(f"Query search: '{query[:50]}...' using method: {method}")
# Get results based on method
results = []
if method == "similarity":
retriever = vector_store_manager.get_retriever("similarity", {"k": k_value})
docs = retriever.invoke(query)
# Try to get actual similarity scores
try:
vector_store = vector_store_manager.get_vector_store()
if hasattr(vector_store, 'similarity_search_with_score'):
docs_with_scores = vector_store.similarity_search_with_score(query, k=k_value)
for i, (doc, score) in enumerate(docs_with_scores):
similarity_score = max(0, 1 - score) if score is not None else 0.8
results.append(_format_ranker_result(doc, similarity_score, i + 1))
else:
# Fallback without scores
for i, doc in enumerate(docs):
score = 0.85 - (i * 0.05)
results.append(_format_ranker_result(doc, score, i + 1))
except Exception as e:
logger.warning(f"Could not get similarity scores: {e}")
for i, doc in enumerate(docs):
score = 0.85 - (i * 0.05)
results.append(_format_ranker_result(doc, score, i + 1))
elif method == "mmr":
retriever = vector_store_manager.get_retriever("mmr", {"k": k_value, "fetch_k": k_value * 2, "lambda_mult": 0.5})
docs = retriever.invoke(query)
for i, doc in enumerate(docs):
results.append(_format_ranker_result(doc, None, i + 1)) # No score for MMR
elif method == "bm25":
retriever = vector_store_manager.get_bm25_retriever(k=k_value)
docs = retriever.invoke(query)
for i, doc in enumerate(docs):
results.append(_format_ranker_result(doc, None, i + 1)) # No score for BM25
elif method == "hybrid":
retriever = vector_store_manager.get_hybrid_retriever(k=k_value, semantic_weight=0.7, keyword_weight=0.3)
docs = retriever.invoke(query)
# Explicitly limit results to k_value since EnsembleRetriever may return more
docs = docs[:k_value]
for i, doc in enumerate(docs):
results.append(_format_ranker_result(doc, None, i + 1)) # No score for Hybrid
return _format_ranker_results_html(results, query, method)
except Exception as e:
error_msg = f"Error during search: {str(e)}"
logger.error(error_msg)
return f"""
<div class="ranker-container">
<div class="ranker-error">
<h3>β Search Error</h3>
<p>{error_msg}</p>
<p class="error-hint">Please check if documents are uploaded and the system is ready.</p>
</div>
</div>
"""
def _format_ranker_result(doc, score, rank):
"""Format a single document result for the ranker."""
metadata = doc.metadata or {}
# Extract metadata
source = metadata.get("source", "Unknown Document")
page = metadata.get("page", "N/A")
chunk_id = metadata.get("chunk_id", f"chunk_{rank}")
# Content length indicator
content_length = len(doc.page_content)
if content_length < 200:
length_indicator = "π Short"
elif content_length < 500:
length_indicator = "π Medium"
else:
length_indicator = "π Long"
# Rank-based confidence levels (applies to all methods)
if rank <= 3:
confidence = "High"
confidence_color = "#22c55e"
confidence_icon = "π’"
elif rank <= 6:
confidence = "Medium"
confidence_color = "#f59e0b"
confidence_icon = "π‘"
else:
confidence = "Low"
confidence_color = "#ef4444"
confidence_icon = "π΄"
result = {
"rank": rank,
"content": doc.page_content,
"source": source,
"page": page,
"chunk_id": chunk_id,
"length_indicator": length_indicator,
"has_score": score is not None,
"confidence": confidence,
"confidence_color": confidence_color,
"confidence_icon": confidence_icon
}
# Only add score if we have a real score (similarity search only)
if score is not None:
result["score"] = round(score, 3)
return result
def _format_ranker_results_html(results, query, method):
"""Format search results as HTML."""
if not results:
return """
<div class="ranker-container">
<div class="ranker-no-results">
<h3>π No Results Found</h3>
<p>No relevant documents found for your query.</p>
<p class="no-results-hint">Try different keywords or check if documents are uploaded.</p>
</div>
</div>
"""
# Method display names
method_labels = {
"similarity": "π― Similarity Search",
"mmr": "π MMR (Diverse)",
"bm25": "π BM25 (Keywords)",
"hybrid": "π Hybrid (Recommended)"
}
method_display = method_labels.get(method, method)
# Start building HTML
html_parts = [f"""
<div class="ranker-container">
<div class="ranker-header">
<div class="ranker-title">
<h3>π Search Results</h3>
<div class="query-display">"{query}"</div>
</div>
<div class="ranker-meta">
<span class="method-badge">{method_display}</span>
<span class="result-count">{len(results)} results</span>
</div>
</div>
"""]
# Add results
for result in results:
rank_emoji = ["π₯", "π₯", "π₯"][result["rank"] - 1] if result["rank"] <= 3 else f"#{result['rank']}"
# Escape content for safe HTML inclusion and JavaScript
escaped_content = result['content'].replace('"', '"').replace("'", "'").replace('\n', '\\n')
# Build score info - always show confidence, only show score for similarity search
score_info_parts = [f"""
<span class="confidence-badge" style="color: {result['confidence_color']}">
{result['confidence_icon']} {result['confidence']}
</span>"""]
# Only add score value if we have real scores (similarity search)
if result.get('has_score', False):
score_info_parts.append(f'<span class="score-value">π― {result["score"]}</span>')
score_info_html = f"""
<div class="score-info">
{''.join(score_info_parts)}
</div>"""
html_parts.append(f"""
<div class="result-card">
<div class="result-header">
<div class="rank-info">
<span class="rank-badge">{rank_emoji} Rank {result['rank']}</span>
<span class="source-info">π {result['source']}</span>
{f"<span class='page-info'>Page {result['page']}</span>" if result['page'] != 'N/A' else ""}
<span class="length-info">{result['length_indicator']}</span>
</div>
{score_info_html}
</div>
<div class="result-content">
<div class="content-text">{result['content']}</div>
</div>
</div>
""")
html_parts.append("</div>")
return "".join(html_parts)
def get_ranker_status():
"""Get current ranker system status."""
try:
# Get collection info
collection_info = vector_store_manager.get_collection_info()
document_count = collection_info.get("document_count", 0)
# Get available methods
available_methods = ["similarity", "mmr", "bm25", "hybrid"]
# Check if system is ready
ingestion_status = document_ingestion_service.get_ingestion_status()
system_ready = ingestion_status.get('system_ready', False)
status_html = f"""
<div class="status-card">
<div class="status-header">
<h3>π Query Ranker Status</h3>
<div class="status-indicator {'status-ready' if system_ready else 'status-not-ready'}">
{'π’ READY' if system_ready else 'π΄ NOT READY'}
</div>
</div>
<div class="status-grid">
<div class="status-item">
<div class="status-label">Available Documents</div>
<div class="status-value">{document_count}</div>
</div>
<div class="status-item">
<div class="status-label">Retrieval Methods</div>
<div class="status-value">{len(available_methods)}</div>
</div>
<div class="status-item">
<div class="status-label">Vector Store</div>
<div class="status-value">{'Ready' if system_ready else 'Not Ready'}</div>
</div>
</div>
<div class="ranker-methods">
<div class="methods-label">Available Methods:</div>
<div class="methods-list">
<span class="method-tag">π― Similarity</span>
<span class="method-tag">π MMR</span>
<span class="method-tag">π BM25</span>
<span class="method-tag">π Hybrid</span>
</div>
</div>
</div>
"""
return status_html
except Exception as e:
error_msg = f"Error getting ranker status: {str(e)}"
logger.error(error_msg)
return f"""
<div class="status-card status-error">
<div class="status-header">
<h3>β System Error</h3>
</div>
<p class="error-message">{error_msg}</p>
</div>
"""
def get_chat_status():
"""Get current chat system status."""
try:
# Check ingestion status
ingestion_status = document_ingestion_service.get_ingestion_status()
# Check usage stats
usage_stats = rag_chat_service.get_usage_stats()
# Get data status for additional context
data_status = data_clearing_service.get_data_status()
# Modern status card design with better styling
status_html = f"""
<div class="status-card">
<div class="status-header">
<h3>π¬ Chat System Status</h3>
<div class="status-indicator {'status-ready' if ingestion_status.get('system_ready', False) else 'status-not-ready'}">
{'π’ READY' if ingestion_status.get('system_ready', False) else 'π΄ NOT READY'}
</div>
</div>
<div class="status-grid">
<div class="status-item">
<div class="status-label">Vector Store Docs</div>
<div class="status-value">{data_status.get('vector_store', {}).get('document_count', 0)}</div>
</div>
<div class="status-item">
<div class="status-label">Chat History Files</div>
<div class="status-value">{data_status.get('chat_history', {}).get('file_count', 0)}</div>
</div>
<div class="status-item">
<div class="status-label">Session Usage</div>
<div class="status-value">{usage_stats.get('session_messages', 0)}/{usage_stats.get('session_limit', 50)}</div>
</div>
<div class="status-item">
<div class="status-label">Environment</div>
<div class="status-value">{'HF Space' if data_status.get('environment') == 'hf_space' else 'Local'}</div>
</div>
</div>
<div class="status-services">
<div class="service-status {'service-ready' if ingestion_status.get('embedding_model_available', False) else 'service-error'}">
<span class="service-icon">π§ </span>
<span>Embedding Model</span>
<span class="service-indicator">{'β
' if ingestion_status.get('embedding_model_available', False) else 'β'}</span>
</div>
<div class="service-status {'service-ready' if ingestion_status.get('vector_store_available', False) else 'service-error'}">
<span class="service-icon">ποΈ</span>
<span>Vector Store</span>
<span class="service-indicator">{'β
' if ingestion_status.get('vector_store_available', False) else 'β'}</span>
</div>
</div>
</div>
"""
return status_html
except Exception as e:
error_msg = f"Error getting chat status: {str(e)}"
logger.error(error_msg)
return f"""
<div class="status-card status-error">
<div class="status-header">
<h3>β System Error</h3>
</div>
<p class="error-message">{error_msg}</p>
</div>
"""
def create_ui():
with gr.Blocks(css="""
/* Global styles */
.gradio-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
/* Document converter styles */
.output-container {
max-height: 420px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 10px;
}
.gradio-container .prose {
overflow: visible;
}
.processing-controls {
display: flex;
justify-content: center;
gap: 10px;
margin-top: 10px;
}
.provider-options-row {
margin-top: 15px;
margin-bottom: 15px;
}
/* Chat Tab Styles - Complete redesign */
.chat-tab-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.chat-header {
text-align: center;
margin-bottom: 30px;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 15px;
color: white;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.chat-header h2 {
margin: 0;
font-size: 1.8em;
font-weight: 600;
}
.chat-header p {
margin: 10px 0 0 0;
opacity: 0.9;
font-size: 1.1em;
}
/* Status Card Styling */
.status-card {
background: #ffffff;
border: 1px solid #e1e5e9;
border-radius: 12px;
padding: 20px;
margin-bottom: 25px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
transition: all 0.3s ease;
}
.status-card:hover {
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.status-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 2px solid #f0f2f5;
}
.status-header h3 {
margin: 0;
color: #2c3e50;
font-size: 1.3em;
font-weight: 600;
}
.status-indicator {
padding: 8px 16px;
border-radius: 25px;
font-weight: 600;
font-size: 0.9em;
letter-spacing: 0.5px;
}
.status-ready {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status-not-ready {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.status-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.status-item {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
text-align: center;
border: 1px solid #e9ecef;
}
.status-label {
font-size: 0.85em;
color: #6c757d;
margin-bottom: 5px;
font-weight: 500;
}
.status-value {
font-size: 1.4em;
font-weight: 700;
color: #495057;
}
.status-services {
display: flex;
gap: 15px;
flex-wrap: wrap;
}
.service-status {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 15px;
border-radius: 8px;
font-weight: 500;
flex: 1;
min-width: 200px;
color: #2c3e50 !important;
}
.service-status span {
color: #2c3e50 !important;
}
.service-ready {
background: #d4edda;
color: #2c3e50 !important;
border: 1px solid #c3e6cb;
}
.service-ready span {
color: #2c3e50 !important;
}
.service-error {
background: #f8d7da;
color: #2c3e50 !important;
border: 1px solid #f5c6cb;
}
.service-error span {
color: #2c3e50 !important;
}
.service-icon {
font-size: 1.2em;
}
.service-indicator {
margin-left: auto;
}
.status-error {
border-color: #dc3545;
background: #f8d7da;
}
.error-message {
color: #721c24;
margin: 0;
font-weight: 500;
}
/* Control buttons styling */
.control-buttons {
display: flex;
gap: 12px;
justify-content: flex-end;
margin-bottom: 25px;
}
.control-btn {
padding: 10px 20px;
border-radius: 8px;
font-weight: 500;
transition: all 0.3s ease;
border: none;
cursor: pointer;
}
.btn-refresh {
background: #17a2b8;
color: white;
}
.btn-refresh:hover {
background: #138496;
transform: translateY(-1px);
}
.btn-new-session {
background: #28a745;
color: white;
}
.btn-new-session:hover {
background: #218838;
transform: translateY(-1px);
}
.btn-clear-data {
background: #dc3545;
color: white;
}
.btn-clear-data:hover {
background: #c82333;
transform: translateY(-1px);
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.btn-primary:hover {
transform: translateY(-1px);
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
/* Chat interface styling */
.chat-main-container {
background: #ffffff;
border-radius: 15px;
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
overflow: hidden;
margin-bottom: 25px;
}
.chat-container {
background: #ffffff;
border-radius: 12px;
border: 1px solid #e1e5e9;
overflow: hidden;
}
/* Custom chatbot styling */
.gradio-chatbot {
border: none !important;
background: #ffffff;
}
.gradio-chatbot .message {
padding: 15px 20px;
margin: 10px;
border-radius: 12px;
}
.gradio-chatbot .message.user {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin-left: 50px;
}
.gradio-chatbot .message.assistant {
background: #f8f9fa;
border: 1px solid #e9ecef;
margin-right: 50px;
}
/* Input area styling */
.chat-input-container {
background: #ffffff;
padding: 20px;
border-top: 1px solid #e1e5e9;
border-radius: 0 0 15px 15px;
}
.input-row {
display: flex;
gap: 12px;
align-items: center;
}
.message-input {
flex: 1;
border: 2px solid #e1e5e9;
border-radius: 25px;
padding: 12px 20px;
font-size: 1em;
transition: all 0.3s ease;
resize: none;
max-height: 120px;
min-height: 48px;
}
.message-input:focus {
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
outline: none;
}
.send-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 12px;
padding: 12px 24px;
min-width: 80px;
height: 48px;
margin-right: 10px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
font-size: 1em;
font-weight: 600;
letter-spacing: 0.5px;
}
.send-button:hover {
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
/* Session info styling */
.session-info {
background: #e7f3ff;
border: 1px solid #b3d9ff;
border-radius: 8px;
padding: 15px;
color: #0056b3;
font-weight: 500;
text-align: center;
}
/* Responsive design */
@media (max-width: 768px) {
.chat-tab-container {
padding: 10px;
}
.status-grid {
grid-template-columns: repeat(2, 1fr);
}
.service-status {
min-width: 100%;
}
.control-buttons {
flex-direction: column;
gap: 8px;
}
.gradio-chatbot .message.user {
margin-left: 20px;
}
.gradio-chatbot .message.assistant {
margin-right: 20px;
}
}
/* Query Ranker Styles */
.ranker-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.ranker-placeholder {
text-align: center;
padding: 40px;
background: #f8f9fa;
border-radius: 12px;
border: 1px solid #e9ecef;
color: #6c757d;
}
.ranker-placeholder h3 {
color: #495057;
margin-bottom: 10px;
}
.ranker-error {
text-align: center;
padding: 30px;
background: #f8d7da;
border: 1px solid #f5c6cb;
border-radius: 12px;
color: #721c24;
}
.ranker-error h3 {
margin-bottom: 15px;
}
.error-hint {
font-style: italic;
margin-top: 10px;
opacity: 0.8;
}
.ranker-no-results {
text-align: center;
padding: 40px;
background: #ffffff;
border: 1px solid #e1e5e9;
border-radius: 12px;
color: #6c757d;
}
.ranker-no-results h3 {
color: #495057;
margin-bottom: 15px;
}
.no-results-hint {
font-style: italic;
margin-top: 10px;
opacity: 0.8;
}
.ranker-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 15px;
margin-bottom: 25px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.ranker-title h3 {
margin: 0 0 10px 0;
font-size: 1.4em;
font-weight: 600;
}
.query-display {
font-size: 1.1em;
opacity: 0.9;
font-style: italic;
margin-bottom: 15px;
}
.ranker-meta {
display: flex;
gap: 15px;
align-items: center;
flex-wrap: wrap;
}
.method-badge {
background: rgba(255, 255, 255, 0.2);
padding: 6px 12px;
border-radius: 20px;
font-weight: 500;
font-size: 0.9em;
}
.result-count {
background: rgba(255, 255, 255, 0.15);
padding: 6px 12px;
border-radius: 20px;
font-weight: 500;
font-size: 0.9em;
}
.result-card {
background: #ffffff;
border: 1px solid #e1e5e9;
border-radius: 12px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
transition: all 0.3s ease;
overflow: hidden;
}
.result-card:hover {
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
transform: translateY(-2px);
}
.result-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
background: #f8f9fa;
border-bottom: 1px solid #e9ecef;
}
.rank-info {
display: flex;
gap: 10px;
align-items: center;
flex-wrap: wrap;
}
.rank-badge {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 4px 10px;
border-radius: 15px;
font-weight: 600;
font-size: 0.85em;
}
.source-info {
background: #e9ecef;
color: #495057;
padding: 4px 8px;
border-radius: 10px;
font-size: 0.85em;
font-weight: 500;
}
.page-info {
background: #d1ecf1;
color: #0c5460;
padding: 4px 8px;
border-radius: 10px;
font-size: 0.85em;
}
.length-info {
background: #f8f9fa;
color: #6c757d;
padding: 4px 8px;
border-radius: 10px;
font-size: 0.85em;
}
.score-info {
display: flex;
gap: 10px;
align-items: center;
}
.confidence-badge {
padding: 4px 8px;
border-radius: 10px;
font-weight: 600;
font-size: 0.85em;
}
.score-value {
background: #2c3e50;
color: white;
padding: 6px 12px;
border-radius: 15px;
font-weight: 600;
font-size: 0.9em;
}
.result-content {
padding: 20px;
}
.content-text {
line-height: 1.6;
color: #2c3e50;
border-left: 3px solid #667eea;
padding-left: 15px;
background: #f8f9fa;
padding: 15px;
border-radius: 0 8px 8px 0;
max-height: 300px;
overflow-y: auto;
}
.result-actions {
display: flex;
gap: 10px;
padding: 15px 20px;
background: #f8f9fa;
border-top: 1px solid #e9ecef;
}
.action-btn {
padding: 8px 16px;
border: none;
border-radius: 8px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
font-size: 0.9em;
display: flex;
align-items: center;
gap: 5px;
}
.copy-btn {
background: #17a2b8;
color: white;
}
.copy-btn:hover {
background: #138496;
transform: translateY(-1px);
}
.info-btn {
background: #6c757d;
color: white;
}
.info-btn:hover {
background: #5a6268;
transform: translateY(-1px);
}
.ranker-methods {
margin-top: 20px;
padding-top: 15px;
border-top: 1px solid #e9ecef;
}
.methods-label {
font-weight: 600;
color: #495057;
margin-bottom: 10px;
font-size: 0.9em;
}
.methods-list {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.method-tag {
background: #e9ecef;
color: #495057;
padding: 4px 10px;
border-radius: 12px;
font-size: 0.8em;
font-weight: 500;
}
/* Ranker controls styling */
.ranker-controls {
background: #ffffff;
border: 1px solid #e1e5e9;
border-radius: 12px;
padding: 20px;
margin-bottom: 25px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.ranker-input-row {
display: flex;
gap: 15px;
align-items: end;
margin-bottom: 15px;
}
.ranker-query-input {
flex: 1;
border: 2px solid #e1e5e9;
border-radius: 25px;
padding: 12px 20px;
font-size: 1em;
transition: all 0.3s ease;
}
.ranker-query-input:focus {
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
outline: none;
}
.ranker-search-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 12px;
padding: 12px 24px;
min-width: 100px;
cursor: pointer;
transition: all 0.3s ease;
font-weight: 600;
font-size: 1em;
}
.ranker-search-btn:hover {
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
.ranker-options-row {
display: flex;
gap: 15px;
align-items: center;
}
/* Responsive design for ranker */
@media (max-width: 768px) {
.ranker-container {
padding: 10px;
}
.ranker-input-row {
flex-direction: column;
gap: 10px;
}
.ranker-options-row {
flex-direction: column;
gap: 10px;
align-items: stretch;
}
.ranker-meta {
justify-content: center;
}
.rank-info {
flex-direction: column;
gap: 5px;
align-items: flex-start;
}
.result-header {
flex-direction: column;
gap: 10px;
align-items: flex-start;
}
.score-info {
align-self: flex-end;
}
.result-actions {
flex-direction: column;
gap: 8px;
}
}
""") as demo:
# Modern title with better styling
gr.Markdown("""
# π Markit
## Document to Markdown Converter with RAG Chat
""")
with gr.Tabs():
# Document Converter Tab
with gr.TabItem("π Document Converter"):
with gr.Column(elem_classes=["chat-tab-container"]):
# Modern header matching other tabs
gr.HTML("""
<div class="chat-header">
<h2>π Document Converter</h2>
<p>Convert documents to Markdown format with advanced OCR and AI processing</p>
</div>
""")
# State to track if cancellation is requested
cancel_requested = gr.State(False)
# State to store the conversion thread
conversion_thread = gr.State(None)
# State to store the output format (fixed to Markdown)
output_format_state = gr.State("Markdown")
# Multi-file input (supports single and multiple files)
files_input = gr.Files(
label="Upload Document(s) - Single file or up to 5 files (20MB max combined)",
file_count="multiple",
file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp", ".webp", ".docx", ".doc", ".pptx", ".ppt", ".xlsx", ".xls", ".txt", ".md", ".html", ".htm"]
)
# Processing type selector (visible only for multiple files)
processing_type_selector = gr.Radio(
choices=["combined", "individual", "summary", "comparison"],
value="combined",
label="Multi-Document Processing Type",
info="How to process multiple documents together",
visible=False
)
# Status text to show file count and processing mode
file_status_text = gr.HTML(
value="<div style='color: #666; font-style: italic;'>Upload documents to begin</div>",
label=""
)
# Provider and OCR options below the file input
with gr.Row(elem_classes=["provider-options-row"]):
with gr.Column(scale=1):
parser_names = ParserRegistry.get_parser_names()
# Make MarkItDown the default parser if available
default_parser = next((p for p in parser_names if p == "MarkItDown"), parser_names[0] if parser_names else "PyPdfium")
provider_dropdown = gr.Dropdown(
label="Provider",
choices=parser_names,
value=default_parser,
interactive=True
)
with gr.Column(scale=1):
default_ocr_options = ParserRegistry.get_ocr_options(default_parser)
default_ocr = default_ocr_options[0] if default_ocr_options else "No OCR"
ocr_dropdown = gr.Dropdown(
label="OCR Options",
choices=default_ocr_options,
value=default_ocr,
interactive=True
)
# Processing controls row with consistent styling
with gr.Row(elem_classes=["control-buttons"]):
convert_button = gr.Button("π Convert", elem_classes=["control-btn", "btn-primary"])
cancel_button = gr.Button("βΉοΈ Cancel", elem_classes=["control-btn", "btn-clear-data"], visible=False)
# Simple output container with just one scrollbar
file_display = gr.HTML(
value="<div class='output-container'></div>",
label="Converted Content"
)
file_download = gr.File(label="Download File")
# Event handlers for document converter
# Update UI when files are uploaded/changed
files_input.change(
fn=update_ui_for_file_count,
inputs=[files_input],
outputs=[processing_type_selector, file_status_text]
)
provider_dropdown.change(
lambda p: gr.Dropdown(
choices=["Plain Text", "Formatted Text"] if "GOT-OCR" in p else ParserRegistry.get_ocr_options(p),
value="Plain Text" if "GOT-OCR" in p else (ParserRegistry.get_ocr_options(p)[0] if ParserRegistry.get_ocr_options(p) else None)
),
inputs=[provider_dropdown],
outputs=[ocr_dropdown]
)
# Reset cancel flag when starting conversion
def start_conversion():
global conversion_cancelled
conversion_cancelled.clear()
logger.info("Starting conversion with cancellation flag cleared")
return gr.update(visible=False), gr.update(visible=True), False
# Set cancel flag and terminate thread when cancel button is clicked
def request_cancellation(thread):
global conversion_cancelled
conversion_cancelled.set()
logger.info("Cancel button clicked, cancellation flag set")
# Try to join the thread with a timeout
if thread is not None:
logger.info(f"Attempting to join conversion thread: {thread}")
thread.join(timeout=0.5)
if thread.is_alive():
logger.warning("Thread did not finish within timeout")
# Add immediate feedback to the user
return gr.update(visible=True), gr.update(visible=False), True, None
# Start conversion sequence
convert_button.click(
fn=start_conversion,
inputs=[],
outputs=[convert_button, cancel_button, cancel_requested],
queue=False # Execute immediately
).then(
fn=handle_convert,
inputs=[files_input, provider_dropdown, ocr_dropdown, output_format_state, processing_type_selector, cancel_requested],
outputs=[file_display, file_download, convert_button, cancel_button, conversion_thread]
)
# Handle cancel button click
cancel_button.click(
fn=request_cancellation,
inputs=[conversion_thread],
outputs=[convert_button, cancel_button, cancel_requested, conversion_thread],
queue=False # Execute immediately
)
# Chat Tab - Completely redesigned
with gr.TabItem("π¬ Chat with Documents"):
with gr.Column(elem_classes=["chat-tab-container"]):
# Modern header
gr.HTML("""
<div class="chat-header">
<h2>π¬ Chat with your converted documents</h2>
<p>Ask questions about your documents using advanced RAG technology</p>
</div>
""")
# Status section with modern design
status_display = gr.HTML(value=get_chat_status())
# Control buttons
with gr.Row(elem_classes=["control-buttons"]):
refresh_status_btn = gr.Button("π Refresh Status", elem_classes=["control-btn", "btn-refresh"])
new_session_btn = gr.Button("π New Session", elem_classes=["control-btn", "btn-new-session"])
clear_data_btn = gr.Button("ποΈ Clear All Data", elem_classes=["control-btn", "btn-clear-data"], variant="stop")
# Main chat interface
with gr.Column(elem_classes=["chat-main-container"]):
chatbot = gr.Chatbot(
elem_classes=["chat-container"],
height=500,
show_label=False,
show_share_button=False,
bubble_full_width=False,
type="messages",
placeholder="Start a conversation by asking questions about your documents..."
)
# Input area
with gr.Row(elem_classes=["input-row"]):
msg_input = gr.Textbox(
placeholder="Ask questions about your documents...",
show_label=False,
scale=5,
lines=1,
max_lines=3,
elem_classes=["message-input"]
)
send_btn = gr.Button("Submit", elem_classes=["send-button"], scale=0)
# Session info with better styling
session_info = gr.HTML(
value='<div class="session-info">No active session - Click "New Session" to start</div>'
)
# Event handlers for chat
def clear_input():
return ""
# Send message when button clicked or Enter pressed
msg_input.submit(
fn=handle_chat_message,
inputs=[msg_input, chatbot],
outputs=[msg_input, chatbot, status_display]
)
send_btn.click(
fn=handle_chat_message,
inputs=[msg_input, chatbot],
outputs=[msg_input, chatbot, status_display]
)
# New session handler with improved feedback
def enhanced_new_session():
history, info = start_new_chat_session()
session_html = f'<div class="session-info">{info}</div>'
updated_status = get_chat_status()
return history, session_html, updated_status
new_session_btn.click(
fn=enhanced_new_session,
inputs=[],
outputs=[chatbot, session_info, status_display]
)
# Refresh status handler
refresh_status_btn.click(
fn=get_chat_status,
inputs=[],
outputs=[status_display]
)
# Clear all data handler
clear_data_btn.click(
fn=handle_clear_all_data,
inputs=[],
outputs=[chatbot, session_info, status_display]
)
# Query Ranker Tab
with gr.TabItem("π Query Ranker"):
with gr.Column(elem_classes=["ranker-container"]):
# Modern header
gr.HTML("""
<div class="chat-header">
<h2>π Query Ranker</h2>
<p>Search and rank document chunks with similarity scores</p>
</div>
""")
# Status section
ranker_status_display = gr.HTML(value=get_ranker_status())
# Control buttons
with gr.Row(elem_classes=["control-buttons"]):
refresh_ranker_status_btn = gr.Button("π Refresh Status", elem_classes=["control-btn", "btn-refresh"])
clear_results_btn = gr.Button("ποΈ Clear Results", elem_classes=["control-btn", "btn-clear-data"])
# Search controls
with gr.Column(elem_classes=["ranker-controls"]):
with gr.Row(elem_classes=["ranker-input-row"]):
query_input = gr.Textbox(
placeholder="Enter your search query...",
show_label=False,
elem_classes=["ranker-query-input"],
scale=4
)
search_btn = gr.Button("π Search", elem_classes=["ranker-search-btn"], scale=0)
with gr.Row(elem_classes=["ranker-options-row"]):
method_dropdown = gr.Dropdown(
choices=[
("π― Similarity Search", "similarity"),
("π MMR (Diverse)", "mmr"),
("π BM25 (Keywords)", "bm25"),
("π Hybrid (Recommended)", "hybrid")
],
value="hybrid",
label="Retrieval Method",
scale=2
)
k_slider = gr.Slider(
minimum=1,
maximum=10,
value=5,
step=1,
label="Number of Results",
scale=1
)
# Results display
results_display = gr.HTML(
value=handle_query_search("", "hybrid", 5), # Initial placeholder
elem_classes=["ranker-results-container"]
)
# Event handlers for Query Ranker
def clear_ranker_results():
"""Clear the search results and reset to placeholder."""
return handle_query_search("", "hybrid", 5), ""
def refresh_ranker_status():
"""Refresh the ranker status display."""
return get_ranker_status()
# Search functionality
query_input.submit(
fn=handle_query_search,
inputs=[query_input, method_dropdown, k_slider],
outputs=[results_display]
)
search_btn.click(
fn=handle_query_search,
inputs=[query_input, method_dropdown, k_slider],
outputs=[results_display]
)
# Control button handlers
refresh_ranker_status_btn.click(
fn=refresh_ranker_status,
inputs=[],
outputs=[ranker_status_display]
)
clear_results_btn.click(
fn=clear_ranker_results,
inputs=[],
outputs=[results_display, query_input]
)
# Update results when method or k changes
method_dropdown.change(
fn=handle_query_search,
inputs=[query_input, method_dropdown, k_slider],
outputs=[results_display]
)
k_slider.change(
fn=handle_query_search,
inputs=[query_input, method_dropdown, k_slider],
outputs=[results_display]
)
return demo
def launch_ui(server_name="0.0.0.0", server_port=7860, share=False):
demo = create_ui()
demo.launch(
server_name=server_name,
server_port=server_port,
root_path="",
show_error=True,
share=share
) |