Create app.py
Browse filesFirst version of MatchMe app — upload an image and get fashion recommendations.
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import transforms
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
+
|
| 10 |
+
# Step 1: Load the precomputed fashion embeddings
|
| 11 |
+
file_path = hf_hub_download(repo_id="Elevi7/MatchMe", filename="fashion_embeddings.pkl", repo_type="dataset")
|
| 12 |
+
with open(file_path, "rb") as f:
|
| 13 |
+
embedding_store = pickle.load(f)
|
| 14 |
+
|
| 15 |
+
# Step 2: Load the same model used to create the embeddings
|
| 16 |
+
# (update this if you used something different, e.g. CLIP, ResNet, etc.)
|
| 17 |
+
import timm
|
| 18 |
+
model = timm.create_model("resnet18", pretrained=True)
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
transform = transforms.Compose([
|
| 22 |
+
transforms.Resize((224, 224)),
|
| 23 |
+
transforms.ToTensor()
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
def extract_embedding(image):
|
| 27 |
+
image = transform(image).unsqueeze(0)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
embedding = model(image).squeeze().numpy()
|
| 30 |
+
return embedding
|
| 31 |
+
|
| 32 |
+
def recommend(image):
|
| 33 |
+
query_embedding = extract_embedding(image)
|
| 34 |
+
|
| 35 |
+
# Get embeddings and paths
|
| 36 |
+
all_embeddings = np.array([item["embedding"] for item in embedding_store])
|
| 37 |
+
paths = [item["image_path"] for item in embedding_store]
|
| 38 |
+
|
| 39 |
+
similarities = cosine_similarity([query_embedding], all_embeddings)[0]
|
| 40 |
+
top_indices = np.argsort(similarities)[-3:][::-1] # Top 3 matches
|
| 41 |
+
|
| 42 |
+
return [paths[i] for i in top_indices]
|
| 43 |
+
|
| 44 |
+
demo = gr.Interface(
|
| 45 |
+
fn=recommend,
|
| 46 |
+
inputs=gr.Image(type="pil"),
|
| 47 |
+
outputs=[gr.Image(type="filepath", label=f"Match {i+1}") for i in range(3)],
|
| 48 |
+
title="MatchMe: Fashion Recommender",
|
| 49 |
+
description="Upload a fashion image and get 3 visually similar items."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
demo.launch()
|