Spaces:
Sleeping
Sleeping
Updated with Content msg, product recommendation and history information.
Browse files
app.py
CHANGED
@@ -39,8 +39,8 @@ else:
|
|
39 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
40 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
41 |
|
42 |
-
#
|
43 |
-
context_msg = ""
|
44 |
|
45 |
# spaCy Setup
|
46 |
spacy_model_path = "/home/user/app/en_core_web_sm-3.8.0"
|
@@ -65,17 +65,15 @@ def extract_keywords(text: str) -> List[str]:
|
|
65 |
def detect_intent(text: str) -> str:
|
66 |
doc = nlp(text.lower())
|
67 |
text_lower = text.lower()
|
68 |
-
if "shirt"
|
69 |
-
return "
|
70 |
-
elif
|
71 |
-
return "recommend_shorts"
|
72 |
-
elif any(token.text in ["what", "who", "company", "do", "products"] for token in doc):
|
73 |
return "company_info"
|
74 |
elif "name" in text_lower:
|
75 |
return "ask_name"
|
76 |
elif re.search(r"\d+\s*[\+\-\*/]\s*\d+", text_lower):
|
77 |
return "math_query"
|
78 |
-
return "
|
79 |
|
80 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
81 |
if not keywords:
|
@@ -101,31 +99,33 @@ def get_product_context(products: List[Dict]) -> str:
|
|
101 |
return product_str
|
102 |
|
103 |
def format_response(response: str, products: List[Dict], intent: str, input_text: str) -> str:
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
109 |
elif intent == "company_info":
|
110 |
-
return f"{response}
|
111 |
elif intent == "ask_name":
|
112 |
-
return "I’m
|
113 |
elif intent == "math_query":
|
114 |
match = re.search(r"(\d+)\s*([\+\-\*/])\s*(\d+)", input_text.lower())
|
115 |
if match:
|
116 |
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
|
117 |
if op == "+":
|
118 |
-
return f"{num1} plus {num2} is {num1 + num2}
|
119 |
elif op == "-":
|
120 |
-
return f"{num1} minus {num2} is {num1 - num2}
|
121 |
elif op == "*":
|
122 |
-
return f"{num1} times {num2} is {num1 * num2}
|
123 |
elif op == "/":
|
124 |
-
return f"{num1} divided by {num2} is {num1 / num2}
|
125 |
-
return "I can
|
126 |
-
|
127 |
-
return response # Let BlenderBot respond freely for unknown intent
|
128 |
-
return response
|
129 |
|
130 |
# Endpoints
|
131 |
@app.get("/")
|
@@ -148,7 +148,7 @@ async def process_prompt(request: PromptRequest):
|
|
148 |
logger.info(f"Products matched: {len(products)}")
|
149 |
|
150 |
history_str = " || ".join(history)
|
151 |
-
full_input = f"{history_str} || {product_context} || {input_text}" if (history or product_context) else input_text
|
152 |
logger.info(f"Full input to model: {full_input}")
|
153 |
|
154 |
logger.info("Tokenizing input...")
|
|
|
39 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
40 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
41 |
|
42 |
+
# Static Context based on Hutter Products GmbH home page
|
43 |
+
context_msg = "I am Hutter, your shopping guide for Hutter Products GmbH. I’m here to help you explore our innovative and sustainable product catalog, featuring eco-friendly items like recycled textiles and ocean plastic goods. Let me assist you in finding the perfect sustainable solution!"
|
44 |
|
45 |
# spaCy Setup
|
46 |
spacy_model_path = "/home/user/app/en_core_web_sm-3.8.0"
|
|
|
65 |
def detect_intent(text: str) -> str:
|
66 |
doc = nlp(text.lower())
|
67 |
text_lower = text.lower()
|
68 |
+
if any(token.text in ["buy", "shop", "find", "recommend", "product", "products", "item", "textile", "jacket", "shirt", "shorts"] for token in doc):
|
69 |
+
return "recommend_product"
|
70 |
+
elif any(token.text in ["what", "who", "company", "do"] for token in doc):
|
|
|
|
|
71 |
return "company_info"
|
72 |
elif "name" in text_lower:
|
73 |
return "ask_name"
|
74 |
elif re.search(r"\d+\s*[\+\-\*/]\s*\d+", text_lower):
|
75 |
return "math_query"
|
76 |
+
return "recommend_product" # Default to product exploration
|
77 |
|
78 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
79 |
if not keywords:
|
|
|
99 |
return product_str
|
100 |
|
101 |
def format_response(response: str, products: List[Dict], intent: str, input_text: str) -> str:
|
102 |
+
# Highlight products if available
|
103 |
+
if products:
|
104 |
+
product = products[0] # Prioritize the first matched product
|
105 |
+
product_highlight = f" How about our '{product['name']}'? It’s {product['description'].lower()}."
|
106 |
+
response += product_highlight
|
107 |
+
|
108 |
+
# Intent-specific tweaks
|
109 |
+
if intent == "recommend_product":
|
110 |
+
return response # Product info already appended if available
|
111 |
elif intent == "company_info":
|
112 |
+
return f"{response} Hutter Products GmbH specializes in sustainable product design and production."
|
113 |
elif intent == "ask_name":
|
114 |
+
return "I’m Hutter, your shopping guide for Hutter Products GmbH. How can I assist you today?"
|
115 |
elif intent == "math_query":
|
116 |
match = re.search(r"(\d+)\s*([\+\-\*/])\s*(\d+)", input_text.lower())
|
117 |
if match:
|
118 |
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
|
119 |
if op == "+":
|
120 |
+
return f"{response} Also, {num1} plus {num2} is {num1 + num2}."
|
121 |
elif op == "-":
|
122 |
+
return f"{response} Also, {num1} minus {num2} is {num1 - num2}."
|
123 |
elif op == "*":
|
124 |
+
return f"{response} Also, {num1} times {num2} is {num1 * num2}."
|
125 |
elif op == "/":
|
126 |
+
return f"{response} Also, {num1} divided by {num2} is {num1 / num2}." if num2 != 0 else f"{response} Can’t divide by zero!"
|
127 |
+
return f"{response} I can handle simple math too—try something like '2 + 2'."
|
128 |
+
return response # Default case includes products if matched
|
|
|
|
|
129 |
|
130 |
# Endpoints
|
131 |
@app.get("/")
|
|
|
148 |
logger.info(f"Products matched: {len(products)}")
|
149 |
|
150 |
history_str = " || ".join(history)
|
151 |
+
full_input = f"{context_msg} || {history_str} || {product_context} || {input_text}" if (history or product_context) else f"{context_msg} || {input_text}"
|
152 |
logger.info(f"Full input to model: {full_input}")
|
153 |
|
154 |
logger.info("Tokenizing input...")
|