Update app.py
Browse files
app.py
CHANGED
|
@@ -355,6 +355,31 @@ def _swatch_color(name):
|
|
| 355 |
return palette[hash(key) % len(palette)]
|
| 356 |
|
| 357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
def pil_to_base64(img, max_size=420):
|
| 359 |
img = img.convert("RGB").copy()
|
| 360 |
img.thumbnail((max_size, max_size))
|
|
@@ -372,13 +397,18 @@ def build_style_card_html(row, caption):
|
|
| 372 |
f'<span style="font-size:11px;color:#6b7280;">{c.title()}</span></div>'
|
| 373 |
for c in colors
|
| 374 |
)
|
|
|
|
| 375 |
return f"""
|
| 376 |
<div style="font-family:inherit;">
|
| 377 |
-
<div style="background:
|
| 378 |
-
border-radius:18px;padding:
|
| 379 |
-
<div style="
|
| 380 |
-
|
| 381 |
-
<div style="font-size:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
</div>
|
| 383 |
<div style="display:flex;gap:14px;flex-wrap:wrap;">
|
| 384 |
<div style="flex:1;min-width:220px;background:#f9fafb;border-radius:14px;padding:18px;">
|
|
@@ -386,7 +416,7 @@ def build_style_card_html(row, caption):
|
|
| 386 |
<div style="display:flex;flex-wrap:wrap;">{swatches}</div>
|
| 387 |
</div>
|
| 388 |
<div style="flex:1;min-width:220px;background:#f9fafb;border-radius:14px;padding:18px;">
|
| 389 |
-
<div style="font-size:11px;letter-spacing:1.5px;color:#a855f7;font-weight:700;margin-bottom:8px;">STYLIST
|
| 390 |
<div style="font-size:14px;color:#1f2937;line-height:1.4;">{caption}</div>
|
| 391 |
</div>
|
| 392 |
</div>
|
|
@@ -394,29 +424,69 @@ def build_style_card_html(row, caption):
|
|
| 394 |
"""
|
| 395 |
|
| 396 |
|
| 397 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
cards = []
|
| 399 |
-
for
|
| 400 |
row = df.iloc[idx]
|
| 401 |
img_b64 = pil_to_base64(images[idx])
|
| 402 |
link = to_shop_link("zara", row["search_query_zara"], row["gender"])
|
| 403 |
cards.append(f"""
|
| 404 |
-
<div style="border:1px solid #eee;border-radius:
|
| 405 |
-
|
| 406 |
-
<
|
| 407 |
-
|
| 408 |
-
<div style="font-size:10px;letter-spacing:1px;color:#9ca3af;font-weight:700;">MATCH #{i + 1} · {sim}</div>
|
| 409 |
-
<div style="font-size:14px;font-weight:600;margin:4px 0 10px;color:#111827;">
|
| 410 |
-
{row['style_preference'].title()} look
|
| 411 |
-
</div>
|
| 412 |
<a href="{link}" target="_blank" style="display:block;text-align:center;background:#111827;
|
| 413 |
-
color:white;border-radius:
|
| 414 |
-
Shop
|
| 415 |
</a>
|
| 416 |
</div>
|
| 417 |
</div>
|
| 418 |
""")
|
| 419 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
|
| 422 |
# ---------------------------------------------------------------------------
|
|
@@ -446,7 +516,10 @@ def run_pipeline(matched_indices, matched_rows, matched_scores, base_image=None,
|
|
| 446 |
answer = answer_question_about_image(edit_base_image, question) # Pattern 2
|
| 447 |
|
| 448 |
style_card_html = build_style_card_html(top_row, caption)
|
| 449 |
-
outfit_cards_html =
|
|
|
|
|
|
|
|
|
|
| 450 |
return style_card_html, new_image, answer, outfit_cards_html
|
| 451 |
|
| 452 |
|
|
@@ -471,6 +544,10 @@ def recommend_from_features(skin_tone, undertone, style, gender, age_group, ques
|
|
| 471 |
CUSTOM_CSS = """
|
| 472 |
.gradio-container {font-family: 'Helvetica Neue', Arial, sans-serif;}
|
| 473 |
#find-btn {font-weight: 700; border-radius: 10px;}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
"""
|
| 475 |
|
| 476 |
with gr.Blocks(title="AI Fashion Stylist", css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
|
@@ -508,17 +585,19 @@ with gr.Blocks(title="AI Fashion Stylist", css=CUSTOM_CSS, theme=gr.themes.Soft(
|
|
| 508 |
label="Quick Starters",
|
| 509 |
)
|
| 510 |
|
| 511 |
-
with gr.Tab("
|
| 512 |
-
|
| 513 |
-
|
|
|
|
| 514 |
skin_b = gr.Dropdown(SKIN_TONES, label="Skin tone", value=SKIN_TONES[0])
|
| 515 |
undertone_b = gr.Dropdown(UNDERTONES, label="Undertone", value=UNDERTONES[0])
|
| 516 |
style_b = gr.Dropdown(STYLES, label="Style preference", value=STYLES[0])
|
|
|
|
| 517 |
gender_b = gr.Dropdown(GENDERS, label="Gender", value=GENDERS[0])
|
| 518 |
age_b = gr.Dropdown(AGE_GROUPS, label="Age group", value=AGE_GROUPS[0])
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
style_card_b = gr.HTML(label="Style Profile")
|
| 523 |
new_img_b = gr.Image(label="✨ Your New AI-Generated Look")
|
| 524 |
answer_b = gr.Textbox(label="Answer to your question")
|
|
@@ -543,4 +622,4 @@ with gr.Blocks(title="AI Fashion Stylist", css=CUSTOM_CSS, theme=gr.themes.Soft(
|
|
| 543 |
)
|
| 544 |
|
| 545 |
if __name__ == "__main__":
|
| 546 |
-
demo.launch()
|
|
|
|
| 355 |
return palette[hash(key) % len(palette)]
|
| 356 |
|
| 357 |
|
| 358 |
+
def _lighten_hex(hex_color, factor=0.82):
|
| 359 |
+
hex_color = hex_color.lstrip("#")
|
| 360 |
+
r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
|
| 361 |
+
r = int(r + (255 - r) * factor)
|
| 362 |
+
g = int(g + (255 - g) * factor)
|
| 363 |
+
b = int(b + (255 - b) * factor)
|
| 364 |
+
return f"#{r:02x}{g:02x}{b:02x}"
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
_PALETTE_NAMES = {
|
| 368 |
+
"fair": {"cool": "Frost Bloom", "warm": "Sunlit Honey", "neutral": "Soft Linen"},
|
| 369 |
+
"light": {"cool": "Moonlit Rose", "warm": "Amber Glow", "neutral": "Quiet Dawn"},
|
| 370 |
+
"medium": {"cool": "Dusty Orchid", "warm": "Spiced Caramel", "neutral": "Warm Clay"},
|
| 371 |
+
"olive": {"cool": "Sage Twilight", "warm": "Bronzed Olive", "neutral": "Earth Tone"},
|
| 372 |
+
"tan": {"cool": "Copper Mist", "warm": "Golden Sand", "neutral": "Desert Bloom"},
|
| 373 |
+
"deep": {"cool": "Midnight Jewel", "warm": "Mahogany Sun", "neutral": "Onyx Velvet"},
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
def get_palette_name(skin_tone, undertone):
|
| 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))
|
|
|
|
| 397 |
f'<span style="font-size:11px;color:#6b7280;">{c.title()}</span></div>'
|
| 398 |
for c in colors
|
| 399 |
)
|
| 400 |
+
palette_name = get_palette_name(row["skin_tone"], row["undertone"])
|
| 401 |
return f"""
|
| 402 |
<div style="font-family:inherit;">
|
| 403 |
+
<div style="position:relative;overflow:hidden;background:#15131f;color:white;
|
| 404 |
+
border-radius:18px;padding:24px 28px;margin-bottom:16px;">
|
| 405 |
+
<div style="position:absolute;top:-40px;right:-40px;width:150px;height:150px;border-radius:50%;
|
| 406 |
+
background:radial-gradient(circle,#5b2a52,transparent 70%);"></div>
|
| 407 |
+
<div style="font-size:11px;letter-spacing:1.5px;color:#f48fb1;font-weight:700;">YOUR COLOR PALETTE</div>
|
| 408 |
+
<div style="font-size:30px;font-weight:800;margin:6px 0 4px;">{palette_name}</div>
|
| 409 |
+
<div style="font-size:13px;color:#bdb8c9;">
|
| 410 |
+
{row['skin_tone'].title()} skin · {row['undertone'].title()} undertone · {row['style_preference'].title()} style
|
| 411 |
+
</div>
|
| 412 |
</div>
|
| 413 |
<div style="display:flex;gap:14px;flex-wrap:wrap;">
|
| 414 |
<div style="flex:1;min-width:220px;background:#f9fafb;border-radius:14px;padding:18px;">
|
|
|
|
| 416 |
<div style="display:flex;flex-wrap:wrap;">{swatches}</div>
|
| 417 |
</div>
|
| 418 |
<div style="flex:1;min-width:220px;background:#f9fafb;border-radius:14px;padding:18px;">
|
| 419 |
+
<div style="font-size:11px;letter-spacing:1.5px;color:#a855f7;font-weight:700;margin-bottom:8px;">STYLIST TIP</div>
|
| 420 |
<div style="font-size:14px;color:#1f2937;line-height:1.4;">{caption}</div>
|
| 421 |
</div>
|
| 422 |
</div>
|
|
|
|
| 424 |
"""
|
| 425 |
|
| 426 |
|
| 427 |
+
def build_outfit_component_cards_html(row):
|
| 428 |
+
"""One card per garment piece of the TOP match (top/bottom/shoes/accessory) -
|
| 429 |
+
mirrors a 'your outfit, broken into shoppable pieces' layout."""
|
| 430 |
+
colors = [c.strip() for c in str(row["recommended_colors"]).split(",") if c.strip()] or ["neutral"]
|
| 431 |
+
components = [
|
| 432 |
+
("TOP", row.get("outfit_top", "Top"), "zara", "search_query_zara"),
|
| 433 |
+
("BOTTOM", row.get("outfit_bottom", "Bottom"), "hm", "search_query_hm"),
|
| 434 |
+
("SHOES", row.get("outfit_shoes", "Shoes"), "asos", "search_query_asos"),
|
| 435 |
+
("ACCESSORY", row.get("outfit_accessory", "Accessory"), "mango", "search_query_mango"),
|
| 436 |
+
]
|
| 437 |
+
cards = []
|
| 438 |
+
for i, (label, item_name, retailer, col) in enumerate(components):
|
| 439 |
+
dot_color = _swatch_color(colors[i % len(colors)])
|
| 440 |
+
banner_color = _lighten_hex(dot_color)
|
| 441 |
+
link = to_shop_link(retailer, row[col], row["gender"])
|
| 442 |
+
cards.append(f"""
|
| 443 |
+
<div style="border:1px solid #eee;border-radius:14px;overflow:hidden;width:185px;
|
| 444 |
+
box-shadow:0 1px 3px rgba(0,0,0,0.05);">
|
| 445 |
+
<div style="background:{banner_color};height:64px;display:flex;align-items:center;justify-content:center;">
|
| 446 |
+
<div style="width:30px;height:30px;border-radius:50%;background:{dot_color};border:2px solid white;"></div>
|
| 447 |
+
</div>
|
| 448 |
+
<div style="padding:12px 14px;">
|
| 449 |
+
<div style="font-size:10px;letter-spacing:1px;color:#d6336c;font-weight:700;">{label}</div>
|
| 450 |
+
<div style="font-size:13px;font-weight:600;margin:4px 0 2px;color:#111827;">{str(item_name).title()}</div>
|
| 451 |
+
<div style="font-size:11px;color:#9ca3af;margin-bottom:10px;">{retailer.upper()}</div>
|
| 452 |
+
<a href="{link}" target="_blank" style="display:block;text-align:center;background:#111827;
|
| 453 |
+
color:white;border-radius:8px;padding:8px;text-decoration:none;font-size:12px;font-weight:600;">
|
| 454 |
+
Shop ↗
|
| 455 |
+
</a>
|
| 456 |
+
</div>
|
| 457 |
+
</div>
|
| 458 |
+
""")
|
| 459 |
+
return f'<div style="display:flex;gap:14px;flex-wrap:wrap;">{"".join(cards)}</div>'
|
| 460 |
+
|
| 461 |
+
|
| 462 |
+
def build_more_matches_html(matched_indices, matched_rows, similarities, skip_first=True):
|
| 463 |
+
"""Smaller secondary row: the other catalog matches, as full-look thumbnails
|
| 464 |
+
(keeps the '3 similar items' requirement visible alongside the per-garment cards above)."""
|
| 465 |
+
items = list(zip(matched_indices, similarities))[1:] if skip_first else list(zip(matched_indices, similarities))
|
| 466 |
+
if not items:
|
| 467 |
+
return ""
|
| 468 |
cards = []
|
| 469 |
+
for idx, sim in items:
|
| 470 |
row = df.iloc[idx]
|
| 471 |
img_b64 = pil_to_base64(images[idx])
|
| 472 |
link = to_shop_link("zara", row["search_query_zara"], row["gender"])
|
| 473 |
cards.append(f"""
|
| 474 |
+
<div style="border:1px solid #eee;border-radius:12px;overflow:hidden;width:140px;">
|
| 475 |
+
<img src="data:image/jpeg;base64,{img_b64}" style="width:100%;height:150px;object-fit:cover;display:block;"/>
|
| 476 |
+
<div style="padding:8px 10px;">
|
| 477 |
+
<div style="font-size:10px;color:#9ca3af;">{sim}</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
<a href="{link}" target="_blank" style="display:block;text-align:center;background:#111827;
|
| 479 |
+
color:white;border-radius:6px;padding:6px;text-decoration:none;font-size:11px;margin-top:6px;">
|
| 480 |
+
Shop ↗
|
| 481 |
</a>
|
| 482 |
</div>
|
| 483 |
</div>
|
| 484 |
""")
|
| 485 |
+
return (
|
| 486 |
+
'<div style="font-size:11px;letter-spacing:1.5px;color:#9ca3af;font-weight:700;'
|
| 487 |
+
'margin:18px 0 8px;">MORE MATCHES LIKE THIS</div>'
|
| 488 |
+
f'<div style="display:flex;gap:12px;flex-wrap:wrap;">{"".join(cards)}</div>'
|
| 489 |
+
)
|
| 490 |
|
| 491 |
|
| 492 |
# ---------------------------------------------------------------------------
|
|
|
|
| 516 |
answer = answer_question_about_image(edit_base_image, question) # Pattern 2
|
| 517 |
|
| 518 |
style_card_html = build_style_card_html(top_row, caption)
|
| 519 |
+
outfit_cards_html = (
|
| 520 |
+
build_outfit_component_cards_html(top_row)
|
| 521 |
+
+ build_more_matches_html(matched_indices, matched_rows, similarity)
|
| 522 |
+
)
|
| 523 |
return style_card_html, new_image, answer, outfit_cards_html
|
| 524 |
|
| 525 |
|
|
|
|
| 544 |
CUSTOM_CSS = """
|
| 545 |
.gradio-container {font-family: 'Helvetica Neue', Arial, sans-serif;}
|
| 546 |
#find-btn {font-weight: 700; border-radius: 10px;}
|
| 547 |
+
.dark-panel {background: #15131f !important; border-radius: 16px !important; padding: 18px !important;}
|
| 548 |
+
.dark-panel label, .dark-panel span {color: #e5e1f0 !important;}
|
| 549 |
+
.big-btn {background: #111827 !important; border: none !important; border-radius: 10px !important;
|
| 550 |
+
font-size: 16px !important; padding: 12px !important;}
|
| 551 |
"""
|
| 552 |
|
| 553 |
with gr.Blocks(title="AI Fashion Stylist", css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
|
|
|
| 585 |
label="Quick Starters",
|
| 586 |
)
|
| 587 |
|
| 588 |
+
with gr.Tab("🎨 Choose Manually"):
|
| 589 |
+
gr.Markdown("Select your skin tone, undertone and style below.")
|
| 590 |
+
with gr.Group(elem_classes="dark-panel"):
|
| 591 |
+
with gr.Row():
|
| 592 |
skin_b = gr.Dropdown(SKIN_TONES, label="Skin tone", value=SKIN_TONES[0])
|
| 593 |
undertone_b = gr.Dropdown(UNDERTONES, label="Undertone", value=UNDERTONES[0])
|
| 594 |
style_b = gr.Dropdown(STYLES, label="Style preference", value=STYLES[0])
|
| 595 |
+
with gr.Row():
|
| 596 |
gender_b = gr.Dropdown(GENDERS, label="Gender", value=GENDERS[0])
|
| 597 |
age_b = gr.Dropdown(AGE_GROUPS, label="Age group", value=AGE_GROUPS[0])
|
| 598 |
+
question_b = gr.Textbox(label="Ask the stylist a question about the top match (optional)",
|
| 599 |
+
placeholder="e.g. Is this outfit formal or casual?")
|
| 600 |
+
btn_b = gr.Button("Get My Look ✨", elem_id="find-btn", elem_classes="big-btn", variant="primary")
|
| 601 |
style_card_b = gr.HTML(label="Style Profile")
|
| 602 |
new_img_b = gr.Image(label="✨ Your New AI-Generated Look")
|
| 603 |
answer_b = gr.Textbox(label="Answer to your question")
|
|
|
|
| 622 |
)
|
| 623 |
|
| 624 |
if __name__ == "__main__":
|
| 625 |
+
demo.launch()
|