Spaces:
Sleeping
Sleeping
File size: 10,167 Bytes
ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 bc594c7 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 c8cbfa0 ae7b6d0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 bc594c7 c8cbfa0 | 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 | """
doc_reader.py
-------------
Extracts full text from .docx, .pdf, and .txt files.
For scanned PDFs: converts each page to image and uses GPT-4o vision.
Falls back to pdfplumber for text-based PDFs.
For DOCX: recursive XML walk to catch nested tables.
Outputs clear section markers so doc_sectioner can locate annexures.
"""
import os
import base64
import io
import re
import pdfplumber
from docx import Document
from docx.oxml.ns import qn
from pathlib import Path
from openai import OpenAI
# ββ PDF: detect if scanned ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _is_scanned_pdf(file_path: str, sample_pages: int = 3) -> bool:
try:
with pdfplumber.open(file_path) as pdf:
pages_to_check = min(sample_pages, len(pdf.pages))
total_chars = sum(
len((pdf.pages[i].extract_text() or "").strip())
for i in range(pages_to_check)
)
avg = total_chars / max(pages_to_check, 1)
print(f" Avg chars/page (first {pages_to_check}): {avg:.0f}")
return avg < 100
except Exception:
return True
# ββ PDF: vision OCR via GPT-4o ββββββββββββββββββββββββββββββββββββββββββββββββ
def _pdf_page_to_base64(file_path: str, page_num: int) -> str:
from pdf2image import convert_from_path
images = convert_from_path(file_path, first_page=page_num + 1, last_page=page_num + 1, dpi=180)
if not images:
return ""
buf = io.BytesIO()
images[0].save(buf, format="PNG")
return base64.b64encode(buf.getvalue()).decode("utf-8")
# Broad prompt used for most pages
_VISION_PROMPT_BODY = (
"This is a page from an Indian HFC/NBFC loan document (CAL/CAM/COE/Annexure). "
"Extract ALL text exactly as it appears. "
"For tables, output each row on one line with columns separated by ' | '. "
"Preserve all numbers, dates, rupee amounts, percentages, PAN numbers, addresses. "
"Do NOT summarize. Output raw extracted text only."
)
# Targeted prompts for specific page types
_VISION_PROMPT_TABLE = (
"This page contains a table from an Indian loan document. "
"Extract ALL rows of the table with columns separated by ' | '. "
"Keep every row including headers and totals. "
"Also include any heading text above or below the table. "
"Do NOT summarize or skip any row."
)
def _extract_text_from_scanned_pdf(file_path: str) -> str:
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY not set β required for scanned PDF OCR.")
client = OpenAI(api_key=api_key)
with pdfplumber.open(file_path) as pdf:
num_pages = len(pdf.pages)
print(f" Scanned PDF β {num_pages} pages, using GPT-4o vision...")
all_text = []
for page_num in range(num_pages):
print(f" Page {page_num + 1}/{num_pages}...")
try:
b64 = _pdf_page_to_base64(file_path, page_num)
if not b64:
continue
# Use table prompt for pages likely to have dense tables (annexures)
# We don't know which pages have tables, so use body prompt for all,
# but request explicit table row formatting
response = client.chat.completions.create(
model="gpt-4o",
max_tokens=3000,
messages=[{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}", "detail": "high"}},
{"type": "text", "text": _VISION_PROMPT_BODY},
]
}]
)
page_text = response.choices[0].message.content or ""
all_text.append(f"\n=== PDF PAGE {page_num + 1} ===\n{page_text}")
except Exception as e:
print(f" Warning: page {page_num + 1} failed: {e}")
all_text.append(f"\n=== PDF PAGE {page_num + 1} === [extraction failed: {e}]")
return "\n".join(all_text).strip()
# ββ PDF: text-based extraction ββββββββββββββββββββββββββββββββββββββββββββββββ
def extract_text_from_pdf(file_path: str) -> str:
if _is_scanned_pdf(file_path):
return _extract_text_from_scanned_pdf(file_path)
print(" Text-based PDF β using pdfplumber...")
text_parts = []
with pdfplumber.open(file_path) as pdf:
for i, page in enumerate(pdf.pages):
page_text = page.extract_text() or ""
if page_text:
text_parts.append(f"\n=== PDF PAGE {i + 1} ===\n{page_text}")
tables = page.extract_tables()
for table in tables:
for row in table:
if row:
row_text = " | ".join(cell.strip() if cell else "" for cell in row)
if row_text.strip(" |"):
text_parts.append(row_text)
return "\n".join(text_parts).strip()
# ββ DOCX helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _extract_cell_text(tc_element, depth: int = 0) -> str:
parts = []
for child in tc_element:
tag = child.tag.split("}")[1] if "}" in child.tag else child.tag
if tag == "p":
text = "".join(r.text for r in child.iter(qn("w:t")) if r.text)
if text.strip():
parts.append(text.strip())
elif tag == "tbl":
for tr in child.findall(".//" + qn("w:tr")):
row_cells = []
for tc in tr.findall(qn("w:tc")):
cell_text = _extract_cell_text(tc, depth + 1)
row_cells.append(cell_text)
deduped = []
for val in row_cells:
if not deduped or val != deduped[-1]:
deduped.append(val)
row_str = " | ".join(deduped)
if row_str.strip(" |"):
parts.append(row_str)
return "\n".join(parts)
# Known heading patterns that mark important document sections
_SECTION_HEADINGS = [
("term sheet", "=== TERM SHEET ==="),
("terms of facility", "=== TERM SHEET ==="),
("annexure ii a", "=== ANNEXURE II A β SECURITY UNITS P1 ==="),
("annexure ii b", "=== ANNEXURE II B β SECURITY UNITS P2 ==="),
("annexure ii", "=== ANNEXURE II β SECURITY UNITS ==="),
("list of unsold units", "=== SECURITY UNITS TABLE ==="),
("list of unsold apartment", "=== SECURITY UNITS TABLE ==="),
("repayment schedule", "=== REPAYMENT SCHEDULE ==="),
("details of co-borrower","=== CO-BORROWERS ==="),
("details of co borrower","=== CO-BORROWERS ==="),
("pre-disbursement condition", "=== PRE-DISBURSEMENT CONDITIONS ==="),
("pre disbursement condition", "=== PRE-DISBURSEMENT CONDITIONS ==="),
("other monitoring condition", "=== MONITORING CONDITIONS ==="),
("special conditions", "=== SPECIAL CONDITIONS ==="),
("exit table", "=== EXIT TABLE ==="),
("collection slot", "=== SI / EXIT TABLE ==="),
("cash flow analysis", "=== CASH FLOW ANALYSIS ==="),
]
def _inject_section_markers(text: str) -> str:
"""Insert section markers before lines that match known headings."""
lines = text.split("\n")
out = []
for line in lines:
ll = line.lower().strip()
for pattern, marker in _SECTION_HEADINGS:
if pattern in ll and len(ll) < 120:
out.append(f"\n{marker}")
break
out.append(line)
return "\n".join(out)
def extract_text_from_docx(file_path: str) -> str:
doc = Document(file_path)
chunks = []
for para in doc.paragraphs:
if para.text.strip():
chunks.append(para.text.strip())
for t_idx, table in enumerate(doc.tables):
for row in table.rows:
row_cells = []
for cell in row.cells:
cell_text = _extract_cell_text(cell._tc)
row_cells.append(cell_text)
deduped = []
for val in row_cells:
if not deduped or val != deduped[-1]:
deduped.append(val)
row_str = " | ".join(deduped)
if row_str.strip(" |"):
chunks.append(row_str)
raw = "\n".join(chunks).strip()
return _inject_section_markers(raw)
# ββ Public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def extract_text(file_path: str) -> str:
ext = Path(file_path).suffix.lower()
if ext == ".pdf":
print(" Format: PDF")
return extract_text_from_pdf(file_path)
elif ext == ".docx":
print(" Format: DOCX")
return extract_text_from_docx(file_path)
elif ext == ".txt":
print(" Format: TXT")
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
return f.read().strip()
elif ext == ".doc":
raise ValueError(".doc is not supported. Save as .docx and re-upload.")
else:
raise ValueError(f"Unsupported format: {ext}. Supported: .pdf, .docx, .txt")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
path = sys.argv[1]
print(f"[TEST] Reading: {path}")
text = extract_text(path)
print(f"[TEST] Extracted {len(text):,} chars")
print("\n--- First 2000 chars ---")
print(text[:2000])
print("\n--- Last 2000 chars ---")
print(text[-2000:])
else:
print("Usage: python doc_reader.py yourfile.pdf/docx/txt") |