@@ -32,44 +32,136 @@ class OcrService:
3232 def __init__ (
3333 self ,
3434 region : Optional [str ] = None ,
35- max_workers : int = 20 ,
36- enhanced_features : Union [bool , List [str ]] = False ,
35+ config : Optional [Dict [str , Any ]] = None ,
36+ backend : Optional [str ] = None ,
37+ max_workers : Optional [int ] = None ,
38+ # Deprecated parameters for backward compatibility
39+ enhanced_features : Optional [Union [bool , List [str ]]] = None ,
3740 dpi : Optional [int ] = None ,
3841 resize_config : Optional [Dict [str , Any ]] = None ,
39- bedrock_config : Dict [str , Any ] = None ,
40- backend : str = "textract" , # New parameter: "textract" or "bedrock"
41- preprocessing_config : Optional [
42- Dict [str , Any ]
43- ] = None , # New parameter for preprocessing
42+ bedrock_config : Optional [Dict [str , Any ]] = None ,
43+ preprocessing_config : Optional [Dict [str , Any ]] = None ,
4444 ):
4545 """
4646 Initialize the OCR service.
4747
4848 Args:
4949 region: AWS region for services
50+ config: Configuration dictionary containing all OCR settings
51+ backend: OCR backend to use ("textract", "bedrock", or "none")
5052 max_workers: Maximum number of concurrent workers for page processing
51- enhanced_features: Controls Textract FeatureTypes for analyze_document API:
52- - If False: Uses basic detect_document_text (faster, no features)
53- - If List[str]: Uses analyze_document with specified features
54- Valid features: TABLES, FORMS, SIGNATURES, LAYOUT
53+
54+ Deprecated parameters (use config instead):
55+ enhanced_features: Controls Textract FeatureTypes for analyze_document API
5556 dpi: DPI (dots per inch) for image generation from PDF pages
56- resize_config: Optional dictionary containing image resizing configuration
57- with 'target_width' and 'target_height' keys
58- backend: OCR backend to use ("textract" or "bedrock")
59- bedrock_config: Optional dictionary containing bedrock configuration if backend is "bedrock"
60- config: Configuration dictionary
57+ resize_config: Image resizing configuration
58+ bedrock_config: Bedrock configuration if backend is "bedrock"
59+ preprocessing_config: Preprocessing configuration
6160
6261 Raises:
63- ValueError: If invalid features are specified in enhanced_features
64- or if an invalid backend is specified
62+ ValueError: If invalid features are specified or if an invalid backend is specified
6563 """
66- self .region = region or os .environ .get ("AWS_REGION" , "us-east-1" )
67- self .max_workers = max_workers
68- self .dpi = dpi
69- self .resize_config = resize_config
70- self .backend = backend .lower ()
71- self .bedrock_config = bedrock_config
72- self .preprocessing_config = preprocessing_config
64+ # Handle backward compatibility
65+ if config is None and any (
66+ [
67+ enhanced_features is not None ,
68+ dpi is not None ,
69+ resize_config is not None ,
70+ bedrock_config is not None ,
71+ preprocessing_config is not None ,
72+ ]
73+ ):
74+ logger .warning (
75+ "Using deprecated parameter pattern. Please migrate to using 'config' parameter. "
76+ "See OCR README for migration guide."
77+ )
78+ # Use old parameters
79+ self .region = region or os .environ .get ("AWS_REGION" , "us-east-1" )
80+ self .max_workers = max_workers or 20
81+ self .dpi = dpi
82+ self .resize_config = resize_config
83+ self .backend = (backend or "textract" ).lower ()
84+ self .bedrock_config = bedrock_config
85+ self .preprocessing_config = preprocessing_config
86+ self .enhanced_features = enhanced_features
87+ else :
88+ # New pattern - extract from config
89+ self .region = region or os .environ .get ("AWS_REGION" , "us-east-1" )
90+ self .config = config or {}
91+ ocr_config = self .config .get ("ocr" , {})
92+
93+ # Extract backend
94+ self .backend = (backend or ocr_config .get ("backend" , "textract" )).lower ()
95+
96+ # Extract max_workers
97+ self .max_workers = max_workers or ocr_config .get ("max_workers" , 20 )
98+
99+ # Extract DPI
100+ self .dpi = ocr_config .get ("dpi" )
101+
102+ # Extract enhanced features
103+ features_config = ocr_config .get ("features" , [])
104+ if features_config :
105+ self .enhanced_features = [
106+ feature ["name" ] for feature in features_config
107+ ]
108+ else :
109+ self .enhanced_features = False
110+
111+ # Extract image configuration
112+ image_config = ocr_config .get ("image" , {})
113+
114+ # Extract resize configuration
115+ target_width = image_config .get ("target_width" )
116+ target_height = image_config .get ("target_height" )
117+ if target_width is not None and target_height is not None :
118+ # Handle empty strings
119+ if isinstance (target_width , str ) and not target_width .strip ():
120+ target_width = None
121+ if isinstance (target_height , str ) and not target_height .strip ():
122+ target_height = None
123+
124+ if target_width is not None and target_height is not None :
125+ try :
126+ self .resize_config = {
127+ "target_width" : int (target_width ),
128+ "target_height" : int (target_height ),
129+ }
130+ except (ValueError , TypeError ):
131+ logger .warning (
132+ f"Invalid resize configuration values: width={ target_width } , height={ target_height } "
133+ )
134+ self .resize_config = None
135+ else :
136+ self .resize_config = None
137+ else :
138+ self .resize_config = None
139+
140+ # Extract preprocessing configuration
141+ preprocessing_value = image_config .get ("preprocessing" )
142+ if preprocessing_value is True or (
143+ isinstance (preprocessing_value , str )
144+ and preprocessing_value .lower () == "true"
145+ ):
146+ self .preprocessing_config = {"enabled" : True }
147+ else :
148+ self .preprocessing_config = None
149+
150+ # Extract Bedrock configuration
151+ if self .backend == "bedrock" :
152+ if all (
153+ key in ocr_config
154+ for key in ["model_id" , "system_prompt" , "task_prompt" ]
155+ ):
156+ self .bedrock_config = {
157+ "model_id" : ocr_config ["model_id" ],
158+ "system_prompt" : ocr_config ["system_prompt" ],
159+ "task_prompt" : ocr_config ["task_prompt" ],
160+ }
161+ else :
162+ self .bedrock_config = None
163+ else :
164+ self .bedrock_config = None
73165
74166 # Log DPI setting for debugging
75167 logger .info (f"OCR Service initialized with DPI: { self .dpi } " )
@@ -92,11 +184,11 @@ def __init__(
92184 VALID_FEATURES = ["TABLES" , "FORMS" , "SIGNATURES" , "LAYOUT" ]
93185
94186 # Validate features if provided as a list
95- if isinstance (enhanced_features , list ):
187+ if isinstance (self . enhanced_features , list ):
96188 # Check for invalid features
97189 invalid_features = [
98190 feature
99- for feature in enhanced_features
191+ for feature in self . enhanced_features
100192 if feature not in VALID_FEATURES
101193 ]
102194 if invalid_features :
@@ -106,15 +198,13 @@ def __init__(
106198
107199 # Log the validated features
108200 logger .info (
109- f"OCR Service initialized with features: { enhanced_features } "
201+ f"OCR Service initialized with features: { self . enhanced_features } "
110202 )
111203
112- self .enhanced_features = enhanced_features
113-
114204 # Initialize Textract client with adaptive retries
115205 adaptive_config = Config (
116206 retries = {"max_attempts" : 100 , "mode" : "adaptive" },
117- max_pool_connections = max_workers * 3 ,
207+ max_pool_connections = self . max_workers * 3 ,
118208 )
119209 self .textract_client = boto3 .client (
120210 "textract" , region_name = self .region , config = adaptive_config
0 commit comments