SyedHutter commited on
Commit
6a04711
·
verified ·
1 Parent(s): 8e7aaf2

app.py Beta 2 (Push 8)

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -63,9 +63,10 @@ def extract_keywords(text: str) -> List[str]:
63
 
64
  def detect_intent(text: str) -> str:
65
  doc = nlp(text.lower())
66
- if any(token.text in ["shirt", "shirts"] for token in doc):
 
67
  return "recommend_shirt"
68
- elif any(token.text in ["short", "shorts"] for token in doc):
69
  return "recommend_shorts"
70
  elif any(token.text in ["what", "who", "company", "do", "products"] for token in doc):
71
  return "company_info"
@@ -75,8 +76,8 @@ def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
75
  if not keywords:
76
  logger.info("No keywords provided, returning empty product list.")
77
  return []
78
- query = {"$or": [{"name": {"$regex": keyword, "$options": "i"}} for keyword in keywords]}
79
- # Convert ObjectId to string and sanitize document
80
  matched_products = [
81
  {
82
  "id": str(p["_id"]),
@@ -92,13 +93,13 @@ def get_product_context(products: List[Dict]) -> str:
92
  if not products:
93
  return ""
94
  product_str = "Here are some products: "
95
- product_str += ", ".join([f"'{p['name']}' (SKU: {p['skuNumber']}) - {p['description']}" for p in products[:2]])
96
  return product_str
97
 
98
  def format_response(response: str, products: List[Dict], intent: str) -> str:
99
  if intent in ["recommend_shirt", "recommend_shorts"] and products:
100
  product = products[0]
101
- return f"{response} For example, check out our '{product['name']}' (SKU: {product['skuNumber']})—it’s {product['description'].lower()}!"
102
  elif intent == "company_info":
103
  return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
104
  return response
 
63
 
64
  def detect_intent(text: str) -> str:
65
  doc = nlp(text.lower())
66
+ # Stricter matching for "shirt" vs "short"
67
+ if "shirt" in [token.text for token in doc]: # Exact match for "shirt"
68
  return "recommend_shirt"
69
+ elif "short" in [token.text for token in doc]: # Exact match for "short"
70
  return "recommend_shorts"
71
  elif any(token.text in ["what", "who", "company", "do", "products"] for token in doc):
72
  return "company_info"
 
76
  if not keywords:
77
  logger.info("No keywords provided, returning empty product list.")
78
  return []
79
+ # Use stricter matching: only return products with exact keyword in name
80
+ query = {"$or": [{"name": {"$regex": f"\\b{keyword}\\b", "$options": "i"}} for keyword in keywords]}
81
  matched_products = [
82
  {
83
  "id": str(p["_id"]),
 
93
  if not products:
94
  return ""
95
  product_str = "Here are some products: "
96
+ product_str += ", ".join([f"'{p['name']}' - {p['description']}" for p in products[:2]])
97
  return product_str
98
 
99
  def format_response(response: str, products: List[Dict], intent: str) -> str:
100
  if intent in ["recommend_shirt", "recommend_shorts"] and products:
101
  product = products[0]
102
+ return f"{response} For example, check out our '{product['name']}'—it’s {product['description'].lower()}!"
103
  elif intent == "company_info":
104
  return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
105
  return response