fork download
  1. def incremental_filter(image_pool, space, style, mood,
  2. color, materials, layout, lighting):
  3.  
  4. # Stage 1 — Tier 1: hard filter, keep top 50%
  5. candidates = filter_by(image_pool, [
  6. CLIP_MAPPINGS["space"][space],
  7. CLIP_MAPPINGS["color"][color],
  8. ", ".join([CLIP_MAPPINGS["materials"][m] for m in materials]),
  9. ], keep_top=0.5)
  10.  
  11. # Stage 2 — Tier 2: re-rank, keep top 50% of remaining
  12. candidates = filter_by(candidates, [
  13. CLIP_MAPPINGS["style"][style],
  14. CLIP_MAPPINGS["lighting"][lighting],
  15. ], keep_top=0.5)
  16.  
  17. # Stage 3 — Tier 3: final re-rank
  18. candidates = filter_by(candidates, [
  19. CLIP_MAPPINGS["layout"][layout],
  20. CLIP_MAPPINGS["mood"][mood],
  21. ], keep_top=1.0) # keep all, just re-rank
  22.  
  23. return candidates
  24.  
  25.  
  26. def filter_by(images, descriptions, keep_top=0.5):
  27. scored = []
  28. for image in images:
  29. score = sum(clip_similarity(image, d) for d in descriptions)
  30. scored.append((image, score))
  31.  
  32. scored.sort(key=lambda x: x[1], reverse=True)
  33. cutoff = max(1, int(len(scored) * keep_top))
  34. return [img for img, _ in scored[:cutoff]]# your code goes here
Success #stdin #stdout 0.01s 7100KB
stdin
Standard input is empty
stdout
Standard output is empty