Spaces:
Sleeping
Sleeping
Create product_recommender.py
Browse files- product_recommender.py +61 -0
product_recommender.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from typing import Dict, List
|
3 |
+
import json
|
4 |
+
import urllib.parse
|
5 |
+
from product_recommender import ProductRecommender
|
6 |
+
|
7 |
+
# Initialize the recommender
|
8 |
+
recommender = ProductRecommender()
|
9 |
+
|
10 |
+
# Sample product database - replace with your actual database
|
11 |
+
product_database = [
|
12 |
+
{
|
13 |
+
"description": "FIFA 24 EA Sports Football Game",
|
14 |
+
"category": "games",
|
15 |
+
"features": ["sports", "multiplayer"],
|
16 |
+
"price": 4999
|
17 |
+
},
|
18 |
+
# Add more products
|
19 |
+
]
|
20 |
+
|
21 |
+
def get_recommendations(text: str) -> Dict:
|
22 |
+
try:
|
23 |
+
# Get recommendations using the multi-model system
|
24 |
+
recommendations = recommender.get_recommendations(text, product_database)
|
25 |
+
|
26 |
+
# Format recommendations with shopping links
|
27 |
+
formatted_recommendations = []
|
28 |
+
for rec in recommendations:
|
29 |
+
query = urllib.parse.quote(rec['description'])
|
30 |
+
formatted_rec = {
|
31 |
+
"product": rec['description'],
|
32 |
+
"price": f"βΉ{rec['price']}",
|
33 |
+
"similarity_score": f"{rec.get('similarity', 0):.2f}",
|
34 |
+
"shopping_links": {
|
35 |
+
"Amazon": f"https://www.amazon.in/s?k={query}",
|
36 |
+
"Flipkart": f"https://www.flipkart.com/search?q={query}",
|
37 |
+
"IGP": f"https://www.igp.com/search?q={query}"
|
38 |
+
}
|
39 |
+
}
|
40 |
+
formatted_recommendations.append(formatted_rec)
|
41 |
+
|
42 |
+
return {
|
43 |
+
"recommendations": formatted_recommendations
|
44 |
+
}
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
return {"error": str(e)}
|
48 |
+
|
49 |
+
# Create Gradio interface
|
50 |
+
demo = gr.Interface(
|
51 |
+
fn=get_recommendations,
|
52 |
+
inputs=gr.Textbox(lines=3),
|
53 |
+
outputs=gr.JSON(),
|
54 |
+
title="π Smart Gift Recommender",
|
55 |
+
description="Get personalized gift suggestions with direct shopping links!"
|
56 |
+
)
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
60 |
+
else:
|
61 |
+
app = demo.app
|