Update app.py
Browse files
app.py
CHANGED
|
@@ -1,670 +1,183 @@
|
|
| 1 |
-
"""
|
| 2 |
-
AI Fashion Stylist — Hugging Face Space app
|
| 3 |
-
Combines: CLIP recommendation engine (Part 3) + 3 GenAI patterns (Part 4):
|
| 4 |
-
1. Small Language Model -> generates text (Qwen2.5-0.5B-Instruct)
|
| 5 |
-
2. Small Vision-Language Model -> answers questions (BLIP-VQA-base)
|
| 6 |
-
3. Small Vision-Language pipeline -> generates an image (clothing segmentation + SD inpainting)
|
| 7 |
-
|
| 8 |
-
Deploy notes:
|
| 9 |
-
- Upload this file + requirements.txt + final_image_embeddings.npy + catalog_metadata.parquet
|
| 10 |
-
to your HF Space repo root.
|
| 11 |
-
- Update HF_DATASET_REPO / HF_WINNING_MODEL below if your repo names differ.
|
| 12 |
-
- Quick Starter sample photos are generated automatically from the dataset at startup -
|
| 13 |
-
no manual file upload needed.
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
import base64
|
| 17 |
-
import os
|
| 18 |
-
import urllib.parse
|
| 19 |
-
from io import BytesIO
|
| 20 |
-
|
| 21 |
-
import numpy as np
|
| 22 |
-
import pandas as pd
|
| 23 |
-
import torch
|
| 24 |
import gradio as gr
|
| 25 |
-
import
|
| 26 |
-
from datasets import load_dataset
|
| 27 |
from PIL import Image
|
| 28 |
-
from scipy import ndimage
|
| 29 |
-
from transformers import (
|
| 30 |
-
CLIPModel, CLIPProcessor, pipeline as hf_pipeline,
|
| 31 |
-
BlipProcessor, BlipForQuestionAnswering,
|
| 32 |
-
SegformerImageProcessor, AutoModelForSemanticSegmentation,
|
| 33 |
-
)
|
| 34 |
-
from diffusers import StableDiffusionInpaintPipeline
|
| 35 |
-
|
| 36 |
-
# ---------------------------------------------------------------------------
|
| 37 |
-
# CONFIG — update these to match your own HF repos
|
| 38 |
-
# ---------------------------------------------------------------------------
|
| 39 |
-
HF_DATASET_REPO = "lihicarmeli/fashion-stylist-multimodal-v2" # your HF dataset repo
|
| 40 |
-
HF_WINNING_MODEL = "openai/clip-vit-base-patch32" # winning embedding model (Part 3)
|
| 41 |
-
EMBEDDINGS_FILE = "final_image_embeddings.npy" # uploaded next to this app.py
|
| 42 |
-
METADATA_FILE = "catalog_metadata.parquet" # uploaded next to this app.py
|
| 43 |
-
|
| 44 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 45 |
-
|
| 46 |
-
# ---------------------------------------------------------------------------
|
| 47 |
-
# LOAD DATA + WINNING EMBEDDING MODEL (runs once, on Space startup)
|
| 48 |
-
# ---------------------------------------------------------------------------
|
| 49 |
-
print("Loading dataset from HF Hub...")
|
| 50 |
-
ds = load_dataset(HF_DATASET_REPO)
|
| 51 |
-
df = ds["train"].to_pandas()
|
| 52 |
-
images = [ds["train"][i]["image_improved"] for i in range(len(ds["train"]))]
|
| 53 |
-
|
| 54 |
-
print("Generating Quick Starter sample photos from the dataset...")
|
| 55 |
-
SAMPLES_DIR = "samples_cache"
|
| 56 |
-
os.makedirs(SAMPLES_DIR, exist_ok=True)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def _pick_index(filter_fn, fallback_idx=0):
|
| 60 |
-
matches = df[df.apply(filter_fn, axis=1)]
|
| 61 |
-
return int(matches.index[0]) if len(matches) else fallback_idx
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
_sample_specs = [
|
| 65 |
-
("demo_woman.jpg", lambda r: r["gender"] == "woman"),
|
| 66 |
-
("demo_man.jpg", lambda r: r["gender"] == "man"),
|
| 67 |
-
("demo_teen.jpg", lambda r: r["age_group"] == "teen"),
|
| 68 |
-
]
|
| 69 |
-
|
| 70 |
-
SAMPLE_PHOTOS = []
|
| 71 |
-
for _filename, _filt in _sample_specs:
|
| 72 |
-
_idx = _pick_index(_filt, fallback_idx=len(SAMPLE_PHOTOS))
|
| 73 |
-
_path = os.path.join(SAMPLES_DIR, _filename)
|
| 74 |
-
images[_idx].convert("RGB").save(_path)
|
| 75 |
-
SAMPLE_PHOTOS.append(_path)
|
| 76 |
-
print(f" -> {len(SAMPLE_PHOTOS)} sample photos ready: {SAMPLE_PHOTOS}")
|
| 77 |
-
|
| 78 |
-
print("Loading precomputed embeddings...")
|
| 79 |
-
image_embeddings = np.load(EMBEDDINGS_FILE).astype("float32")
|
| 80 |
-
|
| 81 |
-
print("Loading winning embedding model (CLIP) from HF Hub...")
|
| 82 |
-
win_model = CLIPModel.from_pretrained(HF_WINNING_MODEL).to(DEVICE).eval()
|
| 83 |
-
win_processor = CLIPProcessor.from_pretrained(HF_WINNING_MODEL)
|
| 84 |
-
|
| 85 |
-
print("Building FAISS index...")
|
| 86 |
-
dimension = image_embeddings.shape[1]
|
| 87 |
-
faiss_img_index = faiss.IndexFlatL2(dimension)
|
| 88 |
-
faiss.normalize_L2(image_embeddings)
|
| 89 |
-
faiss_img_index.add(image_embeddings)
|
| 90 |
-
|
| 91 |
-
# ---------------------------------------------------------------------------
|
| 92 |
-
# GENERATION MODELS — the 3 "Good Examples" patterns
|
| 93 |
-
# ---------------------------------------------------------------------------
|
| 94 |
-
print("Loading small language model for text generation (Qwen2.5-0.5B-Instruct)...")
|
| 95 |
-
caption_gen_pipe = hf_pipeline(
|
| 96 |
-
"text-generation",
|
| 97 |
-
model="Qwen/Qwen2.5-0.5B-Instruct",
|
| 98 |
-
device=0 if DEVICE == "cuda" else -1,
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
print("Loading small vision-language model for VQA (BLIP-VQA-base)...")
|
| 102 |
-
vqa_processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 103 |
-
vqa_model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(DEVICE)
|
| 104 |
-
|
| 105 |
-
print("Loading clothing segmentation model (helper - finds where the clothes are)...")
|
| 106 |
-
seg_processor = SegformerImageProcessor.from_pretrained("mattmdjaga/segformer_b2_clothes")
|
| 107 |
-
seg_model = AutoModelForSemanticSegmentation.from_pretrained("mattmdjaga/segformer_b2_clothes").to(DEVICE)
|
| 108 |
-
|
| 109 |
-
print("Loading image-generation model for the new outfit (Stable Diffusion Inpainting)...")
|
| 110 |
-
inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 111 |
-
"runwayml/stable-diffusion-inpainting",
|
| 112 |
-
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
|
| 113 |
-
safety_checker=None,
|
| 114 |
-
).to(DEVICE)
|
| 115 |
-
|
| 116 |
-
GENDERS = sorted(df["gender"].unique().tolist())
|
| 117 |
-
AGE_GROUPS = sorted(df["age_group"].unique().tolist())
|
| 118 |
-
SKIN_TONES = sorted(df["skin_tone"].unique().tolist())
|
| 119 |
-
UNDERTONES = sorted(df["undertone"].unique().tolist())
|
| 120 |
-
STYLES = sorted(df["style_preference"].unique().tolist())
|
| 121 |
-
|
| 122 |
-
print("Space ready.")
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# ---------------------------------------------------------------------------
|
| 126 |
-
# EMBEDDING + SEARCH (Part 3 logic, unchanged)
|
| 127 |
-
# ---------------------------------------------------------------------------
|
| 128 |
-
@torch.no_grad()
|
| 129 |
-
def embed_query_image(pil_image):
|
| 130 |
-
inputs = win_processor(images=pil_image, return_tensors="pt").to(DEVICE)
|
| 131 |
-
feats = win_model.get_image_features(**inputs)
|
| 132 |
-
feats = feats.pooler_output if hasattr(feats, "pooler_output") else feats
|
| 133 |
-
return feats.cpu().numpy().astype("float32")
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
@torch.no_grad()
|
| 137 |
-
def embed_query_text(sentence):
|
| 138 |
-
inputs = win_processor(text=[sentence], return_tensors="pt", padding=True, truncation=True).to(DEVICE)
|
| 139 |
-
feats = win_model.get_text_features(**inputs)
|
| 140 |
-
feats = feats.pooler_output if hasattr(feats, "pooler_output") else feats
|
| 141 |
-
return feats.cpu().numpy().astype("float32")
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
def build_feature_sentence(skin_tone, undertone, style_preference, gender=None, age_group=None):
|
| 145 |
-
descriptor = " ".join(p for p in [age_group, gender] if p) or "person"
|
| 146 |
-
return (
|
| 147 |
-
f"a {descriptor} with {skin_tone} skin tone and {undertone} undertone, "
|
| 148 |
-
f"wearing a {style_preference} style outfit"
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def faiss_filtered_search(query_emb, top_k=3, exclude_idx=None, gender=None, age_group=None):
|
| 153 |
-
faiss.normalize_L2(query_emb)
|
| 154 |
-
k = len(df)
|
| 155 |
-
distances, indices = faiss_img_index.search(query_emb, k)
|
| 156 |
-
distances, indices = distances[0], indices[0]
|
| 157 |
-
|
| 158 |
-
def collect(require_gender, require_age):
|
| 159 |
-
kept_i, kept_d = [], []
|
| 160 |
-
for idx, dist in zip(indices, distances):
|
| 161 |
-
if idx == -1 or (exclude_idx is not None and idx == exclude_idx):
|
| 162 |
-
continue
|
| 163 |
-
row = df.iloc[idx]
|
| 164 |
-
if require_gender and gender and str(row["gender"]).lower() != str(gender).lower():
|
| 165 |
-
continue
|
| 166 |
-
if require_age and age_group and str(row["age_group"]).lower() != str(age_group).lower():
|
| 167 |
-
continue
|
| 168 |
-
kept_i.append(idx)
|
| 169 |
-
kept_d.append(dist)
|
| 170 |
-
if len(kept_i) == top_k:
|
| 171 |
-
break
|
| 172 |
-
return kept_i, kept_d
|
| 173 |
-
|
| 174 |
-
kept_i, kept_d = collect(True, True)
|
| 175 |
-
if len(kept_i) < top_k:
|
| 176 |
-
kept_i, kept_d = collect(True, False)
|
| 177 |
-
if len(kept_i) < top_k:
|
| 178 |
-
kept_i, kept_d = collect(False, False)
|
| 179 |
-
|
| 180 |
-
return np.array(kept_i), df.iloc[kept_i], np.array(kept_d)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
# ---------------------------------------------------------------------------
|
| 184 |
-
# GENERATION — 3 "Good Examples" patterns (Part 4)
|
| 185 |
-
# ---------------------------------------------------------------------------
|
| 186 |
-
|
| 187 |
-
# --- Pattern 1: small Language Model -> generate text ---
|
| 188 |
-
def generate_stylist_caption(row):
|
| 189 |
-
user_prompt = (
|
| 190 |
-
f"Write one short, warm sentence (max 25 words) from a fashion stylist, recommending this look: "
|
| 191 |
-
f"a {row['style_preference']} style outfit in {row['primary_color']} and {row['secondary_color']}, "
|
| 192 |
-
f"best colors: {row['recommended_colors']}. Be specific and stylish, no hashtags."
|
| 193 |
-
)
|
| 194 |
-
messages = [{"role": "user", "content": user_prompt}]
|
| 195 |
-
output = caption_gen_pipe(messages, max_new_tokens=40, do_sample=True, temperature=0.7)
|
| 196 |
-
return output[0]["generated_text"][-1]["content"].strip()
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
# --- Pattern 2: small Vision-Language Model -> answer questions about an image ---
|
| 200 |
-
@torch.no_grad()
|
| 201 |
-
def answer_question_about_image(pil_image, question):
|
| 202 |
-
inputs = vqa_processor(pil_image.convert("RGB"), question, return_tensors="pt").to(DEVICE)
|
| 203 |
-
output_ids = vqa_model.generate(**inputs, max_new_tokens=20)
|
| 204 |
-
return vqa_processor.decode(output_ids[0], skip_special_tokens=True)
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
# --- Pattern 3: small Vision-Language Model -> generate the FULL LOOK (full-body image) ---
|
| 208 |
-
FACE_LABEL_ID = 11
|
| 209 |
-
HAIR_LABEL_ID = 2
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
@torch.no_grad()
|
| 213 |
-
def get_face_protect_mask(pil_image, use_geometric_fallback=True):
|
| 214 |
-
"""Returns a boolean mask (True = protect) covering the face + hair, at pil_image's own
|
| 215 |
-
resolution. Combines the segmentation model's prediction with a fixed geometric ellipse
|
| 216 |
-
(centered, where a face statistically sits in a headshot crop) as a safety net - so even
|
| 217 |
-
if segmentation misclassifies an unusual headwear/turban, the visible face is still
|
| 218 |
-
guaranteed to be protected."""
|
| 219 |
-
inputs = seg_processor(images=pil_image, return_tensors="pt").to(DEVICE)
|
| 220 |
-
logits = seg_model(**inputs).logits
|
| 221 |
-
upsampled = torch.nn.functional.interpolate(
|
| 222 |
-
logits, size=pil_image.size[::-1], mode="bilinear", align_corners=False
|
| 223 |
-
)
|
| 224 |
-
pred_seg = upsampled.argmax(dim=1)[0].cpu().numpy()
|
| 225 |
-
seg_protect = np.isin(pred_seg, [FACE_LABEL_ID, HAIR_LABEL_ID])
|
| 226 |
-
|
| 227 |
-
if not use_geometric_fallback:
|
| 228 |
-
return seg_protect
|
| 229 |
-
|
| 230 |
-
h, w = seg_protect.shape
|
| 231 |
-
yy, xx = np.mgrid[0:h, 0:w]
|
| 232 |
-
cy, cx = h * 0.42, w * 0.5 # face center: slightly above vertical middle of a headshot
|
| 233 |
-
ry, rx = h * 0.30, w * 0.22 # ellipse radii tuned for a tight headshot/bust crop
|
| 234 |
-
geometric_protect = (((xx - cx) / rx) ** 2 + ((yy - cy) / ry) ** 2) <= 1.0
|
| 235 |
-
|
| 236 |
-
return seg_protect | geometric_protect
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
def build_full_look_prompt(row):
|
| 240 |
-
gender_word = "man" if str(row["gender"]).lower() in ("man", "male") else "woman"
|
| 241 |
-
return (
|
| 242 |
-
f"full body fashion photo of a {row['age_group']} {gender_word}, standing, "
|
| 243 |
-
f"wearing a {row['style_preference']} style outfit: {row['outfit_top']}, "
|
| 244 |
-
f"{row['outfit_bottom']}, {row['outfit_shoes']}, {row['outfit_accessory']}, "
|
| 245 |
-
f"in {row['primary_color']} and {row['secondary_color']}, "
|
| 246 |
-
f"studio lighting, plain background, head to toe, high quality fashion photography"
|
| 247 |
-
)
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
def generate_new_outfit_image(pil_image, row, target_gender=None, canvas_size=(512, 1024),
|
| 251 |
-
head_width_frac=0.45, steps=40, guidance_scale=8.0):
|
| 252 |
-
"""Generates the FULL LOOK: keeps the original face pixel-identical (scaled down to a
|
| 253 |
-
realistic head-to-body proportion) and generates the rest of a standing figure wearing
|
| 254 |
-
the complete recommended outfit (top, bottom, shoes, accessory) around it."""
|
| 255 |
-
if target_gender is not None and str(row["gender"]).lower() != str(target_gender).lower():
|
| 256 |
-
print(f"⚠️ Warning: recommended row gender ({row['gender']}) != expected gender "
|
| 257 |
-
f"({target_gender}) - double-check which matched_rows was passed in.")
|
| 258 |
-
|
| 259 |
-
base = pil_image.convert("RGB")
|
| 260 |
-
protect = get_face_protect_mask(base) # (H, W) bool, at base's own resolution
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
protect_resized_arr = np.array(resized_protect_img) > 127
|
| 279 |
-
mask_arr[paste_y:paste_y + head_h, paste_x:paste_x + head_w][protect_resized_arr] = 0
|
| 280 |
-
mask = Image.fromarray(mask_arr).convert("L")
|
| 281 |
-
|
| 282 |
-
prompt = build_full_look_prompt(row)
|
| 283 |
-
generated = inpaint_pipe(
|
| 284 |
-
prompt=prompt,
|
| 285 |
-
image=canvas,
|
| 286 |
-
mask_image=mask,
|
| 287 |
-
num_inference_steps=steps,
|
| 288 |
-
guidance_scale=guidance_scale,
|
| 289 |
-
height=canvas_h,
|
| 290 |
-
width=canvas_w,
|
| 291 |
-
).images[0]
|
| 292 |
-
|
| 293 |
-
# Hard-composite: guarantees zero change on the protected face/hair pixels
|
| 294 |
-
final_image = Image.composite(generated, canvas, mask)
|
| 295 |
-
return final_image, prompt
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
# ---------------------------------------------------------------------------
|
| 299 |
-
# SHOP LINKS (Part 3 logic, unchanged)
|
| 300 |
-
# ---------------------------------------------------------------------------
|
| 301 |
-
RETAILER_SEARCH_URLS = {
|
| 302 |
-
"zara": "https://www.zara.com/us/en/search?searchTerm={query}§ion={section}",
|
| 303 |
-
"hm": "https://www2.hm.com/en_us/search-results.html?q={query}",
|
| 304 |
-
"asos": "https://www.asos.com/us/{dept}/search/?q={query}",
|
| 305 |
-
"mango": "https://shop.mango.com/us/en/search?kw={query}",
|
| 306 |
-
"shein": "https://us.shein.com/pdsearch/{query}/",
|
| 307 |
-
}
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
def normalize_gender(gender):
|
| 311 |
-
g = str(gender).strip().lower() if gender is not None else ""
|
| 312 |
-
return "men" if g in ("male", "man", "men", "m") else "women"
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
def to_shop_link(retailer, value, gender=None):
|
| 316 |
-
dept = normalize_gender(gender)
|
| 317 |
-
if retailer == "zara":
|
| 318 |
-
section = "MAN" if dept == "men" else "WOMAN"
|
| 319 |
-
return RETAILER_SEARCH_URLS["zara"].format(query=urllib.parse.quote(str(value)), section=section)
|
| 320 |
-
if retailer == "asos":
|
| 321 |
-
return RETAILER_SEARCH_URLS["asos"].format(query=urllib.parse.quote(str(value)), dept=dept)
|
| 322 |
-
gender_word = "men's" if dept == "men" else "women's"
|
| 323 |
-
return RETAILER_SEARCH_URLS[retailer].format(query=urllib.parse.quote(f"{gender_word} {value}"))
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
def shop_links_markdown(row):
|
| 327 |
-
lines = []
|
| 328 |
-
for retailer, col in [("zara", "search_query_zara"), ("hm", "search_query_hm"),
|
| 329 |
-
("asos", "search_query_asos"), ("mango", "search_query_mango"),
|
| 330 |
-
("shein", "search_query_shein")]:
|
| 331 |
-
link = to_shop_link(retailer, row[col], gender=row["gender"])
|
| 332 |
-
lines.append(f"- **{retailer.upper()}**: [{row[col]}]({link})")
|
| 333 |
-
return "\n".join(lines)
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
# ---------------------------------------------------------------------------
|
| 337 |
-
# UI CARDS — original visual layer, built on top of the same Part 3+4 data
|
| 338 |
-
# ---------------------------------------------------------------------------
|
| 339 |
-
_SWATCH_CSS = {
|
| 340 |
-
"ice white": "#f4f3ee", "bold blue": "#1d4ed8", "royal blue": "#1e3a8a",
|
| 341 |
-
"fuchsia": "#c026d3", "cool red": "#dc2626", "deep teal": "#0f766e",
|
| 342 |
-
"plum": "#6b21a8", "silver": "#cbd5e1", "bright pink": "#ec4899",
|
| 343 |
-
"deep jewel tones": "#581c87", "rust": "#b45309", "camel": "#c19a6b",
|
| 344 |
-
"coral": "#fb7185", "ivory": "#fffff0", "olive": "#65730a",
|
| 345 |
-
"terracotta": "#c2643a",
|
| 346 |
}
|
| 347 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
-
def
|
| 350 |
-
|
| 351 |
-
if
|
| 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 |
-
return _PALETTE_NAMES.get(str(skin_tone).strip().lower(), {}).get(
|
| 379 |
-
str(undertone).strip().lower(), "Signature Palette"
|
| 380 |
-
)
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
def pil_to_base64(img, max_size=420):
|
| 384 |
-
img = img.convert("RGB").copy()
|
| 385 |
-
img.thumbnail((max_size, max_size))
|
| 386 |
-
buf = BytesIO()
|
| 387 |
-
img.save(buf, format="JPEG", quality=85)
|
| 388 |
-
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
def build_feature_pills_html(labels, selected, title):
|
| 392 |
-
"""Pill-style recap row (her visual pattern) - but built from your real, actually-used
|
| 393 |
-
attributes (skin tone / undertone / style), not placeholder categories."""
|
| 394 |
-
pills = ""
|
| 395 |
-
for label in labels:
|
| 396 |
-
active = label == selected
|
| 397 |
-
border = "2px solid #C94A72" if active else "1.5px solid #E8E0E8"
|
| 398 |
-
bg = "#FFF0F5" if active else "#FFFFFF"
|
| 399 |
-
tcol = "#C94A72" if active else "#444"
|
| 400 |
-
dot = _swatch_color(label) if title != "Style" else "#C19A6B"
|
| 401 |
-
pills += (
|
| 402 |
-
f'<div style="display:inline-flex;align-items:center;gap:7px;padding:8px 14px;'
|
| 403 |
-
f'border-radius:30px;border:{border};background:{bg};margin:4px;">'
|
| 404 |
-
f'<div style="width:14px;height:14px;border-radius:50%;background:{dot};'
|
| 405 |
-
f'border:1px solid rgba(0,0,0,.12);"></div>'
|
| 406 |
-
f'<span style="font-size:13px;font-weight:500;color:{tcol};">{label.title()}</span></div>'
|
| 407 |
-
)
|
| 408 |
-
return (
|
| 409 |
-
f'<div style="margin-bottom:14px;"><div style="font-size:11px;font-weight:600;color:#aaa;'
|
| 410 |
-
f'text-transform:uppercase;letter-spacing:.1em;margin-bottom:8px;">{title}</div>'
|
| 411 |
-
f'<div style="display:flex;flex-wrap:wrap;margin:-4px;">{pills}</div></div>'
|
| 412 |
-
)
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
def build_features_recap_html(skin_tone, undertone, style):
|
| 416 |
-
return (
|
| 417 |
-
'<div style="background:#fff;border-radius:16px;padding:18px 20px;'
|
| 418 |
-
'margin-bottom:14px;border:1px solid #F0E8F0;">'
|
| 419 |
-
'<div style="font-size:11px;font-weight:600;color:#C94A72;text-transform:uppercase;'
|
| 420 |
-
'letter-spacing:.1em;margin-bottom:14px;">Your Features</div>'
|
| 421 |
-
+ build_feature_pills_html(SKIN_TONES, skin_tone, "Skin tone")
|
| 422 |
-
+ build_feature_pills_html(UNDERTONES, undertone, "Undertone")
|
| 423 |
-
+ build_feature_pills_html(STYLES, style, "Style")
|
| 424 |
-
+ "</div>"
|
| 425 |
-
)
|
| 426 |
-
colors = [c.strip() for c in str(row["recommended_colors"]).split(",") if c.strip()][:4]
|
| 427 |
-
swatches = "".join(
|
| 428 |
-
f'<div style="text-align:center;margin-right:16px;">'
|
| 429 |
-
f'<div style="width:38px;height:38px;border-radius:50%;background:{_swatch_color(c)};'
|
| 430 |
-
f'border:2px solid #fff;box-shadow:0 0 0 1px #e5e7eb;margin:0 auto 6px;"></div>'
|
| 431 |
-
f'<span style="font-size:11px;color:#6b7280;">{c.title()}</span></div>'
|
| 432 |
-
for c in colors
|
| 433 |
-
)
|
| 434 |
-
palette_name = get_palette_name(row["skin_tone"], row["undertone"])
|
| 435 |
-
return f"""
|
| 436 |
-
<div style="font-family:inherit;">
|
| 437 |
-
<div style="position:relative;overflow:hidden;background:#1a1a1a;color:white;
|
| 438 |
-
border-radius:18px;padding:24px 28px;margin-bottom:16px;">
|
| 439 |
-
<div style="position:absolute;top:-40px;right:-40px;width:150px;height:150px;border-radius:50%;
|
| 440 |
-
background:rgba(201,74,114,.25);"></div>
|
| 441 |
-
<div style="font-size:11px;letter-spacing:1.5px;color:#C94A72;font-weight:700;">YOUR COLOR PALETTE</div>
|
| 442 |
-
<div style="font-size:30px;font-weight:800;margin:6px 0 4px;">{palette_name}</div>
|
| 443 |
-
<div style="font-size:13px;color:#bdb8c9;">
|
| 444 |
-
{row['skin_tone'].title()} skin · {row['undertone'].title()} undertone · {row['style_preference'].title()} style
|
| 445 |
</div>
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
| 451 |
</div>
|
| 452 |
-
<div
|
| 453 |
-
|
| 454 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
</div>
|
| 456 |
-
</div>
|
| 457 |
</div>
|
| 458 |
"""
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
def build_outfit_component_cards_html(row):
|
| 462 |
-
"""One card per garment piece of the TOP match (top/bottom/shoes/accessory) -
|
| 463 |
-
mirrors a 'your outfit, broken into shoppable pieces' layout."""
|
| 464 |
-
colors = [c.strip() for c in str(row["recommended_colors"]).split(",") if c.strip()] or ["neutral"]
|
| 465 |
-
components = [
|
| 466 |
-
("TOP", row.get("outfit_top", "Top"), "zara", "search_query_zara"),
|
| 467 |
-
("BOTTOM", row.get("outfit_bottom", "Bottom"), "hm", "search_query_hm"),
|
| 468 |
-
("SHOES", row.get("outfit_shoes", "Shoes"), "asos", "search_query_asos"),
|
| 469 |
-
("ACCESSORY", row.get("outfit_accessory", "Accessory"), "mango", "search_query_mango"),
|
| 470 |
-
]
|
| 471 |
-
cards = []
|
| 472 |
-
for i, (label, item_name, retailer, col) in enumerate(components):
|
| 473 |
-
dot_color = _swatch_color(colors[i % len(colors)])
|
| 474 |
-
banner_color = _lighten_hex(dot_color)
|
| 475 |
-
link = to_shop_link(retailer, row[col], row["gender"])
|
| 476 |
-
cards.append(f"""
|
| 477 |
-
<div style="border:1px solid #eee;border-radius:14px;overflow:hidden;width:185px;
|
| 478 |
-
box-shadow:0 1px 3px rgba(0,0,0,0.05);">
|
| 479 |
-
<div style="background:{banner_color};height:64px;display:flex;align-items:center;justify-content:center;">
|
| 480 |
-
<div style="width:30px;height:30px;border-radius:50%;background:{dot_color};border:2px solid white;"></div>
|
| 481 |
-
</div>
|
| 482 |
-
<div style="padding:12px 14px;">
|
| 483 |
-
<div style="font-size:10px;letter-spacing:1px;color:#C94A72;font-weight:700;">{label}</div>
|
| 484 |
-
<div style="font-size:13px;font-weight:600;margin:4px 0 2px;color:#111827;">{str(item_name).title()}</div>
|
| 485 |
-
<div style="font-size:11px;color:#9ca3af;margin-bottom:10px;">{retailer.upper()}</div>
|
| 486 |
-
<a href="{link}" target="_blank" style="display:block;text-align:center;background:#111827;
|
| 487 |
-
color:white;border-radius:8px;padding:8px;text-decoration:none;font-size:12px;font-weight:600;">
|
| 488 |
-
Shop ↗
|
| 489 |
-
</a>
|
| 490 |
-
</div>
|
| 491 |
-
</div>
|
| 492 |
-
""")
|
| 493 |
-
return f'<div style="display:flex;gap:14px;flex-wrap:wrap;">{"".join(cards)}</div>'
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
def build_more_matches_html(matched_indices, matched_rows, similarities, skip_first=True):
|
| 497 |
-
"""Smaller secondary row: the other catalog matches, as full-look thumbnails
|
| 498 |
-
(keeps the '3 similar items' requirement visible alongside the per-garment cards above)."""
|
| 499 |
-
items = list(zip(matched_indices, similarities))[1:] if skip_first else list(zip(matched_indices, similarities))
|
| 500 |
-
if not items:
|
| 501 |
-
return ""
|
| 502 |
-
cards = []
|
| 503 |
-
for idx, sim in items:
|
| 504 |
-
row = df.iloc[idx]
|
| 505 |
-
img_b64 = pil_to_base64(images[idx])
|
| 506 |
-
link = to_shop_link("zara", row["search_query_zara"], row["gender"])
|
| 507 |
-
cards.append(f"""
|
| 508 |
-
<div style="border:1px solid #eee;border-radius:12px;overflow:hidden;width:140px;">
|
| 509 |
-
<img src="data:image/jpeg;base64,{img_b64}" style="width:100%;height:150px;object-fit:cover;display:block;"/>
|
| 510 |
-
<div style="padding:8px 10px;">
|
| 511 |
-
<div style="font-size:10px;color:#9ca3af;">{sim}</div>
|
| 512 |
-
<a href="{link}" target="_blank" style="display:block;text-align:center;background:#111827;
|
| 513 |
-
color:white;border-radius:6px;padding:6px;text-decoration:none;font-size:11px;margin-top:6px;">
|
| 514 |
-
Shop ↗
|
| 515 |
-
</a>
|
| 516 |
-
</div>
|
| 517 |
-
</div>
|
| 518 |
-
""")
|
| 519 |
return (
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
# ---------------------------------------------------------------------------
|
| 527 |
-
# MAIN PIPELINE — shared by both input paths, runs all 3 GenAI patterns
|
| 528 |
-
# ---------------------------------------------------------------------------
|
| 529 |
-
def run_pipeline(matched_indices, matched_rows, matched_scores, base_image=None, question=None,
|
| 530 |
-
expected_gender=None):
|
| 531 |
-
if len(matched_indices) == 0:
|
| 532 |
-
return "<i>No matches found — try different filters.</i>", None, "", "<div></div>"
|
| 533 |
-
|
| 534 |
-
similarity = [round(1.0 - (d / 2.0), 3) for d in matched_scores]
|
| 535 |
-
|
| 536 |
-
top_row = matched_rows.iloc[0]
|
| 537 |
-
# Pattern 3 needs a base photo: the user's own photo (Path A) or the top catalog match (Path B)
|
| 538 |
-
edit_base_image = base_image if base_image is not None else images[matched_indices[0]]
|
| 539 |
-
# If no explicit gender filter was given, fall back to the matched row's own gender, so
|
| 540 |
-
# the generated outfit's wording always matches the person actually shown in the photo.
|
| 541 |
-
gender_for_generation = expected_gender or top_row["gender"]
|
| 542 |
-
|
| 543 |
-
caption = generate_stylist_caption(top_row) # Pattern 1
|
| 544 |
-
new_image, _prompt = generate_new_outfit_image(
|
| 545 |
-
edit_base_image, top_row, target_gender=gender_for_generation
|
| 546 |
-
) # Pattern 3
|
| 547 |
-
|
| 548 |
-
answer = ""
|
| 549 |
-
if question:
|
| 550 |
-
answer = answer_question_about_image(edit_base_image, question) # Pattern 2
|
| 551 |
-
|
| 552 |
-
style_card_html = build_style_card_html(top_row, caption)
|
| 553 |
-
outfit_cards_html = (
|
| 554 |
-
build_outfit_component_cards_html(top_row)
|
| 555 |
-
+ build_more_matches_html(matched_indices, matched_rows, similarity)
|
| 556 |
)
|
| 557 |
-
return style_card_html, new_image, answer, outfit_cards_html
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
def recommend_from_photo(photo, gender, age_group, question):
|
| 561 |
-
if photo is None:
|
| 562 |
-
return "<i>Please upload a photo or pick a Quick Starter.</i>", None, "", "<div></div>"
|
| 563 |
-
query_emb = embed_query_image(photo)
|
| 564 |
-
idx, rows, scores = faiss_filtered_search(query_emb, gender=gender or None, age_group=age_group or None)
|
| 565 |
-
return run_pipeline(idx, rows, scores, base_image=photo, question=question, expected_gender=gender)
|
| 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 |
-
with gr.
|
| 601 |
-
with gr.
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
gr.
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
age_b = gr.Dropdown(AGE_GROUPS, label="Age group", value=AGE_GROUPS[0])
|
| 638 |
-
question_b = gr.Textbox(label="Ask the stylist a question about the top match (optional)",
|
| 639 |
-
placeholder="e.g. Is this outfit formal or casual?")
|
| 640 |
-
btn_b = gr.Button("Get My Look ✨", elem_id="find-btn", elem_classes="big-btn", variant="primary")
|
| 641 |
-
features_recap_b = gr.HTML(build_features_recap_html(SKIN_TONES[0], UNDERTONES[0], STYLES[0]))
|
| 642 |
-
for _dropdown in (skin_b, undertone_b, style_b):
|
| 643 |
-
_dropdown.change(
|
| 644 |
-
build_features_recap_html, [skin_b, undertone_b, style_b], features_recap_b
|
| 645 |
-
)
|
| 646 |
-
style_card_b = gr.HTML(label="Style Profile")
|
| 647 |
-
new_img_b = gr.Image(label="✨ Your New AI-Generated Look")
|
| 648 |
-
answer_b = gr.Textbox(label="Answer to your question")
|
| 649 |
-
outfit_cards_b = gr.HTML(label="Outfit Picks")
|
| 650 |
-
btn_b.click(
|
| 651 |
-
recommend_from_features,
|
| 652 |
-
[skin_b, undertone_b, style_b, gender_b, age_b, question_b],
|
| 653 |
-
[style_card_b, new_img_b, answer_b, outfit_cards_b],
|
| 654 |
-
)
|
| 655 |
-
|
| 656 |
-
gr.Examples(
|
| 657 |
-
examples=[
|
| 658 |
-
["deep", "cool", "boho", "woman", "young adult", "Is this outfit formal or casual?"],
|
| 659 |
-
["tan", "warm", "elegant", "man", "adult", "What occasion suits this outfit?"],
|
| 660 |
-
["fair", "neutral", "minimalist", "woman", "teen", "What season is this outfit best for?"],
|
| 661 |
-
],
|
| 662 |
-
inputs=[skin_b, undertone_b, style_b, gender_b, age_b, question_b],
|
| 663 |
-
outputs=[style_card_b, new_img_b, answer_b, outfit_cards_b],
|
| 664 |
-
fn=recommend_from_features,
|
| 665 |
-
cache_examples=True, # precomputed at Space startup -> truly "1-click and see a result"
|
| 666 |
-
label="Quick Starters",
|
| 667 |
-
)
|
| 668 |
|
| 669 |
if __name__ == "__main__":
|
| 670 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# ==========================================
|
| 6 |
+
# 1. CORE DATA & ANALYSIS PIPELINE
|
| 7 |
+
# ==========================================
|
| 8 |
+
|
| 9 |
+
COLOR_PALETTES = {
|
| 10 |
+
"Fair-Blonde-Blue": {
|
| 11 |
+
"name": "Icy Cool Blonde",
|
| 12 |
+
"description": "Fair skin · Blonde hair · Blue eyes",
|
| 13 |
+
"swatches": [
|
| 14 |
+
{"name": "Light Blue", "hex": "#A0C4DF"},
|
| 15 |
+
{"name": "Soft Lavender", "hex": "#C39BD3"},
|
| 16 |
+
{"name": "Ivory", "hex": "#F9F9F6"},
|
| 17 |
+
{"name": "Dusty Rose", "hex": "#E8A7A1"}
|
| 18 |
+
],
|
| 19 |
+
"tip": "Avoid harsh black — opt for navy or charcoal instead."
|
| 20 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
+
# The specific items recommended directly based on user traits
|
| 24 |
+
RECOMMENDED_LOOK = [
|
| 25 |
+
{"category": "TOP", "name": "Powder Blue Linen Blouse", "brand": "Zara", "hex": "#A0C4DF", "url": "https://shopping.google.com"},
|
| 26 |
+
{"category": "TROUSERS", "name": "Soft White Wide-Leg Trousers", "brand": "ASOS", "hex": "#F9F9F6", "url": "https://shopping.google.com"},
|
| 27 |
+
{"category": "SHOES", "name": "Nude Strappy Sandals", "brand": "SHEIN", "hex": "#E5C2B3", "url": "https://shopping.google.com"},
|
| 28 |
+
{"category": "JACKET", "name": "Lilac Knit Cardigan", "brand": "H&M", "hex": "#C39BD3", "url": "https://shopping.google.com"},
|
| 29 |
+
{"category": "BAG", "name": "Dusty Rose Mini Bag", "brand": "SHEIN", "hex": "#E8A7A1", "url": "https://shopping.google.com"},
|
| 30 |
+
{"category": "DRESS", "name": "Lavender Midi Wrap Dress", "brand": "ASOS", "hex": "#D2B4DE", "url": "https://shopping.google.com"}
|
| 31 |
+
]
|
| 32 |
|
| 33 |
+
def analyze_and_recommend(image, skin, hair, eye, choice_method):
|
| 34 |
+
# 1. Detect features based on input method
|
| 35 |
+
if choice_method == "Upload a photo" and image is not None:
|
| 36 |
+
# Connect your SigLIP / feature classification logic here
|
| 37 |
+
skin_tone, hair_color, eye_color = "Fair", "Blonde", "Blue"
|
| 38 |
+
else:
|
| 39 |
+
skin_tone, hair_color, eye_color = skin, hair, eye
|
| 40 |
+
|
| 41 |
+
# 2. Get corresponding color palette archetype
|
| 42 |
+
palette_key = f"{skin_tone}-{hair_color}-{eye_color}"
|
| 43 |
+
palette = COLOR_PALETTES.get(palette_key, COLOR_PALETTES["Fair-Blonde-Blue"])
|
| 44 |
+
|
| 45 |
+
# 3. Build HTML for Color Swatches
|
| 46 |
+
swatches_html = "".join([
|
| 47 |
+
f'<div class="swatch-card"><div class="color-bubble" style="background-color: {s["hex"]};"></div><p>{s["name"]}</p></div>'
|
| 48 |
+
for s in palette["swatches"]
|
| 49 |
+
])
|
| 50 |
+
|
| 51 |
+
# 4. Build HTML for Outfit Picks (mapping your FAISS database results here)
|
| 52 |
+
outfit_html = "".join([
|
| 53 |
+
f'''
|
| 54 |
+
<div class="product-card">
|
| 55 |
+
<div class="prod-bubble" style="background-color: {item["hex"]};"></div>
|
| 56 |
+
<div class="prod-meta">
|
| 57 |
+
<span class="prod-cat">{item["category"]}</span>
|
| 58 |
+
<p class="prod-title">{item["name"]}</p>
|
| 59 |
+
<span class="prod-brand">{item["brand"]}</span>
|
| 60 |
+
<a href="{item["url"]}" target="_blank" class="shop-btn">Shop ↗</a>
|
| 61 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
</div>
|
| 63 |
+
''' for item in RECOMMENDED_LOOK
|
| 64 |
+
])
|
| 65 |
+
|
| 66 |
+
palette_results = f"""
|
| 67 |
+
<div class="results-box">
|
| 68 |
+
<div class="palette-banner">
|
| 69 |
+
<h2>{palette["name"]}</h2>
|
| 70 |
+
<p>{palette["description"]}</p>
|
| 71 |
</div>
|
| 72 |
+
<div class="section-split">
|
| 73 |
+
<div>
|
| 74 |
+
<h3>COLORS FOR YOU</h3>
|
| 75 |
+
<div class="swatch-grid">{swatches_html}</div>
|
| 76 |
+
</div>
|
| 77 |
+
<div class="tip-box">
|
| 78 |
+
<h3>STYLIST TIP</h3>
|
| 79 |
+
<p>{palette["tip"]}</p>
|
| 80 |
+
</div>
|
| 81 |
</div>
|
|
|
|
| 82 |
</div>
|
| 83 |
"""
|
| 84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return (
|
| 86 |
+
gr.update(value=skin_tone),
|
| 87 |
+
gr.update(value=hair_color),
|
| 88 |
+
gr.update(value=eye_color),
|
| 89 |
+
palette_results,
|
| 90 |
+
outfit_html,
|
| 91 |
+
gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# ==========================================
|
| 95 |
+
# 2. MATCHING MINIMALIST UI CSS (No Orange)
|
| 96 |
+
# ==========================================
|
| 97 |
+
custom_css = """
|
| 98 |
+
body { background-color: #F9F8F6 !important; font-family: -apple-system, BlinkMacSystemFont, sans-serif; }
|
| 99 |
+
.gradio-container { max-width: 800px !important; margin: 0 auto !important; padding-top: 40px !important; }
|
| 100 |
+
.center-text { text-align: center; }
|
| 101 |
+
.title-main { font-size: 40px; font-weight: 800; color: #111111; margin-bottom: 4px; letter-spacing: -1px; }
|
| 102 |
+
.subtitle-main { font-size: 16px; color: #777777; margin-bottom: 32px; }
|
| 103 |
+
.pill-container { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
|
| 104 |
+
.results-box { background: white; border-radius: 16px; padding: 24px; border: 1px solid #EAE9E6; margin-top: 24px; }
|
| 105 |
+
.palette-banner { background: #1A1A1A; color: white; padding: 24px; border-radius: 12px; margin-bottom: 24px; }
|
| 106 |
+
.palette-banner h2 { font-size: 26px; font-weight: 700; margin: 0 0 4px 0; }
|
| 107 |
+
.palette-banner p { color: #A0A0A0; margin: 0; font-size: 14px; }
|
| 108 |
+
.section-split { display: grid; grid-template-columns: 1.2fr 0.8fr; gap: 24px; margin-bottom: 24px; }
|
| 109 |
+
@media (max-width: 768px) { .section-split { grid-template-columns: 1fr; } }
|
| 110 |
+
.swatch-grid { display: flex; gap: 16px; margin-top: 12px; }
|
| 111 |
+
.swatch-card { text-align: center; font-size: 12px; font-weight: 500; color: #555; }
|
| 112 |
+
.color-bubble { width: 52px; height: 52px; border-radius: 50%; margin: 0 auto 8px auto; border: 1px solid rgba(0,0,0,0.05); }
|
| 113 |
+
.tip-box { background: #F4F3F0; border-radius: 12px; padding: 20px; border: 1px solid #EAE9E6; }
|
| 114 |
+
.tip-box h3, .section-split h3 { font-size: 11px; font-weight: 700; color: #A24A70; letter-spacing: 1px; margin-top: 0; }
|
| 115 |
+
.tip-box p { font-size: 14px; color: #333; line-height: 1.5; margin: 8px 0 0 0; }
|
| 116 |
+
.outfit-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-top: 16px; }
|
| 117 |
+
@media (max-width: 640px) { .outfit-grid { grid-template-columns: repeat(2, 1fr); } }
|
| 118 |
+
.product-card { background: white; border: 1px solid #EAE9E6; border-radius: 12px; padding: 16px; display: flex; flex-direction: column; align-items: center; text-align: center; }
|
| 119 |
+
.prod-bubble { width: 36px; height: 36px; border-radius: 50%; margin-bottom: 12px; border: 1px solid rgba(0,0,0,0.04); }
|
| 120 |
+
.prod-cat { font-size: 10px; font-weight: 700; color: #A24A70; letter-spacing: 0.5px; }
|
| 121 |
+
.prod-title { font-size: 14px; font-weight: 600; color: #111; margin: 4px 0; min-height: 40px; display: flex; align-items: center; }
|
| 122 |
+
.prod-brand { font-size: 12px; color: #888; margin-bottom: 12px; display: block; }
|
| 123 |
+
.shop-btn { display: block; width: 100%; background: #111111; color: white !important; text-align: center; padding: 8px 0; border-radius: 6px; font-size: 13px; font-weight: 500; text-decoration: none !important; }
|
| 124 |
+
.primary-action-btn { background: #111111 !important; color: white !important; border: none !important; border-radius: 8px !important; font-weight: 600 !important; font-size: 16px !important; padding: 12px 0 !important; }
|
| 125 |
"""
|
| 126 |
|
| 127 |
+
# ==========================================
|
| 128 |
+
# 3. GRADIO INTERFACE LAYOUT
|
| 129 |
+
# ==========================================
|
| 130 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 131 |
+
|
| 132 |
+
with gr.Group(elem_classes="center-text"):
|
| 133 |
+
gr.Markdown("PERSONAL COLOR STYLING", elem_classes="prod-cat")
|
| 134 |
+
gr.Markdown("# LookMatch", elem_classes="title-main")
|
| 135 |
+
gr.Markdown("Discover your perfect color palette and outfit", elem_classes="subtitle-main")
|
| 136 |
+
|
| 137 |
+
choice_method = gr.Radio(
|
| 138 |
+
["Upload a photo", "Choose manually"],
|
| 139 |
+
value="Upload a photo",
|
| 140 |
+
show_label=False,
|
| 141 |
+
interactive=True
|
| 142 |
)
|
| 143 |
|
| 144 |
+
with gr.Row():
|
| 145 |
+
with gr.Column() as upload_col:
|
| 146 |
+
image_input = gr.Image(label="Upload your photo — Gemini AI will detect your features", type="pil")
|
| 147 |
+
|
| 148 |
+
with gr.Column(visible=False) as manual_col:
|
| 149 |
+
skin_dropdown = gr.Dropdown(["Fair", "Light", "Medium", "Olive", "Tan", "Deep"], value="Fair", label="Skin tone")
|
| 150 |
+
hair_dropdown = gr.Dropdown(["Blonde", "Auburn/Red", "Brunette", "Black", "Grey/White"], value="Blonde", label="Hair color")
|
| 151 |
+
eye_dropdown = gr.Dropdown(["Blue", "Green", "Hazel", "Brown", "Dark brown/Black"], value="Blue", label="Eye color")
|
| 152 |
+
|
| 153 |
+
submit_btn = gr.Button("Get my look ✨", elem_classes="primary-action-btn")
|
| 154 |
+
|
| 155 |
+
# Lower Dashboard Area (Revealed on click)
|
| 156 |
+
with gr.Column(visible=False) as output_dashboard:
|
| 157 |
+
gr.Markdown("### YOUR FEATURES")
|
| 158 |
+
with gr.Row(elem_classes="pill-container"):
|
| 159 |
+
skin_tag = gr.Button(value="Fair", size="sm")
|
| 160 |
+
hair_tag = gr.Button(value="Blonde", size="sm")
|
| 161 |
+
eye_tag = gr.Button(value="Blue", size="sm")
|
| 162 |
+
|
| 163 |
+
palette_output = gr.HTML()
|
| 164 |
+
|
| 165 |
+
gr.Markdown("### OUTFIT PICKS")
|
| 166 |
+
outfit_output = gr.HTML(elem_classes="outfit-grid")
|
| 167 |
+
|
| 168 |
+
# Navigation controller
|
| 169 |
+
def toggle_input_ui(choice):
|
| 170 |
+
if choice == "Choose manually":
|
| 171 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 172 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 173 |
+
|
| 174 |
+
choice_method.change(toggle_input_ui, inputs=choice_method, outputs=[upload_col, manual_col])
|
| 175 |
+
|
| 176 |
+
submit_btn.click(
|
| 177 |
+
fn=analyze_and_recommend,
|
| 178 |
+
inputs=[image_input, skin_dropdown, hair_dropdown, eye_dropdown, choice_method],
|
| 179 |
+
outputs=[skin_tag, hair_tag, eye_tag, palette_output, outfit_output, output_dashboard]
|
| 180 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
demo.launch()
|