Spaces:
Sleeping
Sleeping
Updated with Content msg, product recommendation and history information5
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import os
|
|
8 |
import logging
|
9 |
import re
|
10 |
import torch
|
11 |
-
import random
|
12 |
|
13 |
# Set up logging
|
14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
@@ -74,11 +74,11 @@ def detect_intent(text: str) -> str:
|
|
74 |
return "recommend_product"
|
75 |
elif any(token.text in ["company", "who", "do"] for token in doc):
|
76 |
return "company_info"
|
77 |
-
elif "name" in text_lower or "yourself" in text_lower or "you" in doc and "about" in doc:
|
78 |
return "ask_name"
|
79 |
elif re.search(r"\d+\s*[\+\-\*/]\s*\d+", text_lower):
|
80 |
return "math_query"
|
81 |
-
return "chat"
|
82 |
|
83 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
84 |
if not keywords:
|
@@ -102,39 +102,44 @@ def get_product_context(products: List[Dict]) -> str:
|
|
102 |
return product_str
|
103 |
|
104 |
def format_response(response: str, products: List[Dict], intent: str, input_text: str, history: List[str]) -> str:
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
"Tell me what you’re after, and I’ll find something great from our eco-friendly range!"
|
109 |
-
]
|
110 |
-
|
111 |
if intent == "recommend_product":
|
112 |
-
if
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
116 |
elif intent == "company_info":
|
117 |
-
return "Hutter Products GmbH
|
|
|
118 |
elif intent == "ask_name":
|
119 |
-
return "I’m Hutter, your shopping guide for Hutter Products GmbH
|
|
|
120 |
elif intent == "math_query":
|
121 |
match = re.search(r"(\d+)\s*([\+\-\*/])\s*(\d+)", input_text.lower())
|
122 |
if match:
|
123 |
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
|
124 |
-
if op == "+": return f"{num1} + {num2} = {num1 + num2}.
|
125 |
-
elif op == "-": return f"{num1} - {num2} = {num1 - num2}.
|
126 |
-
elif op == "*": return f"{num1} * {num2} = {num1 * num2}.
|
127 |
-
elif op == "/": return f"{num1} / {num2} = {num1 / num2}." if num2 != 0 else "Can’t divide by zero!
|
128 |
-
return "I can
|
|
|
129 |
elif intent == "chat":
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
return
|
138 |
|
139 |
# Endpoints
|
140 |
@app.get("/")
|
@@ -170,8 +175,8 @@ async def process_prompt(request: PromptRequest):
|
|
170 |
**inputs,
|
171 |
max_new_tokens=30,
|
172 |
do_sample=True,
|
173 |
-
top_p=0.95,
|
174 |
-
temperature=0.8,
|
175 |
no_repeat_ngram_size=2
|
176 |
)
|
177 |
logger.info("Model generation complete.")
|
|
|
8 |
import logging
|
9 |
import re
|
10 |
import torch
|
11 |
+
import random
|
12 |
|
13 |
# Set up logging
|
14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
74 |
return "recommend_product"
|
75 |
elif any(token.text in ["company", "who", "do"] for token in doc):
|
76 |
return "company_info"
|
77 |
+
elif "name" in text_lower or "yourself" in text_lower or ("you" in doc and "about" in doc):
|
78 |
return "ask_name"
|
79 |
elif re.search(r"\d+\s*[\+\-\*/]\s*\d+", text_lower):
|
80 |
return "math_query"
|
81 |
+
return "chat"
|
82 |
|
83 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
84 |
if not keywords:
|
|
|
102 |
return product_str
|
103 |
|
104 |
def format_response(response: str, products: List[Dict], intent: str, input_text: str, history: List[str]) -> str:
|
105 |
+
# Base response is BlenderBot’s output; adjust based on intent
|
106 |
+
base_response = response if response else "I’m here to help—what’s on your mind?"
|
107 |
+
|
|
|
|
|
|
|
108 |
if intent == "recommend_product":
|
109 |
+
if products:
|
110 |
+
product = products[0]
|
111 |
+
return f"{base_response} Speaking of sustainable products, check out our '{product['name']}'—it’s {product['description'].lower()}."
|
112 |
+
prompts = [
|
113 |
+
f"{base_response} What sustainable items are you looking for today?",
|
114 |
+
f"{base_response} Any specific eco-friendly products you’re curious about?",
|
115 |
+
]
|
116 |
+
return random.choice(prompts)
|
117 |
+
|
118 |
elif intent == "company_info":
|
119 |
+
return f"{base_response} I’m with Hutter Products GmbH—we focus on sustainable items like recycled textiles and ocean plastic goods."
|
120 |
+
|
121 |
elif intent == "ask_name":
|
122 |
+
return f"{base_response} I’m Hutter, your shopping guide for Hutter Products GmbH, here to assist with sustainable products."
|
123 |
+
|
124 |
elif intent == "math_query":
|
125 |
match = re.search(r"(\d+)\s*([\+\-\*/])\s*(\d+)", input_text.lower())
|
126 |
if match:
|
127 |
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
|
128 |
+
if op == "+": return f"{base_response} By the way, {num1} + {num2} = {num1 + num2}."
|
129 |
+
elif op == "-": return f"{base_response} Also, {num1} - {num2} = {num1 - num2}."
|
130 |
+
elif op == "*": return f"{base_response} Oh, and {num1} * {num2} = {num1 * num2}."
|
131 |
+
elif op == "/": return f"{base_response} Plus, {num1} / {num2} = {num1 / num2}." if num2 != 0 else f"{base_response} Can’t divide by zero, though!"
|
132 |
+
return f"{base_response} I can help with math—try something like '2 + 2'."
|
133 |
+
|
134 |
elif intent == "chat":
|
135 |
+
if "yes" in input_text.lower() and history and any(word in history[-1].lower() for word in ["hat", "product", "store"]):
|
136 |
+
if products:
|
137 |
+
product = products[0]
|
138 |
+
return f"{base_response} Great! How about our '{product['name']}'? It’s {product['description'].lower()}."
|
139 |
+
return f"{base_response} Want me to suggest some sustainable items?"
|
140 |
+
return base_response # Let BlenderBot shine for casual chat
|
141 |
+
|
142 |
+
return base_response # Fallback
|
143 |
|
144 |
# Endpoints
|
145 |
@app.get("/")
|
|
|
175 |
**inputs,
|
176 |
max_new_tokens=30,
|
177 |
do_sample=True,
|
178 |
+
top_p=0.95,
|
179 |
+
temperature=0.8,
|
180 |
no_repeat_ngram_size=2
|
181 |
)
|
182 |
logger.info("Model generation complete.")
|