Spaces:
Sleeping
Sleeping
Updated with Content msg, product recommendation and history information2
Browse files
app.py
CHANGED
@@ -39,7 +39,7 @@ else:
|
|
39 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
40 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
41 |
|
42 |
-
# Static Context
|
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
|
@@ -65,15 +65,16 @@ 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 |
-
|
|
|
69 |
return "recommend_product"
|
70 |
-
elif any(token.text in ["
|
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
|
77 |
|
78 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
79 |
if not keywords:
|
@@ -94,22 +95,19 @@ def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
|
94 |
def get_product_context(products: List[Dict]) -> str:
|
95 |
if not products:
|
96 |
return ""
|
97 |
-
product_str = "
|
98 |
product_str += ", ".join([f"'{p['name']}' - {p['description']}" for p in products[:2]])
|
99 |
return product_str
|
100 |
|
101 |
def format_response(response: str, products: List[Dict], intent: str, input_text: str) -> str:
|
102 |
-
#
|
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 |
-
|
|
|
|
|
|
|
111 |
elif intent == "company_info":
|
112 |
-
return
|
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":
|
@@ -117,15 +115,19 @@ def format_response(response: str, products: List[Dict], intent: str, input_text
|
|
117 |
if match:
|
118 |
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
|
119 |
if op == "+":
|
120 |
-
return f"{
|
121 |
elif op == "-":
|
122 |
-
return f"{
|
123 |
elif op == "*":
|
124 |
-
return f"{
|
125 |
elif op == "/":
|
126 |
-
return f"{
|
127 |
-
return
|
128 |
-
|
|
|
|
|
|
|
|
|
129 |
|
130 |
# Endpoints
|
131 |
@app.get("/")
|
@@ -156,7 +158,7 @@ async def process_prompt(request: PromptRequest):
|
|
156 |
logger.info("Input tokenized successfully.")
|
157 |
|
158 |
logger.info("Generating model response...")
|
159 |
-
outputs = model.generate(**inputs, max_length=50, num_beams=1, no_repeat_ngram_size=2)
|
160 |
logger.info("Model generation complete.")
|
161 |
|
162 |
logger.info("Decoding model output...")
|
|
|
39 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
40 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
41 |
|
42 |
+
# Static Context
|
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
|
|
|
65 |
def detect_intent(text: str) -> str:
|
66 |
doc = nlp(text.lower())
|
67 |
text_lower = text.lower()
|
68 |
+
# General product-related intent based on shopping context
|
69 |
+
if any(token.text in ["buy", "shop", "find", "recommend", "product", "products", "item", "store", "catalog"] for token in doc) or "what" in text_lower.split()[:2]:
|
70 |
return "recommend_product"
|
71 |
+
elif any(token.text in ["company", "who", "do"] for token in doc):
|
72 |
return "company_info"
|
73 |
elif "name" in text_lower:
|
74 |
return "ask_name"
|
75 |
elif re.search(r"\d+\s*[\+\-\*/]\s*\d+", text_lower):
|
76 |
return "math_query"
|
77 |
+
return "recommend_product" # Default to product focus for scalability
|
78 |
|
79 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
80 |
if not keywords:
|
|
|
95 |
def get_product_context(products: List[Dict]) -> str:
|
96 |
if not products:
|
97 |
return ""
|
98 |
+
product_str = "Available products: "
|
99 |
product_str += ", ".join([f"'{p['name']}' - {p['description']}" for p in products[:2]])
|
100 |
return product_str
|
101 |
|
102 |
def format_response(response: str, products: List[Dict], intent: str, input_text: str) -> str:
|
103 |
+
# Handle product recommendation intent
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
if intent == "recommend_product":
|
105 |
+
if not products:
|
106 |
+
return "I’d love to recommend something from our sustainable catalog! Could you tell me more about what you’re looking for?"
|
107 |
+
product = products[0]
|
108 |
+
return f"Check out our '{product['name']}'—it’s {product['description'].lower()}. Want to explore more options?"
|
109 |
elif intent == "company_info":
|
110 |
+
return "Hutter Products GmbH specializes in sustainable product design and production, offering eco-friendly items like recycled textiles and ocean plastic goods."
|
111 |
elif intent == "ask_name":
|
112 |
return "I’m Hutter, your shopping guide for Hutter Products GmbH. How can I assist you today?"
|
113 |
elif intent == "math_query":
|
|
|
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}. Need help with shopping too?"
|
119 |
elif op == "-":
|
120 |
+
return f"{num1} minus {num2} is {num1 - num2}. Anything else I can assist with?"
|
121 |
elif op == "*":
|
122 |
+
return f"{num1} times {num2} is {num1 * num2}. Want to explore our products?"
|
123 |
elif op == "/":
|
124 |
+
return f"{num1} divided by {num2} is {num1 / num2}." if num2 != 0 else "Can’t divide by zero! How about some sustainable products instead?"
|
125 |
+
return "I can do simple math—try '2 + 2'. What else can I help you with?"
|
126 |
+
# Fallback with product nudge if available
|
127 |
+
if products:
|
128 |
+
product = products[0]
|
129 |
+
return f"{response} By the way, how about our '{product['name']}'? It’s {product['description'].lower()}."
|
130 |
+
return response if response else "How can I assist you with our sustainable products today?"
|
131 |
|
132 |
# Endpoints
|
133 |
@app.get("/")
|
|
|
158 |
logger.info("Input tokenized successfully.")
|
159 |
|
160 |
logger.info("Generating model response...")
|
161 |
+
outputs = model.generate(**inputs, max_length=50, num_beams=4, length_penalty=1.0, no_repeat_ngram_size=2)
|
162 |
logger.info("Model generation complete.")
|
163 |
|
164 |
logger.info("Decoding model output...")
|