File size: 2,758 Bytes
b3b5141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import pathlib
import uvicorn
import gradio as gr

from .reviewer import PDFReviewer
from .api import app as fastapi_app


def _cli_review(args):
    reviewer = PDFReviewer(debug=args.debug)
    result = reviewer.review_pdf(args.pdf)
    for key, value in result.items():
        print(f"\n{key.upper()}:\n{value}\n")


def _cli_demo(args):
    reviewer = PDFReviewer(debug=args.debug)

    def gradio_interface(file, progress=gr.Progress()):
        result = reviewer.review_pdf(file.name)
        return (
            result["contributions"],
            result["strengths"],
            result["weaknesses"],
            result["requested_changes"],
            result["impact_concerns"],
            result["claims_and_evidence"],
            result["audience_interest"],
        )

    iface = gr.Interface(
        fn=gradio_interface,
        inputs=gr.File(label="Upload PDF"),
        outputs=[
            gr.Textbox(label="Contributions"),
            gr.Textbox(label="Strengths"),
            gr.Textbox(label="Weaknesses"),
            gr.Textbox(label="Requested Changes"),
            gr.Textbox(label="Impact Concerns"),
            gr.Textbox(label="Claims and Evidence"),
            gr.Textbox(label="Audience Interest"),
        ],
        title="PDF Review Parser",
        description="Upload a PDF to parse and review its content.",
    )
    iface.launch(share=True)


def _cli_api(args):
    uvicorn.run(fastapi_app, host=args.host, port=args.port, reload=args.reload)


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="TMLR PDF Reviewer")
    subparsers = parser.add_subparsers(dest="command", required=True)

    # Review command
    p_review = subparsers.add_parser("review", help="Process a PDF and output to console")
    p_review.add_argument("pdf", type=pathlib.Path, help="Path to the PDF file")
    p_review.add_argument("--debug", action="store_true", help="Enable debug mode (uses gpt-4o and drops to pdb on errors)")
    p_review.set_defaults(func=_cli_review)

    # Demo command
    p_demo = subparsers.add_parser("demo", help="Launch Gradio demo")
    p_demo.add_argument("--debug", action="store_true", help="Enable debug mode (uses gpt-4o and drops to pdb on errors)")
    p_demo.set_defaults(func=_cli_demo)

    # API command
    p_api = subparsers.add_parser("api", help="Run FastAPI server")
    p_api.add_argument("--host", default="0.0.0.0")
    p_api.add_argument("--port", type=int, default=8000)
    p_api.add_argument("--reload", action="store_true")
    p_api.set_defaults(func=_cli_api)

    return parser


def main():
    parser = build_parser()
    args = parser.parse_args()
    args.func(args)


if __name__ == "__main__":
    main()