Spaces:
Sleeping
Sleeping
app.py Besh 2 (Push 8)
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
|
6 |
import spacy
|
7 |
import os
|
8 |
import logging
|
|
|
9 |
|
10 |
# Set up logging with detailed output
|
11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
@@ -38,8 +39,8 @@ else:
|
|
38 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
39 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
40 |
|
41 |
-
# Static Context
|
42 |
-
context_msg = "
|
43 |
|
44 |
# spaCy Setup
|
45 |
spacy_model_path = "/home/user/app/en_core_web_sm-3.8.0"
|
@@ -63,20 +64,23 @@ def extract_keywords(text: str) -> List[str]:
|
|
63 |
|
64 |
def detect_intent(text: str) -> str:
|
65 |
doc = nlp(text.lower())
|
66 |
-
|
67 |
-
if "shirt" in [token.text for token in doc]:
|
68 |
return "recommend_shirt"
|
69 |
-
elif "short" in [token.text for token in doc]:
|
70 |
return "recommend_shorts"
|
71 |
elif any(token.text in ["what", "who", "company", "do", "products"] for token in doc):
|
72 |
return "company_info"
|
|
|
|
|
|
|
|
|
73 |
return "unknown"
|
74 |
|
75 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
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 |
{
|
@@ -96,12 +100,31 @@ def get_product_context(products: List[Dict]) -> str:
|
|
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
|
101 |
-
|
102 |
-
|
|
|
|
|
103 |
elif intent == "company_info":
|
104 |
return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
return response
|
106 |
|
107 |
# Endpoints
|
@@ -125,7 +148,7 @@ async def process_prompt(request: PromptRequest):
|
|
125 |
logger.info(f"Products matched: {len(products)}")
|
126 |
|
127 |
history_str = " || ".join(history)
|
128 |
-
full_input = f"{history_str} || {product_context}
|
129 |
logger.info(f"Full input to model: {full_input}")
|
130 |
|
131 |
logger.info("Tokenizing input...")
|
@@ -140,7 +163,7 @@ async def process_prompt(request: PromptRequest):
|
|
140 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
141 |
logger.info(f"Model response: {response}")
|
142 |
|
143 |
-
enhanced_response = format_response(response, products, intent)
|
144 |
qa_response = {
|
145 |
"question": input_text,
|
146 |
"answer": enhanced_response,
|
|
|
6 |
import spacy
|
7 |
import os
|
8 |
import logging
|
9 |
+
import re
|
10 |
|
11 |
# Set up logging with detailed output
|
12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
39 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_dir)
|
40 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_dir)
|
41 |
|
42 |
+
# No Static Context
|
43 |
+
context_msg = ""
|
44 |
|
45 |
# spaCy Setup
|
46 |
spacy_model_path = "/home/user/app/en_core_web_sm-3.8.0"
|
|
|
64 |
|
65 |
def detect_intent(text: str) -> str:
|
66 |
doc = nlp(text.lower())
|
67 |
+
text_lower = text.lower()
|
68 |
+
if "shirt" in [token.text for token in doc]:
|
69 |
return "recommend_shirt"
|
70 |
+
elif "short" in [token.text for token in doc]:
|
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 "unknown"
|
79 |
|
80 |
def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
|
81 |
if not keywords:
|
82 |
logger.info("No keywords provided, returning empty product list.")
|
83 |
return []
|
|
|
84 |
query = {"$or": [{"name": {"$regex": f"\\b{keyword}\\b", "$options": "i"}} for keyword in keywords]}
|
85 |
matched_products = [
|
86 |
{
|
|
|
100 |
product_str += ", ".join([f"'{p['name']}' - {p['description']}" for p in products[:2]])
|
101 |
return product_str
|
102 |
|
103 |
+
def format_response(response: str, products: List[Dict], intent: str, input_text: str) -> str:
|
104 |
+
if intent == "recommend_shirt" or intent == "recommend_shorts":
|
105 |
+
if products:
|
106 |
+
product = products[0]
|
107 |
+
return f"{response} For example, check out our '{product['name']}'—it’s {product['description'].lower()}!"
|
108 |
+
return response
|
109 |
elif intent == "company_info":
|
110 |
return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
|
111 |
+
elif intent == "ask_name":
|
112 |
+
return "I’m Grok, your friendly assistant from Hutter Products GmbH. How can I help you today?"
|
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}!" if num2 != 0 else "Can’t divide by zero!"
|
125 |
+
return "I can do simple math! Try something like '1 + 1'."
|
126 |
+
elif intent == "unknown":
|
127 |
+
return response # Let BlenderBot respond freely for unknown intent
|
128 |
return response
|
129 |
|
130 |
# Endpoints
|
|
|
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...")
|
|
|
163 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
164 |
logger.info(f"Model response: {response}")
|
165 |
|
166 |
+
enhanced_response = format_response(response, products, intent, input_text)
|
167 |
qa_response = {
|
168 |
"question": input_text,
|
169 |
"answer": enhanced_response,
|