Abdul-Ib commited on
Commit
2e82a6c
1 Parent(s): f17e862

Update helper_functions.py

Browse files
Files changed (1) hide show
  1. 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(queries: list, request_json: list) -> None:
263
  """
264
  Check which product is the cheapest within the same category as
265
  each input query.
266
  Args:
267
- queries (list): List of input queries
268
  request_json (list): List of products
269
  """
270
  try:
271
- for query in queries:
272
- query_categories = [
273
- category_map[category]
274
- for category in categorizer.predict(query, k=3, threshold=0.5)[0]
275
- ]
276
-
277
- min_idx = 0
278
- min_price = float('inf') # Initialize min_price as positive infinity
279
- for idx, product in enumerate(request_json):
280
- if (
281
- product["Inferred Category"] in query_categories
282
- and product["price"] <= min_price
283
- ):
284
- min_idx = idx
285
- min_price = product["price"] # Update min_price if a cheaper product is found
286
- for product in request_json:
287
- product["cheapest"] = False # Reset "cheapest" field for all products
288
- request_json[min_idx]["cheapest"] = True # Mark the cheapest product for the current query
 
 
 
 
 
 
 
 
 
 
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,