Anusha806 commited on
Commit
b88fc43
·
1 Parent(s): cd83986
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -472,16 +472,25 @@ def search_fashion(query: str, alpha: float):
472
  imgs_with_captions.append((padded, caption))
473
 
474
  return imgs_with_captions
475
- def search_by_image_only(uploaded_image, top_k=12):
476
- if uploaded_image is None:
477
- return []
478
 
479
- uploaded_image = uploaded_image.convert("RGB")
480
- dense_vec = model.encode(uploaded_image).tolist()
 
 
 
 
 
 
 
 
 
 
 
481
 
 
482
  result = index.query(
483
- vector=dense_vec,
484
- top_k=top_k,
485
  include_metadata=True
486
  )
487
 
@@ -493,12 +502,13 @@ def search_by_image_only(uploaded_image, top_k=12):
493
  if not isinstance(img, Image.Image):
494
  img = Image.fromarray(np.array(img))
495
  padded = ImageOps.pad(img, (256, 256), color="white")
496
- caption = meta.get("productDisplayName", "Unknown Product")
497
  imgs_with_captions.append((padded, caption))
498
 
499
  return imgs_with_captions
500
 
501
 
 
502
  # ------------------- Gradio UI -------------------
503
  custom_css = """
504
  .search-btn {
 
472
  imgs_with_captions.append((padded, caption))
473
 
474
  return imgs_with_captions
 
 
 
475
 
476
+ \from PIL import Image, ImageOps
477
+ import numpy as np
478
+
479
+ def search_by_image(uploaded_image, alpha=0.5):
480
+ """
481
+ Given a PIL image from Gradio, find visually similar products.
482
+ """
483
+ # Preprocess as CLIP expects
484
+ processed = clip_processor(images=uploaded_image, return_tensors="pt").to(device)
485
+
486
+ with torch.no_grad():
487
+ image_vec = clip_model.get_image_features(**processed)
488
+ image_vec = image_vec.cpu().numpy().flatten().tolist()
489
 
490
+ # since your Pinecone is purely visual, we query on visual vector
491
  result = index.query(
492
+ top_k=12,
493
+ vector=image_vec,
494
  include_metadata=True
495
  )
496
 
 
502
  if not isinstance(img, Image.Image):
503
  img = Image.fromarray(np.array(img))
504
  padded = ImageOps.pad(img, (256, 256), color="white")
505
+ caption = str(meta.get("productDisplayName", "Unknown Product"))
506
  imgs_with_captions.append((padded, caption))
507
 
508
  return imgs_with_captions
509
 
510
 
511
+
512
  # ------------------- Gradio UI -------------------
513
  custom_css = """
514
  .search-btn {