ocr_service / app.py
Sebastian Gonzalez
Deploy OCR Service via Script
0a6b0fb
from fastapi import FastAPI, HTTPException, Body
from pydantic import BaseModel
import numpy as np
import cv2
import base64
from typing import Dict, List, Any
import os
import sys
# Add current directory to path to ensure imports work
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from ocr_processors import OCRManager
from unified_extractors import Vendor, VendorSchemaManager
app = FastAPI(title="OCR Service")
# Initialize managers globally
ocr_manager = OCRManager()
schema_manager = VendorSchemaManager()
class OCRRequest(BaseModel):
image: str # Base64 encoded image
vendor_id: str
@app.get("/")
def health_check():
return {"status": "ok", "service": "OCR Service"}
@app.post("/process")
def process_image(request: OCRRequest):
try:
# Decode image
image_data = base64.b64decode(request.image)
nparr = np.frombuffer(image_data, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if image is None:
raise HTTPException(status_code=400, detail="Invalid image data")
# Resolve vendor
try:
vendor = Vendor(request.vendor_id)
except ValueError:
# Fallback for unknown vendors if necessary, or error
# For now, let's assume valid vendor or default
vendor = Vendor.DEFAULT
# Extract text using the EXACT same logic as the original app
# The OCRManager inside this service is the original code
results = ocr_manager.extract_text_with_positions(
image,
vendor,
schema_manager
)
return {"status": "success", "text_blocks": results}
except Exception as e:
print(f"ERROR in OCR Service: {str(e)}")
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)