File size: 8,617 Bytes
b76793f
 
 
 
 
 
 
 
 
b267a19
b76793f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b267a19
b76793f
 
 
b267a19
 
 
b76793f
 
 
360683b
b267a19
b76793f
 
 
 
b267a19
 
b76793f
 
 
 
 
b267a19
 
b76793f
 
 
 
 
b267a19
b76793f
 
 
 
 
 
 
 
 
 
 
360683b
 
 
 
 
b76793f
 
 
b267a19
360683b
 
b76793f
 
 
 
 
 
b267a19
b76793f
 
 
 
 
 
 
 
b267a19
 
b76793f
 
 
 
 
 
b267a19
b76793f
 
 
b267a19
 
 
 
 
 
 
 
 
b76793f
 
 
b267a19
 
b76793f
360683b
 
b76793f
b267a19
360683b
b76793f
b267a19
b76793f
b267a19
360683b
b76793f
360683b
b267a19
 
 
b76793f
b267a19
360683b
 
b267a19
360683b
b267a19
 
360683b
b76793f
b267a19
 
b76793f
 
b267a19
b76793f
360683b
b76793f
 
 
b267a19
 
 
b76793f
 
 
 
b267a19
b76793f
b267a19
 
b76793f
 
 
b267a19
b76793f
 
 
b267a19
b76793f
b267a19
b76793f
 
b267a19
 
 
 
 
b76793f
 
b267a19
b76793f
 
b267a19
b76793f
 
 
 
 
b267a19
b76793f
 
 
 
 
 
 
 
 
b267a19
360683b
b76793f
 
 
b267a19
360683b
b267a19
 
b76793f
 
b267a19
 
b76793f
 
 
360683b
b76793f
 
 
 
 
 
8be8190
360683b
 
b76793f
 
 
 
360683b
 
 
 
 
 
 
 
 
 
 
b76793f
b267a19
1b2d066
 
 
 
 
 
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
import os
import uuid
import time
import logging
import shutil
import tempfile
from typing import Optional, List
from enum import Enum
from pathlib import Path
from contextvars import ContextVar
import uvicorn
import pytesseract
from fastapi import (
    FastAPI, File, UploadFile, Depends, 
    HTTPException, Request, status
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import JSONResponse
from fastapi.concurrency import run_in_threadpool
from pydantic import BaseModel
from dotenv import load_dotenv
from PIL import Image
from pdf2image import convert_from_path

# ==========================================
# 1. CONFIGURATION & LOGGING SETUP
# ==========================================
load_dotenv()

# ContextVar for thread-safe Request ID tracking
request_id_ctx: ContextVar[str] = ContextVar("request_id", default="system")

class Config:
    APP_NAME = os.getenv("APP_NAME", "OCR API")
    API_TOKEN = os.getenv("API_BEARER_TOKEN")
    MAX_SIZE = int(os.getenv("MAX_FILE_SIZE", 52428800)) # 50MB
    ALLOWED_ORIGINS = [o.strip() for o in os.getenv("ALLOWED_ORIGINS", "").split(",") if o.strip()]
    ALLOWED_TYPES = ["image/jpeg", "image/png", "image/bmp", "image/webp", "application/pdf"]

class RequestIdFilter(logging.Filter):
    def filter(self, record):
        # Automatically pull request_id from the context variable
        record.request_id = request_id_ctx.get()
        return True

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s | %(levelname)s | ReqID:%(request_id)s | %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    force=True # Ensures our config is applied
)
logger = logging.getLogger("ocr_api")
logger.addFilter(RequestIdFilter())

# ==========================================
# 2. MODELS
# ==========================================
class StatusEnum(str, Enum):
    SUCCESS = "success"
    ERROR = "error"

class BaseResponse(BaseModel):
    request_id: str
    process_time_ms: float
    status: StatusEnum
    message: Optional[str] = None

class PageResult(BaseModel):
    index: int
    page_number: int
    text: str

class OCRResult(BaseModel):
    filename: str
    content_type: str
    saved_file_path: str
    total_pages: int
    pages_content: List[PageResult]

class APIResponse(BaseResponse):
    data: Optional[OCRResult] = None
    error_message: Optional[str] = None

# ==========================================
# 3. SERVICES
# ==========================================

class SecurityService:
    security_scheme = HTTPBearer()

    @staticmethod
    async def validate_token(credentials: HTTPAuthorizationCredentials = Depends(security_scheme)):
        if credentials.credentials != Config.API_TOKEN:
            logger.warning("Auth Failed: Invalid Token")
            raise HTTPException(status_code=401, detail="Invalid Bearer Token")
        return credentials.credentials

class FileValidator:
    @staticmethod
    def validate(file: UploadFile):
        if file.content_type not in Config.ALLOWED_TYPES:
            raise HTTPException(400, f"Invalid file type: {file.content_type}")

    @staticmethod
    def check_size_and_save(file: UploadFile) -> str:
        suffix = Path(file.filename).suffix
        with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as buffer:
            shutil.copyfileobj(file.file, buffer)
            tmp_path = os.path.abspath(buffer.name)
        
        if os.path.getsize(tmp_path) > Config.MAX_SIZE:
            os.remove(tmp_path)
            raise HTTPException(413, "File too large")
        return tmp_path

class OCRProcessor:
    @classmethod
    def process_file(cls, file_path: str, content_type: str) -> dict:
        """Note: No longer passing request_id; logger picks it up from contextvars automatically."""
        start = time.perf_counter()
        pages_content = []
        
        try:
            logger.info(f"Processing File: {file_path}")

            if content_type == "application/pdf":
                logger.info("Converting PDF to Images...")
                images = convert_from_path(file_path)
                total = len(images)
                
                for idx, img in enumerate(images):
                    page_num = idx + 1
                    logger.info(f"Scanning Page {page_num}/{total}")
                    text = pytesseract.image_to_string(img).strip()
                    pages_content.append({"index": idx, "page_number": page_num, "text": text})
            else:
                logger.info("Scanning Single Image...")
                img = Image.open(file_path)
                text = pytesseract.image_to_string(img).strip()
                pages_content.append({"index": 0, "page_number": 1, "text": text})

            logger.info(f"OCR Complete in {(time.perf_counter()-start)*1000:.2f}ms")
            return {"total_pages": len(pages_content), "pages_content": pages_content}

        except Exception as e:
            logger.error(f"OCR Logic Failure: {str(e)}")
            raise ValueError(str(e))

# ==========================================
# 4. APP & MIDDLEWARE
# ==========================================
app = FastAPI(title=Config.APP_NAME)

app.add_middleware(
    CORSMiddleware,
    allow_origins=Config.ALLOWED_ORIGINS if Config.ALLOWED_ORIGINS else ["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.middleware("http")
async def request_context_middleware(request: Request, call_next):
    # 1. Generate ID
    req_id = str(uuid.uuid4())
    # 2. Set Context (Crucial for thread logging)
    token = request_id_ctx.set(req_id)
    request.state.request_id = req_id

    start_time = time.perf_counter()
    logger.info(f"Start: {request.method} {request.url.path}")

    try:
        response = await call_next(request)
        duration = (time.perf_counter() - start_time) * 1000
        response.headers["X-Request-ID"] = req_id
        logger.info(f"Finish: {response.status_code} in {duration:.2f}ms")
        return response
    except Exception as e:
        logger.exception("Middleware caught crash")
        return JSONResponse(status_code=500, content={"status":"error","message":"Internal Server Error","request_id":req_id})
    finally:
        # 3. Clean up Context
        request_id_ctx.reset(token)

# ==========================================
# 5. ENDPOINTS
# ==========================================

@app.get("/")
async def root(request: Request):
    return {
        "request_id": request.state.request_id,
        "process_time_ms": 0,
        "status": StatusEnum.SUCCESS,
        "message": "OCR API Active"
    }

@app.post("/api/v1/get_data", response_model=APIResponse)
async def extract_data(
    request: Request,
    file: UploadFile = File(...),
    token: str = Depends(SecurityService.validate_token)
):
    start_ts = time.perf_counter()
    tmp_path = None
    req_id = request.state.request_id

    try:
        FileValidator.validate(file)
        tmp_path = FileValidator.check_size_and_save(file)
        
        # CPU heavy task run in thread pool. 
        # ContextVars are automatically copied to the thread.
        result = await run_in_threadpool(
            OCRProcessor.process_file,
            tmp_path,
            file.content_type
        )

        return {
            "request_id": req_id,
            "process_time_ms": (time.perf_counter() - start_ts) * 1000,
            "status": StatusEnum.SUCCESS,
            "message": "OCR Extraction Successful",
            "data": {
                "filename": file.filename,
                "content_type": file.content_type,
                "saved_file_path": tmp_path,
                "total_pages": result["total_pages"],
                "pages_content": result["pages_content"]
            }
        }

    except Exception as e:
        logger.error(f"Request failed: {str(e)}")
        status_code = getattr(e, "status_code", 500)
        return JSONResponse(
            status_code=status_code,
            content={
                "request_id": req_id,
                "process_time_ms": (time.perf_counter() - start_ts) * 1000,
                "status": StatusEnum.ERROR,
                "error_message": getattr(e, "detail", str(e))
            }
        )
    finally:
        if tmp_path:
            logger.info(f"File preserved at: {tmp_path}")
            try:
              os.remove(tmp_path)
              logger.info(f"Temporary file deleted: {tmp_path}")
            except Exception as e:
              logger.warning(f"Failed to delete temp file: {str(e)}")