SyedHutter commited on
Commit
ef28d8e
·
verified ·
1 Parent(s): 7078460

Updated with Content msg, product recommendation and history information5

Browse files
Files changed (1) hide show
  1. app.py +35 -30
app.py CHANGED
@@ -8,7 +8,7 @@ import os
8
  import logging
9
  import re
10
  import torch
11
- import random # For response variety
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" # New fallback for general conversation
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
- no_product_prompts = [
106
- "I’d love to recommend something! What are you looking for in our sustainable catalog?",
107
- "Our sustainable catalog has lots to offer—what catches your interest?",
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 not products:
113
- return random.choice(no_product_prompts)
114
- product = products[0]
115
- return f"Check out our '{product['name']}'—it’s {product['description'].lower()}. Want more options?"
 
 
 
 
 
116
  elif intent == "company_info":
117
- return "Hutter Products GmbH offers sustainable products like recycled textiles and ocean plastic goods."
 
118
  elif intent == "ask_name":
119
- return "I’m Hutter, your shopping guide for Hutter Products GmbH. I’m here to help you find eco-friendly products—how can I assist?"
 
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}. Need shopping help?"
125
- elif op == "-": return f"{num1} - {num2} = {num1 - num2}. Anything else?"
126
- elif op == "*": return f"{num1} * {num2} = {num1 * num2}. Explore our products?"
127
- elif op == "/": return f"{num1} / {num2} = {num1 / num2}." if num2 != 0 else "Can’t divide by zero! Try our products?"
128
- return "I can do math—try '2 + 2'. What else can I help with?"
 
129
  elif intent == "chat":
130
- # Use BlenderBot’s response if appropriate, else nudge toward shopping
131
- if "yes" in input_text.lower() and history and "hat" in history[-1].lower():
132
- return "Great! Besides the Bucket Hat, we have other sustainable items—want to hear more?"
133
- return f"{response} How can I assist with our sustainable products today?" if response else "Im here to help—anything on your mind?"
134
- if products:
135
- product = products[0]
136
- return f"{response} Also, check out '{product['name']}'—it’s {product['description'].lower()}."
137
- return response if response else "How can I assist with our sustainable products?"
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, # Slightly higher for more variety
174
- temperature=0.8, # Slightly higher for creativity
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']}'? Its {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.")