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92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
File: ocr.py
Description: Optical Character Recognition (OCR) using software 2.0 models
Author: Didier Guillevic
Date: 2025-04-06
"""

import os
os.system("bash setup.sh")
import magic
import vlm

import uuid
import shutil
import threading
import time
import pathlib

import pdf2image
from pdf2image.exceptions import PDFPageCountError, PDFSyntaxError
import pypdf
import base64
from contextlib import contextmanager
from typing import List, Optional, Tuple, Union

import logging

class PDFScannerTempManager:
    """
    Manages temporary directory creation and cleanup for PDF scanning operations.
    """
    
    def __init__(self, base_temp_dir: str = 'tmp'):
        """
        Initialize temporary directory manager.
        
        Args:
            base_temp_dir (str): Base directory for temporary files
        """
        self.base_temp_dir = base_temp_dir
        self.active_temp_dirs: list[str] = []
        
        # Ensure base temporary directory exists
        os.makedirs(base_temp_dir, exist_ok=True)
        
        # Set up logging
        logging.basicConfig(level=logging.INFO)
        self.logger = logging.getLogger(__name__)
    
    @contextmanager
    def temp_directory(self) -> str:
        """
        Create a temporary directory with UUID and manage its lifecycle.
        
        Yields:
            str: Path to the temporary directory
        """
        # Generate unique directory name
        dir_uuid = str(uuid.uuid4())
        temp_dir = os.path.join(self.base_temp_dir, dir_uuid)
        
        try:
            # Create directory
            os.makedirs(temp_dir, exist_ok=False)
            self.active_temp_dirs.append(temp_dir)
            
            # Yield directory path
            yield temp_dir
        
        finally:
            # Remove directory and its contents
            self._cleanup_directory(temp_dir)
    
    def _cleanup_directory(self, directory: str) -> None:
        """
        Safely remove a temporary directory.
        
        Args:
            directory (str): Path to directory to remove
        """
        try:
            if os.path.exists(directory):
                shutil.rmtree(directory)
                
            # Remove from active directories
            if directory in self.active_temp_dirs:
                self.active_temp_dirs.remove(directory)
        
        except Exception as e:
            self.logger.error(f"Error cleaning up directory {directory}: {e}")
    
    def cleanup_all(self) -> None:
        """
        Clean up all temporary directories created during the session.
        """
        for directory in list(self.active_temp_dirs):
            self._cleanup_directory(directory)


class PDFScanner:
    """
    A class to perform OCR on PDF files with robust temp management.
    """
    
    def __init__(self,
            dpi: int = 300,
            temp_manager: Optional[PDFScannerTempManager] = None
        ):
        """
        Initialize the PDFScanner.
        
        Args:
            dpi (int): DPI for PDF conversion
            temp_manager (PDFScannerTempManager, optional): Temp directory manager
        """
        self.dpi = dpi
        self.temp_manager = temp_manager or PDFScannerTempManager()      
        self.logger = logging.getLogger(__name__)
    
    def _validate_pdf(self, pdf_path: str) -> Tuple[bool, str, bool]:
        """
        Validate PDF file and check for encryption.
        
        Returns:
            Tuple[bool, str, bool]: (is_valid, error_message, is_encrypted)
        """
        try:
            with open(pdf_path, 'rb') as file:
                # Check if file starts with PDF signature
                if not file.read(4) == b'%PDF':
                    return False, "Not a valid PDF file (missing PDF signature)", False
                
                # Reset file pointer
                file.seek(0)
                
                try:
                    pdf_reader = pypdf.PdfReader(file, strict=False)
                    is_encrypted = pdf_reader.is_encrypted
                    
                    if is_encrypted:
                        return False, "PDF is encrypted and requires password", True
                    
                    num_pages = len(pdf_reader.pages)
                    return True, f"Valid PDF with {num_pages} pages", False
                    
                except pypdf.errors.PdfReadError as e:
                    return False, f"Invalid PDF structure: {str(e)}", False
                    
        except Exception as e:
            return False, f"Error validating PDF: {str(e)}", False
    
    def _repair_pdf(self, pdf_path: str, temp_dir: str) -> str:
        """
        Attempt to repair a corrupted PDF file.
        
        Args:
            pdf_path (str): Path to original PDF
            temp_dir (str): Temporary directory for repair
        
        Returns:
            str: Path to repaired PDF
        """
        repaired_pdf = os.path.join(temp_dir, 'repaired.pdf')
        
        try:
            # pypdf repair attempt
            with open(pdf_path, 'rb') as file:
                reader = pypdf.PdfReader(file, strict=False)
                writer = pypdf.PdfWriter()
                
                for page in reader.pages:
                    writer.add_page(page)
                
                with open(repaired_pdf, 'wb') as output_file:
                    writer.write(output_file)
                    
            if os.path.exists(repaired_pdf):
                return repaired_pdf
            
        except Exception as e:
            self.logger.warning(f"pypdf repair failed: {str(e)}")
        
        # Ghostscript repair attempt
        try:
            gs_command = [
                'gs',
                '-o', repaired_pdf,
                '-sDEVICE=pdfwrite',
                '-dPDFSETTINGS=/prepress',
                pdf_path
            ]
            
            process = subprocess.run(
                gs_command,
                capture_output=True,
                text=True
            )
            
            if process.returncode == 0 and os.path.exists(repaired_pdf):
                return repaired_pdf
            else:
                raise Exception(f"Ghostscript repair failed: {process.stderr}")
                
        except Exception as e:
            self.logger.error(f"PDF repair failed: {str(e)}")
            raise
    
    def _process_images(
            self,
            images: list,
            temp_dir: str,
            language: str
        ) -> list[str]:
        """Helper method to process converted images."""
        extracted_text = []

        for i, image in enumerate(images):
            image_path = os.path.join(temp_dir, f'page_{i+1}.png')
            try:
                # Save with higher quality
                image.save(image_path, 'PNG', quality=100)
                
                # Perform OCR
                text = process_image_file(image_path)
                extracted_text.append(text)
                
            except Exception as e:
                self.logger.error(f"Error processing page {i+1}: {str(e)}")
                extracted_text.append(f"[ERROR ON PAGE {i+1}]")
                
        return extracted_text

    def pdf_to_text(
            self,
            pdf_path: str, 
            language: str = 'eng',
            first_page: Optional[int] = None, 
            last_page: Optional[int] = None, 
            attempt_repair: bool = True
        ) -> list[str]:
        """
        Convert a PDF file to text using OCR with robust error handling.
        
        Args:
            pdf_path (str): Path to the PDF file
            language (str): Language for OCR (default: 'eng')
            first_page (int, optional): First page to process (1-based)
            last_page (int, optional): Last page to process
            attempt_repair (bool): Whether to attempt repairing corrupted PDFs
            
        Returns:
            list[str]: List of extracted text for each page
        """
        if not os.path.exists(pdf_path):
            raise FileNotFoundError(f"PDF file not found: {pdf_path}")
        
        # Use context manager for automatic cleanup
        with self.temp_manager.temp_directory() as temp_dir:
            # Validate PDF
            is_valid, error_message, is_encrypted = self._validate_pdf(pdf_path)
            if not is_valid:
                self.logger.warning(f"PDF validation issue: {error_message}")
                
                if is_encrypted:
                    raise Exception("Cannot process encrypted PDF files")
                
                if attempt_repair:
                    try:
                        pdf_path = self._repair_pdf(pdf_path, temp_dir)
                        self.logger.info("Using repaired PDF file")
                    except Exception as e:
                        self.logger.error(f"Repair failed: {str(e)}")
            
            # Conversion methods with increasing complexity
            conversion_methods = [
                {'use_pdftocairo': True, 'strict': False},
                {'use_pdftocairo': False, 'strict': False},
                {'use_pdftocairo': True, 'strict': False, 'dpi': self.dpi * 2},
                {'use_pdftocairo': False, 'strict': False, 'dpi': self.dpi * 3}
            ]
            
            last_error = None
            for method in conversion_methods:
                try:
                    self.logger.info(f"Trying conversion method: {method}")
                    images = pdf2image.convert_from_path(
                        pdf_path,
                        dpi=method.get('dpi', self.dpi),
                        first_page=first_page,
                        last_page=last_page,
                        thread_count=4,
                        grayscale=True,
                        **{k: v for k, v in method.items() if k != 'dpi'}
                    )
                    
                    if images:
                        return self._process_images(images, temp_dir, language)
                    
                except Exception as e:
                    last_error = e
                    self.logger.warning(f"Method failed: {str(e)}")
                    continue
            
            if last_error:
                raise Exception(f"All conversion methods failed. Last error: {str(last_error)}")

#
# PDFScanner (singleton)
#
pdf_scanner = PDFScanner()


#
# Process one file
#
def process_file(input_file: str):
    """Process given file with OCR"
    """
    file_type = get_file_type(input_file)

    if file_type == "Image":
        return process_image_file(input_file)
    elif file_type == "PDF":
        return process_pdf_file(input_file)
    else:
        return "Unsupported file type. Please upload a PDF, or an image file."


def process_image_file(input_file: str):
    """Process image file with OCR
    """
    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": (
                        #"Could you extract the information present in the image. "
                        #"No need to repeat the task description. Simply respond."
                        "Could you perform optical characer recognition (OCR) on the image? "
                        "Simply return the text without any additional comments. "
                        "The exception would be if the image represents an ID card. "
                        "In such a case, please return the information in a structured format. "
                    )
                },
                {
                    "type": "image_url",
                    "image_url": f"data:image/jpeg;base64,{encode_image(input_file)}"
                }
            ]
        }
    ]
    return vlm.get_response(messages)


def process_pdf_file(input_file: str):
    """Process PDF file with OCR

    Args:
        input_file: the PDF file to process with OCR
    
    Returns:
        the text OCR result
    
    Note:
        Each page of the PDF is processed as an image.
    """
    texts = pdf_scanner.pdf_to_text(pdf_path=input_file.name)
    output_text = '\n\n'.join(texts)
    return output_text


#
# Get file type: PDF or Image or something else
#
def get_file_type(file_path):
    # Check file extension
    file_extension = os.path.splitext(file_path)[1].lower()

    # Check MIME type
    mime = magic.Magic(mime=True)
    mime_type = mime.from_file(file_path)

    # Determine file type
    if file_extension == '.pdf' or mime_type == 'application/pdf':
        return 'PDF'
    elif file_extension in ['.jpg', '.jpeg', '.png', '.gif'] or mime_type.startswith('image/'):
        return 'Image'
    elif file_extension == '.pptx' or mime_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
        return 'PowerPoint'
    else:
        return 'Other'

#
# Encode images as base64
#
def encode_image(image_path):
    """Encode the image to base64."""
    try:
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    except FileNotFoundError:
        print(f"Error: The file {image_path} was not found.")
        return None
    except Exception as e:  # Added general exception handling
        print(f"Error: {e}")
        return None