|
| 1 | +""" |
| 2 | +Shared utility functions for fine-tuning training data generation. |
| 3 | +
|
| 4 | +These functions are used by both finetuning_data_generator and |
| 5 | +finetuning_process_document Lambda functions to avoid code duplication. |
| 6 | +""" |
| 7 | + |
| 8 | +import base64 |
| 9 | +import io |
| 10 | +import logging |
| 11 | +from typing import Any, Dict, List, Tuple |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def get_extraction_fields(baseline: Dict[str, Any]) -> List[str]: |
| 17 | + """ |
| 18 | + Extract field names from baseline extraction data. |
| 19 | +
|
| 20 | + Parses various baseline formats to build a flat list of field names |
| 21 | + for training data generation. |
| 22 | +
|
| 23 | + Supported formats: |
| 24 | + - Format 1: ``{"Sections": [{"Extraction": {"field": ...}}]}`` |
| 25 | + - Format 2: ``{"Extraction": {"field": ...}}`` |
| 26 | + - Format 3: ``{"fields": [{"name": "field"}, ...]}`` |
| 27 | +
|
| 28 | + Args: |
| 29 | + baseline: The baseline extraction result dictionary. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + List of field name strings (empty strings filtered out). |
| 33 | + """ |
| 34 | + fields: List[str] = [] |
| 35 | + |
| 36 | + # Format 1: Sections with Extraction |
| 37 | + sections = baseline.get("Sections", []) |
| 38 | + for section in sections: |
| 39 | + extraction = section.get("Extraction", {}) |
| 40 | + fields.extend(extraction.keys()) |
| 41 | + |
| 42 | + # Format 2: Direct extraction at top level |
| 43 | + extraction = baseline.get("Extraction", {}) |
| 44 | + fields.extend(extraction.keys()) |
| 45 | + |
| 46 | + # Format 3: Fields array |
| 47 | + for field in baseline.get("fields", []): |
| 48 | + if isinstance(field, dict): |
| 49 | + fields.append(field.get("name", "")) |
| 50 | + elif isinstance(field, str): |
| 51 | + fields.append(field) |
| 52 | + |
| 53 | + return [f for f in fields if f] # Filter empty strings |
| 54 | + |
| 55 | + |
| 56 | +def format_baseline_for_training(baseline: Dict[str, Any]) -> Dict[str, Any]: |
| 57 | + """ |
| 58 | + Format baseline extraction data into the structure expected for training. |
| 59 | +
|
| 60 | + Maps baseline result fields to a clean ``field_name -> value`` dict. |
| 61 | + Handles nested value objects (``{"value": ...}`` or ``{"Value": ...}``). |
| 62 | +
|
| 63 | + Supported formats: |
| 64 | + - Format 1: ``{"Sections": [{"Extraction": {"field": value}}]}`` |
| 65 | + - Format 2: ``{"Extraction": {"field": value}}`` |
| 66 | + - Format 3: ``{"fields": [{"name": "field", "value": "val"}]}`` |
| 67 | + - Fallback: returns all non-metadata keys from baseline. |
| 68 | +
|
| 69 | + Args: |
| 70 | + baseline: The baseline extraction result dictionary. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Dictionary mapping field names to their extracted values. |
| 74 | + """ |
| 75 | + result: Dict[str, Any] = {} |
| 76 | + |
| 77 | + # Format 1: Sections with Extraction |
| 78 | + sections = baseline.get("Sections", []) |
| 79 | + for section in sections: |
| 80 | + extraction = section.get("Extraction", {}) |
| 81 | + for key, value in extraction.items(): |
| 82 | + if isinstance(value, dict): |
| 83 | + result[key] = value.get("value", value.get("Value", str(value))) |
| 84 | + else: |
| 85 | + result[key] = value |
| 86 | + |
| 87 | + # Format 2: Direct extraction at top level |
| 88 | + extraction = baseline.get("Extraction", {}) |
| 89 | + for key, value in extraction.items(): |
| 90 | + if isinstance(value, dict): |
| 91 | + result[key] = value.get("value", value.get("Value", str(value))) |
| 92 | + else: |
| 93 | + result[key] = value |
| 94 | + |
| 95 | + # Format 3: Fields array with values |
| 96 | + for field in baseline.get("fields", []): |
| 97 | + if isinstance(field, dict): |
| 98 | + name = field.get("name", "") |
| 99 | + value = field.get("value", "") |
| 100 | + if name: |
| 101 | + result[name] = value |
| 102 | + |
| 103 | + # Fallback: return baseline as-is (cleaned up) |
| 104 | + if not result: |
| 105 | + for key, value in baseline.items(): |
| 106 | + if key not in [ |
| 107 | + "Sections", |
| 108 | + "metadata", |
| 109 | + "Metadata", |
| 110 | + "pageCount", |
| 111 | + "PageCount", |
| 112 | + ]: |
| 113 | + result[key] = value |
| 114 | + |
| 115 | + return result |
| 116 | + |
| 117 | + |
| 118 | +def convert_pdf_to_images( |
| 119 | + pdf_bytes: bytes, |
| 120 | + max_pages: int = 20, |
| 121 | + dpi: int = 150, |
| 122 | +) -> List[Tuple[bytes, str]]: |
| 123 | + """ |
| 124 | + Convert PDF bytes to a list of PNG image byte tuples (one per page). |
| 125 | +
|
| 126 | + Uses pypdfium2 for PDF rendering. Limits to *max_pages* to avoid |
| 127 | + excessive memory usage during training data generation. |
| 128 | +
|
| 129 | + Args: |
| 130 | + pdf_bytes: Raw PDF file content. |
| 131 | + max_pages: Maximum number of pages to convert. |
| 132 | + dpi: Resolution for rendering (default 150). |
| 133 | +
|
| 134 | + Returns: |
| 135 | + List of ``(png_bytes, "png")`` tuples, one per page. |
| 136 | + Returns an empty list if PDF conversion libraries are unavailable |
| 137 | + or an error occurs. |
| 138 | + """ |
| 139 | + try: |
| 140 | + import pypdfium2 as pdfium |
| 141 | + except ImportError: |
| 142 | + logger.error("pypdfium2 is required for PDF to image conversion") |
| 143 | + return [] |
| 144 | + |
| 145 | + images: List[Tuple[bytes, str]] = [] |
| 146 | + try: |
| 147 | + pdf = pdfium.PdfDocument(pdf_bytes) |
| 148 | + num_pages = min(len(pdf), max_pages) |
| 149 | + |
| 150 | + logger.info(f"Converting PDF with {len(pdf)} pages (processing {num_pages})") |
| 151 | + |
| 152 | + for page_idx in range(num_pages): |
| 153 | + page = pdf[page_idx] |
| 154 | + scale = dpi / 72.0 |
| 155 | + bitmap = page.render(scale=scale) |
| 156 | + pil_image = bitmap.to_pil() |
| 157 | + |
| 158 | + buf = io.BytesIO() |
| 159 | + pil_image.save(buf, format="PNG", optimize=True) |
| 160 | + images.append((buf.getvalue(), "png")) |
| 161 | + |
| 162 | + pdf.close() |
| 163 | + except Exception as e: |
| 164 | + logger.error(f"Error converting PDF to images: {e}", exc_info=True) |
| 165 | + return [] |
| 166 | + |
| 167 | + return images |
| 168 | + |
| 169 | + |
| 170 | +def get_document_images( |
| 171 | + s3_client: Any, |
| 172 | + bucket: str, |
| 173 | + document_key: str, |
| 174 | +) -> List[Tuple[str, str]]: |
| 175 | + """ |
| 176 | + Get base64-encoded images for a document stored in S3. |
| 177 | +
|
| 178 | + Supports PDF files (converted to images via :func:`convert_pdf_to_images`) |
| 179 | + and common image formats (PNG, JPG, JPEG, TIFF, BMP, GIF, WEBP). |
| 180 | +
|
| 181 | + Args: |
| 182 | + s3_client: Boto3 S3 client. |
| 183 | + bucket: S3 bucket name. |
| 184 | + document_key: S3 object key for the document. |
| 185 | +
|
| 186 | + Returns: |
| 187 | + List of ``(base64_string, format)`` tuples. |
| 188 | + Returns an empty list for unsupported file types. |
| 189 | + """ |
| 190 | + response = s3_client.get_object(Bucket=bucket, Key=document_key) |
| 191 | + file_bytes = response["Body"].read() |
| 192 | + content_type = response.get("ContentType", "") |
| 193 | + |
| 194 | + lower_key = document_key.lower() |
| 195 | + |
| 196 | + if lower_key.endswith(".pdf") or "pdf" in content_type: |
| 197 | + page_images = convert_pdf_to_images(file_bytes) |
| 198 | + return [ |
| 199 | + (base64.b64encode(img_bytes).decode("utf-8"), fmt) |
| 200 | + for img_bytes, fmt in page_images |
| 201 | + ] |
| 202 | + |
| 203 | + # Map extensions to format names |
| 204 | + extension_map = { |
| 205 | + ".png": "png", |
| 206 | + ".jpg": "jpeg", |
| 207 | + ".jpeg": "jpeg", |
| 208 | + ".gif": "gif", |
| 209 | + ".webp": "webp", |
| 210 | + ".tiff": "tiff", |
| 211 | + ".bmp": "bmp", |
| 212 | + } |
| 213 | + |
| 214 | + for ext, fmt in extension_map.items(): |
| 215 | + if lower_key.endswith(ext): |
| 216 | + return [(base64.b64encode(file_bytes).decode("utf-8"), fmt)] |
| 217 | + |
| 218 | + logger.warning(f"Unsupported file type for {document_key}, treating as PNG") |
| 219 | + return [(base64.b64encode(file_bytes).decode("utf-8"), "png")] |
| 220 | + |
| 221 | + |
| 222 | +def get_document_images_from_uri( |
| 223 | + s3_client: Any, |
| 224 | + s3_uri: str, |
| 225 | +) -> List[Tuple[str, str]]: |
| 226 | + """ |
| 227 | + Convenience wrapper around :func:`get_document_images` that accepts an S3 URI. |
| 228 | +
|
| 229 | + Parses ``s3://bucket/key`` and delegates to :func:`get_document_images`. |
| 230 | +
|
| 231 | + Args: |
| 232 | + s3_client: Boto3 S3 client. |
| 233 | + s3_uri: Full S3 URI (e.g. ``s3://my-bucket/path/to/doc.pdf``). |
| 234 | +
|
| 235 | + Returns: |
| 236 | + List of ``(base64_string, format)`` tuples. |
| 237 | +
|
| 238 | + Raises: |
| 239 | + ValueError: If *s3_uri* does not start with ``s3://``. |
| 240 | + """ |
| 241 | + if not s3_uri.startswith("s3://"): |
| 242 | + raise ValueError(f"Invalid S3 URI: {s3_uri}") |
| 243 | + |
| 244 | + path = s3_uri[5:] |
| 245 | + parts = path.split("/", 1) |
| 246 | + bucket = parts[0] |
| 247 | + key = parts[1] if len(parts) > 1 else "" |
| 248 | + |
| 249 | + return get_document_images(s3_client, bucket, key) |
0 commit comments