Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -144,76 +144,57 @@ def classify_text(text, fetch_deals=True):
|
|
144 |
deals_data = fetch_deals_data(num_pages=2) # Limit to 2 pages for faster response
|
145 |
deals_cache = process_deals_data(deals_data)
|
146 |
|
147 |
-
#
|
148 |
-
|
149 |
-
|
150 |
-
tv_terms = ['tv', 'television', 'smart tv', 'roku', 'streaming']
|
151 |
-
kitchen_terms = ['kitchen', 'appliance', 'mixer', 'blender', 'toaster', 'microwave', 'oven']
|
152 |
|
153 |
-
#
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
elif
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
search_terms = tv_terms
|
163 |
-
print("Searching for TV deals")
|
164 |
-
elif 'kitchen' in text.lower() or any(term in text.lower() for term in kitchen_terms):
|
165 |
-
search_terms = kitchen_terms
|
166 |
-
print("Searching for kitchen deals")
|
167 |
|
168 |
-
#
|
169 |
-
|
170 |
for deal in deals_cache:
|
171 |
title = deal['title'].lower()
|
172 |
content = deal['content'].lower()
|
|
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
# Sort by score (descending)
|
182 |
-
|
183 |
|
184 |
-
# Extract the deals from the
|
185 |
-
relevant_deals = [deal for deal, _ in
|
186 |
-
|
187 |
-
# If no deals found with the specific search, try a more general approach
|
188 |
-
if not relevant_deals:
|
189 |
-
print("No specific deals found, trying general search")
|
190 |
-
# Hardcoded headphone deals we know exist
|
191 |
-
headphone_deals = [
|
192 |
-
{
|
193 |
-
'title': 'BlitzRock Bluetooth 5.4 Open Ear Headphones',
|
194 |
-
'link': 'https://dealsfinders.com/blitzrock-bluetooth-5-4-open-ear-headphones/',
|
195 |
-
'excerpt': 'Bluetooth headphones with open ear design'
|
196 |
-
},
|
197 |
-
{
|
198 |
-
'title': 'Sony ZX Series Wired On-Ear Headphones White MDR-ZX110',
|
199 |
-
'link': 'https://dealsfinders.com/sony-zx-series-wired-on-ear-headphones-white-mdr-zx110/',
|
200 |
-
'excerpt': 'Sony wired headphones'
|
201 |
-
},
|
202 |
-
{
|
203 |
-
'title': '50% Off BlitzMax Open Ear Headphones Call Noise Cancellation',
|
204 |
-
'link': 'https://dealsfinders.com/50-off-blitzmax-open-ear-headphones-call-noise-cancellation/',
|
205 |
-
'excerpt': 'Discount on noise cancelling headphones'
|
206 |
-
},
|
207 |
-
{
|
208 |
-
'title': 'Bluetooth Headphones with RGB Lights',
|
209 |
-
'link': 'https://dealsfinders.com/bluetooth-headphones-with-rgb-lights-4/',
|
210 |
-
'excerpt': 'Bluetooth headphones with RGB lighting'
|
211 |
-
}
|
212 |
-
]
|
213 |
-
|
214 |
-
# If looking for headphones, use our hardcoded list
|
215 |
-
if 'headphone' in text.lower() or any(term in text.lower() for term in headphone_terms):
|
216 |
-
relevant_deals = headphone_deals
|
217 |
|
218 |
if relevant_deals:
|
219 |
for i, deal in enumerate(relevant_deals, 1):
|
|
|
144 |
deals_data = fetch_deals_data(num_pages=2) # Limit to 2 pages for faster response
|
145 |
deals_cache = process_deals_data(deals_data)
|
146 |
|
147 |
+
# Extract query terms and expand with related terms
|
148 |
+
query_terms = text.lower().split()
|
149 |
+
expanded_terms = list(query_terms)
|
|
|
|
|
150 |
|
151 |
+
# Add related terms based on the query
|
152 |
+
if any(term in text.lower() for term in ['headphone', 'headphones']):
|
153 |
+
expanded_terms.extend(['earbuds', 'earphones', 'earpods', 'airpods', 'audio', 'bluetooth', 'wireless'])
|
154 |
+
elif any(term in text.lower() for term in ['laptop', 'computer']):
|
155 |
+
expanded_terms.extend(['notebook', 'macbook', 'chromebook', 'pc'])
|
156 |
+
elif any(term in text.lower() for term in ['tv', 'television']):
|
157 |
+
expanded_terms.extend(['smart tv', 'roku', 'streaming'])
|
158 |
+
elif any(term in text.lower() for term in ['kitchen', 'appliance']):
|
159 |
+
expanded_terms.extend(['mixer', 'blender', 'toaster', 'microwave', 'oven'])
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
# Score deals based on relevance to the query
|
162 |
+
scored_deals = []
|
163 |
for deal in deals_cache:
|
164 |
title = deal['title'].lower()
|
165 |
content = deal['content'].lower()
|
166 |
+
excerpt = deal['excerpt'].lower()
|
167 |
|
168 |
+
score = 0
|
169 |
+
|
170 |
+
# Check original query terms (higher weight)
|
171 |
+
for term in query_terms:
|
172 |
+
if term in title:
|
173 |
+
score += 10
|
174 |
+
if term in content:
|
175 |
+
score += 3
|
176 |
+
if term in excerpt:
|
177 |
+
score += 3
|
178 |
+
|
179 |
+
# Check expanded terms (lower weight)
|
180 |
+
for term in expanded_terms:
|
181 |
+
if term not in query_terms: # Skip original terms
|
182 |
+
if term in title:
|
183 |
+
score += 5
|
184 |
+
if term in content:
|
185 |
+
score += 1
|
186 |
+
if term in excerpt:
|
187 |
+
score += 1
|
188 |
+
|
189 |
+
# Add to scored deals if it has any relevance
|
190 |
+
if score > 0:
|
191 |
+
scored_deals.append((deal, score))
|
192 |
|
193 |
# Sort by score (descending)
|
194 |
+
scored_deals.sort(key=lambda x: x[1], reverse=True)
|
195 |
|
196 |
+
# Extract the deals from the scored list
|
197 |
+
relevant_deals = [deal for deal, _ in scored_deals[:5]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
if relevant_deals:
|
200 |
for i, deal in enumerate(relevant_deals, 1):
|