-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathictc_cluster.py
More file actions
1490 lines (1291 loc) · 61.8 KB
/
ictc_cluster.py
File metadata and controls
1490 lines (1291 loc) · 61.8 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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
ICTC VLM Clustering — Production Script for GPU Cluster (Unified Model)
=========================================================================
Implements "Image Clustering Conditioned on Text Criteria" at scale.
Based on Experiment 3B findings: a single Qwen 3.5 model handles BOTH
vision captioning AND text reasoning, producing better clusters than a
separate VLM + LLM pipeline (higher coherence, better balance, zero
unclassified ads).
Pipeline (unified mode — default):
Step 0 — Discover images from dataset structure
Step 1 — Qwen3.5-27B captioning : image -> JSON {category, brand, text, description}
Step 2a — Same model (text-only) : extract 2-4 word marketing hook per ad
Step 2b — Same model (text-only) : synthesise K cluster definitions from top hooks
Step 3 — Same model (text-only) : assign each ad -> best cluster
In unified mode the model stays loaded for all steps — no expensive
unload/reload between VLM and LLM phases. To use a separate LLM
(e.g. Llama), pass --llm_model explicitly.
Designed for:
* Any number of GPUs (set --num_gpus N, or fine-tune --vlm_tp / --llm_tp separately)
* 200 k+ images (batched inference, multi-day runs)
* SSH-resilient (all output to rotating log files, atomic JSON saves)
* Full resume (every step checkpoints; re-run to continue after crash)
* Any quantization (--quantization awq/gptq/fp8 for fitting larger models)
Quick start (unified — recommended):
python ictc_cluster.py \\
--ads_dir /data/dataset/ads \\
--output_dir /data/results/run1 \\
--num_clusters 5
Separate VLM + LLM (legacy):
python ictc_cluster.py \\
--ads_dir /data/ads --output_dir /data/out \\
--llm_model meta-llama/Llama-3.1-8B-Instruct
Smoke test (100 images):
python ictc_cluster.py --ads_dir ... --output_dir ... --max_images 100 --verbose
GPU memory guide (bfloat16, no quantization):
Model VRAM Recommended config
─────────────────────────────────────────────────────────────
Qwen3.5-27B ~54 GB --vlm_tp 2 (two A100-80GB) <- default (unified)
Qwen3.5-27B-FP8 ~27 GB --vlm_tp 1 --quantization fp8
Qwen2.5-VL-7B ~14 GB --vlm_tp 1 (single A100/L40)
Qwen2.5-VL-32B ~64 GB --vlm_tp 2
Qwen3-VL-30B ~60 GB --vlm_tp 2
Tip: --num_gpus 4 sets vlm_tp=4 automatically.
Override individually with --vlm_tp / --llm_tp.
"""
import argparse
import gc
import json
import logging
import os
import re
import shutil
import signal
import sys
import time
from collections import Counter
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, List, Optional
# ---------------------------------------------------------------------------
# PROMPTS (identical to the notebook so results are comparable)
# ---------------------------------------------------------------------------
STEP1_PROMPT = """Analyze this image for an ad database. Return a valid JSON object.
### STEP 1: CLASSIFY
Determine the image category:
- "ADVERTISEMENT": A clear, valid commercial ad.
- "UI_ONLY": Social media feed, settings menu, or app interface with NO specific ad.
- "BROKEN": Black screen, loading spinner, error message, or system lock screen.
### STEP 2: DESCRIBE
- If "ADVERTISEMENT": Extract brand_name, main_text, and a visual_summary.
- If "UI_ONLY" or "BROKEN": Leave description fields null or empty.
### OUTPUT FORMAT
{
"category": "ADVERTISEMENT" | "UI_ONLY" | "BROKEN",
"brand_name": "String or null",
"main_text": "String or null",
"visual_summary": "String or null"
}"""
STEP2A_SYSTEM = (
'Analyze this ad description and identify the core "marketing hook" or psychological '
"mechanism used. Do NOT categorize it yet. Just describe the specific appeal in 2-4 words.\n"
'Examples: "scarcity urgency", "social proof testimonial", "luxury status signaling", '
'"problem-solution utility".\nOutput ONLY the hook phrase.'
)
STEP2B_TEMPLATE = """You are an expert analyst specializing in {criterion}.
I have analyzed a dataset of items and extracted the following specific patterns/hooks:
{hooks_json}
TASK:
Group these patterns into exactly {k} distinct, high-level {criterion} clusters.
The categories must be mutually exclusive and collectively exhaustive for this dataset.
OUTPUT JSON FORMAT ONLY:
{{
"clusters": [
{{
"name": "CATEGORY_NAME (2-3 words)",
"definition": "A 1-sentence definition of what this cluster entails.",
"keywords": ["list", "of", "representative", "hooks"]
}}
]
}}"""
STEP3_SYSTEM_TEMPLATE = """You are classifying ads into specific strategies.
AVAILABLE STRATEGIES:
{clusters_context}
TASK:
Assign the advertisement below to the SINGLE best fitting strategy from the list above.
OUTPUT FORMAT:
Return ONLY the exact strategy name from the list. Nothing else."""
# ---------------------------------------------------------------------------
# RESPONSE CLEANING (Qwen 3.5 may produce <think> blocks and code fences)
# ---------------------------------------------------------------------------
def _strip_thinking(text: str) -> str:
"""Remove <think>...</think> blocks and markdown code fences from model output."""
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
text = re.sub(r"```(?:json)?\s*\n?", "", text).strip()
return text.strip("`").strip()
# ---------------------------------------------------------------------------
# LOGGING
# ---------------------------------------------------------------------------
def setup_logging(log_dir: Path, verbose: bool = False) -> logging.Logger:
log_dir.mkdir(parents=True, exist_ok=True)
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = log_dir / f"ictc_{ts}.log"
level = logging.DEBUG if verbose else logging.INFO
fmt = "%(asctime)s [%(levelname)s] %(message)s"
handlers: list = [logging.StreamHandler(sys.stdout), logging.FileHandler(log_file)]
logging.basicConfig(level=level, format=fmt, handlers=handlers)
log = logging.getLogger("ictc")
log.info(f"Log file: {log_file}")
return log
# ---------------------------------------------------------------------------
# CHECKPOINT MANAGER
# ---------------------------------------------------------------------------
class CheckpointManager:
"""Atomic JSON save/load. Writes to .tmp then renames to avoid corruption."""
def __init__(self, output_dir: Path):
self.d = output_dir
self.d.mkdir(parents=True, exist_ok=True)
def save(self, data: dict, filename: str) -> None:
path = self.d / filename
tmp = path.with_suffix(".tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
f.flush()
os.fsync(f.fileno())
# os.replace is atomic on POSIX and handles pre-existing target on Windows
os.replace(tmp, path)
def load(self, filename: str) -> Optional[dict]:
path = self.d / filename
if path.exists():
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
return None
def exists(self, filename: str) -> bool:
return (self.d / filename).exists()
# ---------------------------------------------------------------------------
# DATA DISCOVERY
# ---------------------------------------------------------------------------
def discover_images(
ads_dir: Path,
max_images: Optional[int] = None,
log: Optional[logging.Logger] = None,
) -> Dict[str, dict]:
"""
Walk ads_dir looking for the notebook dataset structure:
ads/<uuid>/full_data.json + *.jpg
Falls back to treating every .jpg/.png directly in ads_dir as an image,
using the file stem as the observation_id.
Returns {observation_id: {image_path, platform, ad_format, timestamp}}
"""
log = log or logging.getLogger("ictc")
mapping: Dict[str, dict] = {}
subdirs = sorted(d for d in ads_dir.iterdir() if d.is_dir())
if subdirs:
log.info(f"Found {len(subdirs)} subdirectories — using full_data.json structure")
for ad_dir in subdirs:
fd = ad_dir / "full_data.json"
jpg_files = sorted(ad_dir.glob("*.jpg"))
if not jpg_files:
jpg_files = sorted(ad_dir.glob("*.png"))
if not jpg_files:
continue
obs_id: str
platform = "UNKNOWN"
ad_format = "UNKNOWN"
timestamp = ""
if fd.exists():
try:
with open(fd, "r", encoding="utf-8") as fh:
data = json.load(fh)
obs_id = data.get("observation_id") or ad_dir.name
obs = data.get("observation", {})
platform = obs.get("platform", "UNKNOWN")
ad_format = obs.get("ad_format", "UNKNOWN")
timestamp = data.get("timestamp", "")
except Exception:
obs_id = ad_dir.name
else:
obs_id = ad_dir.name
mapping[obs_id] = {
"image_path": str(jpg_files[0]),
"platform": platform,
"ad_format": ad_format,
"timestamp": timestamp,
}
else:
log.info("No subdirectories — using flat image directory")
all_imgs = sorted(ads_dir.glob("*.jpg")) + sorted(ads_dir.glob("*.png"))
for p in all_imgs:
mapping[p.stem] = {
"image_path": str(p),
"platform": "UNKNOWN",
"ad_format": "UNKNOWN",
"timestamp": "",
}
if max_images:
keys = list(mapping.keys())[:max_images]
mapping = {k: mapping[k] for k in keys}
log.info(f"Capped at {max_images} images (--max_images)")
log.info(f"Total images to process: {len(mapping)}")
return mapping
# ---------------------------------------------------------------------------
# VLM PROCESSOR (Step 1 — unified Qwen3.5 via vLLM, or any HF VLM)
# ---------------------------------------------------------------------------
class VLMProcessor:
"""
Wraps a vLLM-served vision-language model for batched image captioning.
Supports any VLM available on HuggingFace (Qwen3-VL, Qwen2.5-VL, LLaVA, etc.)
Falls back to HuggingFace transformers if vLLM is unavailable.
"""
def __init__(
self,
model_name: str,
tp: int = 2,
max_model_len: int = 4096,
max_image_tokens: int = 1280,
gpu_util: float = 0.90,
dtype: str = "bfloat16",
quantization: Optional[str] = None,
enforce_eager: bool = False,
swap_space: int = 4,
max_tokens: int = 300,
seed: int = 42,
log: Optional[logging.Logger] = None,
):
self.log = log or logging.getLogger("ictc")
self.max_tokens = max_tokens
self.model_name = model_name
self.dtype = dtype
q_tag = f" quantization={quantization}" if quantization else ""
self.log.info(f"Loading VLM: {model_name} (tp={tp}, dtype={dtype}{q_tag})")
try:
from vllm import LLM, SamplingParams
from transformers import AutoProcessor
self.sampling_params = SamplingParams(
temperature=0.0,
top_p=0.001,
max_tokens=max_tokens,
repetition_penalty=1.1,
seed=seed,
)
lm_kwargs: dict = dict(
model=model_name,
tensor_parallel_size=tp,
max_model_len=max_model_len,
gpu_memory_utilization=gpu_util,
limit_mm_per_prompt={"image": 1},
dtype=dtype,
enforce_eager=enforce_eager,
swap_space=swap_space,
seed=seed,
trust_remote_code=True,
)
if quantization:
lm_kwargs["quantization"] = quantization
# Qwen3-VL / Qwen2.5-VL: allow larger image token budget
lm_kwargs["mm_processor_kwargs"] = {"max_pixels": max_image_tokens * 28 * 28}
self.llm = LLM(**lm_kwargs)
# Processor only used for chat template formatting, not tokenisation
self.processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
self.backend = "vllm"
self.log.info("VLM ready (vLLM backend)")
except Exception as exc:
self.log.warning(f"vLLM load failed ({exc}), falling back to HuggingFace transformers")
self._load_hf(model_name, dtype)
def _load_hf(self, model_name: str, dtype: str = "bfloat16") -> None:
"""
HuggingFace transformers fallback.
Uses device_map='auto' which spreads across all GPUs visible via
CUDA_VISIBLE_DEVICES — set that env var before launch to control placement.
"""
import torch
from transformers import AutoModelForVision2Seq, AutoProcessor
torch_dtype = {"bfloat16": torch.bfloat16, "float16": torch.float16}.get(
dtype, torch.bfloat16
)
self.processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
self.model = AutoModelForVision2Seq.from_pretrained(
model_name,
torch_dtype=torch_dtype,
device_map="auto", # spreads across all CUDA_VISIBLE_DEVICES
trust_remote_code=True,
)
self.model.eval()
self.backend = "hf"
self.log.info("VLM ready (HuggingFace transformers fallback)")
def _fmt_prompt(self) -> str:
"""Return a chat-template-formatted string with an image placeholder."""
messages = [
{
"role": "user",
"content": [
{"type": "image"}, # placeholder; vLLM substitutes multi_modal_data
{"type": "text", "text": STEP1_PROMPT},
],
}
]
return self.processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
def process_batch(self, image_paths: List[Path]) -> List[dict]:
"""Caption a batch of images. Returns list aligned 1:1 with image_paths."""
from PIL import Image as PILImage
images: list = []
valid_indices: list = []
for i, p in enumerate(image_paths):
try:
with PILImage.open(p) as raw:
images.append(raw.convert("RGB"))
valid_indices.append(i)
except Exception as exc:
self.log.warning(f"Cannot open {p}: {exc}")
# Build result list aligned with original image_paths (broken for failed opens)
results = [_broken_entry(p) for p in image_paths]
if not images:
return results
valid_paths = [image_paths[i] for i in valid_indices]
if self.backend == "vllm":
valid_results = self._process_vllm(images, valid_paths)
else:
valid_results = self._process_hf(images, valid_paths)
for idx, result in zip(valid_indices, valid_results):
results[idx] = result
return results
def _process_vllm(self, images: list, paths: list) -> List[dict]:
prompt_text = self._fmt_prompt()
inputs = [
{"prompt": prompt_text, "multi_modal_data": {"image": img}}
for img in images
]
try:
outputs = self.llm.generate(inputs, self.sampling_params)
return [self._parse_response(o.outputs[0].text, p) for o, p in zip(outputs, paths)]
except Exception as exc:
self.log.error(f"vLLM generate error: {exc}")
return [_broken_entry(p) for p in paths]
def _process_hf(self, images: list, paths: list) -> List[dict]:
"""Process one image at a time (HF Qwen batching with variable-size images is fragile)."""
import torch
results = []
for img, p in zip(images, paths):
try:
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": img},
{"type": "text", "text": STEP1_PROMPT},
],
}
]
text = self.processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
device = next(self.model.parameters()).device
inputs = self.processor(text=[text], images=[img], return_tensors="pt").to(device)
with torch.no_grad():
gen_ids = self.model.generate(
**inputs, max_new_tokens=self.max_tokens, do_sample=False
)
generated = gen_ids[0][inputs["input_ids"].shape[-1]:]
raw = self.processor.decode(generated, skip_special_tokens=True)
results.append(self._parse_response(raw, p))
except Exception as exc:
self.log.warning(f"HF VLM error on {p.name}: {exc}")
results.append(_broken_entry(p))
return results
@staticmethod
def _parse_response(raw: str, path: Path) -> dict:
"""Extract JSON from model output, with keyword-based fallback."""
raw = _strip_thinking(raw)
try:
m = re.search(r"\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}", raw, re.DOTALL)
if m:
return json.loads(m.group())
except (json.JSONDecodeError, Exception):
pass
# Heuristic fallback when JSON extraction fails — use path.name in summary for traceability
category = "ADVERTISEMENT"
low = raw.lower()
if any(w in low for w in ["ui only", "ui_only", "interface", "menu", "settings"]):
category = "UI_ONLY"
elif any(w in low for w in ["broken", "black screen", "error", "loading", "spinner"]):
category = "BROKEN"
return {
"category": category,
"brand_name": None,
"main_text": raw[:200] if raw else None,
"visual_summary": f"[parse_fallback:{path.name}] {raw[:160]}" if raw else None,
}
# ── Text-only generation (unified mode: reuse VLM for Steps 2a/2b/3) ──
def _format_text_prompt(self, system: str, user: str) -> str:
"""Format a text-only chat prompt using the VLM's processor."""
messages = [
{"role": "system", "content": system},
{"role": "user", "content": user},
]
return self.processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
def generate_batch(
self,
system_prompt: str,
user_texts: List[str],
temperature: float = 0.3,
max_tokens: int = 100,
) -> List[str]:
"""Batch text-only generation — same interface as LLMProcessor."""
from vllm import SamplingParams
if self.backend != "vllm":
raise RuntimeError("Text generation in unified mode requires vLLM backend")
prompts = [self._format_text_prompt(system_prompt, u) for u in user_texts]
sp = SamplingParams(temperature=temperature, max_tokens=max_tokens)
outputs = self.llm.generate(prompts, sp)
return [_strip_thinking(o.outputs[0].text.strip()) for o in outputs]
def generate_single(self, system: str, user: str, max_tokens: int = 800) -> str:
"""Single text-only call — used for Step 2b cluster synthesis."""
results = self.generate_batch(system, [user], temperature=0.3, max_tokens=max_tokens)
return results[0] if results else ""
def unload(self) -> None:
"""Free GPU memory."""
import torch
import torch.distributed as dist
if getattr(self, "backend", None) == "vllm":
if hasattr(self, "llm"):
del self.llm
else:
if hasattr(self, "model"):
del self.model
if hasattr(self, "processor"):
del self.processor
gc.collect()
torch.cuda.empty_cache()
if dist.is_initialized():
try:
dist.destroy_process_group()
except Exception:
pass
time.sleep(3) # let CUDA actually release memory
# ---------------------------------------------------------------------------
# LLM PROCESSOR (Steps 2a / 2b / 3 — separate LLM via vLLM, legacy mode)
# ---------------------------------------------------------------------------
class LLMProcessor:
"""
Wraps vLLM for high-throughput text-only inference.
Supports any causal LLM on HuggingFace (Llama, Mistral, Qwen, etc.)
All prompts in a step are submitted together in a single generate() call.
"""
def __init__(
self,
model_name: str,
tp: int = 1,
max_model_len: int = 4096,
gpu_util: float = 0.90,
dtype: str = "bfloat16",
quantization: Optional[str] = None,
enforce_eager: bool = False,
swap_space: int = 4,
seed: int = 42,
log: Optional[logging.Logger] = None,
):
self.log = log or logging.getLogger("ictc")
q_tag = f" quantization={quantization}" if quantization else ""
self.log.info(f"Loading LLM: {model_name} (tp={tp}, dtype={dtype}{q_tag})")
from vllm import LLM
from transformers import AutoTokenizer
lm_kwargs: dict = dict(
model=model_name,
tensor_parallel_size=tp,
max_model_len=max_model_len,
gpu_memory_utilization=gpu_util,
dtype=dtype,
enforce_eager=enforce_eager,
swap_space=swap_space,
seed=seed,
trust_remote_code=True,
)
if quantization:
lm_kwargs["quantization"] = quantization
self.llm = LLM(**lm_kwargs)
self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
self.log.info("LLM ready")
def _format_prompt(self, system: str, user: str) -> str:
msgs = [{"role": "system", "content": system}, {"role": "user", "content": user}]
return self.tokenizer.apply_chat_template(
msgs, tokenize=False, add_generation_prompt=True
)
def generate_batch(
self,
system_prompt: str,
user_texts: List[str],
temperature: float = 0.3,
max_tokens: int = 100,
) -> List[str]:
"""Batch-generate for many user inputs sharing the same system prompt."""
from vllm import SamplingParams
prompts = [self._format_prompt(system_prompt, u) for u in user_texts]
sp = SamplingParams(temperature=temperature, max_tokens=max_tokens)
outputs = self.llm.generate(prompts, sp)
return [o.outputs[0].text.strip() for o in outputs]
def generate_single(self, system: str, user: str, max_tokens: int = 800) -> str:
"""Single call — used for Step 2b cluster synthesis."""
results = self.generate_batch(system, [user], temperature=0.3, max_tokens=max_tokens)
return results[0] if results else ""
def unload(self) -> None:
import torch
import torch.distributed as dist
if hasattr(self, "llm"):
del self.llm
if hasattr(self, "tokenizer"):
del self.tokenizer
gc.collect()
torch.cuda.empty_cache()
if dist.is_initialized():
try:
dist.destroy_process_group()
except Exception:
pass
time.sleep(3)
# ---------------------------------------------------------------------------
# HELPERS
# ---------------------------------------------------------------------------
def _broken_entry(path: Path) -> dict:
return {
"category": "BROKEN",
"brand_name": None,
"main_text": None,
"visual_summary": f"Failed to process {path.name}",
}
def _safe_copy(src: Path, dst: Path) -> None:
if not dst.exists():
try:
shutil.copy2(src, dst)
except Exception:
pass
# ---------------------------------------------------------------------------
# PIPELINE STEPS
# ---------------------------------------------------------------------------
def run_step1(
image_mapping: Dict[str, dict],
ckpt: CheckpointManager,
vlm: VLMProcessor,
batch_size: int,
checkpoint_interval: int,
log: logging.Logger,
) -> Dict[str, dict]:
"""
VLM-caption every image.
Output: step1_captions.json (same schema as the notebook)
"""
CAPTIONS_FILE = "step1_captions.json"
UI_FILE = "ui_only_images.json"
BROKEN_FILE = "broken_images.json"
captions: Dict[str, dict] = ckpt.load(CAPTIONS_FILE) or {}
ui_only: Dict[str, dict] = ckpt.load(UI_FILE) or {}
broken_imgs: Dict[str, dict] = ckpt.load(BROKEN_FILE) or {}
total_done = len(captions) + len(ui_only) + len(broken_imgs)
if total_done >= len(image_mapping):
log.info(
f"Step 1 already complete — "
f"valid={len(captions)} ui={len(ui_only)} broken={len(broken_imgs)}"
)
return captions
done_ids = set(captions) | set(ui_only) | set(broken_imgs)
todo = [(k, v) for k, v in image_mapping.items() if k not in done_ids]
log.info(f"Step 1: {len(done_ids)}/{len(image_mapping)} done, {len(todo)} remaining")
base_out = ckpt.d
for sub in ["categorized_images/valid_ads", "categorized_images/ui_only", "categorized_images/broken"]:
(base_out / sub).mkdir(parents=True, exist_ok=True)
t0 = time.time()
processed = len(done_ids)
last_ckpt = processed
total = len(image_mapping)
for batch_start in range(0, len(todo), batch_size):
chunk = todo[batch_start: batch_start + batch_size]
obs_ids = [item[0] for item in chunk]
img_paths = [Path(item[1]["image_path"]) for item in chunk]
meta_list = [item[1] for item in chunk]
parsed_list = vlm.process_batch(img_paths)
for obs_id, meta, src_path, parsed in zip(obs_ids, meta_list, img_paths, parsed_list):
category = parsed.get("category", "BROKEN")
base_entry = {
"image_file": str(src_path),
"platform": meta["platform"],
"ad_format": meta["ad_format"],
}
if category == "ADVERTISEMENT":
dest = base_out / "categorized_images" / "valid_ads" / f"{obs_id}.jpg"
_safe_copy(src_path, dest)
captions[obs_id] = {
**base_entry,
"status": "valid",
"brand": parsed.get("brand_name"),
"text": parsed.get("main_text"),
"description": parsed.get("visual_summary"),
"categorized_path": str(dest),
}
elif category == "UI_ONLY":
dest = base_out / "categorized_images" / "ui_only" / f"{obs_id}.jpg"
_safe_copy(src_path, dest)
ui_only[obs_id] = {**base_entry, "category": "UI_ONLY", "raw_response": parsed}
else:
dest = base_out / "categorized_images" / "broken" / f"{obs_id}.jpg"
_safe_copy(src_path, dest)
broken_imgs[obs_id] = {**base_entry, "category": "BROKEN", "raw_response": parsed}
processed += len(chunk)
if processed - last_ckpt >= checkpoint_interval:
last_ckpt = processed
ckpt.save(captions, CAPTIONS_FILE)
ckpt.save(ui_only, UI_FILE)
ckpt.save(broken_imgs, BROKEN_FILE)
elapsed = time.time() - t0
rate = processed / elapsed if elapsed > 0 else 1
eta_secs = int((total - processed) / rate) if rate > 0 else 0
log.info(
f" Step 1 — {processed}/{total} ({100*processed/total:.1f}%) "
f"rate={rate:.2f} img/s ETA={timedelta(seconds=eta_secs)}"
)
ckpt.save(captions, CAPTIONS_FILE)
ckpt.save(ui_only, UI_FILE)
ckpt.save(broken_imgs, BROKEN_FILE)
log.info(
f"Step 1 complete — valid={len(captions)} ui={len(ui_only)} broken={len(broken_imgs)}"
)
return captions
def run_step2a(
captions: Dict[str, dict],
ckpt: CheckpointManager,
llm, # LLMProcessor or VLMProcessor (unified mode) — duck-typed via generate_batch()
batch_size: int,
checkpoint_interval: int,
log: logging.Logger,
) -> Dict[str, dict]:
"""Extract a 2-4 word marketing hook from each caption."""
HOOKS_FILE = "step2a_hooks.json"
hooks: Dict[str, dict] = ckpt.load(HOOKS_FILE) or {}
if len(hooks) >= len(captions):
log.info(f"Step 2a already complete ({len(hooks)} hooks)")
return hooks
todo = [(k, v) for k, v in captions.items() if k not in hooks]
log.info(f"Step 2a: {len(hooks)}/{len(captions)} done, {len(todo)} remaining")
processed = len(hooks)
last_ckpt = processed
for batch_start in range(0, len(todo), batch_size):
chunk = todo[batch_start: batch_start + batch_size]
obs_ids = [item[0] for item in chunk]
user_texts = [
(item[1].get("text") or item[1].get("description") or "").strip()
for item in chunk
]
# Items with no text get a placeholder so they aren't retried on resume
for obs_id, txt in zip(obs_ids, user_texts):
if not txt and obs_id not in hooks:
hooks[obs_id] = {"hook": "no description", "description_snippet": ""}
valid_pairs = [(i, t) for i, t in zip(obs_ids, user_texts) if t]
if not valid_pairs:
processed += len(chunk)
continue
v_ids, v_texts = zip(*valid_pairs)
raw_hooks = llm.generate_batch(
STEP2A_SYSTEM, list(v_texts), temperature=0.3, max_tokens=20
)
for obs_id, hook_raw, txt in zip(v_ids, raw_hooks, v_texts):
clean = _strip_thinking(hook_raw).lower().strip("\"'").split("\n")[0]
clean = re.sub(r"^(marketing hook:|hook:)\s*", "", clean).strip()
hooks[obs_id] = {"hook": clean, "description_snippet": txt[:100]}
processed += len(chunk)
if processed - last_ckpt >= checkpoint_interval:
last_ckpt = processed
ckpt.save(hooks, HOOKS_FILE)
log.info(f" Step 2a — {processed}/{len(captions)}")
ckpt.save(hooks, HOOKS_FILE)
top5 = Counter(d["hook"] for d in hooks.values()).most_common(5)
log.info(f"Step 2a complete — {len(hooks)} hooks. Top 5: {top5}")
return hooks
def run_step2b(
hooks: Dict[str, dict],
ckpt: CheckpointManager,
llm, # LLMProcessor or VLMProcessor (unified mode) — duck-typed via generate_single()
num_clusters: int,
top_n_hooks: int,
criterion: str,
log: logging.Logger,
) -> Dict:
"""Synthesise K cluster definitions from the top hooks — single LLM call."""
CLUSTERS_FILE = "step2b_dynamic_clusters.json"
META_FILE = "step2b_metadata.json"
existing = ckpt.load(CLUSTERS_FILE)
meta = ckpt.load(META_FILE)
if (
existing and meta
and meta.get("num_hooks") == len(hooks)
and meta.get("num_clusters") == num_clusters
and meta.get("criterion") == criterion
):
log.info(f"Step 2b already complete ({len(existing.get('clusters', []))} clusters)")
for c in existing.get("clusters", []):
log.info(f" [{c['name']}]: {c['definition']}")
return existing
all_hooks = [d["hook"] for d in hooks.values()]
top_hooks = [h for h, _ in Counter(all_hooks).most_common(top_n_hooks)]
log.info(f"Step 2b: {len(top_hooks)} top hooks -> {num_clusters} clusters [criterion: {criterion}]")
user_msg = "Create the clusters now.\n\n" + STEP2B_TEMPLATE.format(
hooks_json=json.dumps(top_hooks, indent=2), k=num_clusters, criterion=criterion
)
response = llm.generate_single(
f"You are an expert analyst specializing in {criterion}.", user_msg, max_tokens=800
)
response = _strip_thinking(response)
try:
m = re.search(r"\{.*\}", response, re.DOTALL)
cluster_def = json.loads(m.group()) if m else {}
assert "clusters" in cluster_def and len(cluster_def["clusters"]) >= 1
except Exception as exc:
log.error(f"Step 2b JSON parse failed ({exc}):\n{response[:600]}")
cluster_def = {
"clusters": [
{
"name": f"Cluster_{i+1}",
"definition": f"Auto-generated cluster {i+1}.",
"keywords": [],
}
for i in range(num_clusters)
]
}
ckpt.save(cluster_def, CLUSTERS_FILE)
ckpt.save({"num_hooks": len(hooks), "num_clusters": num_clusters, "criterion": criterion}, META_FILE)
for c in cluster_def.get("clusters", []):
log.info(f" [{c['name']}]: {c['definition']}")
return cluster_def
def run_step3(
captions: Dict[str, dict],
cluster_def: Dict,
ckpt: CheckpointManager,
llm, # LLMProcessor or VLMProcessor (unified mode) — duck-typed via generate_batch()
batch_size: int,
checkpoint_interval: int,
log: logging.Logger,
) -> Dict[str, dict]:
"""Assign each ad to its best-fitting cluster."""
ASSIGN_FILE = "step3_final_assignment.json"
META_FILE = "step3_metadata.json"
assignments: Dict[str, dict] = ckpt.load(ASSIGN_FILE) or {}
meta = ckpt.load(META_FILE)
current_cluster_names = sorted(c["name"] for c in cluster_def.get("clusters", []))
cached_cluster_names = sorted(meta.get("cluster_names", [])) if meta else []
if current_cluster_names != cached_cluster_names and assignments:
log.info(
"Cluster definitions changed (criterion or K updated) — "
"re-running Step 3 from scratch with new clusters"
)
assignments = {}
if len(assignments) >= len(captions):
log.info(f"Step 3 already complete ({len(assignments)} assignments)")
dist = Counter(v["cluster"] for v in assignments.values())
for name, count in dist.most_common():
log.info(f" {name}: {count}")
return assignments
clusters = cluster_def.get("clusters", [])
valid_names = [c["name"] for c in clusters]
ctx = "\n".join(f"- {c['name']}: {c['definition']}" for c in clusters)
system_prompt = STEP3_SYSTEM_TEMPLATE.format(clusters_context=ctx)
todo = [(k, v) for k, v in captions.items() if k not in assignments]
log.info(f"Step 3: {len(assignments)}/{len(captions)} done, {len(todo)} remaining")
processed = len(assignments)
last_ckpt = processed
for batch_start in range(0, len(todo), batch_size):
chunk = todo[batch_start: batch_start + batch_size]
obs_ids = [item[0] for item in chunk]
user_texts = [
(item[1].get("text") or item[1].get("description") or "").strip()
for item in chunk
]
# Items with no text get "Unclassified" so they aren't retried on resume
for obs_id, txt in zip(obs_ids, user_texts):
if not txt and obs_id not in assignments:
assignments[obs_id] = {"cluster": "Unclassified", "original_description": ""}
valid_pairs = [(i, t) for i, t in zip(obs_ids, user_texts) if t]
if not valid_pairs:
processed += len(chunk)
continue
v_ids, v_texts = zip(*valid_pairs)
responses = llm.generate_batch(
system_prompt, list(v_texts), temperature=0.2, max_tokens=30
)
for obs_id, resp, txt in zip(v_ids, responses, v_texts):
resp = _strip_thinking(resp)
matched = "Unclassified"
# Multi-strategy matching: exact → substring → word-overlap (from Exp 3B)
resp_lower = resp.lower().strip().strip("\"'")
for name in valid_names:
if resp_lower == name.lower():
matched = name
break
if matched == "Unclassified":
for name in valid_names:
if name.lower() in resp_lower or resp_lower in name.lower():
matched = name
break
if matched == "Unclassified":
resp_words = set(resp_lower.split())
best_score, best = 0.0, None
for name in valid_names:
nw = set(name.lower().split())
if not nw:
continue
score = len(resp_words & nw) / len(nw)
if score > best_score and score >= 0.5:
best_score, best = score, name
if best:
matched = best
assignments[obs_id] = {"cluster": matched, "original_description": txt[:100]}
processed += len(chunk)
if processed - last_ckpt >= checkpoint_interval:
last_ckpt = processed
ckpt.save(assignments, ASSIGN_FILE)
log.info(f" Step 3 — {processed}/{len(captions)}")
ckpt.save(assignments, ASSIGN_FILE)
ckpt.save({"cluster_names": current_cluster_names}, META_FILE)
dist = Counter(v["cluster"] for v in assignments.values())
log.info("Step 3 complete. Distribution:")
for name, count in dist.most_common():
log.info(f" {name}: {count}")
return assignments
def export_results(
image_mapping: Dict[str, dict],
captions: Dict[str, dict],
hooks: Dict[str, dict],
cluster_def: Dict,
assignments: Dict[str, dict],
ckpt: CheckpointManager,
log: logging.Logger,
) -> None:
"""Write ictc_final_results.json — matches the notebook's export format."""
cluster_names = [c["name"] for c in cluster_def.get("clusters", [])]
rows = []
for obs_id, info in image_mapping.items():