Spaces:
Sleeping
Sleeping
File size: 16,882 Bytes
519c06d |
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 87 88 89 90 91 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
"""
PDF Processor Component
Processes PDF files to extract text and metadata
"""
import os
import re
import warnings
from typing import List, Dict, Optional, Any
from datetime import datetime
from pathlib import Path
# PDF processing libraries
import pypdf
try:
import pdfplumber
import fitz # PyMuPDF
PDF_ENHANCED = True
except ImportError:
PDF_ENHANCED = False
warnings.filterwarnings('ignore')
class PDFProcessor:
"""
Processes PDF files to extract text, metadata, and structure
Supports multiple PDF processing libraries for better compatibility
"""
def __init__(self, config=None):
# Import Config only when needed to avoid dependency issues
if config is None:
try:
from .config import Config
self.config = Config()
except ImportError:
# Fallback to None if Config cannot be imported
self.config = None
else:
self.config = config
self.supported_formats = ['.pdf']
# Check available libraries
self.libraries = {
'pypdf': True,
'pdfplumber': PDF_ENHANCED,
'PyMuPDF': PDF_ENHANCED
}
print(f"PDF Processor initialized with libraries: {[k for k, v in self.libraries.items() if v]}")
def extract_text_from_file(self, file_path: str, method: str = 'auto') -> Dict[str, Any]:
"""
Extract text from PDF file
Args:
file_path: Path to PDF file
method: Extraction method ('auto', 'pypdf', 'pdfplumber', 'pymupdf')
Returns:
Dictionary with extracted text and metadata
"""
if not os.path.exists(file_path):
return {'error': f"File not found: {file_path}"}
if not file_path.lower().endswith('.pdf'):
return {'error': f"Not a PDF file: {file_path}"}
try:
print(f"Processing PDF: {os.path.basename(file_path)}")
# Try different methods based on preference
if method == 'auto':
# Try methods in order of preference
methods = ['pdfplumber', 'pymupdf', 'pypdf']
for m in methods:
if self.libraries.get(m.replace('pymupdf', 'PyMuPDF').replace('pdfplumber', 'pdfplumber').replace('pypdf', 'pypdf')):
result = self._extract_with_method(file_path, m)
if result and not result.get('error'):
return result
# If all methods fail, return error
return {'error': 'All extraction methods failed'}
else:
return self._extract_with_method(file_path, method)
except Exception as e:
return {'error': f"Error processing PDF: {str(e)}"}
def _extract_with_method(self, file_path: str, method: str) -> Dict[str, Any]:
"""
Extract text using a specific method
Args:
file_path: Path to PDF file
method: Extraction method
Returns:
Dictionary with extracted text and metadata
"""
try:
if method == 'pdfplumber' and self.libraries['pdfplumber']:
return self._extract_with_pdfplumber(file_path)
elif method == 'pymupdf' and self.libraries['PyMuPDF']:
return self._extract_with_pymupdf(file_path)
elif method == 'pypdf' and self.libraries['pypdf']:
return self._extract_with_pypdf(file_path)
else:
return {'error': f"Method {method} not available"}
except Exception as e:
return {'error': f"Error with method {method}: {str(e)}"}
def _extract_with_pdfplumber(self, file_path: str) -> Dict[str, Any]:
"""Extract text using pdfplumber (best for tables and layout)"""
import pdfplumber
text_content = []
metadata = {
'method': 'pdfplumber',
'pages': 0,
'tables': 0,
'images': 0
}
with pdfplumber.open(file_path) as pdf:
metadata['pages'] = len(pdf.pages)
for page_num, page in enumerate(pdf.pages):
# Extract text
page_text = page.extract_text()
if page_text:
text_content.append(f"--- Page {page_num + 1} ---\n{page_text}")
# Count tables
tables = page.extract_tables()
if tables:
metadata['tables'] += len(tables)
# Add table content
for table in tables:
table_text = self._format_table(table)
text_content.append(f"--- Table on Page {page_num + 1} ---\n{table_text}")
# Count images
if hasattr(page, 'images'):
metadata['images'] += len(page.images)
full_text = '\n\n'.join(text_content)
return {
'text': full_text,
'metadata': metadata,
'word_count': len(full_text.split()),
'char_count': len(full_text),
'extracted_at': datetime.now().isoformat(),
'file_path': file_path
}
def _extract_with_pymupdf(self, file_path: str) -> Dict[str, Any]:
"""Extract text using PyMuPDF (fast and accurate)"""
import fitz
doc = fitz.open(file_path)
text_content = []
metadata = {
'method': 'pymupdf',
'pages': len(doc),
'images': 0,
'links': 0
}
for page_num in range(len(doc)):
page = doc[page_num]
# Extract text
page_text = page.get_text()
if page_text.strip():
text_content.append(f"--- Page {page_num + 1} ---\n{page_text}")
# Count images
images = page.get_images()
metadata['images'] += len(images)
# Count links
links = page.get_links()
metadata['links'] += len(links)
doc.close()
full_text = '\n\n'.join(text_content)
return {
'text': full_text,
'metadata': metadata,
'word_count': len(full_text.split()),
'char_count': len(full_text),
'extracted_at': datetime.now().isoformat(),
'file_path': file_path
}
def _extract_with_pypdf(self, file_path: str) -> Dict[str, Any]:
"""Extract text using pypdf (basic but reliable)"""
text_content = []
metadata = {
'method': 'pypdf',
'pages': 0
}
with open(file_path, 'rb') as file:
pdf_reader = pypdf.PdfReader(file)
metadata['pages'] = len(pdf_reader.pages)
for page_num, page in enumerate(pdf_reader.pages):
page_text = page.extract_text()
if page_text.strip():
text_content.append(f"--- Page {page_num + 1} ---\n{page_text}")
full_text = '\n\n'.join(text_content)
return {
'text': full_text,
'metadata': metadata,
'word_count': len(full_text.split()),
'char_count': len(full_text),
'extracted_at': datetime.now().isoformat(),
'file_path': file_path
}
def _format_table(self, table: List[List[str]]) -> str:
"""Format a table for text output"""
if not table:
return ""
formatted_rows = []
for row in table:
if row: # Skip empty rows
formatted_row = ' | '.join(str(cell) if cell else '' for cell in row)
formatted_rows.append(formatted_row)
return '\n'.join(formatted_rows)
def extract_text_from_bytes(self, pdf_bytes: bytes, filename: str = "uploaded.pdf") -> Dict[str, Any]:
"""
Extract text from PDF bytes (for uploaded files)
Args:
pdf_bytes: PDF file bytes
filename: Original filename
Returns:
Dictionary with extracted text and metadata
"""
try:
# Save bytes to temporary file
import tempfile
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
tmp_file.write(pdf_bytes)
tmp_path = tmp_file.name
# Extract text
result = self.extract_text_from_file(tmp_path)
# Clean up
os.unlink(tmp_path)
# Update metadata
if 'metadata' in result:
result['metadata']['original_filename'] = filename
result['metadata']['file_size'] = len(pdf_bytes)
return result
except Exception as e:
return {'error': f"Error processing PDF bytes: {str(e)}"}
def validate_pdf(self, file_path: str) -> Dict[str, Any]:
"""
Validate PDF file
Args:
file_path: Path to PDF file
Returns:
Validation result
"""
try:
if not os.path.exists(file_path):
return {'valid': False, 'error': 'File not found'}
if not file_path.lower().endswith('.pdf'):
return {'valid': False, 'error': 'Not a PDF file'}
# Try to open with pypdf
with open(file_path, 'rb') as file:
pdf_reader = pypdf.PdfReader(file)
page_count = len(pdf_reader.pages)
# Check if encrypted
is_encrypted = pdf_reader.is_encrypted
# Get file size
file_size = os.path.getsize(file_path)
return {
'valid': True,
'pages': page_count,
'encrypted': is_encrypted,
'file_size': file_size,
'file_path': file_path
}
except Exception as e:
return {'valid': False, 'error': str(e)}
def get_pdf_metadata(self, file_path: str) -> Dict[str, Any]:
"""
Extract metadata from PDF
Args:
file_path: Path to PDF file
Returns:
PDF metadata
"""
try:
metadata = {}
# Try pypdf first
try:
with open(file_path, 'rb') as file:
pdf_reader = pypdf.PdfReader(file)
if pdf_reader.metadata:
metadata.update({
'title': pdf_reader.metadata.get('/Title', ''),
'author': pdf_reader.metadata.get('/Author', ''),
'subject': pdf_reader.metadata.get('/Subject', ''),
'creator': pdf_reader.metadata.get('/Creator', ''),
'producer': pdf_reader.metadata.get('/Producer', ''),
'creation_date': pdf_reader.metadata.get('/CreationDate', ''),
'modification_date': pdf_reader.metadata.get('/ModDate', '')
})
except Exception:
pass
# Try PyMuPDF for additional metadata
if self.libraries['PyMuPDF']:
try:
import fitz
doc = fitz.open(file_path)
doc_metadata = doc.metadata
doc.close()
if doc_metadata:
metadata.update({
'format': doc_metadata.get('format', ''),
'encryption': doc_metadata.get('encryption', ''),
'keywords': doc_metadata.get('keywords', '')
})
except Exception:
pass
# Add file system metadata
stat = os.stat(file_path)
metadata.update({
'file_size': stat.st_size,
'created': datetime.fromtimestamp(stat.st_ctime).isoformat(),
'modified': datetime.fromtimestamp(stat.st_mtime).isoformat(),
'accessed': datetime.fromtimestamp(stat.st_atime).isoformat()
})
return metadata
except Exception as e:
return {'error': f"Error extracting metadata: {str(e)}"}
def split_pdf_text(self, text: str, chunk_size: int = None, chunk_overlap: int = None) -> List[str]:
"""
Split PDF text into chunks for processing
Args:
text: Extracted text
chunk_size: Size of each chunk
chunk_overlap: Overlap between chunks
Returns:
List of text chunks
"""
# Use provided values or defaults if config is None
if chunk_size is None:
chunk_size = self.config.CHUNK_SIZE if self.config else 1000
if chunk_overlap is None:
chunk_overlap = self.config.CHUNK_OVERLAP if self.config else 200
if len(text) <= chunk_size:
return [text]
chunks = []
start = 0
while start < len(text):
end = start + chunk_size
# Try to break at sentence boundary
if end < len(text):
# Look for sentence ending
sentence_end = text.rfind('.', start, end)
if sentence_end > start:
end = sentence_end + 1
else:
# Look for paragraph break
para_end = text.rfind('\n\n', start, end)
if para_end > start:
end = para_end + 2
else:
# Look for any line break
line_end = text.rfind('\n', start, end)
if line_end > start:
end = line_end + 1
chunk = text[start:end].strip()
if chunk:
chunks.append(chunk)
start = end - chunk_overlap
return chunks
def clean_text(self, text: str) -> str:
"""
Clean extracted text
Args:
text: Raw extracted text
Returns:
Cleaned text
"""
if not text:
return ""
# Remove extra whitespace
text = re.sub(r'\s+', ' ', text)
# Remove page headers/footers (basic)
text = re.sub(r'Page \d+', '', text)
# Remove email addresses (optional)
text = re.sub(r'\S+@\S+', '', text)
# Remove URLs (optional)
text = re.sub(r'https?://\S+', '', text)
# Fix common OCR errors
text = text.replace('fi', 'fi')
text = text.replace('fl', 'fl')
text = text.replace('ff', 'ff')
text = text.replace('ffi', 'ffi')
text = text.replace('ffl', 'ffl')
return text.strip()
def get_processing_stats(self) -> Dict[str, Any]:
"""
Get PDF processing statistics
Returns:
Processing statistics
"""
return {
'available_libraries': self.libraries,
'supported_formats': self.supported_formats,
'enhanced_features': PDF_ENHANCED,
'config': {
'chunk_size': self.config.CHUNK_SIZE if self.config else 1000,
'chunk_overlap': self.config.CHUNK_OVERLAP if self.config else 200
}
}
|