#!/usr/bin/env python3 import argparse import glob import json import os import sys from collections import defaultdict from runners.run_chatgpt import run_chatgpt from runners.run_gemini import run_gemini from olmocr.data.renderpdf import render_pdf_to_base64png def parse_rules_file(file_path): """Parse the rules file and organize rules by PDF.""" pdf_rules = defaultdict(list) with open(file_path, "r") as f: for line in f: line = line.strip() if not line: continue try: rule = json.loads(line) if "pdf" in rule: pdf_rules[rule["pdf"]].append(rule) except json.JSONDecodeError: print(f"Warning: Could not parse line as JSON: {line}") return pdf_rules def get_model_outputs(pdf_path): """Get outputs from both models for a given PDF.""" try: print(f"Attempting to process PDF: {pdf_path}") print(f"File exists: {os.path.exists(pdf_path)}") chatgpt_output = run_chatgpt(pdf_path) gemini_output = run_gemini(pdf_path) return chatgpt_output, gemini_output except Exception as e: print(f"Error getting model outputs for {pdf_path}: {str(e)}") return f"Error: {str(e)}", f"Error: {str(e)}" def find_pdfs_in_directory(directory): """Find all PDF files in the given directory.""" if not os.path.isdir(directory): print(f"Warning: {directory} is not a directory.") return [] pdf_files = [] for ext in ["pdf", "PDF"]: pattern = os.path.join(directory, f"*.{ext}") pdf_files.extend(glob.glob(pattern)) print(f"Found {len(pdf_files)} PDF files in {directory}") for pdf in pdf_files: print(f" - {pdf}") return pdf_files def generate_html(pdf_rules, rules_file_path, pdfs_to_process=None): """Generate the HTML page with PDF renderings and model outputs.""" pdf_paths = [] if pdfs_to_process: for pdf_item in pdfs_to_process: if os.path.isdir(pdf_item): pdf_paths.extend(find_pdfs_in_directory(pdf_item)) elif os.path.isfile(pdf_item): pdf_paths.append(pdf_item) else: print(f"Warning: {pdf_item} is neither a valid file nor directory") else: pdf_base_dir = os.path.join(os.path.dirname(rules_file_path), "pdfs") pdf_paths = [os.path.join(pdf_base_dir, pdf_name) for pdf_name in list(pdf_rules.keys())[:10]] pdf_paths = list(set(pdf_paths)) print("Processing the following PDFs:") for path in pdf_paths: print(f" - {path} (exists: {os.path.exists(path)})") html = """
Loading differences...