-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathiterative_improve.py
More file actions
942 lines (758 loc) · 32.3 KB
/
iterative_improve.py
File metadata and controls
942 lines (758 loc) · 32.3 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
"""
Iterative Prompt Improvement Workflow
Run batches of 5 images, analyze results, improve prompts, repeat until diminishing returns.
Usage:
# Interactive mode (requires terminal input)
python iterative_improve.py --folder ./test_images/ --provider anthropic --api-key KEY
# Auto mode (non-interactive, runs N batches automatically)
python iterative_improve.py --folder ./test_images/ --provider anthropic --api-key KEY --auto 10
# Resume from previous session
python iterative_improve.py --folder ./test_images/ --provider anthropic --api-key KEY --resume ./improvement_sessions/session_WOMAN_20251210_101551
# With subject filter
python iterative_improve.py --folder ./test_images/ --subject WOMAN --provider anthropic --api-key KEY
"""
import argparse
import base64
import io
import json
import os
import re
import sys
import time
import random
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from copy import deepcopy
# Load .env file if present
try:
from dotenv import load_dotenv
env_path = Path(__file__).parent / ".env"
if env_path.exists():
load_dotenv(env_path)
except ImportError:
pass # dotenv not installed, use system env vars
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Import from standalone components file (no ComfyUI dependencies)
from prompt_components import COMPONENTS, SUBJECT_COMPONENTS, DETAIL_MODES
# ============================================================================
# Configuration
# ============================================================================
BATCH_SIZE = 5
OUTPUT_DIR = "./improvement_sessions"
SUPPORTED_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
# ============================================================================
# Working Prompts (editable during session)
# ============================================================================
# Start with copy of original prompts
WORKING_PROMPTS = deepcopy(COMPONENTS)
# ============================================================================
# Utilities
# ============================================================================
def load_image_as_base64(image_path: str) -> Tuple[str, int, int]:
from PIL import Image
with Image.open(image_path) as img:
if img.mode in ("RGBA", "P"):
img = img.convert("RGB")
width, height = img.size
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=95)
base64_str = base64.standard_b64encode(buffer.getvalue()).decode("utf-8")
return base64_str, width, height
def find_images(folder: str) -> List[str]:
folder = Path(folder)
images = []
for ext in SUPPORTED_EXTENSIONS:
images.extend(folder.glob(f"*{ext}"))
images.extend(folder.glob(f"*{ext.upper()}"))
return sorted([str(p) for p in images])
def create_client(provider: str, api_key: str):
if provider == "anthropic":
import anthropic
return anthropic.Anthropic(api_key=api_key)
elif provider == "openai":
from openai import OpenAI
return OpenAI(api_key=api_key)
elif provider == "grok":
from openai import OpenAI
return OpenAI(api_key=api_key, base_url="https://api.x.ai/v1")
raise ValueError(f"Unknown provider: {provider}")
def get_default_model(provider: str) -> str:
return {"anthropic": "claude-sonnet-4-5-20250929", "openai": "gpt-4o", "grok": "grok-2-vision-1212"}.get(provider)
def parse_json_response(text: str) -> dict:
json_match = re.search(r'```(?:json)?\s*([\s\S]*?)\s*```', text)
if json_match:
text = json_match.group(1)
json_match = re.search(r'\{[\s\S]*\}', text)
if json_match:
try:
return json.loads(json_match.group(0))
except:
pass
return {"raw": text, "parse_error": True}
# ============================================================================
# Analysis Functions
# ============================================================================
def analyze_component(client, model: str, provider: str, base64_image: str,
component_key: str, prompt: str) -> Tuple[dict, float]:
"""Run single component analysis."""
system = "You are an expert visual analyst. Output valid JSON only."
start = time.time()
try:
if provider == "anthropic":
# Use prompt caching for cost/latency reduction
resp = client.messages.create(
model=model, max_tokens=1000, temperature=0.3,
system=[{"type": "text", "text": system, "cache_control": {"type": "ephemeral"}}],
messages=[{"role": "user", "content": [
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": base64_image}},
{"type": "text", "text": prompt}
]}]
)
raw = resp.content[0].text
else:
resp = client.chat.completions.create(
model=model, max_tokens=1000, temperature=0.3,
messages=[
{"role": "system", "content": system},
{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}},
{"type": "text", "text": prompt}
]}
]
)
raw = resp.choices[0].message.content
return parse_json_response(raw), time.time() - start
except Exception as e:
return {"error": str(e)}, time.time() - start
def detect_subject(client, model: str, provider: str, base64_image: str) -> str:
"""Detect subject type."""
result, _ = analyze_component(
client, model, provider, base64_image,
"subject_detection", WORKING_PROMPTS["subject_detection"]["prompt"]
)
return result.get("subject_type", "OTHER").upper()
def run_batch(
client, model: str, provider: str,
image_paths: List[str],
components: List[str],
target_subject: Optional[str] = None,
) -> List[dict]:
"""Run a batch of images through specified components."""
# Make a local copy of components to avoid any reference issues
components = list(components)
results = []
for img_path in image_paths:
print(f"\n Processing: {Path(img_path).name}")
try:
base64_img, w, h = load_image_as_base64(img_path)
# Detect subject
subject = detect_subject(client, model, provider, base64_img)
print(f" Subject: {subject}")
# Skip if not target subject
if target_subject and subject != target_subject.upper():
print(f" [Skipped - not {target_subject}]")
continue
# Run each component
img_results = {
"image": Path(img_path).name,
"subject": subject,
"components": {}
}
for comp_key in components:
if comp_key not in WORKING_PROMPTS:
continue
print(f" {comp_key}...", end=" ", flush=True)
result, duration = analyze_component(
client, model, provider, base64_img,
comp_key, WORKING_PROMPTS[comp_key]["prompt"]
)
img_results["components"][comp_key] = result
print(f"{duration:.1f}s")
results.append(img_results)
except Exception as e:
print(f" ERROR: {e}")
results.append({"image": Path(img_path).name, "error": str(e)})
return results
def calculate_quality_score(results: List[dict], components: List[str]) -> dict:
"""Calculate quality metrics from batch results."""
scores = {
"total_images": len(results),
"successful": 0,
"parse_errors": 0,
"empty_fields": 0,
"component_scores": {},
}
for comp in components:
scores["component_scores"][comp] = {
"success": 0,
"parse_error": 0,
"empty_prompt_desc": 0,
"has_error_field": 0,
}
for result in results:
if "error" in result:
continue
scores["successful"] += 1
for comp, data in result.get("components", {}).items():
if comp not in scores["component_scores"]:
continue
cs = scores["component_scores"][comp]
if data.get("parse_error"):
cs["parse_error"] += 1
scores["parse_errors"] += 1
else:
cs["success"] += 1
if data.get("error"):
cs["has_error_field"] += 1
# Check for empty prompt_description
if not data.get("prompt_description"):
cs["empty_prompt_desc"] += 1
scores["empty_fields"] += 1
return scores
def display_batch_results(results: List[dict], components: List[str]):
"""Display results in a readable format."""
print("\n" + "="*70)
print("BATCH RESULTS")
print("="*70)
for result in results:
if "error" in result:
print(f"\n[{result['image']}] ERROR: {result['error']}")
continue
print(f"\n[{result['image']}] Subject: {result['subject']}")
for comp, data in result.get("components", {}).items():
print(f"\n {comp}:")
if data.get("parse_error"):
print(f" [PARSE ERROR]")
print(f" Raw: {str(data.get('raw', ''))[:100]}...")
continue
if data.get("error"):
print(f" [ERROR] {data['error']}")
continue
# Show key fields based on component
if comp == "framing":
print(f" Shot: {data.get('shot_type', '?')}, Pose: {data.get('subject_pose', '?')}")
print(f" Color: {data.get('color_mode', '?')}, Fill: {data.get('frame_fill_percent', '?')}%")
elif comp == "ethnicity":
print(f" Ethnicity: {data.get('ethnicity', '?')}")
print(f" Skin: {data.get('skin_tone_base', '?')} ({data.get('skin_tone_undertone', '?')})")
print(f" Gender: {data.get('gender', '?')}, Age: {data.get('age_range', '?')}")
elif comp == "hair":
print(f" Arrangement: {data.get('arrangement', '?')}")
print(f" Color: {data.get('base_color', '?')}, Texture: {data.get('texture', '?')}")
print(f" Crown: {data.get('crown_description', '?')}")
elif comp == "eyes":
print(f" Color: {data.get('color', '?')}, Shape: {data.get('shape', '?')}")
print(f" Gaze: {data.get('gaze', '?')}, Expression: {data.get('expression', '?')}")
elif comp == "clothing":
garments = data.get("garments", [])
if garments:
for g in garments[:2]:
print(f" {g.get('type', '?')}: {g.get('color', '?')} {g.get('material', '?')}")
elif comp == "body_pose":
print(f" Posture: {data.get('posture', '?')}")
print(f" Body angle: {data.get('body_angle', '?')}")
# Show prompt_description if present
desc = data.get("prompt_description", "")
if desc:
print(f" Prompt: {desc[:80]}{'...' if len(desc) > 80 else ''}")
def display_quality_metrics(scores: dict, iteration: int):
"""Display quality metrics."""
print("\n" + "="*70)
print(f"QUALITY METRICS - Iteration {iteration}")
print("="*70)
print(f"\nOverall:")
print(f" Images processed: {scores['successful']}/{scores['total_images']}")
print(f" Parse errors: {scores['parse_errors']}")
print(f" Empty fields: {scores['empty_fields']}")
print(f"\nBy Component:")
for comp, cs in scores["component_scores"].items():
total = cs["success"] + cs["parse_error"]
if total == 0:
continue
success_rate = cs["success"] / total * 100 if total > 0 else 0
print(f" {comp}:")
print(f" Success rate: {success_rate:.0f}% ({cs['success']}/{total})")
if cs["empty_prompt_desc"] > 0:
print(f" Empty prompt_description: {cs['empty_prompt_desc']}")
def edit_prompt_interactive(component_key: str) -> bool:
"""Interactive prompt editor."""
if component_key not in WORKING_PROMPTS:
print(f"Unknown component: {component_key}")
return False
current = WORKING_PROMPTS[component_key]["prompt"]
print(f"\n{'='*70}")
print(f"EDITING: {component_key}")
print(f"{'='*70}")
print(f"\nCurrent prompt ({len(current)} chars):")
print("-"*40)
print(current)
print("-"*40)
print("\nOptions:")
print(" 1. Replace entire prompt (paste new, then type END on new line)")
print(" 2. Append to prompt")
print(" 3. Find/replace text")
print(" 4. Cancel")
choice = input("\nChoice [1-4]: ").strip()
if choice == "1":
print("\nPaste new prompt, then type END on a new line:")
lines = []
while True:
line = input()
if line.strip() == "END":
break
lines.append(line)
new_prompt = "\n".join(lines)
if new_prompt.strip():
WORKING_PROMPTS[component_key]["prompt"] = new_prompt
print(f"[OK] Prompt replaced ({len(new_prompt)} chars)")
return True
elif choice == "2":
print("\nText to append (type END on new line when done):")
lines = []
while True:
line = input()
if line.strip() == "END":
break
lines.append(line)
append_text = "\n".join(lines)
if append_text.strip():
WORKING_PROMPTS[component_key]["prompt"] = current + "\n\n" + append_text
print(f"[OK] Text appended")
return True
elif choice == "3":
find = input("Find text: ")
replace = input("Replace with: ")
if find and find in current:
WORKING_PROMPTS[component_key]["prompt"] = current.replace(find, replace)
print(f"[OK] Replaced '{find}' with '{replace}'")
return True
else:
print(f"Text not found: '{find}'")
print("No changes made")
return False
def save_session(session_dir: str, iteration: int, results: List[dict],
scores: dict, prompts_changed: List[str]):
"""Save session state."""
os.makedirs(session_dir, exist_ok=True)
# Save iteration results
iter_file = os.path.join(session_dir, f"iteration_{iteration:02d}.json")
with open(iter_file, "w", encoding="utf-8") as f:
json.dump({
"iteration": iteration,
"timestamp": datetime.now().isoformat(),
"results": results,
"scores": scores,
"prompts_changed": prompts_changed,
}, f, indent=2, ensure_ascii=False)
# Save current working prompts
prompts_file = os.path.join(session_dir, f"prompts_iter_{iteration:02d}.json")
prompts_export = {}
for key, comp in WORKING_PROMPTS.items():
prompts_export[key] = {"name": comp["name"], "prompt": comp["prompt"]}
with open(prompts_file, "w", encoding="utf-8") as f:
json.dump(prompts_export, f, indent=2, ensure_ascii=False)
print(f"\n[Saved] {iter_file}")
print(f"[Saved] {prompts_file}")
def load_prompts_from_file(filepath: str):
"""Load prompts from a JSON file."""
global WORKING_PROMPTS
with open(filepath, "r", encoding="utf-8") as f:
loaded = json.load(f)
for key, data in loaded.items():
if key in WORKING_PROMPTS:
if isinstance(data, dict):
WORKING_PROMPTS[key]["prompt"] = data.get("prompt", data)
else:
WORKING_PROMPTS[key]["prompt"] = data
print(f"[Loaded] Prompts from {filepath}")
# ============================================================================
# Persistent Tracking
# ============================================================================
def get_tracking_file(folder: str) -> str:
"""Get path to tracking file for a folder."""
folder_hash = abs(hash(os.path.abspath(folder))) % 100000
return os.path.join(OUTPUT_DIR, f"tracking_{folder_hash}.json")
def load_processed_images(folder: str) -> set:
"""Load set of already-processed image filenames."""
tracking_file = get_tracking_file(folder)
if os.path.exists(tracking_file):
try:
with open(tracking_file, "r", encoding="utf-8") as f:
data = json.load(f)
return set(data.get("processed", []))
except:
pass
return set()
def save_processed_images(folder: str, processed: set):
"""Save set of processed image filenames."""
os.makedirs(OUTPUT_DIR, exist_ok=True)
tracking_file = get_tracking_file(folder)
with open(tracking_file, "w", encoding="utf-8") as f:
json.dump({
"folder": os.path.abspath(folder),
"processed": list(processed),
"count": len(processed),
"last_updated": datetime.now().isoformat(),
}, f, indent=2)
def add_to_processed(folder: str, image_names: List[str]):
"""Add image names to processed set."""
processed = load_processed_images(folder)
for name in image_names:
processed.add(name)
save_processed_images(folder, processed)
def get_unprocessed_images(folder: str, all_images: List[str]) -> List[str]:
"""Filter to only unprocessed images."""
processed = load_processed_images(folder)
unprocessed = [img for img in all_images if Path(img).name not in processed]
return unprocessed
def reset_tracking(folder: str):
"""Reset tracking for a folder."""
tracking_file = get_tracking_file(folder)
if os.path.exists(tracking_file):
os.remove(tracking_file)
print(f"[Reset] Tracking cleared for {folder}")
# ============================================================================
# Main Workflow
# ============================================================================
def run_auto_session(
folder: str,
provider: str,
api_key: str,
num_batches: int = 10,
model: Optional[str] = None,
target_subject: str = "WOMAN",
components: Optional[List[str]] = None,
load_prompts: Optional[str] = None,
reset: bool = False,
):
"""Run automatic (non-interactive) batch session."""
print("\n" + "="*70)
print("AUTO BATCH SESSION (Non-Interactive)")
print("="*70)
print(f"\nFolder: {folder}")
print(f"Provider: {provider}")
print(f"Target Subject: {target_subject}")
print(f"Batches to run: {num_batches}")
# Reset tracking if requested
if reset:
reset_tracking(folder)
# Find all images
all_images = find_images(folder)
print(f"Total images in folder: {len(all_images)}")
# Filter to unprocessed only
unprocessed = get_unprocessed_images(folder, all_images)
already_processed = len(all_images) - len(unprocessed)
print(f"Already processed: {already_processed}")
print(f"Remaining unprocessed: {len(unprocessed)}")
if len(unprocessed) == 0:
print("\nAll images have been processed!")
print("Use --reset to start fresh.")
return
# Default components - make a COPY
if components is None:
components = list(SUBJECT_COMPONENTS.get(target_subject.upper(), {}).get("Standard", []))
else:
components = list(components)
print(f"Components: {', '.join(components)}")
# Create client
model = model or get_default_model(provider)
print(f"Model: {model}")
client = create_client(provider, api_key)
# Load prompts if specified
if load_prompts:
load_prompts_from_file(load_prompts)
# Session directory
session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
session_dir = os.path.join(OUTPUT_DIR, f"auto_{target_subject}_{session_id}")
os.makedirs(session_dir, exist_ok=True)
print(f"Session dir: {session_dir}")
# Shuffle unprocessed images
random.shuffle(unprocessed)
image_queue = list(unprocessed)
all_scores = []
all_results = []
print("\n" + "="*70)
print(f"Starting {num_batches} batches...")
print("="*70)
for iteration in range(1, num_batches + 1):
print(f"\n{'#'*70}")
print(f"# BATCH {iteration}/{num_batches}")
print(f"{'#'*70}")
# Check if we have enough images
if len(image_queue) < BATCH_SIZE:
print(f"\nOnly {len(image_queue)} images remaining.")
if len(image_queue) == 0:
print("No more unprocessed images. Stopping.")
break
# Use remaining images
batch = image_queue
image_queue = []
else:
batch = image_queue[:BATCH_SIZE]
image_queue = image_queue[BATCH_SIZE:]
print(f"\nProcessing {len(batch)} images:")
for img in batch:
print(f" - {Path(img).name}")
# Run batch
print(f"\nRunning analysis...")
results = run_batch(client, model, provider, batch, components, target_subject)
all_results.extend(results)
# Track processed images
processed_names = [Path(img).name for img in batch]
add_to_processed(folder, processed_names)
# Calculate metrics
scores = calculate_quality_score(results, components)
display_quality_metrics(scores, iteration)
all_scores.append(scores)
# Save iteration
save_session(session_dir, iteration, results, scores, [])
# Show progress
remaining = len(image_queue)
total_processed = already_processed + (iteration * BATCH_SIZE)
print(f"\n[Progress] {total_processed}/{len(all_images)} images processed, {remaining} remaining in queue")
# Final summary
print("\n" + "="*70)
print("AUTO SESSION COMPLETE")
print("="*70)
print(f"Batches completed: {len(all_scores)}")
print(f"Total images analyzed: {len(all_results)}")
if all_scores:
total_parse_errors = sum(s['parse_errors'] for s in all_scores)
total_empty = sum(s['empty_fields'] for s in all_scores)
print(f"Total parse errors: {total_parse_errors}")
print(f"Total empty fields: {total_empty}")
print(f"\nResults saved in: {session_dir}")
print(f"Tracking file: {get_tracking_file(folder)}")
def run_improvement_session(
folder: str,
provider: str,
api_key: str,
model: Optional[str] = None,
target_subject: str = "WOMAN",
components: Optional[List[str]] = None,
load_prompts: Optional[str] = None,
resume_session: Optional[str] = None,
reset: bool = False,
):
"""Run interactive improvement session."""
print("\n" + "="*70)
print("ITERATIVE PROMPT IMPROVEMENT SESSION")
print("="*70)
print(f"\nFolder: {folder}")
print(f"Provider: {provider}")
print(f"Target Subject: {target_subject}")
# Reset tracking if requested
if reset:
reset_tracking(folder)
# Find all images
all_images = find_images(folder)
print(f"Total images in folder: {len(all_images)}")
# Filter to unprocessed only
unprocessed = get_unprocessed_images(folder, all_images)
already_processed = len(all_images) - len(unprocessed)
print(f"Already processed: {already_processed}")
print(f"Remaining unprocessed: {len(unprocessed)}")
if len(all_images) == 0:
print("No images found!")
return
if len(unprocessed) == 0:
print("\nAll images have been processed!")
print("Use --reset to start fresh, or run without tracking.")
return
# Default components for WOMAN - make a COPY to avoid reference issues
if components is None:
components = list(SUBJECT_COMPONENTS.get(target_subject.upper(), {}).get("Standard", []))
else:
components = list(components) # Ensure we have a copy
print(f"Components: {', '.join(components)}")
# Create client
model = model or get_default_model(provider)
print(f"Model: {model}")
client = create_client(provider, api_key)
# Load prompts if specified
if load_prompts:
load_prompts_from_file(load_prompts)
# Resume from session if specified
if resume_session and os.path.exists(resume_session):
# Load prompts from last iteration
prompt_files = sorted(Path(resume_session).glob("prompts_iter_*.json"))
if prompt_files:
load_prompts_from_file(str(prompt_files[-1]))
print(f"[Resumed] Loaded prompts from {prompt_files[-1].name}")
# Session directory
session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
session_dir = os.path.join(OUTPUT_DIR, f"session_{target_subject}_{session_id}")
os.makedirs(session_dir, exist_ok=True)
print(f"Session dir: {session_dir}")
# Shuffle UNPROCESSED images only
random.shuffle(unprocessed)
image_queue = list(unprocessed)
iteration = 1
all_scores = []
print("\n" + "="*70)
print("Starting improvement loop...")
print("Run batches of 5, review results, improve prompts, repeat.")
print("="*70)
while True:
print(f"\n{'#'*70}")
print(f"# ITERATION {iteration}")
print(f"{'#'*70}")
# Get next batch
if len(image_queue) < BATCH_SIZE:
print(f"\nOnly {len(image_queue)} images remaining.")
if len(image_queue) == 0:
print("All unprocessed images have been analyzed!")
print("Use --reset to start fresh.")
break
# Use remaining images
batch = image_queue
image_queue = []
else:
batch = image_queue[:BATCH_SIZE]
image_queue = image_queue[BATCH_SIZE:]
print(f"\nBatch {iteration}: {len(batch)} images")
for img in batch:
print(f" - {Path(img).name}")
# Run batch
print(f"\nRunning analysis...")
results = run_batch(client, model, provider, batch, components, target_subject)
# Track processed images
processed_names = [Path(img).name for img in batch]
add_to_processed(folder, processed_names)
# Display results
display_batch_results(results, components)
# Calculate and display metrics
scores = calculate_quality_score(results, components)
display_quality_metrics(scores, iteration)
all_scores.append(scores)
# Show trend if multiple iterations
if len(all_scores) > 1:
print(f"\n{'='*70}")
print("TREND (parse errors / empty fields):")
for i, s in enumerate(all_scores, 1):
print(f" Iter {i}: {s['parse_errors']} parse errors, {s['empty_fields']} empty fields")
# Check for diminishing returns
if len(all_scores) >= 3:
recent = all_scores[-3:]
if all(s['parse_errors'] == 0 and s['empty_fields'] <= 2 for s in recent):
print("\n[!] Quality appears stable. Consider stopping.")
# Save this iteration
save_session(session_dir, iteration, results, scores, [])
# Interactive menu
print(f"\n{'='*70}")
print("OPTIONS:")
print(" [1-9] Edit component prompt (1=framing, 2=ethnicity, 3=hair, etc.)")
print(" [c] Continue to next batch")
print(" [r] Re-run same batch with current prompts")
print(" [l] List components")
print(" [v] View a component's current prompt")
print(" [s] Save & show final prompts")
print(" [q] Quit")
while True:
choice = input("\nChoice: ").strip().lower()
if choice == "c":
iteration += 1
break
elif choice == "r":
print("\nRe-running same batch...")
image_queue = batch + image_queue # Put batch back
break
elif choice == "l":
print("\nComponents:")
for i, comp in enumerate(components, 1):
print(f" {i}. {comp}")
elif choice == "v":
comp = input("Component name: ").strip()
if comp in WORKING_PROMPTS:
print(f"\n{WORKING_PROMPTS[comp]['prompt']}")
else:
print(f"Unknown: {comp}")
elif choice == "s":
# Save final prompts
final_file = os.path.join(session_dir, "FINAL_PROMPTS.json")
prompts_export = {}
for key, comp in WORKING_PROMPTS.items():
prompts_export[key] = {"name": comp["name"], "prompt": comp["prompt"]}
with open(final_file, "w", encoding="utf-8") as f:
json.dump(prompts_export, f, indent=2, ensure_ascii=False)
print(f"\n[Saved] {final_file}")
print("\nTo use these prompts, copy them to sid_zimage_prompt_generator_advanced_v2.py")
elif choice == "q":
print("\nSession ended.")
print(f"Results saved in: {session_dir}")
return
elif choice.isdigit():
idx = int(choice) - 1
if 0 <= idx < len(components):
comp_key = components[idx]
if edit_prompt_interactive(comp_key):
print(f"\nPrompt for '{comp_key}' updated. Use 'r' to re-run batch.")
else:
print(f"Invalid number. Range: 1-{len(components)}")
else:
# Try as component name
if choice in WORKING_PROMPTS:
if edit_prompt_interactive(choice):
print(f"\nPrompt updated. Use 'r' to re-run batch.")
else:
print("Invalid choice")
def main():
parser = argparse.ArgumentParser(description="Iterative prompt improvement")
parser.add_argument("--folder", "-f", required=True, help="Folder with test images")
parser.add_argument("--provider", "-p", default="anthropic", choices=["anthropic", "openai", "grok"])
parser.add_argument("--api-key", "-k", help="API key (or use env var)")
parser.add_argument("--model", "-m", help="Model name")
parser.add_argument("--subject", "-s", default="WOMAN", help="Target subject type")
parser.add_argument("--components", "-c", nargs="+", help="Specific components to test")
parser.add_argument("--load-prompts", help="Load prompts from JSON file")
# New options
parser.add_argument("--auto", type=int, metavar="N", help="Run N batches automatically (non-interactive)")
parser.add_argument("--reset", action="store_true", help="Reset tracking, re-process all images")
parser.add_argument("--resume", help="Resume from previous session directory")
parser.add_argument("--status", action="store_true", help="Show tracking status and exit")
args = parser.parse_args()
# Status check
if args.status:
all_images = find_images(args.folder)
processed = load_processed_images(args.folder)
print(f"\nFolder: {args.folder}")
print(f"Total images: {len(all_images)}")
print(f"Processed: {len(processed)}")
print(f"Remaining: {len(all_images) - len(processed)}")
print(f"Tracking file: {get_tracking_file(args.folder)}")
return
# Get API key
api_key = args.api_key
if not api_key:
env_vars = {"anthropic": "ANTHROPIC_API_KEY", "openai": "OPENAI_API_KEY", "grok": "GROK_API_KEY"}
api_key = os.environ.get(env_vars.get(args.provider, ""))
if not api_key:
parser.error("API key required")
# Auto mode vs interactive mode
if args.auto:
run_auto_session(
folder=args.folder,
provider=args.provider,
api_key=api_key,
num_batches=args.auto,
model=args.model,
target_subject=args.subject,
components=args.components,
load_prompts=args.load_prompts,
reset=args.reset,
)
else:
run_improvement_session(
folder=args.folder,
provider=args.provider,
api_key=api_key,
model=args.model,
target_subject=args.subject,
components=args.components,
load_prompts=args.load_prompts,
resume_session=args.resume,
reset=args.reset,
)
if __name__ == "__main__":
main()