File size: 9,857 Bytes
2182983 5cab071 d2eaf99 5cab071 2182983 5cab071 5ba189e 2182983 5ba189e 2182983 5ba189e 2182983 d2eaf99 2182983 d2eaf99 2182983 5ba189e 2182983 5ba189e 2182983 d2eaf99 2182983 5ba189e d2eaf99 5cab071 5ba189e 2182983 5ba189e 2182983 5ba189e 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 5ba189e 2182983 5ba189e 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 5cab071 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 5cab071 2182983 d2eaf99 2182983 d2eaf99 2182983 d2eaf99 2182983 | 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 | """
HBL Production PDF Extractor
----------------------------
PDF ingestion pipeline for RAG.
Input:
/pdfs/*.pdf
Output:
extracted/
documents.json
chunks.json
markdown/
Pipeline:
PDF
|
Docling layout extraction
|
OCR fallback
|
Document classification
|
Metadata enrichment
|
Semantic chunks
|
JSON ready for embeddings
"""
import os
# Raw strings so backslashes are never silently mis-parsed
os.environ["HF_HOME"] = r"D:\hf_cache"
os.environ["HF_HUB_DISABLE_XET"] = "1"
os.environ["HF_HUB_OFFLINE"] = "1"
import logging
import json
import gc
import time
import hashlib
from pathlib import Path
from datetime import datetime
from tqdm import tqdm
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
import fitz # pymupdf
from langdetect import detect
logging.basicConfig(level=logging.INFO)
# --------------------------------------------------
# CONFIG
# --------------------------------------------------
PDF_FOLDER = Path("./classified_pdfs/text_pdfs")
OUTPUT_FOLDER = Path("./extracted_textpdfs")
MARKDOWN_FOLDER = OUTPUT_FOLDER / "markdown"
DOCUMENT_JSON = OUTPUT_FOLDER / "documents.json"
CHUNKS_JSON = OUTPUT_FOLDER / "chunks.json"
# Checkpoint files let the script resume after a crash instead of
# reprocessing everything from scratch.
DOCUMENTS_CKPT = OUTPUT_FOLDER / "documents_checkpoint.jsonl"
CHUNKS_CKPT = OUTPUT_FOLDER / "chunks_checkpoint.jsonl"
FAILED_LOG = OUTPUT_FOLDER / "failed.log"
CHUNK_SIZE = 800
CHUNK_OVERLAP = 150
# --------------------------------------------------
# INIT DOCLING
# --------------------------------------------------
print("Initialising Docling models....")
pdf_options = PdfPipelineOptions()
pdf_options.artifacts_path = r"D:\hf_cache\docling_artifacts"
pdf_options.do_ocr = False
pdf_options.do_table_structure = False
pdf_options.generate_page_images = False
pdf_options.generate_picture_images = False
converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_options=pdf_options
)
}
)
print("Docling ready, starting extraction now.....")
# --------------------------------------------------
# HASH
# --------------------------------------------------
def file_hash(path):
md5 = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
md5.update(chunk)
return md5.hexdigest()
# --------------------------------------------------
# PDF METADATA
# --------------------------------------------------
def get_pdf_metadata(path):
doc = fitz.open(path)
images = 0
pages = len(doc)
for p in doc:
images += len(p.get_images())
doc.close()
return {
"pages": pages,
"images": images,
"has_images": images > 0
}
# --------------------------------------------------
# CLASSIFIER
# --------------------------------------------------
def classify_document(text):
t = text.lower()
if any(x in t for x in [
"application form",
"account opening",
"signature",
"cnic",
"customer information"
]):
return "FORM"
if any(x in t for x in [
"circular",
"notification",
"effective date"
]):
return "CIRCULAR"
if any(x in t for x in [
"policy",
"procedure",
"guidelines"
]):
return "POLICY"
if any(x in t for x in [
"rate",
"pricing",
"charges",
"fee"
]):
return "RATE_SHEET"
return "GENERAL"
# --------------------------------------------------
# FORM DETECTION
# --------------------------------------------------
def detect_form(pdf_meta, text):
keywords = [
"signature",
"tick",
"checkbox",
"applicant name",
"date of birth"
]
score = 0
for k in keywords:
if k in text.lower():
score += 1
if pdf_meta["has_images"]:
score += 1
return score >= 3
# --------------------------------------------------
# LANGUAGE
# --------------------------------------------------
def detect_language(text):
try:
return detect(text[:1000])
except Exception:
return "unknown"
# --------------------------------------------------
# EXTRACT PDF
# --------------------------------------------------
def extract_pdf(pdf_path):
print(f"\nProcessing {pdf_path.name}")
print(f"\n Starting conversion for {pdf_path.name}...")
start = time.time()
result = converter.convert(pdf_path)
print(f"Conversion completed in {time.time() - start:.2f} seconds.")
start = time.time()
markdown = result.document.export_to_markdown()
print(f"Markdown export completed in {time.time() - start:.2f} seconds")
pdf_meta = get_pdf_metadata(pdf_path)
metadata = {
"filename": pdf_path.name,
"hash": file_hash(pdf_path),
"size_bytes": pdf_path.stat().st_size,
"pages": pdf_meta["pages"],
"extracted_at": datetime.utcnow().isoformat(),
"language": detect_language(markdown),
"document_type": classify_document(markdown),
"is_form": detect_form(pdf_meta, markdown)
}
return metadata, markdown
# --------------------------------------------------
# CHUNKER
# --------------------------------------------------
def create_chunks(text, metadata):
words = text.split()
chunks = []
start = 0
chunk_id = 0
while start < len(words):
end = start + CHUNK_SIZE
chunk_words = words[start:end]
chunk = " ".join(chunk_words)
chunks.append({
"chunk_id": chunk_id,
"text": chunk,
"metadata": metadata
})
chunk_id += 1
start = end - CHUNK_OVERLAP
return chunks
# --------------------------------------------------
# CHECKPOINT HELPERS
# --------------------------------------------------
def load_processed_hashes():
"""Read the checkpoint file to find which PDFs are already done,
so a crash + rerun doesn't reprocess them."""
processed = set()
if DOCUMENTS_CKPT.exists():
with open(DOCUMENTS_CKPT, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
doc = json.loads(line)
processed.add(doc["hash"])
except Exception:
continue
return processed
def append_jsonl(path, obj):
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(obj, ensure_ascii=False) + "\n")
def rebuild_final_json_from_checkpoints():
"""Convert the append-only checkpoint files into the final
documents.json / chunks.json the rest of the pipeline expects."""
documents = []
if DOCUMENTS_CKPT.exists():
with open(DOCUMENTS_CKPT, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
documents.append(json.loads(line))
chunks = []
if CHUNKS_CKPT.exists():
with open(CHUNKS_CKPT, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
chunks.append(json.loads(line))
DOCUMENT_JSON.write_text(
json.dumps(documents, indent=2, ensure_ascii=False),
encoding="utf-8"
)
CHUNKS_JSON.write_text(
json.dumps(chunks, indent=2, ensure_ascii=False),
encoding="utf-8"
)
return documents, chunks
# --------------------------------------------------
# MAIN
# --------------------------------------------------
def main():
OUTPUT_FOLDER.mkdir(exist_ok=True)
MARKDOWN_FOLDER.mkdir(exist_ok=True)
pdfs = list(PDF_FOLDER.glob("*.pdf"))
print(f"Found {len(pdfs)} PDFs")
# Resume support: skip PDFs whose hash is already in the checkpoint.
seen_hashes = load_processed_hashes()
if seen_hashes:
print(f"Resuming: {len(seen_hashes)} PDFs already processed, skipping those.")
for pdf in tqdm(pdfs):
try:
# Hash first, before the expensive Docling conversion,
# so duplicates and already-processed files cost almost nothing.
h = file_hash(pdf)
if h in seen_hashes:
print("Already processed / duplicate, skipping", pdf.name)
continue
meta, markdown = extract_pdf(pdf)
# extract_pdf recomputes the hash internally; keep them consistent
meta["hash"] = h
md_file = MARKDOWN_FOLDER / (pdf.stem + ".md")
md_file.write_text(markdown, encoding="utf-8")
meta["markdown_file"] = str(md_file)
doc_entry = {**meta, "text_length": len(markdown)}
# Write to checkpoint immediately so a crash on the NEXT
# file doesn't lose this one's results.
append_jsonl(DOCUMENTS_CKPT, doc_entry)
if not meta["is_form"]:
for chunk in create_chunks(markdown, meta):
append_jsonl(CHUNKS_CKPT, chunk)
seen_hashes.add(h)
except Exception as e:
print("FAILED", pdf.name, e)
with open(FAILED_LOG, "a", encoding="utf-8") as f:
f.write(f"{datetime.utcnow().isoformat()} {pdf.name} {e}\n")
finally:
gc.collect()
documents, chunks = rebuild_final_json_from_checkpoints()
print("\nDONE")
print("Documents:", len(documents))
print("Chunks:", len(chunks))
if __name__ == "__main__":
main() |