#!/usr/bin/env python3
from tkinter.constants import TRUE
import base64
import gradio as gr
import tempfile
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
import threading
from src.parsers import BibParser
from src.fetchers import ArxivFetcher, ScholarFetcher, CrossRefFetcher, SemanticScholarFetcher, OpenAlexFetcher, DBLPFetcher
from src.analyzers import MetadataComparator, DuplicateDetector
from src.report.generator import EntryReport
from src.config.workflow import get_default_workflow
def format_entry_card(entry_report, duplicate_groups=None):
"""格式化单个文献条目为 HTML 卡片"""
entry = entry_report.entry
comparison = entry_report.comparison
# 判断状态
if comparison and comparison.is_match:
card_class = "verified"
status_icon = "✓"
status_text = "Verified"
elif comparison and comparison.has_issues:
card_class = "warning"
status_icon = "⚠️"
status_text = "Issues Found"
else:
card_class = "error"
status_icon = "✗"
status_text = "Not Found"
# 格式化 BibTeX(BibEntry 用 raw_entry 存原始字段)
bibtex_str = f"@{entry.entry_type}{{{entry.key},\n"
for field, value in (entry.raw_entry or {}).items():
if field in ("ID", "ENTRYTYPE"):
continue
if value is not None and str(value).strip():
bibtex_str += f" {field}={{{value}}},\n"
bibtex_str = bibtex_str.rstrip(",\n") + "\n}"
# Link for header (Open paper / DOI) when we have reference
link_url = ""
link_label = "Open paper"
if comparison and getattr(comparison, "source", "") != "unable":
if getattr(comparison, "fetched_doi", None) and str(comparison.fetched_doi).strip():
link_url = "https://doi.org/" + str(comparison.fetched_doi).strip()
link_label = "DOI"
elif getattr(comparison, "fetched_url", None) and str(comparison.fetched_url).strip():
link_url = str(comparison.fetched_url).strip()
# 收集标签
tags = []
if comparison:
if comparison.is_match:
tags.append(('✓ Verified', 0))
if comparison.source:
tags.append((f'Source: {comparison.source}', 0))
# 问题标签(ComparisonResult 使用 *_match,用 not *_match 表示 mismatch)
if not comparison.title_match:
tags.append(('⚠️ Title Mismatch', 1))
if not comparison.author_match:
tags.append(('⚠️ Author Mismatch', 1))
if not comparison.year_match:
tags.append(('⚠️ Year Mismatch', 1))
if hasattr(comparison, 'venue_match') and not comparison.venue_match:
tags.append(('⚠️ Venue Mismatch', 1))
if not comparison.is_match and not comparison.has_issues:
tags.append(('✗ Not Found', 2))
# 检查是否是重复条目
if duplicate_groups:
for group in duplicate_groups:
if entry.key in group.entry_keys:
tags.append(('⚠️ Duplicate Entry', 1))
break
# 按优先级排序标签
tags.sort(key=lambda x: x[1])
tags_html = '\n'.join([tag[0] for tag in tags])
# 详细信息
metadata_info = ""
if comparison:
if comparison.is_match:
confidence = getattr(comparison, 'confidence', 0)
metadata_info = f"Verification Info: All fields matched successfully | Confidence: {confidence * 100:.2f}%"
elif comparison.has_issues:
issues = []
if not comparison.title_match:
issues.append("• Title mismatch detected")
if not comparison.author_match:
issues.append("• Author list differs from database")
if not comparison.year_match:
issues.append("• Publication year mismatch")
if hasattr(comparison, 'venue_match') and not comparison.venue_match:
issues.append("• Venue/journal name differs")
metadata_info = f"Issue Details:
" + "
".join(issues)
else:
metadata_info = f"""Issue Details:
• Entry not found in any database
• Possible causes: incorrect title, author errors, or non-existent reference
• Suggestion: verify the original source or use a search engine"""
# Ground truth (reference): compact title, author, year, doi only (no Copy, no full BibTeX)
fetched_bibtex_html = ""
if comparison and getattr(comparison, "source", "") != "unable" and (
getattr(comparison, "fetched_title", None) or getattr(comparison, "fetched_authors", None)
):
src = getattr(comparison, "source", "reference")
fa = getattr(comparison, "fetched_authors", None)
authors_str = " and ".join(fa) if isinstance(fa, list) else (fa or "")
ft = (getattr(comparison, "fetched_title", None) or "").strip()
fy = (getattr(comparison, "fetched_year", None) or "").strip()
fdoi = (getattr(comparison, "fetched_doi", None) or "").strip()
def _line(label, value):
if not value:
return ""
esc = (value or "").replace("&", "&").replace("<", "<").replace(">", ">")
return f'
No entries in this category.
" return f"{REPORT_CSS}Please run Verify first.
" entry_reports, duplicate_groups = state return render_results(entry_reports, duplicate_groups, filter_choice, include_summary=False) # Bar 单段 HTML(大数字 + 小标签),配色与图片一致 def _bar_segment_html(num, label, num_color): return f'' def bar_segments_html(verified_count, warning_count, error_count, total): """返回 4 段 (Verified, Issues Found, Not Found, Total) 的 HTML,用于图片式 bar。""" return ( _bar_segment_html(verified_count, "✓ Verified", "#32CD32"), _bar_segment_html(warning_count, "⚠️ Issues Found", "#FFA500"), _bar_segment_html(error_count, "✗ Not Found", "#FF0000"), _bar_segment_html(total, "Total", "#ffffff"), ) def process_bibtex(bibtex_input, progress=gr.Progress()): """处理用户输入的 BibTeX 并进行检测。返回 (html, state, seg1, seg2, seg3, seg4) 供 bar 展示与筛选。""" zero_segs = bar_segments_html(0, 0, 0, 0) if not bibtex_input.strip(): return "Please enter BibTeX content
", None, *zero_segs try: # 解析 BibTeX progress(0, desc="Parsing BibTeX...") parser = BibParser() # 写入临时文件 with tempfile.NamedTemporaryFile(mode='w', suffix='.bib', delete=False) as f: f.write(bibtex_input) temp_bib_path = f.name entries = parser.parse_file(temp_bib_path) Path(temp_bib_path).unlink() # 删除临时文件 if not entries: return "No valid BibTeX entries found
", None, *bar_segments_html(0, 0, 0, 0) # 初始化检测器 progress(0.1, desc="Initializing fetchers...") arxiv_fetcher = ArxivFetcher() crossref_fetcher = CrossRefFetcher() scholar_fetcher = ScholarFetcher() semantic_scholar_fetcher = SemanticScholarFetcher() openalex_fetcher = OpenAlexFetcher() dblp_fetcher = DBLPFetcher() comparator = MetadataComparator() duplicate_detector = DuplicateDetector() # 检测重复 duplicate_groups = duplicate_detector.find_duplicates(entries) # 获取工作流 workflow_config = get_default_workflow() # 处理每个条目 entry_reports = [] progress_lock = threading.Lock() verified_count = 0 warning_count = 0 error_count = 0 def process_single_entry(entry, idx, total): from src.utils.normalizer import TextNormalizer comparison_result = None all_results = [] for step in workflow_config.get_enabled_steps(): result = None if step.name == "arxiv_id" and entry.has_arxiv and arxiv_fetcher: arxiv_meta = arxiv_fetcher.fetch_by_id(entry.arxiv_id) if arxiv_meta: result = comparator.compare_with_arxiv(entry, arxiv_meta) elif step.name == "crossref_doi" and entry.doi and crossref_fetcher: crossref_result = crossref_fetcher.search_by_doi(entry.doi) if crossref_result: result = comparator.compare_with_crossref(entry, crossref_result) elif step.name == "semantic_scholar" and entry.title and semantic_scholar_fetcher: ss_result = semantic_scholar_fetcher.fetch_by_doi(entry.doi) if entry.doi else None if not ss_result: ss_result = semantic_scholar_fetcher.search_by_title(entry.title) if ss_result: result = comparator.compare_with_semantic_scholar(entry, ss_result) elif step.name == "dblp" and entry.title and dblp_fetcher: dblp_result = dblp_fetcher.search_by_title(entry.title) if dblp_result: result = comparator.compare_with_dblp(entry, dblp_result) elif step.name == "openalex" and entry.title and openalex_fetcher: oa_result = openalex_fetcher.fetch_by_doi(entry.doi) if entry.doi else None if not oa_result: oa_result = openalex_fetcher.search_by_title(entry.title) if oa_result: result = comparator.compare_with_openalex(entry, oa_result) elif step.name == "arxiv_title" and entry.title and arxiv_fetcher: results = arxiv_fetcher.search_by_title(entry.title, max_results=3) if results: best_result = None best_sim = 0.0 norm1 = TextNormalizer.normalize_for_comparison(entry.title) for r in results: sim = TextNormalizer.similarity_ratio(norm1, TextNormalizer.normalize_for_comparison(r.title)) if sim > best_sim: best_sim, best_result = sim, r if best_result and best_sim > 0.5: result = comparator.compare_with_arxiv(entry, best_result) elif step.name == "crossref_title" and entry.title and crossref_fetcher: crossref_result = crossref_fetcher.search_by_title(entry.title) if crossref_result: result = comparator.compare_with_crossref(entry, crossref_result) elif step.name == "google_scholar" and entry.title and scholar_fetcher: scholar_result = scholar_fetcher.search_by_title(entry.title) if scholar_result: result = comparator.compare_with_scholar(entry, scholar_result) if result: all_results.append(result) if result.is_match: comparison_result = result break if not comparison_result and all_results: all_results.sort(key=lambda r: r.confidence, reverse=True) comparison_result = all_results[0] elif not comparison_result: comparison_result = comparator.create_unable_result(entry, "Unable to find this paper in any data source") return EntryReport(entry=entry, comparison=comparison_result) max_workers = min(10, len(entries)) with ThreadPoolExecutor(max_workers=max_workers) as executor: future_to_entry = {executor.submit(process_single_entry, e, i, len(entries)): (e, i) for i, e in enumerate(entries)} for future in as_completed(future_to_entry): entry, idx = future_to_entry[future] try: entry_report = future.result() with progress_lock: entry_reports.append(entry_report) if entry_report.comparison and entry_report.comparison.is_match: verified_count += 1 elif entry_report.comparison and entry_report.comparison.has_issues: warning_count += 1 else: error_count += 1 progress((idx + 1) / len(entries), desc=f"Verifying entries {idx + 1}/{len(entries)}...") except Exception as e: with progress_lock: error_count += 1 print(f"Error processing {entry.key}: {e}") # 生成 HTML(默认 Total 视图,不含 bar),并保存结果供分类筛选 progress(1.0, desc="Generating report...") final_html = render_results(entry_reports, duplicate_groups, "Total", include_summary=False) segs = bar_segments_html(verified_count, warning_count, error_count, len(entry_reports)) return final_html, (entry_reports, duplicate_groups), *segs except Exception as e: import traceback error_msg = f"Error: {str(e)}
{traceback.format_exc()}"
return error_msg, None, *bar_segments_html(0, 0, 0, 0)
# 官网示例:点击即可填入输入框进行测试
BIBTEX_EXAMPLES = [
(
"""@article{gpt2,
title={Language models are unsupervised multitask},
author={Radford, Alec and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya and others},
journal={OpenAI blog},
volume={1},
number={8},
pages={9},
year={2021}
}""",
"GPT-2 (OpenAI blog)",
),
(
"""@article{devlin2018bert,
year={2018},
journal={arXiv preprint arXiv:1810.04805},
author={Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
title={BERT: Pre-training of deep bidirectional transformers for language understanding}
}""",
"BERT (arXiv)",
),
(
"""@article{vaswani2017attention,
title={Attention is all you need},
author={Vaswani, Ashish and Shazeer, Noam and others},
journal={Advances in neural information processing systems},
year={2017}
}
@article{brown2020language,
title={Language models are few-shot learners},
author={Brown, Tom B and Mann, Benjamin and others},
year={2020}
}""",
"Attention + GPT-3 (multiple entries)",
),
]
# Bar 图片式 UI:深灰背景 #2E3035,大数字 + 小标签,绿/橙/红/白
BAR_CSS = """
.status-bar-row { background: #2E3035 !important; border-radius: 8px !important; padding: 20px !important; margin-bottom: 20px !important; display: flex !important; justify-content: space-around !important; align-items: stretch !important; gap: 12px !important; }
.bar-segment-col { flex: 1 !important; text-align: center !important; position: relative !important; min-width: 0 !important; }
.bar-segment-col .bar-seg { display: flex !important; flex-direction: column !important; align-items: center !important; justify-content: center !important; padding: 8px 4px !important; }
.bar-segment-col .bar-num { font-size: 32px !important; font-weight: bold !important; line-height: 1.2 !important; display: block !important; }
.bar-segment-col .bar-label { font-size: 13px !important; color: #ffffff !important; margin-top: 4px !important; display: block !important; }
.bar-segment-btn { position: absolute !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important; opacity: 0 !important; cursor: pointer !important; }
"""
# 创建 Gradio 界面
with gr.Blocks(title="CiteScan - Check References, Confirm Truth.", theme=gr.themes.Soft(), css=BAR_CSS) as demo:
gr.Markdown("""
# CiteScan - Check References, Confirm Truth.
1️⃣ Paste your BibTeX below, or **click an example** to load it.
2️⃣ Click "Verify" button to have the system verify the authenticity and accuracy of each reference.
**Important: We check very carefully. Sometimes the result might look different from Google Scholar or ArXiv. We think it's best to use the official version from places like ACM, ACL, or CVF to cite your sources.
We will also add a feature soon to help change citations from pre-print versions (like arXiv or bioRxiv) into the final official ones (like from NeurIPS, ACL, or CVF).**
""")
with gr.Row():
with gr.Column():
bibtex_input = gr.Textbox(
label="📝 Paste your BibTeX",
placeholder="""Paste your BibTeX or click an example below. for example:
@article{gpt2,
title={Language models are unsupervised multitask},
author={Radford, Alec and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya and others},
journal={OpenAI blog},
volume={1},
number={8},
pages={9},
year={2021}
}
@article{devlin2018bert,
year={2018},
journal={arXiv preprint arXiv:1810.04805},
author={Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
title={BERT: Pre-training of deep bidirectional transformers for language understanding}
}
""",
lines=15,
max_lines=20
)
submit_btn = gr.Button("🚀 Verify", variant="primary", size="lg")
gr.Examples(
examples=[[ex[0]] for ex in BIBTEX_EXAMPLES],
inputs=[bibtex_input],
label="📋 Examples (click to load)",
examples_per_page=6,
)
result_state = gr.State(value=None)
# Bar:图片式 UI(深灰 #2E3035,大数字 + 小标签),每段可点击筛选
zero_segs = bar_segments_html(0, 0, 0, 0)
with gr.Row(elem_classes=["status-bar-row"]):
with gr.Column(elem_classes=["bar-segment-col"], scale=1):
bar_seg_verified = gr.HTML(zero_segs[0])
btn_verified = gr.Button("Verified", elem_classes=["bar-segment-btn"], visible=True)
with gr.Column(elem_classes=["bar-segment-col"], scale=1):
bar_seg_issues = gr.HTML(zero_segs[1])
btn_issues = gr.Button("Issues", elem_classes=["bar-segment-btn"], visible=True)
with gr.Column(elem_classes=["bar-segment-col"], scale=1):
bar_seg_notfound = gr.HTML(zero_segs[2])
btn_notfound = gr.Button("Not Found", elem_classes=["bar-segment-btn"], visible=True)
with gr.Column(elem_classes=["bar-segment-col"], scale=1):
bar_seg_total = gr.HTML(zero_segs[3])
btn_total = gr.Button("Total", elem_classes=["bar-segment-btn"], visible=True)
with gr.Row():
output_html = gr.HTML(label="Detection Results")
submit_btn.click(
fn=process_bibtex,
inputs=[bibtex_input],
outputs=[output_html, result_state, bar_seg_verified, bar_seg_issues, bar_seg_notfound, bar_seg_total],
)
def filter_to_verified(state):
return filter_display(state, "Verified")
def filter_to_issues(state):
return filter_display(state, "Issues Found")
def filter_to_notfound(state):
return filter_display(state, "Not Found")
def filter_to_total(state):
return filter_display(state, "Total")
btn_verified.click(fn=filter_to_verified, inputs=[result_state], outputs=[output_html])
btn_issues.click(fn=filter_to_issues, inputs=[result_state], outputs=[output_html])
btn_notfound.click(fn=filter_to_notfound, inputs=[result_state], outputs=[output_html])
btn_total.click(fn=filter_to_total, inputs=[result_state], outputs=[output_html])
gr.Markdown("""
*Case Study for False positive* in CiteScan:
1. **Authors Mismatch**:
- *Observation*: Different databases deal with a longer list of authors with different strategies, like truncation.
- *Action*: Verify if main authors match
2. **Venues Mismatch**:
- *Observation*: Abbreviations vs. full names, such as "ICLR" v.s. "International Conference on Learning Representations"
- *Action*: Both are correct.
3. **Year GAP (±1 Year)**:
- *Observation*: Delay between preprint (arXiv) and final version publication
- *Action*: Verify which version you intend to cite, We recommend you to cite the version from the official press website. Less number of pre-print version bibs will make your submission more convincing.
4. **Non-academic Sources**:
- *Observation*: Blogs, and APIs are not indexed in academic databases.
- *Action*: Verify URL, year, and title manually.
---
**Supported Data Sources:** arXiv, CrossRef, DBLP, Semantic Scholar, ACL Anthology, ACM, theCVF,
""")
# Partner logos and contact (embed images as base64 so they work when served)
_root = Path(__file__).resolve().parent
def _logo_b64(path: Path) -> str | None:
if path.exists():
return base64.b64encode(path.read_bytes()).decode("utf-8")
return None
_nus = _logo_b64(_root / "assets" / "logo_nus.png")
_sjtu = _logo_b64(_root / "assets" / "logo_sjtu.png")
_logos_html = []
if _nus:
_logos_html.append(f'