Spaces:
Sleeping
Sleeping
Update product_recommender.py
Browse files- product_recommender.py +103 -32
product_recommender.py
CHANGED
@@ -1,41 +1,112 @@
|
|
1 |
-
from
|
|
|
|
|
|
|
|
|
|
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
-
import torch
|
4 |
import numpy as np
|
5 |
-
|
6 |
-
import json
|
7 |
|
8 |
-
class
|
9 |
def __init__(self):
|
10 |
-
self.
|
|
|
|
|
|
|
11 |
|
12 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
}
|
|
|
1 |
+
from typing import Dict, List
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
import aiohttp
|
5 |
+
import asyncio
|
6 |
+
import json
|
7 |
from sentence_transformers import SentenceTransformer
|
|
|
8 |
import numpy as np
|
9 |
+
import re
|
|
|
10 |
|
11 |
+
class DynamicRecommender:
|
12 |
def __init__(self):
|
13 |
+
self.headers = {
|
14 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
15 |
+
}
|
16 |
+
self.model = SentenceTransformer('all-mpnet-base-v2')
|
17 |
|
18 |
+
async def search_amazon(self, query: str) -> List[Dict]:
|
19 |
+
"""Search Amazon for products"""
|
20 |
+
search_url = f"https://www.amazon.in/s?k={query}"
|
21 |
+
async with aiohttp.ClientSession() as session:
|
22 |
+
async with session.get(search_url, headers=self.headers) as response:
|
23 |
+
if response.status == 200:
|
24 |
+
html = await response.text()
|
25 |
+
return self._parse_amazon_results(html)
|
26 |
+
return []
|
27 |
+
|
28 |
+
async def search_flipkart(self, query: str) -> List[Dict]:
|
29 |
+
"""Search Flipkart for products"""
|
30 |
+
search_url = f"https://www.flipkart.com/search?q={query}"
|
31 |
+
async with aiohttp.ClientSession() as session:
|
32 |
+
async with session.get(search_url, headers=self.headers) as response:
|
33 |
+
if response.status == 200:
|
34 |
+
html = await response.text()
|
35 |
+
return self._parse_flipkart_results(html)
|
36 |
+
return []
|
37 |
+
|
38 |
+
def _parse_amazon_results(self, html: str) -> List[Dict]:
|
39 |
+
soup = BeautifulSoup(html, 'html.parser')
|
40 |
+
products = []
|
41 |
+
for item in soup.select('.s-result-item'):
|
42 |
+
try:
|
43 |
+
name = item.select_one('.a-text-normal')
|
44 |
+
price = item.select_one('.a-price-whole')
|
45 |
+
if name and price:
|
46 |
+
products.append({
|
47 |
+
'name': name.text.strip(),
|
48 |
+
'price': price.text.strip(),
|
49 |
+
'source': 'Amazon',
|
50 |
+
'url': 'https://amazon.in' + item.select_one('a')['href']
|
51 |
+
})
|
52 |
+
except Exception:
|
53 |
+
continue
|
54 |
+
return products[:5]
|
55 |
+
|
56 |
+
def _parse_flipkart_results(self, html: str) -> List[Dict]:
|
57 |
+
soup = BeautifulSoup(html, 'html.parser')
|
58 |
+
products = []
|
59 |
+
for item in soup.select('._1AtVbE'):
|
60 |
+
try:
|
61 |
+
name = item.select_one('._4rR01T')
|
62 |
+
price = item.select_one('._30jeq3')
|
63 |
+
if name and price:
|
64 |
+
products.append({
|
65 |
+
'name': name.text.strip(),
|
66 |
+
'price': price.text.strip(),
|
67 |
+
'source': 'Flipkart',
|
68 |
+
'url': 'https://flipkart.com' + item.select_one('a')['href']
|
69 |
+
})
|
70 |
+
except Exception:
|
71 |
+
continue
|
72 |
+
return products[:5]
|
73 |
+
|
74 |
+
def _extract_keywords(self, text: str) -> List[str]:
|
75 |
+
"""Extract relevant search keywords from input"""
|
76 |
+
age_match = re.search(r'age\s+(\d+)', text.lower())
|
77 |
+
age = age_match.group(1) if age_match else None
|
78 |
+
|
79 |
+
interests = []
|
80 |
+
if 'software' in text.lower() or 'engineer' in text.lower():
|
81 |
+
interests.extend(['programming books', 'tech gadgets'])
|
82 |
+
if 'books' in text.lower():
|
83 |
+
interests.append('books')
|
84 |
+
if 'successful' in text.lower():
|
85 |
+
interests.extend(['self help books', 'business books'])
|
86 |
+
|
87 |
+
return [f"{interest} for {age} year old" if age else interest for interest in interests]
|
88 |
+
|
89 |
+
async def get_recommendations(self, text: str) -> Dict:
|
90 |
+
"""Get personalized recommendations"""
|
91 |
try:
|
92 |
+
keywords = self._extract_keywords(text)
|
93 |
+
all_products = []
|
94 |
+
|
95 |
+
for keyword in keywords:
|
96 |
+
amazon_products = await self.search_amazon(keyword)
|
97 |
+
flipkart_products = await self.search_flipkart(keyword)
|
98 |
+
all_products.extend(amazon_products + flipkart_products)
|
99 |
+
|
100 |
+
# Remove duplicates and sort by relevance
|
101 |
+
seen = set()
|
102 |
+
unique_products = []
|
103 |
+
for product in all_products:
|
104 |
+
if product['name'] not in seen:
|
105 |
+
seen.add(product['name'])
|
106 |
+
unique_products.append(product)
|
107 |
+
|
108 |
+
return unique_products[:5]
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
except Exception as e:
|
111 |
print(f"Error in recommendations: {str(e)}")
|
112 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|