File size: 33,774 Bytes
46df5f0 e5e8568 46df5f0 e5e8568 46df5f0 e5e8568 46df5f0 e5e8568 46df5f0 |
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 |
#!/usr/bin/env python3
"""
BibGuard Gradio Web Application
A web interface for checking bibliography and LaTeX quality.
"""
import gradio as gr
import tempfile
import shutil
from pathlib import Path
from typing import Optional, Tuple
import base64
from src.parsers import BibParser, TexParser
from src.fetchers import ArxivFetcher, CrossRefFetcher, SemanticScholarFetcher, OpenAlexFetcher, DBLPFetcher
from src.analyzers import MetadataComparator, UsageChecker, DuplicateDetector
from src.report.generator import ReportGenerator, EntryReport
from src.config.yaml_config import BibGuardConfig, FilesConfig, BibliographyConfig, SubmissionConfig, OutputConfig, WorkflowStep
from src.config.workflow import WorkflowConfig, WorkflowStep as WFStep, get_default_workflow
from src.checkers import CHECKER_REGISTRY
from src.report.line_report import LineByLineReportGenerator
from app_helper import fetch_and_compare_with_workflow
# Custom CSS for better Markdown rendering
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
* {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
"""
WELCOME_HTML = """
<div class="scrollable-report-area">
<div class="report-card" style="max-width: 800px; margin: 0 auto;">
<div class="card-header">
<h3 class="card-title" style="font-size: 1.5em;">π Welcome to BibGuard</h3>
</div>
<div class="card-content" style="line-height: 1.6; color: #374151;">
<p style="font-size: 1.1em; margin-bottom: 24px;">
Ensure your academic paper is flawless. Upload your <code>.bib</code> and <code>.tex</code> files on the left and click <strong>"Check Now"</strong>.
</p>
<div style="display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));">
<div style="background: #fefce8; padding: 16px; border-radius: 8px; border: 1px solid #fde047;">
<strong style="color: #854d0e; display: block; margin-bottom: 8px;">β οΈ Metadata Check Defaults</strong>
"π Metadata" is <strong>disabled by default</strong>. It verifies your entries against ArXiv/DBLP/Crossref but takes time (1-3 mins) to fetch data. Enable it if you want strict verification.
</div>
<div style="background: #eff6ff; padding: 16px; border-radius: 8px; border: 1px solid #bfdbfe;">
<strong style="color: #1e40af; display: block; margin-bottom: 8px;">π Go Pro with Local Version</strong>
LLM-based context relevance checking (is this citation actually relevant?) is excluded here. Clone the <a href="https://github.com/thinkwee/BibGuard" target="_blank" style="color: #2563eb; text-decoration: underline; font-weight: 600;">GitHub repo</a> to use the full power with your API key.
</div>
</div>
<h4 style="margin: 24px 0 12px 0; color: #111827; font-size: 1.1em;">π Understanding Your Reports</h4>
<div style="display: grid; gap: 12px;">
<div style="display: flex; gap: 12px; align-items: baseline;">
<span style="background: #e0e7ff; color: #3730a3; padding: 2px 8px; border-radius: 4px; font-size: 0.9em; font-weight: 600; white-space: nowrap;">π Bibliography</span>
<span>Validates metadata fields, detects duplicates, and checks citation counts.</span>
</div>
<div style="display: flex; gap: 12px; align-items: baseline;">
<span style="background: #dcfce7; color: #166534; padding: 2px 8px; border-radius: 4px; font-size: 0.9em; font-weight: 600; white-space: nowrap;">π LaTeX Quality</span>
<span>Syntax check, caption validation, acronym consistency, and style suggestions.</span>
</div>
<div style="display: flex; gap: 12px; align-items: baseline;">
<span style="background: #f3f4f6; color: #4b5563; padding: 2px 8px; border-radius: 4px; font-size: 0.9em; font-weight: 600; white-space: nowrap;">π Line-by-Line</span>
<span>Maps every issue found directly to the line number in your source file.</span>
</div>
</div>
</div>
</div>
</div>
"""
CUSTOM_CSS += """
/* Global Reset */
body, gradio-app {
overflow: hidden !important; /* Prevent double scrollbars on the page */
}
.gradio-container {
max-width: none !important;
width: 100% !important;
/* height: 100vh !important; <-- Removed to prevent iframe infinite loop */
padding: 0 !important;
margin: 0 !important;
}
/* Header Styling */
.app-header {
padding: 20px;
background: white;
border-bottom: 1px solid #e5e7eb;
}
/* Sidebar Styling */
.app-sidebar {
height: auto !important;
max-height: calc(100vh - 100px) !important;
overflow-y: auto !important;
padding: 20px !important;
border-right: 1px solid #e5e7eb;
}
/* Main Content Area */
.app-content {
height: auto !important;
max-height: calc(100vh - 100px) !important;
padding: 0 !important;
}
/* The Magic Scroll Container - Clean and Explicit */
.scrollable-report-area {
/* Fixed height relative to viewport can cause loops in Spaces */
max-height: 800px !important;
height: auto !important;
min-height: 500px !important;
overflow-y: auto !important;
padding: 24px;
background-color: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 8px;
margin-top: 10px;
}
/* Report Card Styling */
.report-card {
background: white;
border-radius: 12px;
padding: 24px;
margin-bottom: 16px; /* Spacing between cards */
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
border: 1px solid #e5e7eb;
transition: transform 0.2s, box-shadow 0.2s;
}
.report-card:hover {
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
transform: translateY(-2px);
}
/* Card Internals */
.card-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 16px;
padding-bottom: 16px;
border-bottom: 1px solid #f3f4f6;
}
.card-title {
font-size: 1.1em;
font-weight: 600;
color: #111827;
margin: 0 0 4px 0;
}
.card-subtitle {
font-size: 0.9em;
color: #6b7280;
font-family: monospace;
}
.card-content {
font-size: 0.95em;
color: #374151;
line-height: 1.5;
}
/* Badges */
.badge {
display: inline-flex;
align-items: center;
padding: 4px 10px;
border-radius: 9999px;
font-size: 0.8em;
font-weight: 500;
}
.badge-success { background-color: #dcfce7; color: #166534; }
.badge-warning { background-color: #fef9c3; color: #854d0e; }
.badge-error { background-color: #fee2e2; color: #991b1b; }
.badge-info { background-color: #dbeafe; color: #1e40af; }
.badge-neutral { background-color: #f3f4f6; color: #4b5563; }
/* Stats Grid */
.stats-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
gap: 16px;
margin-bottom: 24px;
}
.stat-card {
padding: 16px;
border-radius: 12px;
color: white;
text-align: center;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.stat-value { font-size: 1.8em; font-weight: 700; }
.stat-label { font-size: 0.9em; opacity: 0.9; }
/* Detail Grid - Flexbox for better filling */
.detail-grid {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
width: 100%;
}
.detail-item {
background: #f9fafb;
padding: 10px 12px;
border-radius: 8px;
border: 1px solid #f3f4f6;
/* Flex sizing: grow, shrink, min-basis */
flex: 1 1 160px;
min-width: 0; /* Important for word-break to work in flex children */
/* Layout control */
display: flex;
flex-direction: column;
/* Height constraint to prevent one huge card from stretching the row */
max-height: 100px;
overflow-y: auto;
}
/* Custom scrollbar for detail items */
.detail-item::-webkit-scrollbar {
width: 4px;
}
.detail-item::-webkit-scrollbar-thumb {
background-color: #d1d5db;
border-radius: 4px;
}
.detail-label {
font-size: 0.75em;
color: #6b7280;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 2px;
position: sticky;
top: 0;
background: #f9fafb; /* Maintain bg on scroll */
z-index: 1;
}
.detail-value {
font-weight: 500;
color: #1f2937;
font-size: 0.9em;
line-height: 1.4;
word-break: break-word; /* Fix overflow */
overflow-wrap: break-word;
} border: 1px solid #e5e7eb;
transition: all 0.2s;
}
.report-card:hover {
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
/* Card Header */
.card-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 12px;
border-bottom: 1px solid #f3f4f6;
padding-bottom: 12px;
}
.card-title {
font-size: 1.1em;
font-weight: 600;
color: #1f2937;
margin: 0;
}
.card-subtitle {
font-size: 0.9em;
color: #6b7280;
margin-top: 4px;
}
/* Status Badges */
.badge {
display: inline-flex;
align-items: center;
padding: 4px 10px;
border-radius: 9999px;
font-size: 0.8em;
font-weight: 500;
}
.badge-success { background-color: #dcfce7; color: #166534; }
.badge-warning { background-color: #fef9c3; color: #854d0e; }
.badge-error { background-color: #fee2e2; color: #991b1b; }
.badge-info { background-color: #dbeafe; color: #1e40af; }
.badge-neutral { background-color: #f3f4f6; color: #374151; }
/* Content Styling */
.card-content {
font-size: 15px;
color: #374151;
line-height: 1.6;
}
.card-content code {
background-color: #f3f4f6;
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
font-size: 0.9em;
color: #c2410c;
}
/* Grid for details */
.detail-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 12px;
margin-top: 12px;
}
.detail-item {
background: #f9fafb;
padding: 10px;
border-radius: 6px;
}
.detail-label {
font-size: 0.8em;
color: #6b7280;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.detail-value {
font-weight: 500;
color: #111827;
}
/* Summary Stats */
.stats-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
margin-bottom: 24px;
}
.stat-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 12px;
text-align: center;
box-shadow: 0 4px 6px rgba(102, 126, 234, 0.25);
}
.stat-value {
font-size: 2em;
font-weight: 700;
}
.stat-label {
font-size: 0.9em;
opacity: 0.9;
margin-top: 4px;
}
/* Button styling */
.primary-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
border: none !important;
font-weight: 600 !important;
}
/* Tab styling */
.tab-nav button {
font-weight: 500 !important;
font-size: 15px !important;
}
"""
def create_config_from_ui(
check_metadata: bool,
check_usage: bool,
check_duplicates: bool,
check_preprint_ratio: bool,
caption: bool,
reference: bool,
formatting: bool,
equation: bool,
ai_artifacts: bool,
sentence: bool,
consistency: bool,
acronym: bool,
number: bool,
citation_quality: bool,
anonymization: bool
) -> BibGuardConfig:
"""Create a BibGuardConfig from UI settings."""
config = BibGuardConfig()
config.bibliography = BibliographyConfig(
check_metadata=check_metadata,
check_usage=check_usage,
check_duplicates=check_duplicates,
check_preprint_ratio=check_preprint_ratio,
check_relevance=False # Disabled for web
)
config.submission = SubmissionConfig(
caption=caption,
reference=reference,
formatting=formatting,
equation=equation,
ai_artifacts=ai_artifacts,
sentence=sentence,
consistency=consistency,
acronym=acronym,
number=number,
citation_quality=citation_quality,
anonymization=anonymization
)
config.output = OutputConfig(quiet=True, minimal_verified=False)
return config
def generate_bibliography_html(report_gen: ReportGenerator, entries: list) -> str:
"""Generate HTML content for bibliography report."""
html = ['<div class="scrollable-report-area">']
# 1. Summary Stats
total = len(entries)
verified = sum(1 for e in report_gen.entries if e.comparison and e.comparison.is_match)
used = sum(1 for e in report_gen.entries if e.usage and e.usage.is_used)
html.append('<div class="stats-container">')
html.append(f'<div class="stat-card"><div class="stat-value">{total}</div><div class="stat-label">Total Entries</div></div>')
html.append(f'<div class="stat-card"><div class="stat-value">{verified}</div><div class="stat-label">Verified</div></div>')
html.append(f'<div class="stat-card"><div class="stat-value">{used}</div><div class="stat-label">Used in Text</div></div>')
html.append('</div>')
# 2. Entries
for report in report_gen.entries:
entry = report.entry
status_badges = []
# Metadata Status
if report.comparison:
if report.comparison.is_match:
status_badges.append('<span class="badge badge-success">β Verified</span>')
if report.comparison.source:
status_badges.append(f'<span class="badge badge-info">{report.comparison.source.upper()}</span>')
else:
status_badges.append('<span class="badge badge-error">β Metadata Mismatch</span>')
else:
status_badges.append('<span class="badge badge-neutral">No Metadata Check</span>')
# Usage Status
if report.usage:
if report.usage.is_used:
status_badges.append(f'<span class="badge badge-success">Used: {report.usage.usage_count}x</span>')
else:
status_badges.append('<span class="badge badge-warning">Unused</span>')
# Build Card
html.append(f'''
<div class="report-card">
<div class="card-header">
<div>
<h3 class="card-title">{entry.title or "No Title"}</h3>
<div class="card-subtitle">{entry.key} β’ {entry.year} β’ {entry.entry_type}</div>
</div>
<div style="display: flex; gap: 8px;">
{" ".join(status_badges)}
</div>
</div>
<div class="card-content">
<div class="detail-grid">
{
(lambda e: "".join([
f'<div class="detail-item"><div class="detail-label">{k}</div><div class="detail-value">{v}</div></div>'
for k, v in filter(None, [
("Authors", e.author or "N/A"),
("Venue", e.journal or e.booktitle or e.publisher or "N/A"),
("DOI", e.doi) if e.doi else None,
("ArXiv", e.arxiv_id) if e.arxiv_id and not e.doi else None,
("Volume/Pages", f"{'Vol.'+e.volume if e.volume else ''} {'pp.'+e.pages if e.pages else ''}".strip()) if e.volume or e.pages else None,
("URL", f'<a href="{e.url}" target="_blank" style="text-decoration:underline;">Link</a>') if e.url else None
])
]))(entry)
}
</div>
''')
# Add issues if any
issues = []
if report.comparison and not report.comparison.is_match:
# Add main message derived from match status
if report.comparison.issues:
for issue in report.comparison.issues:
issues.append(f'<div style="margin-left: 20px; font-size: 0.9em; color: #b91c1c;">β’ {issue}</div>')
else:
issues.append(f'<div style="margin-left: 20px; font-size: 0.9em; color: #b91c1c;">β’ Verification failed</div>')
if issues:
html.append('<div style="margin-top: 16px; padding-top: 12px; border-top: 1px solid #eee;">')
html.append("".join(issues))
html.append('</div>')
html.append('</div></div>') # Close card-content and report-card
html.append('</div>') # Close container
return "".join(html)
def generate_latex_html(results: list) -> str:
"""Generate HTML for LaTeX quality check."""
from src.checkers import CheckSeverity
html = ['<div class="scrollable-report-area">']
# Stats
errors = sum(1 for r in results if r.severity == CheckSeverity.ERROR)
warnings = sum(1 for r in results if r.severity == CheckSeverity.WARNING)
infos = sum(1 for r in results if r.severity == CheckSeverity.INFO)
html.append('<div class="stats-container">')
html.append(f'<div class="stat-card" style="background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%);"><div class="stat-value">{errors}</div><div class="stat-label">Errors</div></div>')
html.append(f'<div class="stat-card" style="background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);"><div class="stat-value">{warnings}</div><div class="stat-label">Warnings</div></div>')
html.append(f'<div class="stat-card" style="background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);"><div class="stat-value">{infos}</div><div class="stat-label">Suggestions</div></div>')
html.append('</div>')
if not results:
html.append('<div class="report-card"><div class="card-content" style="text-align: center; padding: 40px; color: #166534; font-size: 1.2em;">β
No issues found in LaTeX code!</div></div>')
else:
# Group by Checker
results.sort(key=lambda x: x.checker_name)
current_checker = None
for result in results:
badge_class = "badge-neutral"
if result.severity == CheckSeverity.ERROR: badge_class = "badge-error"
elif result.severity == CheckSeverity.WARNING: badge_class = "badge-warning"
elif result.severity == CheckSeverity.INFO: badge_class = "badge-info"
html.append(f'''
<div class="report-card">
<div class="card-header">
<div>
<h3 class="card-title">{result.checker_name}</h3>
<div class="card-subtitle">Line {result.line_number}</div>
</div>
<span class="badge {badge_class}">{result.severity.name}</span>
</div>
<div class="card-content">
{result.message}
{f'<div style="margin-top: 8px; background: #f3f4f6; padding: 8px; border-radius: 4px; font-family: monospace;">{result.line_content}</div>' if result.line_content else ''}
{f'<div style="margin-top: 8px; color: #166534;">π‘ Suggestion: {result.suggestion}</div>' if result.suggestion else ''}
</div>
</div>
''')
html.append('</div>')
return "".join(html)
def generate_line_html(content: str, results: list) -> str:
"""Generate HTML for Line-by-Line report."""
# Build a dictionary of line_number -> list of issues
issues_by_line = {}
for r in results:
if r.line_number not in issues_by_line:
issues_by_line[r.line_number] = []
issues_by_line[r.line_number].append(r)
lines = content.split('\n')
html = ['<div class="scrollable-report-area">']
html.append('<div class="report-card"><div class="card-content">Issues are mapped to specific lines below.</div></div>')
for i, line in enumerate(lines, 1):
if i in issues_by_line:
# Highlight this line
line_issues = issues_by_line[i]
html.append(f'''
<div class="report-card" style="border-left: 4px solid #ef4444; padding: 12px;">
<div style="font-family: monospace; color: #6b7280; font-size: 0.9em; margin-bottom: 4px;">Line {i}</div>
<div style="font-family: monospace; background: #fee2e2; padding: 4px; border-radius: 4px; overflow-x: auto; white-space: pre;">{line}</div>
<div style="margin-top: 8px;">
''')
for issue in line_issues:
html.append(f'<div style="color: #991b1b; font-size: 0.95em; margin-top: 4px;">β’ {issue.message}</div>')
html.append('</div></div>')
html.append('</div>')
return "".join(html)
def run_check(
bib_file,
tex_file,
check_metadata: bool,
check_usage: bool,
check_duplicates: bool,
check_preprint_ratio: bool,
caption: bool,
reference: bool,
formatting: bool,
equation: bool,
ai_artifacts: bool,
sentence: bool,
consistency: bool,
acronym: bool,
number: bool,
citation_quality: bool,
anonymization: bool,
progress=gr.Progress()
) -> Tuple[str, str, str]:
"""Run BibGuard checks and return three reports."""
if bib_file is None or tex_file is None:
return (
"β οΈ Please upload both `.bib` and `.tex` files.",
"β οΈ Please upload both `.bib` and `.tex` files.",
"β οΈ Please upload both `.bib` and `.tex` files."
)
try:
# Create config from UI
config = create_config_from_ui(
check_metadata, check_usage, check_duplicates, check_preprint_ratio,
caption, reference, formatting, equation, ai_artifacts,
sentence, consistency, acronym, number, citation_quality, anonymization
)
# Get file paths from uploaded files
bib_path = bib_file.name
tex_path = tex_file.name
# Read tex content for checkers
tex_content = Path(tex_path).read_text(encoding='utf-8', errors='replace')
# Parse files
bib_parser = BibParser()
entries = bib_parser.parse_file(bib_path)
tex_parser = TexParser()
tex_parser.parse_file(tex_path)
bib_config = config.bibliography
# Initialize components
arxiv_fetcher = None
crossref_fetcher = None
semantic_scholar_fetcher = None
openalex_fetcher = None
dblp_fetcher = None
comparator = None
usage_checker = None
duplicate_detector = None
if bib_config.check_metadata:
arxiv_fetcher = ArxivFetcher()
semantic_scholar_fetcher = SemanticScholarFetcher()
openalex_fetcher = OpenAlexFetcher()
dblp_fetcher = DBLPFetcher()
crossref_fetcher = CrossRefFetcher()
comparator = MetadataComparator()
if bib_config.check_usage:
usage_checker = UsageChecker(tex_parser)
if bib_config.check_duplicates:
duplicate_detector = DuplicateDetector()
# Initialize report generator
report_gen = ReportGenerator(
minimal_verified=False,
check_preprint_ratio=bib_config.check_preprint_ratio,
preprint_warning_threshold=bib_config.preprint_warning_threshold
)
report_gen.set_metadata([bib_file.name], [tex_file.name])
# Run submission quality checks
progress(0.2, desc="Running LaTeX quality checks...")
submission_results = []
enabled_checkers = config.submission.get_enabled_checkers()
for checker_name in enabled_checkers:
if checker_name in CHECKER_REGISTRY:
checker = CHECKER_REGISTRY[checker_name]()
results = checker.check(tex_content, {})
for r in results:
r.file_path = tex_file.name
submission_results.extend(results)
report_gen.set_submission_results(submission_results, None)
# Check for duplicates
if bib_config.check_duplicates and duplicate_detector:
duplicate_groups = duplicate_detector.find_duplicates(entries)
report_gen.set_duplicate_groups(duplicate_groups)
# Check missing citations
if bib_config.check_usage and usage_checker:
missing = usage_checker.get_missing_entries(entries)
report_gen.set_missing_citations(missing)
# Build workflow
workflow_config = get_default_workflow()
# Process entries
progress(0.3, desc="Processing bibliography entries...")
total_entries = len(entries)
for i, entry in enumerate(entries):
progress(0.3 + 0.5 * (i / total_entries), desc=f"Checking: {entry.key}")
# Check usage
usage_result = None
if usage_checker:
usage_result = usage_checker.check_usage(entry)
# Fetch and compare metadata
comparison_result = None
if bib_config.check_metadata and comparator:
comparison_result = fetch_and_compare_with_workflow(
entry, workflow_config, arxiv_fetcher, crossref_fetcher,
semantic_scholar_fetcher, openalex_fetcher, dblp_fetcher, comparator
)
# Create entry report
entry_report = EntryReport(
entry=entry,
comparison=comparison_result,
usage=usage_result,
evaluations=[]
)
report_gen.add_entry_report(entry_report)
progress(0.85, desc="Generating structured reports...")
# Generate Bibliography HTML Report
bib_report = generate_bibliography_html(report_gen, entries)
# Generate LaTeX Quality HTML Report
latex_report = generate_latex_html(submission_results)
# Generate Line-by-Line HTML Report
line_report = ""
if submission_results:
line_report = generate_line_html(tex_content, submission_results)
else:
line_report = '<div class="report-container"><div class="report-card"><div class="card-content">No issues to display line-by-line.</div></div></div>'
progress(1.0, desc="Done!")
return bib_report, latex_report, line_report
except Exception as e:
error_msg = f"β Error: {str(e)}"
import traceback
error_msg += f"\n\n```\n{traceback.format_exc()}\n```"
return error_msg, error_msg, error_msg
def create_app():
"""Create and configure the Gradio app."""
# Load icon as base64
icon_html = ""
try:
icon_path = Path("assets/icon-192.png")
if icon_path.exists():
with open(icon_path, "rb") as f:
encoding = base64.b64encode(f.read()).decode()
icon_html = f'<img src="data:image/png;base64,{encoding}" style="width: 48px; height: 48px; border-radius: 8px;" alt="BibGuard">'
else:
icon_html = '<span style="font-size: 48px;">π</span>'
except Exception:
icon_html = '<span style="font-size: 48px;">π</span>'
with gr.Blocks(title="BibGuard - Bibliography & LaTeX Quality Checker") as app:
# Header with icon
with gr.Row(elem_classes=["app-header"]):
gr.HTML(f"""
<div style="display: flex; align-items: center; gap: 12px; margin-bottom: 16px;">
{icon_html}
<div>
<h1 style="margin: 0; font-size: 1.8em;">BibGuard</h1>
<p style="margin: 0; color: #666; font-size: 14px;">Bibliography & LaTeX Quality Checker</p>
</div>
</div>
""")
with gr.Row(elem_classes=["app-body"]):
# Left column: Upload & Settings
with gr.Column(scale=1, min_width=280, elem_classes=["app-sidebar"]):
gr.Markdown("### π Upload Files")
bib_file = gr.File(
label="Bibliography (.bib)",
file_types=[".bib"],
file_count="single"
)
tex_file = gr.File(
label="LaTeX Source (.tex)",
file_types=[".tex"],
file_count="single"
)
# Check options in grid layout
gr.Markdown("#### βοΈ Options")
with gr.Row():
check_metadata = gr.Checkbox(label="π Metadata", value=False)
check_usage = gr.Checkbox(label="π Usage", value=True)
with gr.Row():
check_duplicates = gr.Checkbox(label="π― Duplicates", value=True)
check_preprint_ratio = gr.Checkbox(label="π Preprints", value=True)
with gr.Row():
caption = gr.Checkbox(label="πΌοΈ Captions", value=True)
reference = gr.Checkbox(label="π References", value=True)
with gr.Row():
formatting = gr.Checkbox(label="β¨ Formatting", value=True)
equation = gr.Checkbox(label="π’ Equations", value=True)
with gr.Row():
ai_artifacts = gr.Checkbox(label="π€ AI Artifacts", value=True)
sentence = gr.Checkbox(label="π Sentences", value=True)
with gr.Row():
consistency = gr.Checkbox(label="π Consistency", value=True)
acronym = gr.Checkbox(label="π€ Acronyms", value=True)
with gr.Row():
number = gr.Checkbox(label="π’ Numbers", value=True)
citation_quality = gr.Checkbox(label="π Citations", value=True)
with gr.Row():
anonymization = gr.Checkbox(label="π Anonymization", value=True)
run_btn = gr.Button("π Check Now", variant="primary", size="lg")
gr.HTML("""
<div style="text-align: center; margin-top: 16px;">
<a href="https://github.com/thinkwee/BibGuard" target="_blank" style="text-decoration: none; color: #666; display: inline-flex; align-items: center; gap: 6px;">
<svg height="20" width="20" viewBox="0 0 16 16"><path fill="currentColor" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path></svg>
GitHub
</a>
<p style="margin: 8px 0 0 0; color: #999; font-size: 12px;">Developed with β€οΈ for researchers</p>
</div>
""")
# Right column: Reports
with gr.Column(scale=4, elem_classes=["app-content"]):
with gr.Tabs():
with gr.Tab("π Bibliography Report"):
bib_report = gr.HTML(
value=WELCOME_HTML,
elem_classes=["report-panel"]
)
with gr.Tab("π LaTeX Quality"):
latex_report = gr.HTML(
value=WELCOME_HTML,
elem_classes=["report-panel"]
)
with gr.Tab("π Line-by-Line"):
line_report = gr.HTML(
value=WELCOME_HTML,
elem_classes=["report-panel"]
)
# Event handling
run_btn.click(
fn=run_check,
inputs=[
bib_file, tex_file,
check_metadata, check_usage, check_duplicates, check_preprint_ratio,
caption, reference, formatting, equation, ai_artifacts,
sentence, consistency, acronym, number, citation_quality, anonymization
],
outputs=[bib_report, latex_report, line_report]
)
return app
# Create the app
app = create_app()
if __name__ == "__main__":
app.launch(
favicon_path="assets/icon-192.png",
show_error=True,
css=CUSTOM_CSS,
theme=gr.themes.Soft()
)
|