import gradio as gr from transformers import AutoProcessor, AutoModelForVision2Seq from pdf2image import convert_from_path import base64 import io import spaces from PIL import Image # Load the OCR model and processor from Hugging Face try: processor = AutoProcessor.from_pretrained("allenai/olmOCR-7B-0225-preview") model = AutoModelForVision2Seq.from_pretrained("allenai/olmOCR-7B-0225-preview") except ImportError as e: processor = None model = None print(f"Error loading model: {str(e)}. Please ensure PyTorch is installed.") except ValueError as e: processor = None model = None print(f"Error with model configuration: {str(e)}") @spaces.GPU def process_pdf(pdf_file): """ Process the uploaded PDF file, extract text from each page, and generate HTML to display each page's image and text with copy buttons. """ if processor is None or model is None: return "
Error: Model could not be loaded. Check environment setup (PyTorch may be missing) or model compatibility.
" # Check if a PDF file was uploaded if pdf_file is None: return "Please upload a PDF file.
" # Convert PDF to images try: pages = convert_from_path(pdf_file.name) except Exception as e: return f"Error converting PDF to images: {str(e)}
" # Start building the HTML output html = '