-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.py
More file actions
431 lines (352 loc) · 15 KB
/
validator.py
File metadata and controls
431 lines (352 loc) · 15 KB
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
"""LLM-based image validation."""
from __future__ import annotations
import os
import json
import base64
from io import BytesIO
from pathlib import Path
from datetime import datetime
from openai import OpenAI
import boto3
import config
import storage
def get_openai_client():
api_key = os.environ.get("OPENAI_API_KEY") or config.OPENAI_API_KEY
if not api_key:
raise ValueError("OPENAI_API_KEY not set")
return OpenAI(api_key=api_key)
_s3_client = None
def get_s3_client():
"""Get or create S3 client."""
global _s3_client
if _s3_client is None:
_s3_client = boto3.client("s3", region_name=config.S3_REGION)
return _s3_client
def encode_image_from_s3(s3_path: str) -> str:
"""Load image from S3 directly into memory and encode to base64 (no disk I/O)."""
# Extract key from s3://bucket/key
if s3_path.startswith("s3://"):
parts = s3_path[5:].split("/", 1)
s3_key = parts[1]
else:
s3_key = s3_path
client = get_s3_client()
response = client.get_object(Bucket=config.S3_BUCKET, Key=s3_key)
image_bytes = response["Body"].read()
return base64.b64encode(image_bytes).decode("utf-8")
def encode_image_from_local(filepath: str) -> str:
"""Encode local image to base64."""
with open(filepath, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
def validate_images_batch(images: list, category: str, batch_size: int = 5, model: str = None) -> list:
"""Validate multiple images in a single LLM call."""
prompt = config.VALIDATION_PROMPTS.get(category)
if not prompt:
raise ValueError(f"No validation prompt for category: {category}")
# Use provided model or fall back to config
model = model or getattr(config, "OPENAI_MODEL", "gpt-4o-mini")
# Encode all images
encoded_images = []
valid_indices = []
for i, img in enumerate(images):
image_path = img.get("s3_processed_path", "")
try:
if image_path.startswith("s3://") or image_path.startswith("processed/") or image_path.startswith("raw/"):
b64 = encode_image_from_s3(image_path)
else:
b64 = encode_image_from_local(image_path)
encoded_images.append((i, img, b64))
valid_indices.append(i)
except Exception as e:
print(f"✗ {img['id']}: Failed to load image - {e}")
if not encoded_images:
return [{"valid": False, "reason": "Failed to load image"} for _ in images]
# Build batch prompt - use full prompt, just change the response format
batch_prompt = f"""You will analyze {len(encoded_images)} images.
{prompt.split("Respond with JSON")[0]}
Respond with a JSON object containing a "results" array with one object per image, in the same order as provided:
{{
"results": [
{{"image": 1, "valid": true/false, "reason": "brief reason if invalid, null if valid"}},
{{"image": 2, "valid": true/false, "reason": "..."}},
...
]
}}"""
# Build content array with all images
content = [{"type": "text", "text": batch_prompt}]
for idx, (i, img, b64) in enumerate(encoded_images):
content.append({"type": "text", "text": f"Image {idx + 1} ({img['id']}):"})
content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{b64}", "detail": "low"},
})
client = get_openai_client()
try:
if os.environ.get("DEBUG"):
print(f" [DEBUG] Calling {model} with {len(encoded_images)} images...")
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": content}],
max_completion_tokens=100 * len(encoded_images),
response_format={"type": "json_object"},
)
result_text = response.choices[0].message.content
if os.environ.get("DEBUG"):
print(f" [DEBUG] Raw response: {result_text[:500] if result_text else 'EMPTY'}")
# Parse the result - handle both array and object with "results" key
parsed = json.loads(result_text)
if isinstance(parsed, dict) and "results" in parsed:
results = parsed["results"]
elif isinstance(parsed, list):
results = parsed
else:
results = [parsed]
except Exception as e:
print(f"✗ Batch LLM error: {e}")
if os.environ.get("DEBUG"):
import traceback
traceback.print_exc()
return [{"valid": False, "reason": f"LLM error: {e}"} for _ in images]
# Map results back to original images
final_results = [{"valid": False, "reason": "Failed to load image"} for _ in images]
for idx, (orig_idx, img, _) in enumerate(encoded_images):
if idx < len(results):
result = results[idx]
final_results[orig_idx] = result
status = "✓" if result.get("valid") else "✗"
reason = result.get("reason", "")
print(f"{status} {img['id']}: {reason if reason else 'Valid'}")
return final_results
def validate_image(image_data: dict, category: str = None, model: str = None) -> dict:
"""Validate a single image using LLM.
Args:
image_data: Image dict with s3_processed_path or local path
category: Validation prompt key (defaults to image's category)
model: Model to use (defaults to config.OPENAI_MODEL)
"""
category = category or image_data.get("category")
prompt = config.VALIDATION_PROMPTS.get(category)
if not prompt:
raise ValueError(f"No validation prompt for category: {category}")
# Get image path (S3 or local)
image_path = image_data.get("s3_processed_path", "")
# Encode image (direct memory load from S3, no temp files)
try:
if image_path.startswith("s3://") or image_path.startswith("processed/") or image_path.startswith("raw/"):
image_b64 = encode_image_from_s3(image_path)
else:
image_b64 = encode_image_from_local(image_path)
except Exception as e:
print(f"✗ {image_data['id']}: Failed to load image - {e}")
return {"valid": False, "reason": f"Failed to load image: {e}"}
client = get_openai_client()
model = model or getattr(config, "OPENAI_MODEL", "gpt-4o-mini")
try:
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_b64}",
"detail": "low",
},
},
],
}
],
max_completion_tokens=200,
response_format={"type": "json_object"},
)
result_text = response.choices[0].message.content
if os.environ.get("DEBUG"):
print(f" [DEBUG] Raw response: {result_text[:500] if result_text else 'EMPTY'}")
result = json.loads(result_text)
except Exception as e:
print(f"✗ {image_data['id']}: LLM error - {e}")
if os.environ.get("DEBUG"):
import traceback
traceback.print_exc()
return {"valid": False, "reason": f"LLM error: {e}"}
# Print result
status = "✓" if result.get("valid") else "✗"
reason = result.get("reason", "")
print(f"{status} {image_data['id']}: {reason if reason else 'Valid'}")
return result
def validate_s3_image(s3_key: str, category: str) -> dict:
"""Validate a single image directly from S3 key (no disk I/O)."""
prompt = config.VALIDATION_PROMPTS.get(category)
if not prompt:
raise ValueError(f"No validation prompt for category: {category}")
client = get_openai_client()
image_b64 = encode_image_from_s3(s3_key)
# Determine image type from key
ext = Path(s3_key).suffix.lower()
media_type = "image/jpeg" if ext in [".jpg", ".jpeg"] else "image/png"
model = getattr(config, "OPENAI_MODEL", "gpt-4o-mini")
try:
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:{media_type};base64,{image_b64}",
"detail": "low",
},
},
],
}
],
max_completion_tokens=500,
response_format={"type": "json_object"},
)
result_text = response.choices[0].message.content
result = json.loads(result_text)
except Exception as e:
print(f"Validation failed for {s3_key}: {e}")
return {"s3_key": s3_key, "valid": False, "error": str(e)}
result["s3_key"] = s3_key
return result
def validate_s3_batch(category: str, source: str = "autodev", limit: int = 10) -> list[dict]:
"""Validate images from S3 bucket directly (no disk I/O)."""
s3 = get_s3_client()
prefix = f"processed/{category}/{source}/"
# List objects in S3
response = s3.list_objects_v2(Bucket=config.S3_BUCKET, Prefix=prefix, MaxKeys=limit)
keys = [obj["Key"] for obj in response.get("Contents", [])]
if not keys:
print(f"No images found at s3://{config.S3_BUCKET}/{prefix}")
return []
# Build lookup from DB for original URLs
db_images = storage.get_images(source, category)
url_lookup = {img["id"]: img.get("original_url") for img in db_images}
total = len(keys)
print(f"Validating {total} images from S3...\n")
results = []
valid_count = 0
for i, s3_key in enumerate(keys, 1):
result = validate_s3_image(s3_key, category)
results.append(result)
is_valid = result.get("valid")
if is_valid:
valid_count += 1
# Get image ID from s3_key (e.g., "processed/.../autodev_ABC123_0.jpg" -> "autodev_ABC123_0")
image_id = Path(s3_key).stem
original_url = url_lookup.get(image_id, "")
# Print each result
status = "✓" if is_valid else "✗"
reason = result.get("reason") or result.get("rejection_reason") or result.get("error") or ""
print(f"[{i}/{total}] {status} {image_id}")
if original_url:
print(f" {original_url}")
if reason:
print(f" {reason}")
print(f"\n=== Validation Summary ===")
print(f"Valid: {valid_count} | Invalid: {len(results) - valid_count}")
return results
def validate_batch(
source: str,
category: str,
limit: int = None,
batch_size: int = 1,
status: str = "processed",
prompt_key: str = None,
model: str = None,
) -> tuple:
"""Validate unvalidated images from DB.
Args:
source: Image source (e.g., 'pixabay', 'autodev', 'amazon_reviews')
category: Category to filter images by
limit: Max images to process
batch_size: Number of images per LLM call (1 = serial, >1 = batched)
status: Image status to filter by (default "processed")
prompt_key: Validation prompt key (defaults to category if not specified)
model: Model to use for validation (defaults to config.OPENAI_MODEL)
"""
images = storage.get_images(source=source, category=category, status=status, with_s3_path=True)
if not images:
print(f"No images found for {category} from {source} with status={status}")
return [], []
# Filter to unvalidated only
images = [img for img in images if not img.get("validated_at")]
if not images:
print(f"All images already validated for {category} from {source}")
return [], []
if limit:
images = images[:limit]
# Determine which prompt to use
validation_prompt_key = prompt_key or category
print(f"\n=== Validating {len(images)} images ({category}/{source}) ===")
print(f"Prompt: {validation_prompt_key}")
print(f"Model: {model or config.OPENAI_MODEL}")
print(f"Batch size: {batch_size}\n")
valid = []
invalid = []
# Process in batches
for i in range(0, len(images), batch_size):
batch = images[i:i + batch_size]
if batch_size == 1:
# Single image mode
results = [validate_image(batch[0], validation_prompt_key, model=model)]
else:
# Batch mode
results = validate_images_batch(batch, validation_prompt_key, batch_size, model=model)
# Update DB for each result
for img, result in zip(batch, results):
new_status = "validated" if result.get("valid") else "rejected"
storage.update_image_status(
img["id"],
new_status,
valid=result.get("valid", False),
validation_result=result,
rejection_reason=result.get("reason"),
validated_at=datetime.now().isoformat(),
)
if result.get("valid"):
valid.append(img)
else:
invalid.append(img)
print(f"\n=== Summary ===")
print(f"Valid: {len(valid)} | Invalid: {len(invalid)}")
return valid, invalid
def show_stats(source: str):
"""Show validation stats."""
stats = storage.get_stats(source)
print(f"\n=== {source} stats ===")
for status, count in stats.items():
print(f" {status}: {count}")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Validate images using LLM")
parser.add_argument("source", help="Image source (e.g., 'amazon_reviews', 'pixabay') or 's3' for direct S3 mode")
parser.add_argument("category", help="Category to filter by (for DB) or S3 prefix category")
parser.add_argument("--limit", "-l", type=int, help="Max images to validate")
parser.add_argument("--batch-size", "-b", type=int, default=1, help="Images per LLM call (default: 1)")
parser.add_argument("--status", "-s", default="processed", help="Image status to filter (default: processed)")
parser.add_argument("--prompt", "-p", help="Validation prompt key (defaults to category)")
parser.add_argument("--model", "-m", help="Model to use (default: config.OPENAI_MODEL)")
args = parser.parse_args()
if args.source == "s3":
# Direct S3 mode (legacy)
results = validate_s3_batch(args.category, "autodev", args.limit or 10)
print("\n=== Full Results ===")
print(json.dumps(results, indent=2))
else:
validate_batch(
source=args.source,
category=args.category,
limit=args.limit,
batch_size=args.batch_size,
status=args.status,
prompt_key=args.prompt,
model=args.model,
)