Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import requests
|
| 6 |
+
from sentence_transformers import SentenceTransformer, util
|
| 7 |
+
import torch
|
| 8 |
|
| 9 |
+
# Load the dataset (מכיל מוצרים עם שם ותיאור)
|
| 10 |
+
df = pd.read_parquet("train-00000-of-00002-6cff4c59f91661c3.parquet")
|
| 11 |
+
|
| 12 |
+
# נניח שהעמודות החשובות הן אלה — אם צריך עדכון, שימי לב לשמות העמודות
|
| 13 |
+
df = df[["productDisplayName", "gender", "usage", "masterCategory", "subCategory"]].dropna()
|
| 14 |
+
|
| 15 |
+
# Create a full-text field to encode
|
| 16 |
+
df["full_text"] = df["productDisplayName"] + " | " + df["gender"] + " | " + df["usage"] + " | " + df["masterCategory"] + " > " + df["subCategory"]
|
| 17 |
+
|
| 18 |
+
# Load model
|
| 19 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 20 |
+
embeddings = model.encode(df["full_text"].tolist(), convert_to_tensor=True, show_progress_bar=True)
|
| 21 |
+
|
| 22 |
+
# Recommendation logic
|
| 23 |
+
def recommend_products(user_input, top_k=5):
|
| 24 |
+
if not user_input.strip():
|
| 25 |
+
return "⚠️ Please enter a product description.", []
|
| 26 |
+
|
| 27 |
+
user_vector = model.encode(user_input, convert_to_tensor=True)
|
| 28 |
+
similarities = util.cos_sim(user_vector, embeddings)[0]
|
| 29 |
+
top_indices = similarities.argsort(descending=True)[:top_k]
|
| 30 |
+
|
| 31 |
+
results = []
|
| 32 |
+
for idx in top_indices:
|
| 33 |
+
row = df.iloc[idx]
|
| 34 |
+
title = row["productDisplayName"]
|
| 35 |
+
description = f"{row['gender']} - {row['usage']} - {row['masterCategory']} > {row['subCategory']}"
|
| 36 |
+
|
| 37 |
+
# Placeholder image – אין תמונות מקוריות בדאטה
|
| 38 |
+
image_url = "https://via.placeholder.com/300x400.png?text=No+Image"
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
response = requests.get(image_url)
|
| 42 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 43 |
+
except:
|
| 44 |
+
img = Image.new("RGB", (300, 400), color=(200, 200, 200))
|
| 45 |
+
|
| 46 |
+
results.append((img, f"**{title}**\n{description}"))
|
| 47 |
+
|
| 48 |
+
return "", results
|
| 49 |
+
|
| 50 |
+
# Custom CSS
|
| 51 |
+
custom_css = """
|
| 52 |
+
<style>
|
| 53 |
+
.gradio-container {font-family: 'Segoe UI', sans-serif;}
|
| 54 |
+
.gr-button {background-color: #4CAF50 !important; color: white !important; font-weight: bold;}
|
| 55 |
+
.gr-button:hover {background-color: #388e3c !important;}
|
| 56 |
+
img {border-radius: 8px;}
|
| 57 |
+
</style>
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
# Example inputs
|
| 61 |
+
examples = [
|
| 62 |
+
"red summer dress",
|
| 63 |
+
"black leather boots",
|
| 64 |
+
"formal white shirt",
|
| 65 |
+
"cotton trousers for men",
|
| 66 |
+
"sports t-shirt"
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
# Build the interface
|
| 70 |
+
with gr.Blocks(title="Fashion Product Recommender") as demo:
|
| 71 |
+
gr.HTML(custom_css)
|
| 72 |
+
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
## 🛍️ Fashion Product Recommender
|
| 75 |
+
Type in a product you're looking for, and get AI-based recommendations from our fashion dataset.
|
| 76 |
+
Use keywords like *'summer dress'* or *'men sports shoes'* to begin.
|
| 77 |
+
""")
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
with gr.Column(scale=1):
|
| 81 |
+
user_input = gr.Textbox(
|
| 82 |
+
label="🔎 What are you looking for?",
|
| 83 |
+
placeholder="e.g. red formal shirt, denim jacket, kids shoes...",
|
| 84 |
+
lines=2
|
| 85 |
+
)
|
| 86 |
+
submit_btn = gr.Button("✨ Recommend Products")
|
| 87 |
+
quick_ex = gr.Examples(examples=examples, inputs=user_input, label="💡 Try these examples")
|
| 88 |
+
error_box = gr.Textbox(visible=False, interactive=False, show_label=False)
|
| 89 |
+
|
| 90 |
+
with gr.Column(scale=2):
|
| 91 |
+
output_gallery = gr.Gallery(
|
| 92 |
+
label="🎯 Top Matching Products",
|
| 93 |
+
show_label=True,
|
| 94 |
+
columns=2,
|
| 95 |
+
rows=4,
|
| 96 |
+
height=600,
|
| 97 |
+
object_fit="cover"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
submit_btn.click(fn=recommend_products, inputs=user_input, outputs=[error_box, output_gallery])
|
| 101 |
+
|
| 102 |
+
demo.launch()
|
| 103 |
|