romi2001 commited on
Commit
b0d783f
·
verified ·
1 Parent(s): 2e49b9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -225
app.py CHANGED
@@ -259,7 +259,7 @@ def generate_new_outfit_image(pil_image, row, target_gender=None):
259
  resized_face_crop = base.resize((head_w, head_h))
260
  resized_protect_img = Image.fromarray(protect.astype(np.uint8) * 255).resize((head_w, head_h), resample=Image.NEAREST)
261
 
262
- # FIX: Calculation of coordinates performed BEFORE pasting inside the canvas space
263
  paste_x = (canvas_w - head_w) // 2
264
  paste_y = int(canvas_h * 0.03)
265
 
@@ -308,227 +308,4 @@ def build_outfit_component_cards_html(row):
308
  {"category": "DRESS", "name": "Classic Slip Midi Dress", "retailer": "zara", "query": "terracotta slip midi dress"}
309
  ]
310
 
311
- cards = []
312
- for i, comp in enumerate(components):
313
- if isinstance(comp, dict):
314
- label, item_name, retailer = comp["category"], comp["name"], comp["retailer"]
315
- link = to_shop_link(retailer, comp["query"], row["gender"])
316
- else:
317
- label, item_name, retailer, col = comp
318
- link = to_shop_link(retailer, row.get(col, item_name), row["gender"])
319
-
320
- raw_color = _swatch_color(colors[i % len(colors)])
321
- banner_color = _lighten_hex(raw_color, 0.88)
322
-
323
- card_html = f'<div class="product-card">'
324
- card_html += f'<div class="card-color-header" style="background-color:{banner_color};">'
325
- card_html += f'<div class="prod-bubble" style="background-color:{raw_color};"></div>'
326
- card_html += f'</div>'
327
- card_html += f'<div class="prod-meta">'
328
- card_html += f'<span class="prod-cat">{label}</span>'
329
- card_html += f'<p class="prod-title">{str(item_name).title()}</p>'
330
- card_html += f'<span class="prod-brand">{retailer.upper()}</span>'
331
- card_html += f'<a href="{link}" target="_blank" rel="noopener noreferrer" class="shop-btn">Shop ↗</a>'
332
- card_html += f'</div>'
333
- card_html += f'</div>'
334
- cards.append(card_html)
335
-
336
- return f'<div class="outfit-grid">{"".join(cards)}</div>'
337
-
338
- def build_more_matches_html(matched_indices, similarities):
339
- cards = []
340
- for idx, sim in list(zip(matched_indices, similarities))[1:4]:
341
- row = df.iloc[idx]
342
- img_b64 = pil_to_base64(images[idx])
343
- link = to_shop_link("zara", row.get("search_query_zara", "clothing"), row["gender"])
344
- card_html = f'<div class="product-card">'
345
- card_html += f'<img src="data:image/jpeg;base64,{img_b64}" style="width:100%;height:150px;object-fit:cover;display:block;"/>'
346
- card_html += f'<div class="prod-meta">'
347
- card_html += f'<span class="prod-brand">Match score: {sim}</span>'
348
- card_html += f'<a href="{link}" target="_blank" rel="noopener noreferrer" class="shop-btn">Shop ↗</a>'
349
- card_html += f'</div></div>'
350
- cards.append(card_html)
351
- return f'<div class="more-matches-label">MORE MATCHES LIKE THIS</div><div class="outfit-grid">{"".join(cards)}</div>'
352
-
353
- # ---------------------------------------------------------------------------
354
- # MAIN PIPELINES
355
- # ---------------------------------------------------------------------------
356
- def run_pipeline(matched_indices, matched_rows, matched_scores, base_image=None, question=None, expected_gender=None):
357
- if len(matched_indices) == 0:
358
- return "<i>No matches found — try different filters.</i>", None, "", "<div></div>"
359
- similarity = [round(1.0 - (d / 2.0), 3) for d in matched_scores]
360
- top_row = matched_rows.iloc[0]
361
- edit_base_image = base_image if base_image is not None else images[matched_indices[0]]
362
- gender_for_generation = expected_gender or top_row["gender"]
363
-
364
- caption = generate_stylist_caption(top_row)
365
- new_image, _ = generate_new_outfit_image(edit_base_image, top_row, target_gender=gender_for_generation)
366
- answer = answer_question_about_image(edit_base_image, question) if question else ""
367
-
368
- style_card_html = build_style_card_html(top_row, caption)
369
- outfit_cards_html = build_outfit_component_cards_html(top_row) + build_more_matches_html(matched_indices, similarity)
370
- return style_card_html, new_image, answer, outfit_cards_html
371
-
372
- def recommend_from_photo(photo, gender, age_group, question):
373
- if photo is None:
374
- return "<i>Please upload a photo or pick a Quick Starter.</i>", None, "", "<div></div>"
375
- query_emb = embed_query_image(photo)
376
- idx, rows, scores = faiss_filtered_search(query_emb, gender=gender or None, age_group=age_group or None)
377
- return run_pipeline(idx, rows, scores, base_image=photo, question=question, expected_gender=gender)
378
-
379
- def recommend_from_features(skin_tone, undertone, style, gender, age_group, question):
380
- sentence = build_feature_sentence(skin_tone, undertone, style, gender, age_group)
381
- query_emb = embed_query_text(sentence)
382
- idx, rows, scores = faiss_filtered_search(query_emb, gender=gender or None, age_group=age_group or None)
383
- return run_pipeline(idx, rows, scores, base_image=None, question=question, expected_gender=gender)
384
-
385
- def build_feature_pills_html(labels, selected, title):
386
- pills = ""
387
- for label in labels:
388
- active = str(label).strip().lower() == str(selected).strip().lower()
389
- border = "2px solid #D2527F" if active else "1.5px solid #ECE4D6"
390
- bg = "#FFF0F5" if active else "#FFFFFF"
391
- tcol = "#D2527F" if active else "#2C2A29"
392
- dot = _swatch_color(label) if title != "Style" else "#C69E6E"
393
- pills += f'<div style="display:inline-flex;align-items:center;gap:7px;padding:8px 14px;border-radius:30px;border:{border};background:{bg};margin:4px;"><div style="width:12px;height:12px;border-radius:50%;background:{dot};border:1px solid rgba(0,0,0,.1);"></div><span style="font-size:13px;font-weight:600;color:{tcol};">{str(label).title()}</span></div>'
394
- return f'<div style="margin-bottom:14px;"><div style="font-size:11px;font-weight:700;color:#8A7F6E;text-transform:uppercase;letter-spacing:.1em;margin-bottom:8px;">{title}</div><div style="display:flex;flex-wrap:wrap;margin:-4px;">{pills}</div></div>'
395
-
396
- def build_features_recap_html(skin_tone, undertone, style):
397
- return f'<div style="background:#fff;border-radius:16px;padding:24px;margin-bottom:20px;border:1px solid #ECE4D6;"><div style="font-size:12px;font-weight:700;color:#D2527F;text-transform:uppercase;letter-spacing:.1em;margin-bottom:16px;">Your Selected Specifications</div>{build_feature_pills_html(SKIN_TONES, skin_tone, "Skin tone")}{build_feature_pills_html(UNDERTONES, undertone, "Undertone")}{build_feature_pills_html(STYLES, style, "Style")}</div>'
398
-
399
- # ---------------------------------------------------------------------------
400
- # FIXED GRADIO 6 COMPLIANT LUXURY THEME
401
- # ---------------------------------------------------------------------------
402
- CUSTOM_CSS = """
403
- @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700&family=Inter:wght@400;500;600;700&display=swap');
404
- body, .gradio-container { background-color: #F8F5F5 !important; font-family: 'Inter', sans-serif !important; }
405
- .gradio-container { max-width: 850px !important; margin: 0 auto !important; padding-top: 20px !important; }
406
- footer { display: none !important; }
407
-
408
- h1, h2, h3, p, span, label, input, select, textarea, button { color: #2C2A29 !important; }
409
-
410
- .tab-nav button { font-size: 14px !important; font-weight: 600 !important; padding: 14px 24px !important; color: #555 !important; }
411
- .tab-nav button.selected { color: #D2527F !important; border-bottom: 2px solid #D2527F !important; }
412
-
413
- #find-btn, .big-btn { background: #161617 !important; color: #FFFFFF !important; border: none !important; border-radius: 8px !important; font-weight: 700 !important; font-size: 14px !important; padding: 14px !important; text-transform: uppercase; letter-spacing: .08em !important; width: 100% !important; margin-top: 10px; }
414
- #find-btn:hover, .big-btn:hover { background: #2D2D2F !important; }
415
-
416
- .dark-panel { background: #FFFFFF !important; border-radius: 16px !important; padding: 24px !important; border: 1px solid #ECE4D6 !important; margin-bottom: 20px; }
417
- .dark-panel label, .dark-panel span { color: #2C2A29 !important; font-weight: 600; }
418
-
419
- input, select, .secondary, .wrap, .slots, .single-select, .select-wrap {
420
- color: #2C2A29 !important;
421
- background-color: #FFFFFF !important;
422
- border: 1px solid #E3DFDA !important;
423
- border-radius: 4px !important;
424
- }
425
- div.form { background: transparent !important; border: none !important; box-shadow: none !important; }
426
- fieldset { display: flex !important; justify-content: center !important; gap: 24px !important; border: none !important; background: transparent !important; }
427
-
428
- .palette-premium-banner { background: #FAF3ED; padding: 24px; border-radius: 12px; margin-bottom: 20px; border-left: 5px solid #C69E6E; }
429
- .section-split { display: grid !important; grid-template-columns: repeat(2, 1fr) !important; gap: 20px !important; margin-top: 20px !important; width: 100% !important; }
430
- @media (max-width: 768px) { .section-split { grid-template-columns: 1fr !important; } }
431
-
432
- .results-box { background: #FFFFFF; border-radius: 16px; padding: 24px; border: 1px solid #EFECE8; }
433
- .results-box h3 { font-size: 11px; font-weight: 700; color: #9C8E82 !important; letter-spacing: 1.5px; text-transform: uppercase; margin: 0 0 14px; }
434
- .swatch-grid { display: flex; gap: 16px; flex-wrap: wrap; }
435
- .swatch-card { text-align: center; font-size: 12px; color: #666666 !important; width: 60px; }
436
- .color-bubble { width: 44px; height: 44px; border-radius: 50%; margin: 0 auto 6px; border: 1px solid rgba(0,0,0,.06); box-shadow: inset 0 0 0 2px #FFF; }
437
- .tip-text { font-size: 15px; color: #222222 !important; line-height: 1.6; margin: 0; font-style: italic; }
438
-
439
- .outfit-grid { display: grid !important; grid-template-columns: repeat(3, 1fr) !important; gap: 20px !important; margin-top: 20px !important; width: 100% !important; }
440
- @media (max-width: 768px) { .outfit-grid { grid-template-columns: repeat(2, 1fr) !important; } }
441
- @media (max-width: 480px) { .outfit-grid { grid-template-columns: 1fr !important; } }
442
-
443
- .product-card { background: #FFFFFF; border: 1px solid #EFECE8; border-radius: 16px; overflow: hidden; display: flex; flex-direction: column; box-shadow: 0 4px 12px rgba(0,0,0,0.01); width: 100% !important; }
444
- .card-color-header { width: 100%; height: 95px; display: flex; align-items: center; justify-content: center; }
445
- .prod-bubble { width: 46px; height: 46px; border-radius: 50%; box-shadow: 0 2px 8px rgba(0,0,0,0.04); }
446
- .prod-meta { padding: 20px; display: flex; flex-direction: column; align-items: flex-start; text-align: left; width: 100%; }
447
- .prod-cat { font-size: 11px; font-weight: 700; color: #D2527F !important; letter-spacing: 0.5px; text-transform: uppercase; margin-bottom: 4px; }
448
- .prod-title { font-size: 15px; font-weight: 700; color: #111111 !important; margin: 0 0 4px 0; line-height: 1.3; min-height: 40px; display: flex; align-items: center; }
449
- .prod-brand { font-size: 13px; color: #999999 !important; margin-bottom: 14px; display: block; }
450
-
451
- .shop-btn { display: block; width: 100%; background: #161617; color: #FFFFFF !important; text-align: center; padding: 11px 0; border-radius: 8px; font-size: 13px; font-weight: 700; text-decoration: none !important; letter-spacing: 0.5px; }
452
- .shop-btn:hover { background: #2D2D2F; color: #FFFFFF !important; }
453
- .more-matches-label { font-size: 11px; font-weight: 700; color: #9C8E82 !important; letter-spacing: 1.6px; text-transform: uppercase; margin: 24px 0 12px; }
454
- .palette-name { font-family: 'Playfair Display', serif !important; }
455
- """
456
-
457
- # ---------------------------------------------------------------------------
458
- # INTERFACE BUILD
459
- # ---------------------------------------------------------------------------
460
- with gr.Blocks(title="Personal Color Styling") as demo:
461
- gr.HTML("""
462
- <div style="text-align:center; padding:24px 20px 10px;">
463
- <div style="font-size:11px; font-weight:700; color:#D2527F; letter-spacing:.18em; text-transform:uppercase; margin-bottom:8px;">Personal Color Styling</div>
464
- <div class="palette-name" style="font-size:38px; font-weight:700; color:#111; margin-bottom:6px;">LookMatch</div>
465
- <div style="font-size:14px; color:#666; max-width:480px; margin:0 auto; line-height:1.6;">
466
- Upload a photo or describe your features — receive a curated palette, a personal stylist note, and a brand-new look generated just for you.
467
- </div>
468
- </div>
469
- """)
470
-
471
- with gr.Tab("📸 Upload a Photo"):
472
- with gr.Row():
473
- photo_in = gr.Image(type="pil", label="Your photo")
474
- with gr.Column():
475
- gender_a = gr.Dropdown(GENDERS, label="Gender (optional)")
476
- age_a = gr.Dropdown(AGE_GROUPS, label="Age group (optional)")
477
- question_a = gr.Textbox(label="Ask the stylist a question about your photo (optional)", placeholder="e.g. What style would suit me best?")
478
- btn_a = gr.Button("Get my look ✨", elem_id="find-btn", variant="primary")
479
-
480
- style_card_a = gr.HTML()
481
- new_img_a = gr.Image(label="✨ Your New AI-Generated Look")
482
- answer_a = gr.Textbox(label="Answer to your question")
483
- outfit_cards_a = gr.HTML()
484
-
485
- btn_a.click(
486
- recommend_from_photo,
487
- [photo_in, gender_a, age_a, question_a],
488
- [style_card_a, new_img_a, answer_a, outfit_cards_a],
489
- )
490
-
491
- gr.Examples(
492
- examples=[
493
- [SAMPLE_PHOTOS[0], "woman", "adult", "What style would suit me best?"],
494
- [SAMPLE_PHOTOS[1], "man", "adult", "What style would suit me best?"],
495
- [SAMPLE_PHOTOS[2], "woman", "teen", "What style would suit me best?"]
496
- ],
497
- inputs=[photo_in, gender_a, age_a, question_a],
498
- outputs=[style_card_a, new_img_a, answer_a, outfit_cards_a],
499
- fn=recommend_from_photo,
500
- cache_examples=False,
501
- label="Quick Starters",
502
- )
503
-
504
- with gr.Tab("🎨 Choose Manually"):
505
- gr.Markdown("Select your skin tone, undertone and style below.")
506
- with gr.Group(elem_classes="dark-panel"):
507
- with gr.Row():
508
- skin_b = gr.Dropdown(SKIN_TONES, label="Skin tone", value=SKIN_TONES[0])
509
- undertone_b = gr.Dropdown(UNDERTONES, label="Undertone", value=UNDERTONES[0])
510
- style_b = gr.Dropdown(STYLES, label="Style preference", value=STYLES[0])
511
- with gr.Row():
512
- gender_b = gr.Dropdown(GENDERS, label="Gender", value=GENDERS[0])
513
- age_b = gr.Dropdown(AGE_GROUPS, label="Age group", value=AGE_GROUPS[0])
514
- question_b = gr.Textbox(label="Ask the stylist a question about the top match (optional)", placeholder="e.g. Is this outfit formal or casual?")
515
- btn_b = gr.Button("Get my look ✨", elem_id="find-btn", variant="primary")
516
-
517
- features_recap_b = gr.HTML(build_features_recap_html(SKIN_TONES[0], UNDERTONES[0], STYLES[0]))
518
- for _dropdown in (skin_b, undertone_b, style_b):
519
- _dropdown.change(build_features_recap_html, [skin_b, undertone_b, style_b], features_recap_b)
520
-
521
- style_card_b = gr.HTML()
522
- new_img_b = gr.Image(label="✨ Your New AI-Generated Look")
523
- answer_b = gr.Textbox(label="Answer to your question")
524
- outfit_cards_b = gr.HTML()
525
-
526
- btn_b.click(
527
- recommend_from_features,
528
- [skin_b, undertone_b, style_b, gender_b, age_b, question_b],
529
- [style_card_b, new_img_b, answer_b, outfit_cards_b],
530
- )
531
-
532
- if __name__ == "__main__":
533
- demo.launch(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="amber"))
534
-
 
259
  resized_face_crop = base.resize((head_w, head_h))
260
  resized_protect_img = Image.fromarray(protect.astype(np.uint8) * 255).resize((head_w, head_h), resample=Image.NEAREST)
261
 
262
+ # תיקון הלוגיקה: הגדרת מיקומי ההדבקה מתרחשת עכשיו לפני השימוש בהם בקנבס
263
  paste_x = (canvas_w - head_w) // 2
264
  paste_y = int(canvas_h * 0.03)
265
 
 
308
  {"category": "DRESS", "name": "Classic Slip Midi Dress", "retailer": "zara", "query": "terracotta slip midi dress"}
309
  ]
310
 
311
+ cards = []