Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from sentence_transformers import SentenceTransformer, util
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# βββ Load catalog & compute embeddings βββββββββββββββββββββββββ
|
6 |
+
PRODUCTS_CSV = "products.csv"
|
7 |
+
df = pd.read_csv(PRODUCTS_CSV)
|
8 |
+
descriptions = df["description"].tolist()
|
9 |
+
|
10 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
11 |
+
embeddings = model.encode(descriptions, convert_to_tensor=True, normalize_embeddings=True)
|
12 |
+
|
13 |
+
# βββ Semantic-search function βββββββββββββββββββββββββββββββββ
|
14 |
+
def search_products(query: str, top_k: int = 5):
|
15 |
+
if not query.strip():
|
16 |
+
return pd.DataFrame(columns=df.columns.tolist() + ["score"])
|
17 |
+
q_emb = model.encode(query, convert_to_tensor=True, normalize_embeddings=True)
|
18 |
+
hits = util.cos_sim(q_emb, embeddings)[0].topk(k=top_k)
|
19 |
+
indices = hits.indices.cpu().tolist()
|
20 |
+
scores = [round(float(s), 3) for s in hits.values.cpu().tolist()]
|
21 |
+
|
22 |
+
results = df.iloc[indices].copy()
|
23 |
+
results["score"] = scores
|
24 |
+
return results.reset_index(drop=True)
|
25 |
+
|
26 |
+
# βββ Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββ
|
27 |
+
with gr.Blocks(title="ποΈ Salon Catalog Semantic Search") as demo:
|
28 |
+
gr.Markdown("""
|
29 |
+
# ποΈ Salon Product Search
|
30 |
+
**Natural-language queries** β **top matching products** via MiniLM embeddings
|
31 |
+
(Runs entirely on free CPU; no paid APIs)
|
32 |
+
""")
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
query_in = gr.Textbox(placeholder="e.g. sulfate-free shampoo under $15", label="Search query")
|
36 |
+
topk = gr.Slider(1, 10, value=5, step=1, label="Number of results")
|
37 |
+
search_btn = gr.Button("Search π", variant="primary")
|
38 |
+
|
39 |
+
table = gr.Dataframe(
|
40 |
+
headers=list(df.columns) + ["score"],
|
41 |
+
datatype=["number","str","str","str","number","number"],
|
42 |
+
interactive=False,
|
43 |
+
row_count= topk.value,
|
44 |
+
label="Results"
|
45 |
+
)
|
46 |
+
|
47 |
+
search_btn.click(search_products, [query_in, topk], table)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
demo.launch(server_name="0.0.0.0")
|