selvaonline commited on
Commit
6f89f62
·
verified ·
1 Parent(s): 08f5082

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +42 -61
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
- # Direct approach to find relevant deals
148
- headphone_terms = ['headphone', 'headphones', 'earbuds', 'earphones', 'earpods', 'airpods', 'audio', 'bluetooth', 'wireless']
149
- laptop_terms = ['laptop', 'notebook', 'computer', 'macbook', 'chromebook', 'pc']
150
- tv_terms = ['tv', 'television', 'smart tv', 'roku', 'streaming']
151
- kitchen_terms = ['kitchen', 'appliance', 'mixer', 'blender', 'toaster', 'microwave', 'oven']
152
 
153
- # Determine which category to search for
154
- search_terms = []
155
- if 'headphone' in text.lower() or any(term in text.lower() for term in headphone_terms):
156
- search_terms = headphone_terms
157
- print("Searching for headphone deals")
158
- elif 'laptop' in text.lower() or any(term in text.lower() for term in laptop_terms):
159
- search_terms = laptop_terms
160
- print("Searching for laptop deals")
161
- elif 'tv' in text.lower() or any(term in text.lower() for term in tv_terms):
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
- # Find deals matching the search terms
169
- matched_deals = []
170
  for deal in deals_cache:
171
  title = deal['title'].lower()
172
  content = deal['content'].lower()
 
173
 
174
- # Check if any search term is in the title (highest priority)
175
- if any(term in title for term in search_terms):
176
- matched_deals.append((deal, 100)) # High score for title matches
177
- # Check if any search term is in the content
178
- elif any(term in content for term in search_terms):
179
- matched_deals.append((deal, 50)) # Lower score for content matches
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  # Sort by score (descending)
182
- matched_deals.sort(key=lambda x: x[1], reverse=True)
183
 
184
- # Extract the deals from the matched list
185
- relevant_deals = [deal for deal, _ in matched_deals[:5]]
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):