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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from PIL import Image, ImageOps
|
| 8 |
+
from tqdm.auto import tqdm
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
from sentence_transformers import SentenceTransformer
|
| 11 |
+
from pinecone import Pinecone, ServerlessSpec
|
| 12 |
+
from pinecone_text.sparse import BM25Encoder
|
| 13 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 14 |
+
import openai
|
| 15 |
+
|
| 16 |
+
# ------------------- Keys & Setup -------------------
|
| 17 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
+
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
| 19 |
+
spec = ServerlessSpec(cloud=os.getenv("PINECONE_CLOUD") or "aws", region=os.getenv("PINECONE_REGION") or "us-east-1")
|
| 20 |
+
index_name = "hybrid-image-search"
|
| 21 |
+
|
| 22 |
+
if index_name not in pc.list_indexes().names():
|
| 23 |
+
pc.create_index(index_name, dimension=512, metric='dotproduct', spec=spec)
|
| 24 |
+
while not pc.describe_index(index_name).status['ready']:
|
| 25 |
+
time.sleep(1)
|
| 26 |
+
index = pc.Index(index_name)
|
| 27 |
+
|
| 28 |
+
# ------------------- Models & Dataset -------------------
|
| 29 |
+
fashion = load_dataset("ashraq/fashion-product-images-small", split="train")
|
| 30 |
+
images = fashion["image"]
|
| 31 |
+
metadata = fashion.remove_columns("image").to_pandas()
|
| 32 |
+
bm25 = BM25Encoder()
|
| 33 |
+
bm25.fit(metadata["productDisplayName"])
|
| 34 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 35 |
+
model = SentenceTransformer('sentence-transformers/clip-ViT-B-32', device=device)
|
| 36 |
+
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
|
| 37 |
+
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 38 |
+
|
| 39 |
+
# ------------------- Helper Functions -------------------
|
| 40 |
+
def hybrid_scale(dense, sparse, alpha: float):
|
| 41 |
+
if alpha < 0 or alpha > 1:
|
| 42 |
+
raise ValueError("Alpha must be between 0 and 1")
|
| 43 |
+
hsparse = {
|
| 44 |
+
'indices': sparse['indices'],
|
| 45 |
+
'values': [v * (1 - alpha) for v in sparse['values']]
|
| 46 |
+
}
|
| 47 |
+
hdense = [v * alpha for v in dense]
|
| 48 |
+
return hdense, hsparse
|
| 49 |
+
|
| 50 |
+
def extract_intent_from_openai(query: str):
|
| 51 |
+
prompt = f"""
|
| 52 |
+
You are an assistant for a fashion search engine. Extract the user's intent from the following query.
|
| 53 |
+
Return a Python dictionary with keys: category, gender, subcategory, color.
|
| 54 |
+
If something is missing, use null.
|
| 55 |
+
|
| 56 |
+
Query: "{query}"
|
| 57 |
+
Only return the dictionary.
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
response = openai.ChatCompletion.create(
|
| 61 |
+
model="gpt-4",
|
| 62 |
+
messages=[{"role": "user", "content": prompt}],
|
| 63 |
+
temperature=0
|
| 64 |
+
)
|
| 65 |
+
raw = response.choices[0].message['content']
|
| 66 |
+
structured = eval(raw)
|
| 67 |
+
return structured
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"⚠️ OpenAI intent extraction failed: {e}")
|
| 70 |
+
return {"include": {}, "exclude": {}}
|
| 71 |
+
#-----------------below changed------------------------------#
|
| 72 |
+
|
| 73 |
+
import imagehash
|
| 74 |
+
from PIL import Image
|
| 75 |
+
|
| 76 |
+
def is_duplicate(img, existing_hashes, hash_size=16, tolerance=0):
|
| 77 |
+
"""
|
| 78 |
+
Checks if the image is a near-duplicate based on perceptual hash.
|
| 79 |
+
:param img: PIL Image
|
| 80 |
+
:param existing_hashes: set of previously seen hashes
|
| 81 |
+
:param hash_size: size of the hash (default=16 for more precision)
|
| 82 |
+
:param tolerance: allowable Hamming distance for near-duplicates
|
| 83 |
+
:return: (bool) whether image is duplicate
|
| 84 |
+
"""
|
| 85 |
+
img_hash = imagehash.phash(img, hash_size=hash_size)
|
| 86 |
+
for h in existing_hashes:
|
| 87 |
+
if abs(img_hash - h) <= tolerance:
|
| 88 |
+
return True
|
| 89 |
+
existing_hashes.add(img_hash)
|
| 90 |
+
return False
|
| 91 |
+
|
| 92 |
+
def extract_metadata_filters(query: str):
|
| 93 |
+
query_lower = query.lower()
|
| 94 |
+
gender = None
|
| 95 |
+
category = None
|
| 96 |
+
subcategory = None
|
| 97 |
+
color = None
|
| 98 |
+
|
| 99 |
+
# --- Gender Mapping ---
|
| 100 |
+
gender_map = {
|
| 101 |
+
"men": "Men", "man": "Men", "mens": "Men", "mans": "Men", "male": "Men",
|
| 102 |
+
"women": "Women", "woman": "Women", "womens": "Women", "female": "Women",
|
| 103 |
+
"boys": "Boys", "boy": "Boys",
|
| 104 |
+
"girls": "Girls", "girl": "Girls",
|
| 105 |
+
"kids": "Kids", "kid": "Kids",
|
| 106 |
+
"unisex": "Unisex"
|
| 107 |
+
}
|
| 108 |
+
for term, mapped_value in gender_map.items():
|
| 109 |
+
if term in query_lower:
|
| 110 |
+
gender = mapped_value
|
| 111 |
+
break
|
| 112 |
+
|
| 113 |
+
# --- Category Mapping ---
|
| 114 |
+
category_map = {
|
| 115 |
+
"shirt": "Shirts",
|
| 116 |
+
"tshirt": "Tshirts",
|
| 117 |
+
"t-shirt": "Tshirts",
|
| 118 |
+
"jeans": "Jeans",
|
| 119 |
+
"watch": "Watches",
|
| 120 |
+
"kurta": "Kurtas",
|
| 121 |
+
"dress": "Dresses",
|
| 122 |
+
"trousers": "Trousers", "pants": "Trousers",
|
| 123 |
+
"shorts": "Shorts",
|
| 124 |
+
"footwear": "Footwear",
|
| 125 |
+
"shoes": "Shoes",
|
| 126 |
+
"fashion": "Apparel"
|
| 127 |
+
}
|
| 128 |
+
for term, mapped_value in category_map.items():
|
| 129 |
+
if term in query_lower:
|
| 130 |
+
category = mapped_value
|
| 131 |
+
break
|
| 132 |
+
|
| 133 |
+
# --- SubCategory Mapping ---
|
| 134 |
+
subCategory_list = [
|
| 135 |
+
"Accessories", "Apparel Set", "Bags", "Bath and Body", "Beauty Accessories",
|
| 136 |
+
"Belts", "Bottomwear", "Cufflinks", "Dress", "Eyes", "Eyewear", "Flip Flops",
|
| 137 |
+
"Fragrance", "Free Gifts", "Gloves", "Hair", "Headwear", "Home Furnishing",
|
| 138 |
+
"Innerwear", "Jewellery", "Lips", "Loungewear and Nightwear", "Makeup",
|
| 139 |
+
"Mufflers", "Nails", "Perfumes", "Sandal", "Saree", "Scarves", "Shoe Accessories",
|
| 140 |
+
"Shoes", "Skin", "Skin Care", "Socks", "Sports Accessories", "Sports Equipment",
|
| 141 |
+
"Stoles", "Ties", "Topwear", "Umbrellas", "Vouchers", "Wallets", "Watches",
|
| 142 |
+
"Water Bottle", "Wristbands"
|
| 143 |
+
]
|
| 144 |
+
if "topwear" in query_lower or "top" in query_lower:
|
| 145 |
+
subcategory = "Topwear"
|
| 146 |
+
else:
|
| 147 |
+
query_words = query_lower.split()
|
| 148 |
+
for subcat in subCategory_list:
|
| 149 |
+
if subcat.lower() in query_words:
|
| 150 |
+
subcategory = subcat
|
| 151 |
+
break
|
| 152 |
+
|
| 153 |
+
# --- Color Extraction ---
|
| 154 |
+
color_list = [
|
| 155 |
+
"red", "blue", "green", "yellow", "black", "white",
|
| 156 |
+
"orange", "pink", "purple", "brown", "grey", "beige"
|
| 157 |
+
]
|
| 158 |
+
for c in color_list:
|
| 159 |
+
if c in query_lower:
|
| 160 |
+
color = c.capitalize()
|
| 161 |
+
break
|
| 162 |
+
|
| 163 |
+
# --- Invalid pairs ---
|
| 164 |
+
invalid_pairs = {
|
| 165 |
+
("Men", "Dresses"), ("Men", "Sarees"), ("Men", "Skirts"),
|
| 166 |
+
("Boys", "Dresses"), ("Boys", "Sarees"),
|
| 167 |
+
("Girls", "Boxers"), ("Men", "Heels")
|
| 168 |
+
}
|
| 169 |
+
if (gender, category) in invalid_pairs:
|
| 170 |
+
print(f"⚠️ Invalid pair: {gender} + {category}, dropping gender")
|
| 171 |
+
gender = None
|
| 172 |
+
|
| 173 |
+
# --- Fallback for missing category ---
|
| 174 |
+
if gender and not category:
|
| 175 |
+
category = "Apparel"
|
| 176 |
+
|
| 177 |
+
# --- Refine subcategory for party/wedding-related queries ---
|
| 178 |
+
if "party" in query_lower or "wedding" in query_lower or "cocktail" in query_lower:
|
| 179 |
+
if subcategory in ["Loungewear and Nightwear", "Nightdress", "Innerwear"]:
|
| 180 |
+
subcategory = None # reset it to avoid filtering into wrong items
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
return gender, category, subcategory, color
|
| 184 |
+
|
| 185 |
+
# ------------------- Search Functions -------------------
|
| 186 |
+
def search_fashion(query: str, alpha: float, start: int = 0, end: int = 12, gender_override: str = None):
|
| 187 |
+
intent = extract_intent_from_openai(query)
|
| 188 |
+
|
| 189 |
+
include = intent.get("include", {})
|
| 190 |
+
exclude = intent.get("exclude", {})
|
| 191 |
+
|
| 192 |
+
gender = include.get("gender")
|
| 193 |
+
category = include.get("category")
|
| 194 |
+
subcategory = include.get("subcategory")
|
| 195 |
+
color = include.get("color")
|
| 196 |
+
|
| 197 |
+
# Apply override from dropdown
|
| 198 |
+
if gender_override:
|
| 199 |
+
gender = gender_override
|
| 200 |
+
|
| 201 |
+
# Build Pinecone filter
|
| 202 |
+
filter = {}
|
| 203 |
+
|
| 204 |
+
# Inclusion filters
|
| 205 |
+
if gender:
|
| 206 |
+
filter["gender"] = gender
|
| 207 |
+
if category:
|
| 208 |
+
if category in ["Footwear", "Shoes"]:
|
| 209 |
+
filter["articleType"] = {"$regex": ".*(Shoe|Footwear).*"}
|
| 210 |
+
else:
|
| 211 |
+
filter["articleType"] = category
|
| 212 |
+
if subcategory:
|
| 213 |
+
filter["subCategory"] = subcategory
|
| 214 |
+
|
| 215 |
+
# Step 4: Exclude irrelevant items for party-like queries
|
| 216 |
+
query_lower = query.lower()
|
| 217 |
+
if any(word in query_lower for word in ["party", "wedding", "cocktail", "traditional", "reception"]):
|
| 218 |
+
filter.setdefault("subCategory", {})
|
| 219 |
+
if isinstance(filter["subCategory"], dict):
|
| 220 |
+
filter["subCategory"]["$nin"] = [
|
| 221 |
+
"Loungewear and Nightwear", "Nightdress", "Innerwear", "Sleepwear", "Vests", "Boxers"
|
| 222 |
+
]
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
if color:
|
| 226 |
+
filter["baseColour"] = color
|
| 227 |
+
|
| 228 |
+
# Exclusion filters
|
| 229 |
+
exclude_filter = {}
|
| 230 |
+
if exclude.get("color"):
|
| 231 |
+
exclude_filter["baseColour"] = {"$ne": exclude["color"]}
|
| 232 |
+
if exclude.get("subcategory"):
|
| 233 |
+
exclude_filter["subCategory"] = {"$ne": exclude["subcategory"]}
|
| 234 |
+
if exclude.get("category"):
|
| 235 |
+
exclude_filter["articleType"] = {"$ne": exclude["category"]}
|
| 236 |
+
|
| 237 |
+
# Combine all filters
|
| 238 |
+
if filter and exclude_filter:
|
| 239 |
+
final_filter = {"$and": [filter, exclude_filter]}
|
| 240 |
+
elif filter:
|
| 241 |
+
final_filter = filter
|
| 242 |
+
elif exclude_filter:
|
| 243 |
+
final_filter = exclude_filter
|
| 244 |
+
else:
|
| 245 |
+
final_filter = None
|
| 246 |
+
|
| 247 |
+
print(f"🔍 Using filter: {final_filter} (showing {start} to {end})")
|
| 248 |
+
|
| 249 |
+
# Hybrid encoding
|
| 250 |
+
sparse = bm25.encode_queries(query)
|
| 251 |
+
dense = model.encode(query).tolist()
|
| 252 |
+
hdense, hsparse = hybrid_scale(dense, sparse, alpha=alpha)
|
| 253 |
+
|
| 254 |
+
result = index.query(
|
| 255 |
+
top_k=100,
|
| 256 |
+
vector=hdense,
|
| 257 |
+
sparse_vector=hsparse,
|
| 258 |
+
include_metadata=True,
|
| 259 |
+
filter=final_filter
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
# Retry fallback
|
| 263 |
+
if len(result["matches"]) == 0:
|
| 264 |
+
print("⚠️ No results, retrying with alpha=0 sparse only")
|
| 265 |
+
hdense, hsparse = hybrid_scale(dense, sparse, alpha=0)
|
| 266 |
+
result = index.query(
|
| 267 |
+
top_k=100,
|
| 268 |
+
vector=hdense,
|
| 269 |
+
sparse_vector=hsparse,
|
| 270 |
+
include_metadata=True,
|
| 271 |
+
filter=final_filter
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
# Format results
|
| 275 |
+
imgs_with_captions = []
|
| 276 |
+
seen_hashes = set()
|
| 277 |
+
|
| 278 |
+
for r in result["matches"]:
|
| 279 |
+
idx = int(r["id"])
|
| 280 |
+
img = images[idx]
|
| 281 |
+
meta = r.get("metadata", {})
|
| 282 |
+
if not isinstance(img, Image.Image):
|
| 283 |
+
img = Image.fromarray(np.array(img))
|
| 284 |
+
padded = ImageOps.pad(img, (256, 256), color="white")
|
| 285 |
+
caption = str(meta.get("productDisplayName", "Unknown Product"))
|
| 286 |
+
|
| 287 |
+
if not is_duplicate(padded, seen_hashes):
|
| 288 |
+
imgs_with_captions.append((padded, caption))
|
| 289 |
+
|
| 290 |
+
if len(imgs_with_captions) >= end:
|
| 291 |
+
break
|
| 292 |
+
|
| 293 |
+
return imgs_with_captions
|
| 294 |
+
|
| 295 |
+
def search_by_image(uploaded_image, alpha=0.5, start=0, end=12):
|
| 296 |
+
# Step 1: Preprocess image for CLIP model
|
| 297 |
+
processed = clip_processor(images=uploaded_image, return_tensors="pt").to(device)
|
| 298 |
+
|
| 299 |
+
with torch.no_grad():
|
| 300 |
+
image_vec = clip_model.get_image_features(**processed)
|
| 301 |
+
image_vec = image_vec.cpu().numpy().flatten().tolist()
|
| 302 |
+
|
| 303 |
+
# Step 2: Query Pinecone index for similar images
|
| 304 |
+
result = index.query(
|
| 305 |
+
top_k=100, # fetch more to allow deduplication
|
| 306 |
+
vector=image_vec,
|
| 307 |
+
include_metadata=True
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
matches = result["matches"]
|
| 311 |
+
imgs_with_captions = []
|
| 312 |
+
seen_hashes = set()
|
| 313 |
+
|
| 314 |
+
# Step 3: Deduplicate based on image hash
|
| 315 |
+
for r in matches:
|
| 316 |
+
idx = int(r["id"])
|
| 317 |
+
img = images[idx]
|
| 318 |
+
meta = r.get("metadata", {})
|
| 319 |
+
caption = str(meta.get("productDisplayName", "Unknown Product"))
|
| 320 |
+
|
| 321 |
+
if not isinstance(img, Image.Image):
|
| 322 |
+
img = Image.fromarray(np.array(img))
|
| 323 |
+
|
| 324 |
+
padded = ImageOps.pad(img, (256, 256), color="white")
|
| 325 |
+
|
| 326 |
+
if not is_duplicate(padded, seen_hashes):
|
| 327 |
+
imgs_with_captions.append((padded, caption))
|
| 328 |
+
|
| 329 |
+
if len(imgs_with_captions) >= end:
|
| 330 |
+
break
|
| 331 |
+
|
| 332 |
+
return imgs_with_captions
|
| 333 |
+
|
| 334 |
+
import gradio as gr
|
| 335 |
+
import whisper
|
| 336 |
+
|
| 337 |
+
asr_model = whisper.load_model("base")
|
| 338 |
+
|
| 339 |
+
def handle_voice_search(vf_path, a, offset, gender_ui):
|
| 340 |
+
try:
|
| 341 |
+
transcription = asr_model.transcribe(vf_path)["text"].strip()
|
| 342 |
+
except:
|
| 343 |
+
transcription = ""
|
| 344 |
+
filters = extract_intent_from_openai(transcription) if transcription else {}
|
| 345 |
+
gender_override = gender_ui if gender_ui else filters.get("gender")
|
| 346 |
+
results = search_fashion(transcription, a, 0, 12, gender_override)
|
| 347 |
+
seen_ids = {r[1] for r in results}
|
| 348 |
+
return results, 12, transcription, None, gender_override, results, seen_ids
|
| 349 |
+
|
| 350 |
+
custom_css = """
|
| 351 |
+
/* === Background Styling === */
|
| 352 |
+
# html, body {
|
| 353 |
+
# margin: 0;
|
| 354 |
+
# padding: 0;
|
| 355 |
+
# height: 100%;
|
| 356 |
+
# overflow: auto;
|
| 357 |
+
# }
|
| 358 |
+
html, body {
|
| 359 |
+
height: auto;
|
| 360 |
+
min-height: 100%;
|
| 361 |
+
overflow-x: hidden;
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
# #app-bg {
|
| 367 |
+
# min-height: 100vh;
|
| 368 |
+
# display: flex;
|
| 369 |
+
# justify-content: center;
|
| 370 |
+
# align-items: flex-start;
|
| 371 |
+
# background: radial-gradient(circle at center, #C2C5EF 0%, #E0E2F5 100%) !important;
|
| 372 |
+
# background-attachment: fixed;
|
| 373 |
+
# position: relative;
|
| 374 |
+
# overflow-y: auto;
|
| 375 |
+
# padding: 24px;
|
| 376 |
+
# }
|
| 377 |
+
#app-bg {
|
| 378 |
+
background: radial-gradient(circle at center, #C2C5EF 0%, #E0E2F5 100%) !important;
|
| 379 |
+
background-attachment: fixed;
|
| 380 |
+
padding: 24px;
|
| 381 |
+
width: 100%;
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
/* === Main Content Container === */
|
| 388 |
+
# #main-container {
|
| 389 |
+
# width: 95%;
|
| 390 |
+
# max-width: 1100px;
|
| 391 |
+
# margin: 20px auto;
|
| 392 |
+
# padding: 24px;
|
| 393 |
+
# background: #ffffff;
|
| 394 |
+
# border-radius: 18px;
|
| 395 |
+
# box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
|
| 396 |
+
# # border: 2px solid #C2C5EF;
|
| 397 |
+
# border: 2px solid black;
|
| 398 |
+
# position: relative;
|
| 399 |
+
# z-index: 1;
|
| 400 |
+
# overflow: visible;
|
| 401 |
+
# }
|
| 402 |
+
|
| 403 |
+
#main-container {
|
| 404 |
+
width: 95%;
|
| 405 |
+
max-width: 1100px;
|
| 406 |
+
margin: 20px auto;
|
| 407 |
+
padding: 24px;
|
| 408 |
+
background: #ffffff;
|
| 409 |
+
border-radius: 18px;
|
| 410 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
|
| 411 |
+
# border: 2px solid #C2C5EF;
|
| 412 |
+
border: 2px solid black;
|
| 413 |
+
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
/* === Card Containers === */
|
| 419 |
+
.gr-box, .gr-block, .gr-column, .gr-row, .gr-tab {
|
| 420 |
+
background-color: #C2C5EF !important;
|
| 421 |
+
color: #22284F !important;
|
| 422 |
+
border-radius: 12px;
|
| 423 |
+
padding: 16px !important;
|
| 424 |
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
/* === Headings === */
|
| 428 |
+
h1, .gr-markdown h1 {
|
| 429 |
+
font-size: 2.2rem !important;
|
| 430 |
+
font-weight: bold;
|
| 431 |
+
color: #22284F;
|
| 432 |
+
text-align: center;
|
| 433 |
+
margin-bottom: 1rem;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
/* === Inputs === */
|
| 437 |
+
input[type="text"],
|
| 438 |
+
.gr-textbox textarea,
|
| 439 |
+
.gr-dropdown,
|
| 440 |
+
.gr-slider {
|
| 441 |
+
background-color: #C2C5EF !important;
|
| 442 |
+
color: #22284F !important;
|
| 443 |
+
border-radius: 8px;
|
| 444 |
+
border: 1px solid #999 !important;
|
| 445 |
+
padding: 10px !important;
|
| 446 |
+
font-size: 16px;
|
| 447 |
+
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
/* === Gallery Grid === */
|
| 451 |
+
.gr-gallery {
|
| 452 |
+
padding-top: 12px;
|
| 453 |
+
overflow-y: auto;
|
| 454 |
+
}
|
| 455 |
+
.gr-gallery-item {
|
| 456 |
+
width: 128px !important;
|
| 457 |
+
height: 128px !important;
|
| 458 |
+
border-radius: 8px;
|
| 459 |
+
overflow: hidden;
|
| 460 |
+
background-color: #C2C5EF;
|
| 461 |
+
color: #22284F;
|
| 462 |
+
transition: transform 0.3s ease-in-out;
|
| 463 |
+
}
|
| 464 |
+
.gr-gallery-item:hover {
|
| 465 |
+
transform: scale(1.06);
|
| 466 |
+
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
|
| 467 |
+
}
|
| 468 |
+
.gr-gallery-item img {
|
| 469 |
+
object-fit: cover;
|
| 470 |
+
width: 100%;
|
| 471 |
+
height: 100%;
|
| 472 |
+
border-radius: 8px;
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
/* === Audio & Image === */
|
| 476 |
+
.gr-audio, .gr-image {
|
| 477 |
+
width: 100% !important;
|
| 478 |
+
max-width: 500px !important;
|
| 479 |
+
margin: 0 auto;
|
| 480 |
+
border-radius: 12px;
|
| 481 |
+
background-color: #C2C5EF !important;
|
| 482 |
+
color: #22284F !important;
|
| 483 |
+
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
.gr-image {
|
| 487 |
+
height: 256px !important;
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
/* === Buttons === */
|
| 491 |
+
.gr-button {
|
| 492 |
+
background-image: linear-gradient(92.88deg, #455EB5 9.16%, #5643CC 43.89%, #673FD7 64.72%);
|
| 493 |
+
color: #ffffff !important;
|
| 494 |
+
border-radius: 8px;
|
| 495 |
+
font-size: 16px;
|
| 496 |
+
font-weight: 500;
|
| 497 |
+
height: 3.5rem;
|
| 498 |
+
padding: 0 1.5rem;
|
| 499 |
+
border: none;
|
| 500 |
+
box-shadow: rgba(80, 63, 205, 0.5) 0 1px 30px;
|
| 501 |
+
transition: all 0.3s;
|
| 502 |
+
}
|
| 503 |
+
.gr-button:hover {
|
| 504 |
+
transform: translateY(-2px);
|
| 505 |
+
box-shadow: rgba(80, 63, 205, 0.8) 0 2px 20px;
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
/* === Tab Labels === */
|
| 509 |
+
button[role="tab"] {
|
| 510 |
+
color: #22284F !important;
|
| 511 |
+
font-weight: 500;
|
| 512 |
+
font-size: 16px;
|
| 513 |
+
}
|
| 514 |
+
button[role="tab"][aria-selected="true"] {
|
| 515 |
+
color: #f57c00 !important;
|
| 516 |
+
font-weight: bold;
|
| 517 |
+
}
|
| 518 |
+
button[role="tab"]:hover {
|
| 519 |
+
color: #f57c00 !important;
|
| 520 |
+
font-weight: 600;
|
| 521 |
+
cursor: pointer;
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
/* === Footer === */
|
| 525 |
+
.gr-markdown:last-child {
|
| 526 |
+
text-align: center;
|
| 527 |
+
font-size: 14px;
|
| 528 |
+
color: #666;
|
| 529 |
+
padding-top: 1rem;
|
| 530 |
+
}
|
| 531 |
+
|
| 532 |
+
/* === Responsive === */
|
| 533 |
+
@media (max-width: 768px) {
|
| 534 |
+
#main-container {
|
| 535 |
+
width: 100%;
|
| 536 |
+
margin: 8px;
|
| 537 |
+
padding: 12px;
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
.gr-button {
|
| 541 |
+
font-size: 14px;
|
| 542 |
+
height: 3.2rem;
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
input[type="text"], .gr-textbox textarea, .gr-dropdown, .gr-slider {
|
| 546 |
+
font-size: 14px;
|
| 547 |
+
padding: 8px !important;
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
h1, .gr-markdown h1 {
|
| 551 |
+
font-size: 1.6rem !important;
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
.gr-gallery-item {
|
| 555 |
+
width: 100px !important;
|
| 556 |
+
height: 100px !important;
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
.gr-image {
|
| 560 |
+
height: auto !important;
|
| 561 |
+
}
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
"""
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 569 |
+
with gr.Column(elem_id="app-bg"):
|
| 570 |
+
with gr.Column(elem_id="main-container"):
|
| 571 |
+
gr.Markdown("# 🛍️ Fashion Product Hybrid Search")
|
| 572 |
+
|
| 573 |
+
alpha = gr.Slider(0, 1, value=0.5, label="Hybrid Weight (alpha: 0=sparse, 1=dense)")
|
| 574 |
+
|
| 575 |
+
with gr.Tabs():
|
| 576 |
+
with gr.Tab("Text Search"):
|
| 577 |
+
query = gr.Textbox(
|
| 578 |
+
label="Text Query",
|
| 579 |
+
placeholder="e.g., floral summer dress for women"
|
| 580 |
+
)
|
| 581 |
+
gender_dropdown = gr.Dropdown(
|
| 582 |
+
["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"],
|
| 583 |
+
label="Gender Filter (optional)"
|
| 584 |
+
)
|
| 585 |
+
text_search_btn = gr.Button("Search by Text", elem_classes="search-btn")
|
| 586 |
+
with gr.Tab("🎙️ Voice Search"):
|
| 587 |
+
voice_input = gr.Audio(label="Speak Your Query", type="filepath")
|
| 588 |
+
voice_gender_dropdown = gr.Dropdown(["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"], label="Gender")
|
| 589 |
+
voice_search_btn = gr.Button("Search by Voice")
|
| 590 |
+
|
| 591 |
+
|
| 592 |
+
with gr.Tab("Image Search"):
|
| 593 |
+
# image_input = gr.Image(
|
| 594 |
+
# type="pil",
|
| 595 |
+
# label="Upload an image",
|
| 596 |
+
# sources=["upload", "clipboard"],
|
| 597 |
+
# height=256,
|
| 598 |
+
# width=356
|
| 599 |
+
# )
|
| 600 |
+
image_input = gr.Image(
|
| 601 |
+
type="pil",
|
| 602 |
+
label="Upload an image",
|
| 603 |
+
sources=["upload", "clipboard"],
|
| 604 |
+
# tool=None,
|
| 605 |
+
height=400
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
image_gender_dropdown = gr.Dropdown(
|
| 609 |
+
["", "Men", "Women", "Boys", "Girls", "Kids", "Unisex"],
|
| 610 |
+
label="Gender Filter (optional)"
|
| 611 |
+
)
|
| 612 |
+
image_search_btn = gr.Button("Search by Image", elem_classes="search-btn")
|
| 613 |
+
|
| 614 |
+
gallery = gr.Gallery(label="Search Results", columns=6, height=None)
|
| 615 |
+
load_more_btn = gr.Button("Load More")
|
| 616 |
+
|
| 617 |
+
# --- UI State Holders ---
|
| 618 |
+
search_offset = gr.State(0)
|
| 619 |
+
current_query = gr.State("")
|
| 620 |
+
current_image = gr.State(None)
|
| 621 |
+
current_gender = gr.State("")
|
| 622 |
+
shown_results = gr.State([])
|
| 623 |
+
shown_ids = gr.State(set())
|
| 624 |
+
|
| 625 |
+
# --- Unified Search Function ---
|
| 626 |
+
def unified_search(q, uploaded_image, a, offset, gender_ui):
|
| 627 |
+
start = 0
|
| 628 |
+
end = 12
|
| 629 |
+
|
| 630 |
+
filters = extract_intent_from_openai(q) if q.strip() else {}
|
| 631 |
+
gender_override = gender_ui if gender_ui else filters.get("gender")
|
| 632 |
+
|
| 633 |
+
if uploaded_image is not None:
|
| 634 |
+
results = search_by_image(uploaded_image, a, start, end)
|
| 635 |
+
elif q.strip():
|
| 636 |
+
results = search_fashion(q, a, start, end, gender_override)
|
| 637 |
+
else:
|
| 638 |
+
results = []
|
| 639 |
+
|
| 640 |
+
seen_ids = {r[1] for r in results}
|
| 641 |
+
return results, end, q, uploaded_image, gender_override, results, seen_ids
|
| 642 |
+
|
| 643 |
+
# Text Search
|
| 644 |
+
# Text Search
|
| 645 |
+
text_search_btn.click(
|
| 646 |
+
unified_search,
|
| 647 |
+
inputs=[query, gr.State(None), alpha, search_offset, gender_dropdown],
|
| 648 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 649 |
+
)
|
| 650 |
+
|
| 651 |
+
voice_search_btn.click(
|
| 652 |
+
handle_voice_search,
|
| 653 |
+
inputs=[voice_input, alpha, search_offset, voice_gender_dropdown],
|
| 654 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 655 |
+
)
|
| 656 |
+
|
| 657 |
+
# Image Search
|
| 658 |
+
image_search_btn.click(
|
| 659 |
+
unified_search,
|
| 660 |
+
inputs=[gr.State(""), image_input, alpha, search_offset, image_gender_dropdown],
|
| 661 |
+
outputs=[gallery, search_offset, current_query, current_image, current_gender, shown_results, shown_ids]
|
| 662 |
+
)
|
| 663 |
+
|
| 664 |
+
# --- Load More Button ---
|
| 665 |
+
def load_more_fn(a, offset, q, img, gender_ui, prev_results, prev_ids):
|
| 666 |
+
start = offset
|
| 667 |
+
end = offset + 12
|
| 668 |
+
gender_override = gender_ui
|
| 669 |
+
|
| 670 |
+
if img is not None:
|
| 671 |
+
new_results = search_by_image(img, a, start, end)
|
| 672 |
+
elif q.strip():
|
| 673 |
+
new_results = search_fashion(q, a, start, end, gender_override)
|
| 674 |
+
else:
|
| 675 |
+
new_results = []
|
| 676 |
+
|
| 677 |
+
filtered_new = []
|
| 678 |
+
new_ids = set()
|
| 679 |
+
for item in new_results:
|
| 680 |
+
img_obj, caption = item
|
| 681 |
+
if caption not in prev_ids:
|
| 682 |
+
filtered_new.append(item)
|
| 683 |
+
new_ids.add(caption)
|
| 684 |
+
|
| 685 |
+
combined = prev_results + filtered_new
|
| 686 |
+
updated_ids = prev_ids.union(new_ids)
|
| 687 |
+
|
| 688 |
+
return combined, end, combined, updated_ids
|
| 689 |
+
|
| 690 |
+
load_more_btn.click(
|
| 691 |
+
load_more_fn,
|
| 692 |
+
inputs=[alpha, search_offset, current_query, current_image, current_gender, shown_results, shown_ids],
|
| 693 |
+
outputs=[gallery, search_offset, shown_results, shown_ids]
|
| 694 |
+
)
|
| 695 |
+
|
| 696 |
+
# gr.Markdown("🧠 Powered by OpenAI + Hybrid AI Fashion Search")
|
| 697 |
+
|
| 698 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
openai
|
| 3 |
+
sentence-transformers==2.6.1
|
| 4 |
+
torch>=2.0.0
|
| 5 |
+
transformers==4.41.1
|
| 6 |
+
datasets
|
| 7 |
+
Pillow
|
| 8 |
+
pinecone-client==3.2.2
|
| 9 |
+
pinecone-text
|
| 10 |
+
scikit-learn
|
| 11 |
+
tqdm
|
| 12 |
+
numpy
|
| 13 |
+
imagehash
|
| 14 |
+
openai-whisper
|
| 15 |
+
|