Spaces:
Sleeping
Sleeping
Update product_recommender.py
Browse files- product_recommender.py +37 -57
product_recommender.py
CHANGED
@@ -1,61 +1,41 @@
|
|
1 |
-
import
|
2 |
-
from
|
|
|
|
|
|
|
3 |
import json
|
4 |
-
import urllib.parse
|
5 |
-
from product_recommender import ProductRecommender
|
6 |
|
7 |
-
|
8 |
-
|
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 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
|
36 |
-
|
37 |
-
|
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 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoModelForTokenClassification
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from typing import Dict, List, Optional
|
6 |
import json
|
|
|
|
|
7 |
|
8 |
+
class ProductRecommender:
|
9 |
+
def __init__(self):
|
10 |
+
self.model_analyzer = MultiModelAnalyzer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def get_recommendations(self, query: str, product_database: List[Dict]) -> List[Dict]:
|
13 |
+
try:
|
14 |
+
query_analysis = self.model_analyzer.analyze_text(query)
|
15 |
+
|
16 |
+
# For testing, return a simple recommendation
|
17 |
+
return [{
|
18 |
+
"name": "Test Product",
|
19 |
+
"price": "βΉ999",
|
20 |
+
"category": "test",
|
21 |
+
"similarity": 0.95
|
22 |
+
}]
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error in recommendations: {str(e)}")
|
25 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
class MultiModelAnalyzer:
|
28 |
+
def __init__(self):
|
29 |
+
try:
|
30 |
+
self.category_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
31 |
+
self.category_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
32 |
+
self.semantic_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Error initializing models: {str(e)}")
|
35 |
+
|
36 |
+
def analyze_text(self, text: str) -> Dict:
|
37 |
+
return {
|
38 |
+
"category": "test",
|
39 |
+
"embedding": np.zeros(10),
|
40 |
+
"features": ["test"]
|
41 |
+
}
|