eaglelandsonce commited on
Commit
3ac1f08
·
verified ·
1 Parent(s): 2e6e362

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import torch
5
+ import torch.nn.functional as F
6
+ from sentence_transformers import SentenceTransformer
7
+
8
+ CSV_PATH = "Shopeasy_product_dataset.csv"
9
+ EMB_PATH = "embeddings.pt"
10
+ MODEL_NAME = "all-mpnet-base-v2"
11
+
12
+ # Load catalog
13
+ data = pd.read_csv(CSV_PATH, low_memory=True)
14
+
15
+ # Load embeddings (force CPU so it works on Spaces without GPU)
16
+ sentence_embeddings = torch.load(EMB_PATH, map_location="cpu").float()
17
+
18
+ # Normalize embeddings once (so dot product == cosine similarity)
19
+ sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
20
+
21
+ # Load model (CPU by default; will still work fine)
22
+ mpnet_base = SentenceTransformer(MODEL_NAME, device="cpu")
23
+
24
+ def clean_text(x: str) -> str:
25
+ x = str(x).lower()
26
+ x = re.sub(r"[^A-Za-z0-9]+", " ", x).strip()
27
+ return x
28
+
29
+ def get_recommendations(query, top_k=10):
30
+ query = clean_text(query)
31
+ if not query:
32
+ return pd.DataFrame(columns=["Rank", "Product Name"])
33
+
34
+ query_embedding = mpnet_base.encode(query, convert_to_tensor=True)
35
+ query_embedding = F.normalize(query_embedding, p=2, dim=0)
36
+
37
+ # cosine similarity (since both normalized)
38
+ similarity_scores = sentence_embeddings @ query_embedding
39
+
40
+ top_k = int(top_k)
41
+ top_scores, top_indices = torch.topk(similarity_scores, k=min(top_k, similarity_scores.shape[0]))
42
+ top_indices = top_indices.cpu().numpy()
43
+
44
+ recs = data.iloc[top_indices]["product_name"].reset_index(drop=True)
45
+ out = recs.to_frame(name="Product Name")
46
+ out.insert(0, "Rank", range(1, len(out) + 1))
47
+ return out
48
+
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("## 🤖 AI-Powered Product Recommendation")
51
+ gr.Markdown("Enter a query to see the top recommended products (SentenceTransformer embeddings).")
52
+
53
+ query = gr.Textbox(label="Query", placeholder="e.g., wireless headphones noise cancelling")
54
+ top_k = gr.Slider(1, 25, value=10, step=1, label="Top K")
55
+
56
+ btn = gr.Button("Recommend")
57
+ table = gr.Dataframe(label="Recommendations", interactive=False)
58
+
59
+ btn.click(get_recommendations, inputs=[query, top_k], outputs=table)
60
+
61
+ demo.launch()