File size: 21,843 Bytes
1628024 |
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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
#!/usr/bin/env python3
"""
OCR Backend API with Azure Document Intelligence - Cleaned and Optimized
Supports file uploads, URL processing, and web scraping fallback
"""
import os
import io
import requests
import numpy as np
import logging
from typing import Optional, List, Dict, Any
from urllib.parse import urlparse, urljoin
from pathlib import Path
import mimetypes
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, HttpUrl
import uvicorn
# Import unified configuration
try:
from configs import get_config
config = get_config().ocr
print("β
Using unified configuration")
except ImportError:
print("β οΈ Unified config not available, using fallback configuration")
from dotenv import load_dotenv
load_dotenv()
class FallbackConfig:
HOST = os.getenv("HOST", "0.0.0.0")
PORT = int(os.getenv("OCR_PORT", "8400"))
DEBUG = os.getenv("DEBUG", "True").lower() == "true"
# Azure Document Intelligence configuration
AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT", "")
AZURE_DOCUMENT_INTELLIGENCE_KEY = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_KEY", "")
# Web scraping configuration
MAX_IMAGES_PER_PAGE = int(os.getenv("MAX_IMAGES_PER_PAGE", "10"))
REQUEST_TIMEOUT = int(os.getenv("REQUEST_TIMEOUT", "30"))
USER_AGENT = os.getenv("USER_AGENT", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
# File size limits
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB
config = FallbackConfig()
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
from azure.core.exceptions import HttpResponseError
from bs4 import BeautifulSoup
from PIL import Image
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(
title="OCR Backend API",
description="OCR service with Azure Document Intelligence, supporting file uploads, URLs, and web scraping",
version="2.0.0",
debug=config.DEBUG
)
# CORS configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Pydantic models
class URLRequest(BaseModel):
url: HttpUrl
extract_images: bool = True
class OCRResponse(BaseModel):
success: bool
content: str
pages: List[Dict[str, Any]]
source_type: str # 'file_upload', 'direct_url', 'web_scraped'
source_url: Optional[str] = None
error: Optional[str] = None
class WebScrapingResult(BaseModel):
text_content: str
images_found: List[str]
ocr_results: List[Dict[str, Any]]
# Utility functions
def format_bounding_box(bounding_box):
"""Format bounding box coordinates for display"""
if not bounding_box:
return "N/A"
reshaped_bounding_box = np.array(bounding_box).reshape(-1, 2)
return ", ".join(["[{}, {}]".format(x, y) for x, y in reshaped_bounding_box])
def is_supported_file_type(content_type: str, filename: str = "") -> bool:
"""Check if the file type is supported for OCR"""
supported_types = {
'application/pdf',
'image/jpeg',
'image/jpg',
'image/png',
'image/tiff',
'image/bmp',
'image/gif'
}
if content_type and content_type.lower() in supported_types:
return True
# Check by file extension if content type is unclear
if filename:
supported_extensions = {'.pdf', '.jpg', '.jpeg', '.png', '.tiff', '.tif', '.bmp', '.gif'}
file_ext = Path(filename).suffix.lower()
return file_ext in supported_extensions
return False
def get_document_intelligence_client():
"""Initialize Azure Document Intelligence client"""
if (config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT == "" or
config.AZURE_DOCUMENT_INTELLIGENCE_KEY == "" or
config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT == "YOUR_FORM_RECOGNIZER_ENDPOINT" or
config.AZURE_DOCUMENT_INTELLIGENCE_KEY == "YOUR_FORM_RECOGNIZER_KEY"):
raise HTTPException(
status_code=500,
detail="Azure Document Intelligence credentials not configured"
)
return DocumentIntelligenceClient(
endpoint=config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT,
credential=AzureKeyCredential(config.AZURE_DOCUMENT_INTELLIGENCE_KEY)
)
async def process_ocr_from_url(url: str) -> Dict[str, Any]:
"""Process OCR from a direct URL"""
try:
client = get_document_intelligence_client()
logger.info(f"Processing OCR from URL: {url}")
poller = client.begin_analyze_document(
"prebuilt-read",
AnalyzeDocumentRequest(url_source=url)
)
result = poller.result()
return format_ocr_result(result, "direct_url", url)
except HttpResponseError as e:
logger.error(f"Azure OCR error for URL {url}: {e}")
raise HTTPException(status_code=400, detail=f"OCR processing failed: {e}")
except Exception as e:
logger.error(f"Unexpected error processing URL {url}: {e}")
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
async def process_ocr_from_bytes(file_bytes: bytes, filename: str = "") -> Dict[str, Any]:
"""Process OCR from file bytes"""
try:
client = get_document_intelligence_client()
logger.info(f"Processing OCR from file: {filename} ({len(file_bytes)} bytes)")
poller = client.begin_analyze_document(
"prebuilt-read",
AnalyzeDocumentRequest(bytes_source=file_bytes)
)
result = poller.result()
return format_ocr_result(result, "file_upload", filename)
except HttpResponseError as e:
logger.error(f"Azure OCR error for file {filename}: {e}")
raise HTTPException(status_code=400, detail=f"OCR processing failed: {e}")
except Exception as e:
logger.error(f"Unexpected error processing file {filename}: {e}")
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
def format_ocr_result(result, source_type: str, source_identifier: str = "") -> Dict[str, Any]:
"""Format Azure Document Intelligence result into standardized response"""
pages_data = []
for page in result.pages:
page_data = {
"page_number": page.page_number,
"width": page.width,
"height": page.height,
"unit": page.unit,
"lines": [],
"words": []
}
# Process lines
if hasattr(page, 'lines') and page.lines:
for line_idx, line in enumerate(page.lines):
page_data["lines"].append({
"line_number": line_idx,
"content": line.content,
"bounding_box": format_bounding_box(line.polygon) if hasattr(line, 'polygon') else "N/A"
})
# Process words
if hasattr(page, 'words') and page.words:
for word in page.words:
page_data["words"].append({
"content": word.content,
"confidence": word.confidence if hasattr(word, 'confidence') else None
})
pages_data.append(page_data)
# Check for handwritten content
handwritten_detected = False
if hasattr(result, 'styles') and result.styles:
for style in result.styles:
if hasattr(style, 'is_handwritten') and style.is_handwritten:
handwritten_detected = True
break
return {
"success": True,
"content": result.content if hasattr(result, 'content') else "",
"pages": pages_data,
"source_type": source_type,
"source_url": source_identifier if source_type == "direct_url" else None,
"handwritten_detected": handwritten_detected,
"error": None
}
async def scrape_web_content(url: str, extract_images: bool = True) -> WebScrapingResult:
"""Scrape web content and extract text and images"""
try:
headers = {
'User-Agent': config.USER_AGENT
}
logger.info(f"Scraping web content from: {url}")
response = requests.get(url, headers=headers, timeout=config.REQUEST_TIMEOUT)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Extract text content
text_content = soup.get_text(separator=' ', strip=True)
images_found = []
ocr_results = []
if extract_images:
# Find all images
img_tags = soup.find_all('img')
for img in img_tags[:config.MAX_IMAGES_PER_PAGE]:
img_src = img.get('src')
if img_src:
# Make absolute URL
img_url = urljoin(url, img_src)
images_found.append(img_url)
# Try to process image with OCR
try:
# Check if image URL is accessible and is an image
img_response = requests.head(img_url, headers=headers, timeout=10)
content_type = img_response.headers.get('content-type', '')
if is_supported_file_type(content_type):
ocr_result = await process_ocr_from_url(img_url)
if ocr_result['content'].strip(): # Only add if there's actual text
ocr_results.append({
"image_url": img_url,
"ocr_content": ocr_result['content'],
"pages": ocr_result['pages']
})
except Exception as e:
logger.warning(f"Failed to process image {img_url}: {e}")
continue
return WebScrapingResult(
text_content=text_content,
images_found=images_found,
ocr_results=ocr_results
)
except requests.RequestException as e:
logger.error(f"Failed to scrape URL {url}: {e}")
raise HTTPException(status_code=400, detail=f"Failed to scrape URL: {e}")
except Exception as e:
logger.error(f"Unexpected error scraping URL {url}: {e}")
raise HTTPException(status_code=500, detail=f"Unexpected error during web scraping: {e}")
def check_url_is_direct_file(url: str) -> tuple[bool, str]:
"""Check if URL points directly to a file"""
try:
headers = {
'User-Agent': config.USER_AGENT
}
response = requests.head(url, headers=headers, timeout=10, allow_redirects=True)
content_type = response.headers.get('content-type', '').lower()
# Check content disposition for filename
content_disposition = response.headers.get('content-disposition', '')
filename = ""
if 'filename=' in content_disposition:
filename = content_disposition.split('filename=')[1].strip('"')
# Parse URL for filename
if not filename:
parsed_url = urlparse(url)
filename = Path(parsed_url.path).name
is_file = is_supported_file_type(content_type, filename)
return is_file, content_type
except Exception as e:
logger.warning(f"Failed to check URL {url}: {e}")
return False, ""
# API Endpoints
@app.get("/")
async def root():
azure_di_available = bool(
config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT and
config.AZURE_DOCUMENT_INTELLIGENCE_KEY and
config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT != "YOUR_FORM_RECOGNIZER_ENDPOINT" and
config.AZURE_DOCUMENT_INTELLIGENCE_KEY != "YOUR_FORM_RECOGNIZER_KEY"
)
return {
"message": "OCR Backend API",
"version": "2.0.0",
"status": "operational",
"features": {
"file_upload": True,
"url_processing": True,
"web_scraping": True,
"azure_document_intelligence": azure_di_available,
"supported_formats": ["PDF", "JPEG", "PNG", "TIFF", "BMP", "GIF"]
},
"limits": {
"max_file_size_mb": config.MAX_FILE_SIZE / (1024 * 1024),
"max_images_per_page": config.MAX_IMAGES_PER_PAGE,
"request_timeout_seconds": config.REQUEST_TIMEOUT
}
}
@app.get("/health")
async def health_check():
azure_di_available = bool(
config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT and
config.AZURE_DOCUMENT_INTELLIGENCE_KEY and
config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT != "YOUR_FORM_RECOGNIZER_ENDPOINT" and
config.AZURE_DOCUMENT_INTELLIGENCE_KEY != "YOUR_FORM_RECOGNIZER_KEY"
)
# Test Azure DI connection if configured
azure_di_status = "not_configured"
if azure_di_available:
try:
# Quick test of Azure DI client initialization
get_document_intelligence_client()
azure_di_status = "configured"
except Exception as e:
azure_di_status = f"error: {str(e)[:100]}"
return {
"status": "healthy",
"service": "OCR Backend API",
"version": "2.0.0",
"azure_document_intelligence": azure_di_status,
"configuration": {
"max_file_size_mb": config.MAX_FILE_SIZE / (1024 * 1024),
"max_images_per_page": config.MAX_IMAGES_PER_PAGE,
"request_timeout": config.REQUEST_TIMEOUT,
"endpoint_configured": bool(config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT),
"key_configured": bool(config.AZURE_DOCUMENT_INTELLIGENCE_KEY)
}
}
@app.post("/ocr/upload", response_model=OCRResponse)
async def ocr_upload_file(file: UploadFile = File(...)):
"""Upload a file for OCR processing"""
# Validate file type
if not is_supported_file_type(file.content_type, file.filename):
raise HTTPException(
status_code=400,
detail=f"Unsupported file type: {file.content_type}. Supported types: PDF, JPEG, PNG, TIFF, BMP, GIF"
)
try:
# Read file content
file_bytes = await file.read()
# Check file size
if len(file_bytes) > config.MAX_FILE_SIZE:
raise HTTPException(
status_code=400,
detail=f"File too large. Maximum size: {config.MAX_FILE_SIZE / (1024*1024):.0f}MB"
)
# Process OCR
result = await process_ocr_from_bytes(file_bytes, file.filename)
return OCRResponse(**result)
except HTTPException:
raise
except Exception as e:
logger.error(f"Unexpected error processing uploaded file: {e}")
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
@app.post("/ocr/url", response_model=OCRResponse)
async def ocr_from_url(request: URLRequest):
"""Process OCR from URL - either direct file or web scraping"""
url_str = str(request.url)
# Check if URL points to a direct file
is_direct_file, content_type = check_url_is_direct_file(url_str)
if is_direct_file:
# Process as direct file URL
try:
result = await process_ocr_from_url(url_str)
return OCRResponse(**result)
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to process direct file URL: {e}")
# Fall back to web scraping
pass
# Web scraping approach
try:
scraping_result = await scrape_web_content(url_str, request.extract_images)
# Combine text content and OCR results
combined_content = scraping_result.text_content
if scraping_result.ocr_results:
ocr_content = "\n\n--- OCR from Images ---\n"
for ocr_result in scraping_result.ocr_results:
ocr_content += f"\nImage: {ocr_result['image_url']}\n"
ocr_content += ocr_result['ocr_content'] + "\n"
combined_content += ocr_content
# Format response
pages_data = [{
"page_number": 1,
"content_type": "web_scraped",
"text_content": scraping_result.text_content,
"images_found": len(scraping_result.images_found),
"ocr_results": len(scraping_result.ocr_results)
}]
return OCRResponse(
success=True,
content=combined_content,
pages=pages_data,
source_type="web_scraped",
source_url=url_str,
error=None
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to process URL {url_str}: {e}")
return OCRResponse(
success=False,
content="",
pages=[],
source_type="web_scraped",
source_url=url_str,
error=str(e)
)
@app.post("/ocr/analyze")
async def analyze_document(
file: Optional[UploadFile] = File(None),
url: Optional[str] = Form(None),
extract_images: bool = Form(True)
):
"""Unified endpoint for document analysis - accepts either file upload or URL"""
if not file and not url:
raise HTTPException(status_code=400, detail="Either file or URL must be provided")
if file and url:
raise HTTPException(status_code=400, detail="Provide either file or URL, not both")
try:
if file:
# Process uploaded file
if not is_supported_file_type(file.content_type, file.filename):
raise HTTPException(
status_code=400,
detail=f"Unsupported file type: {file.content_type}"
)
file_bytes = await file.read()
# Check file size
if len(file_bytes) > config.MAX_FILE_SIZE:
raise HTTPException(
status_code=400,
detail=f"File too large. Maximum size: {config.MAX_FILE_SIZE / (1024*1024):.0f}MB"
)
result = await process_ocr_from_bytes(file_bytes, file.filename)
return result
else:
# Process URL
url_request = URLRequest(url=url, extract_images=extract_images)
response = await ocr_from_url(url_request)
return response.dict()
except HTTPException:
raise
except Exception as e:
logger.error(f"Unexpected error in analyze_document: {e}")
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
# Additional utility endpoints
@app.get("/supported-formats")
async def get_supported_formats():
"""Get list of supported file formats"""
return {
"supported_formats": {
"documents": ["PDF"],
"images": ["JPEG", "JPG", "PNG", "TIFF", "TIF", "BMP", "GIF"]
},
"content_types": [
"application/pdf",
"image/jpeg",
"image/jpg",
"image/png",
"image/tiff",
"image/bmp",
"image/gif"
],
"max_file_size_mb": config.MAX_FILE_SIZE / (1024 * 1024),
"max_images_per_page": config.MAX_IMAGES_PER_PAGE
}
@app.get("/config")
async def get_configuration():
"""Get current service configuration (for debugging)"""
return {
"service": "OCR Backend API",
"version": "2.0.0",
"configuration": {
"host": config.HOST,
"port": config.PORT,
"debug": config.DEBUG,
"max_file_size_mb": config.MAX_FILE_SIZE / (1024 * 1024),
"max_images_per_page": config.MAX_IMAGES_PER_PAGE,
"request_timeout": config.REQUEST_TIMEOUT,
"azure_di_configured": bool(
config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT and
config.AZURE_DOCUMENT_INTELLIGENCE_KEY
)
}
}
if __name__ == "__main__":
print("π§ Loading OCR service configuration...")
print(f"π Will start server on {config.HOST}:{config.PORT}")
print(f"π Azure Document Intelligence: {'β
Configured' if config.AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT else 'β Not configured'}")
print(f"π Max file size: {config.MAX_FILE_SIZE / (1024*1024):.0f}MB")
uvicorn.run(
"ocr_service:app",
host=config.HOST,
port=config.PORT,
reload=config.DEBUG,
log_level="info"
) |