Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 12,995 Bytes
61d29fc | 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 | #!/usr/bin/env python3
"""
Universal document text extractor for government documents.
Handles: PDF, PowerPoint, Word, Excel, HTML, Images (OCR)
Usage:
from extraction.universal_extractor import UniversalDocumentExtractor
extractor = UniversalDocumentExtractor()
result = extractor.extract_from_url("https://example.com/agenda.pdf")
print(result['text'])
"""
import io
from pathlib import Path
from typing import Optional, Dict
import httpx
from loguru import logger
# PDF extraction
try:
from PyPDF2 import PdfReader
except ImportError:
PdfReader = None
logger.warning("PDF support disabled. Install: pip install PyPDF2")
try:
import pdfplumber
except ImportError:
pdfplumber = None
logger.debug("pdfplumber not available (optional)")
# PowerPoint extraction
try:
from pptx import Presentation
except ImportError:
Presentation = None
logger.warning("PowerPoint support disabled. Install: pip install python-pptx")
# Word extraction
try:
from docx import Document
except ImportError:
Document = None
logger.warning("Word support disabled. Install: pip install python-docx")
# Excel extraction
try:
import pandas as pd
except ImportError:
pd = None
logger.warning("Excel support disabled. Install: pip install openpyxl pandas")
# HTML extraction
try:
from bs4 import BeautifulSoup
except ImportError:
BeautifulSoup = None
logger.warning("HTML support disabled. Install: pip install beautifulsoup4")
# OCR extraction (for images/scanned PDFs)
try:
import pytesseract
from PIL import Image
except ImportError:
pytesseract = None
Image = None
logger.debug("OCR support disabled (optional). Install: pip install pytesseract pillow")
class UniversalDocumentExtractor:
"""Extract text from any government document format."""
def __init__(self):
"""Initialize extractor with HTTP client."""
self.client = httpx.Client(timeout=30, follow_redirects=True)
def extract_from_url(self, url: str) -> Dict[str, any]:
"""
Download document from URL and extract text.
Args:
url: Document URL
Returns:
Dict with:
- url: Source URL
- format: File format (.pdf, .pptx, etc.)
- text: Extracted text
- file_size_kb: Size in KB
- text_length: Length of extracted text
- success: Whether extraction succeeded
"""
logger.info(f"Downloading: {url}")
try:
# Download file
response = self.client.get(url)
response.raise_for_status()
file_bytes = response.content
# Detect format from URL or Content-Type
file_ext = self._detect_format(url, response.headers.get('content-type', ''))
logger.debug(f"Detected format: {file_ext}")
# Extract based on format
if file_ext == '.pdf':
text = self.extract_pdf(file_bytes)
elif file_ext in ['.ppt', '.pptx']:
text = self.extract_powerpoint(file_bytes)
elif file_ext in ['.doc', '.docx']:
text = self.extract_word(file_bytes)
elif file_ext in ['.xls', '.xlsx']:
text = self.extract_excel(file_bytes)
elif file_ext in ['.html', '.htm']:
text = self.extract_html(file_bytes)
elif file_ext in ['.jpg', '.jpeg', '.png', '.tiff', '.tif']:
text = self.extract_image_ocr(file_bytes)
else:
logger.warning(f"Unknown format: {file_ext}")
text = ""
success = bool(text.strip())
return {
'url': url,
'format': file_ext,
'text': text,
'file_size_kb': len(file_bytes) // 1024,
'text_length': len(text),
'success': success
}
except Exception as e:
logger.error(f"Failed to extract from {url}: {e}")
return {
'url': url,
'format': 'unknown',
'text': '',
'file_size_kb': 0,
'text_length': 0,
'success': False,
'error': str(e)
}
def _detect_format(self, url: str, content_type: str) -> str:
"""Detect document format from URL or Content-Type."""
# Try URL extension first
url_lower = url.lower()
for ext in ['.pdf', '.pptx', '.ppt', '.docx', '.doc', '.xlsx', '.xls',
'.html', '.htm', '.jpg', '.jpeg', '.png', '.tiff', '.tif']:
if ext in url_lower:
return ext
# Try Content-Type
content_type_lower = content_type.lower()
if 'pdf' in content_type_lower:
return '.pdf'
elif 'powerpoint' in content_type_lower or 'presentation' in content_type_lower:
return '.pptx'
elif 'word' in content_type_lower or 'msword' in content_type_lower:
return '.docx'
elif 'excel' in content_type_lower or 'spreadsheet' in content_type_lower:
return '.xlsx'
elif 'html' in content_type_lower:
return '.html'
elif 'image' in content_type_lower:
return '.jpg'
return '.unknown'
def extract_pdf(self, file_bytes: bytes) -> str:
"""Extract text from PDF."""
if PdfReader is None:
logger.error("PyPDF2 not installed")
return ""
try:
# Try PyPDF2 first (faster)
pdf_reader = PdfReader(io.BytesIO(file_bytes))
text = ""
for page in pdf_reader.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
# If no text extracted, might be scanned PDF
if not text.strip() and pdfplumber:
logger.info("PDF appears to be scanned, trying pdfplumber...")
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
text = "\n".join(page.extract_text() or "" for page in pdf.pages)
return text.strip()
except Exception as e:
logger.error(f"PDF extraction failed: {e}")
return ""
def extract_powerpoint(self, file_bytes: bytes) -> str:
"""Extract text from PowerPoint (.ppt, .pptx)."""
if Presentation is None:
logger.error("python-pptx not installed")
return ""
try:
prs = Presentation(io.BytesIO(file_bytes))
text_parts = []
for slide_num, slide in enumerate(prs.slides, 1):
# Extract text from all shapes
slide_text = []
for shape in slide.shapes:
if hasattr(shape, "text") and shape.text:
slide_text.append(shape.text)
if slide_text:
text_parts.append(f"=== Slide {slide_num} ===")
text_parts.append("\n".join(slide_text))
text_parts.append("")
# Extract speaker notes if available
if slide.has_notes_slide:
notes = slide.notes_slide.notes_text_frame.text
if notes:
text_parts.append(f"Notes: {notes}")
text_parts.append("")
return "\n".join(text_parts).strip()
except Exception as e:
logger.error(f"PowerPoint extraction failed: {e}")
return ""
def extract_word(self, file_bytes: bytes) -> str:
"""Extract text from Word (.doc, .docx)."""
if Document is None:
logger.error("python-docx not installed")
return ""
try:
doc = Document(io.BytesIO(file_bytes))
text_parts = []
# Extract paragraphs
for para in doc.paragraphs:
if para.text.strip():
text_parts.append(para.text)
# Extract tables
for table in doc.tables:
for row in table.rows:
row_text = " | ".join(cell.text.strip() for cell in row.cells)
if row_text.strip():
text_parts.append(row_text)
return "\n".join(text_parts).strip()
except Exception as e:
logger.error(f"Word extraction failed: {e}")
return ""
def extract_excel(self, file_bytes: bytes) -> str:
"""Extract text from Excel (.xls, .xlsx)."""
if pd is None:
logger.error("pandas/openpyxl not installed")
return ""
try:
# Use pandas to read all sheets
excel_file = io.BytesIO(file_bytes)
all_sheets = pd.read_excel(excel_file, sheet_name=None, engine='openpyxl')
text_parts = []
for sheet_name, df in all_sheets.items():
text_parts.append(f"=== Sheet: {sheet_name} ===")
# Convert DataFrame to text
text_parts.append(df.to_string(index=False))
text_parts.append("")
return "\n".join(text_parts).strip()
except Exception as e:
logger.error(f"Excel extraction failed: {e}")
return ""
def extract_html(self, file_bytes: bytes) -> str:
"""Extract text from HTML."""
if BeautifulSoup is None:
logger.error("BeautifulSoup not installed")
return ""
try:
soup = BeautifulSoup(file_bytes, 'html.parser')
# Remove script and style tags
for script in soup(["script", "style", "nav", "header", "footer"]):
script.decompose()
# Get text
text = soup.get_text()
# Clean up whitespace
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = '\n'.join(chunk for chunk in chunks if chunk)
return text.strip()
except Exception as e:
logger.error(f"HTML extraction failed: {e}")
return ""
def extract_image_ocr(self, file_bytes: bytes) -> str:
"""Extract text from image using OCR (for scanned documents)."""
if pytesseract is None or Image is None:
logger.error("pytesseract/PIL not installed")
logger.info("Install: pip install pytesseract pillow")
logger.info("Also install tesseract: sudo apt-get install tesseract-ocr")
return ""
try:
image = Image.open(io.BytesIO(file_bytes))
# Run OCR
text = pytesseract.image_to_string(image)
return text.strip()
except Exception as e:
logger.error(f"OCR extraction failed: {e}")
logger.info("Make sure tesseract is installed: sudo apt-get install tesseract-ocr")
return ""
def close(self):
"""Close HTTP client."""
self.client.close()
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit."""
self.close()
# Example usage and testing
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python universal_extractor.py <url>")
print("\nExample:")
print(" python universal_extractor.py https://example.com/agenda.pdf")
sys.exit(1)
url = sys.argv[1]
with UniversalDocumentExtractor() as extractor:
result = extractor.extract_from_url(url)
print(f"\n{'='*70}")
print(f"URL: {result['url']}")
print(f"Format: {result['format']}")
print(f"File Size: {result['file_size_kb']} KB")
print(f"Text Length: {result['text_length']} characters")
print(f"Success: {result['success']}")
print(f"{'='*70}\n")
if result['success']:
# Show first 500 characters
preview = result['text'][:500]
print("Preview:")
print(preview)
if len(result['text']) > 500:
print("\n... (truncated)")
else:
print(f"Error: {result.get('error', 'Unknown error')}")
|