Spaces:
Runtime error
Runtime error
Update helper_functions.py
Browse files- helper_functions.py +30 -20
helper_functions.py
CHANGED
@@ -259,33 +259,43 @@ def check_validity(query: str, keyword_search: BM25L) -> np.ndarray:
|
|
259 |
detail=f"An error occurred during query validity check: {e}",
|
260 |
)
|
261 |
|
262 |
-
def is_cheapest(
|
263 |
"""
|
264 |
Check which product is the cheapest within the same category as
|
265 |
each input query.
|
266 |
Args:
|
267 |
-
|
268 |
request_json (list): List of products
|
269 |
"""
|
270 |
try:
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
except Exception as e:
|
290 |
raise HTTPException(
|
291 |
status_code=500,
|
|
|
259 |
detail=f"An error occurred during query validity check: {e}",
|
260 |
)
|
261 |
|
262 |
+
def is_cheapest(query: str, request_json: list) -> list:
|
263 |
"""
|
264 |
Check which product is the cheapest within the same category as
|
265 |
each input query.
|
266 |
Args:
|
267 |
+
query (str): The input query
|
268 |
request_json (list): List of products
|
269 |
"""
|
270 |
try:
|
271 |
+
query_categories = [
|
272 |
+
category_map[category][0]
|
273 |
+
for category in categorizer.predict(query, k=3, threshold=0.5)[0]
|
274 |
+
]
|
275 |
+
|
276 |
+
# print(f"Query {query} categories: {query_categories}")
|
277 |
+
|
278 |
+
min_idx = 0
|
279 |
+
min_price = float("inf") # Initialize min_price as positive infinity
|
280 |
+
for idx, product in enumerate(request_json):
|
281 |
+
if (
|
282 |
+
product["Inferred Category"] in query_categories
|
283 |
+
and product["price"] <= min_price
|
284 |
+
):
|
285 |
+
min_idx = idx
|
286 |
+
min_price = product[
|
287 |
+
"price"
|
288 |
+
] # Update min_price if a cheaper product is found
|
289 |
+
for product in request_json:
|
290 |
+
product["cheapest"] = False # Reset "cheapest" field for all products
|
291 |
+
|
292 |
+
# print(f"Cheapest product: {request_json[min_idx]['name']}, Price: {request_json[min_idx]['price']}")
|
293 |
+
request_json[min_idx][
|
294 |
+
"cheapest"
|
295 |
+
] = True # Mark the cheapest product for the current query
|
296 |
+
|
297 |
+
# print(request_json)
|
298 |
+
return request_json
|
299 |
except Exception as e:
|
300 |
raise HTTPException(
|
301 |
status_code=500,
|