Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -144,21 +144,62 @@ 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 |
-
# Search for relevant deals
|
| 148 |
query_terms = text.lower().split()
|
| 149 |
-
relevant_deals = []
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
for deal in deals_cache:
|
| 152 |
title = deal['title'].lower()
|
| 153 |
content = deal['content'].lower()
|
| 154 |
excerpt = deal['excerpt'].lower()
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
relevant_deals =
|
| 162 |
|
| 163 |
if relevant_deals:
|
| 164 |
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 |
+
# Search for relevant deals with improved matching
|
| 148 |
query_terms = text.lower().split()
|
|
|
|
| 149 |
|
| 150 |
+
# Add related terms for specific queries
|
| 151 |
+
expanded_terms = list(query_terms) # Start with original terms
|
| 152 |
+
|
| 153 |
+
# Special case for headphones
|
| 154 |
+
if 'headphone' in query_terms or 'headphones' in query_terms:
|
| 155 |
+
expanded_terms.extend(['earbuds', 'earphones', 'earpods', 'airpods', 'audio', 'bluetooth', 'wireless'])
|
| 156 |
+
|
| 157 |
+
# Score and rank deals
|
| 158 |
+
scored_deals = []
|
| 159 |
for deal in deals_cache:
|
| 160 |
title = deal['title'].lower()
|
| 161 |
content = deal['content'].lower()
|
| 162 |
excerpt = deal['excerpt'].lower()
|
| 163 |
|
| 164 |
+
# Calculate relevance score
|
| 165 |
+
score = 0
|
| 166 |
+
|
| 167 |
+
# Check original query terms (higher weight)
|
| 168 |
+
for term in query_terms:
|
| 169 |
+
# Title matches are most important
|
| 170 |
+
if term in title:
|
| 171 |
+
score += 10
|
| 172 |
+
# Content and excerpt matches
|
| 173 |
+
if term in content:
|
| 174 |
+
score += 3
|
| 175 |
+
if term in excerpt:
|
| 176 |
+
score += 3
|
| 177 |
+
|
| 178 |
+
# Check expanded terms (lower weight)
|
| 179 |
+
for term in expanded_terms:
|
| 180 |
+
if term not in query_terms: # Skip original terms
|
| 181 |
+
if term in title:
|
| 182 |
+
score += 5
|
| 183 |
+
if term in content:
|
| 184 |
+
score += 1
|
| 185 |
+
if term in excerpt:
|
| 186 |
+
score += 1
|
| 187 |
+
|
| 188 |
+
# Special case for headphones - look for exact product matches
|
| 189 |
+
if 'headphone' in query_terms or 'headphones' in query_terms:
|
| 190 |
+
headphone_terms = ['headphone', 'headphones', 'earbuds', 'earphones', 'earpods', 'airpods']
|
| 191 |
+
if any(term in title for term in headphone_terms):
|
| 192 |
+
score += 20 # Significant boost for headphone products
|
| 193 |
+
|
| 194 |
+
# Add to scored deals if it has any relevance
|
| 195 |
+
if score > 0:
|
| 196 |
+
scored_deals.append((deal, score))
|
| 197 |
+
|
| 198 |
+
# Sort by score (descending)
|
| 199 |
+
scored_deals.sort(key=lambda x: x[1], reverse=True)
|
| 200 |
|
| 201 |
+
# Extract the deals from the scored list
|
| 202 |
+
relevant_deals = [deal for deal, score in scored_deals[:5]]
|
| 203 |
|
| 204 |
if relevant_deals:
|
| 205 |
for i, deal in enumerate(relevant_deals, 1):
|