Spaces:
Sleeping
Sleeping
File size: 9,767 Bytes
835ecb4 |
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 |
import os
import json
import base64
import hashlib
from pathlib import Path
from typing import List, Dict, Tuple
import pdfplumber
import pymupdf
from PIL import Image
import io
class PDFProcessor:
"""Processes PDFs to extract text, tables, and images."""
def __init__(self, pdf_dir: str = "./pdfs", cache_file: str = ".pdf_cache.json"):
self.pdf_dir = pdf_dir
self.cache_file = cache_file
self.cache = self._load_cache()
os.makedirs(pdf_dir, exist_ok=True)
def _load_cache(self) -> Dict:
"""Load processing cache to avoid reprocessing PDFs."""
if os.path.exists(self.cache_file):
with open(self.cache_file, 'r') as f:
return json.load(f)
return {}
def _save_cache(self):
"""Save processing cache."""
with open(self.cache_file, 'w') as f:
json.dump(self.cache, f, indent=2)
def _get_file_hash(self, filepath: str) -> str:
"""Generate hash of file for change detection."""
hash_md5 = hashlib.md5()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def _extract_images_from_page(self, pdf_path: str, page_num: int) -> List[Dict]:
"""Extract images from specific page using PyMuPDF."""
images = []
try:
doc = pymupdf.open(pdf_path)
# Verify page exists
if page_num >= len(doc):
print(f"⚠️ Page {page_num} does not exist")
doc.close()
return images
page = doc[page_num]
# Get image list - returns list of tuples
image_list = page.get_images()
if not image_list:
doc.close()
return images
print(f"Found {len(image_list)} images on page {page_num}")
# Process each image
for img_index, img_info in enumerate(image_list):
try:
# FIXED: Extract xref from tuple (first element)
xref = img_info[0]
# Validate xref is integer
if not isinstance(xref, int):
print(f"⚠️ Invalid xref type: {type(xref).__name__}")
continue
# Extract image
img_data = doc.extract_image(xref)
if not img_data or "image" not in img_data:
print(f"⚠️ No image data at xref {xref}")
continue
# Encode to base64
image_bytes = img_data["image"]
img_base64 = base64.b64encode(image_bytes).decode()
images.append({
"type": "image",
"format": img_data.get("ext", "png"),
"base64": img_base64,
"page": page_num,
"index": img_index,
"xref": xref
})
print(f"✅ Image {img_index + 1}/{len(image_list)}")
except ValueError as e:
if "bad xref" in str(e).lower():
print(f"⚠️ Bad xref {xref}: {e}")
else:
print(f"⚠️ Error at xref {xref}: {e}")
continue
except Exception as e:
print(f"⚠️ Error extracting image {img_index}: {e}")
continue
doc.close()
except Exception as e:
print(f"❌ Error in _extract_images_from_page: {e}")
return images
def _extract_tables_from_page(self, pdf_path: str, page_num: int) -> List[Dict]:
"""Extract tables from specific page using pdfplumber."""
tables = []
try:
with pdfplumber.open(pdf_path) as pdf:
page = pdf.pages[page_num]
extracted_tables = page.extract_tables()
for table_idx, table in enumerate(extracted_tables or []):
# Convert table to markdown format
table_md = self._table_to_markdown(table)
tables.append({
"type": "table",
"content": table_md,
"page": page_num,
"index": table_idx
})
except Exception as e:
print(f"Error extracting tables from page {page_num}: {e}")
return tables
def _table_to_markdown(self, table: List[List]) -> str:
"""Convert table to markdown format."""
if not table:
return ""
md = "| " + " | ".join(str(cell or "") for cell in table[0]) + " |\n"
md += "| " + " | ".join(["---"] * len(table[0])) + " |\n"
for row in table[1:]:
md += "| " + " | ".join(str(cell or "") for cell in row) + " |\n"
return md
def extract_pdf_content(self, pdf_path: str) -> Dict:
"""
Extract all content from PDF (text, tables, images).
Uses cache to avoid reprocessing.
"""
pdf_name = os.path.basename(pdf_path)
file_hash = self._get_file_hash(pdf_path)
# Check cache
if pdf_name in self.cache and self.cache[pdf_name].get("hash") == file_hash:
print(f"Using cached data for {pdf_name}")
return self.cache[pdf_name]["content"]
print(f"Processing PDF: {pdf_name}")
content = {
"filename": pdf_name,
"pages": []
}
try:
# Count pages
with pdfplumber.open(pdf_path) as pdf:
num_pages = len(pdf.pages)
# Process each page
for page_num in range(num_pages):
page_content = {
"page_number": page_num + 1,
"text": "",
"tables": [],
"images": []
}
# Extract text
with pdfplumber.open(pdf_path) as pdf:
page = pdf.pages[page_num]
page_content["text"] = page.extract_text() or ""
# Extract tables
page_content["tables"] = self._extract_tables_from_page(pdf_path, page_num)
# Extract images
page_content["images"] = self._extract_images_from_page(pdf_path, page_num)
content["pages"].append(page_content)
except Exception as e:
print(f"Error processing {pdf_path}: {e}")
return None
# Cache the result
self.cache[pdf_name] = {
"hash": file_hash,
"content": content
}
self._save_cache()
return content
def process_all_pdfs(self, pdf_dir: str = None) -> List[Dict]:
"""Process all PDFs in directory."""
if pdf_dir is None:
pdf_dir = self.pdf_dir
all_content = []
pdf_files = list(Path(pdf_dir).glob("*.pdf"))
if not pdf_files:
print(f"No PDF files found in {pdf_dir}")
return all_content
for pdf_file in pdf_files:
content = self.extract_pdf_content(str(pdf_file))
if content:
all_content.append(content)
return all_content
def prepare_documents_for_embedding(pdf_content: Dict) -> List[Tuple[str, Dict]]:
"""
Prepare extracted PDF content for embedding.
Returns list of (text, metadata) tuples.
"""
documents = []
for page in pdf_content.get("pages", []):
page_num = page.get("page_number")
filename = pdf_content.get("filename")
# Add text chunks
if page.get("text"):
documents.append((
page["text"],
{
"type": "text",
"page": page_num,
"filename": filename
}
))
# Add table summaries
for table in page.get("tables", []):
documents.append((
f"Table on page {page_num}:\n{table['content']}",
{
"type": "table",
"page": page_num,
"filename": filename
}
))
# Add image descriptions (we'll get these from OpenAI)
for image in page.get("images", []):
documents.append((
f"Image on page {page_num}",
{
"type": "image",
"page": page_num,
"filename": filename,
"image_base64": image.get("base64"),
"image_format": image.get("format")
}
))
return documents |