selvaonline commited on
Commit
0e09fb7
·
verified ·
1 Parent(s): 80eae08

Upload app.py with huggingface_hub

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