from __future__ import annotations from pathlib import Path from typing import Any import gradio as gr from src.space_service import RefCheckOptions, run_refcheck_file def _uploaded_path(uploaded: Any) -> str | None: if not uploaded: return None if isinstance(uploaded, str): return uploaded if isinstance(uploaded, dict): return uploaded.get("path") or uploaded.get("name") name = getattr(uploaded, "name", None) if name: return str(name) return None def process_bib( uploaded: Any, remove_unverified: bool, enable_google_scholar: bool, max_workers: int, ) -> tuple[str, str | None, str | None]: file_path = _uploaded_path(uploaded) if not file_path: return "## RefCheck Report\n\nNo BibTeX file was uploaded.", None, None try: options = RefCheckOptions( remove_unverified=remove_unverified, enable_google_scholar=enable_google_scholar, max_workers=int(max_workers), ) result = run_refcheck_file(Path(file_path), options) return result.report_markdown, result.fixed_bib_path, result.report_path except Exception as exc: return f"## RefCheck Report\n\nProcessing failed: `{exc}`", None, None with gr.Blocks(title="RefCheck") as demo: gr.Markdown("# RefCheck") with gr.Row(): with gr.Column(scale=1): bib_file = gr.File( label="BibTeX file", file_types=[".bib", ".txt"], type="filepath", ) remove_unverified = gr.Checkbox( label="Remove unverifiable entries", value=True, ) enable_google_scholar = gr.Checkbox( label="Google Scholar fallback", value=False, ) max_workers = gr.Slider( label="Parallel lookups", minimum=1, maximum=8, step=1, value=4, ) run_button = gr.Button("Run RefCheck", variant="primary") with gr.Column(scale=2): report = gr.Markdown(label="Report") fixed_bib = gr.File(label="Fixed BibTeX") report_file = gr.File(label="Markdown report") run_button.click( fn=process_bib, inputs=[bib_file, remove_unverified, enable_google_scholar, max_workers], outputs=[report, fixed_bib, report_file], api_name="refcheck", ) if __name__ == "__main__": demo.queue(default_concurrency_limit=2).launch()