Spaces:
Sleeping
Sleeping
Commit
·
43c0263
1
Parent(s):
bc2d699
Upd safety
Browse files- api/chatbot.py +25 -4
api/chatbot.py
CHANGED
|
@@ -68,10 +68,23 @@ class CookingTutorChatbot:
|
|
| 68 |
# Keep original language for native search - no translation needed
|
| 69 |
# The search engines now support native language sources
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
cooking_keywords =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
query_lower = user_query.lower()
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
logger.warning(f"[SAFETY] Non-cooking query detected: {user_query}")
|
| 76 |
return "⚠️ I'm a cooking tutor! Please ask me about recipes, cooking techniques, ingredients, or anything food-related."
|
| 77 |
|
|
@@ -163,7 +176,15 @@ class CookingTutorChatbot:
|
|
| 163 |
# Basic cooking relevance check for response
|
| 164 |
if response and len(response) > 50:
|
| 165 |
response_lower = response.lower()
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
logger.warning(f"[SAFETY] Non-cooking response detected, redirecting to cooking topic")
|
| 168 |
response = "⚠️ Let's stick to cooking-related topics. Try asking about recipes, techniques, or ingredients!"
|
| 169 |
|
|
|
|
| 68 |
# Keep original language for native search - no translation needed
|
| 69 |
# The search engines now support native language sources
|
| 70 |
|
| 71 |
+
# Multilingual cooking relevance check
|
| 72 |
+
cooking_keywords = {
|
| 73 |
+
'en': ['recipe', 'cooking', 'baking', 'food', 'ingredient', 'kitchen', 'chef', 'meal', 'dish', 'cuisine', 'cook', 'bake', 'roast', 'grill', 'fry', 'boil', 'steam', 'season', 'spice', 'herb', 'sauce', 'marinade', 'dressing', 'appetizer', 'main course', 'dessert', 'breakfast', 'lunch', 'dinner'],
|
| 74 |
+
'vi': ['công thức', 'nấu ăn', 'nướng', 'thức ăn', 'nguyên liệu', 'bếp', 'đầu bếp', 'bữa ăn', 'món ăn', 'ẩm thực', 'nấu', 'nướng', 'rang', 'nướng vỉ', 'chiên', 'luộc', 'hấp', 'gia vị', 'thảo mộc', 'nước sốt', 'tẩm ướp', 'khai vị', 'món chính', 'tráng miệng', 'sáng', 'trưa', 'tối', 'bún', 'phở', 'chả', 'nem', 'gỏi', 'canh', 'cháo', 'cơm', 'bánh', 'chè'],
|
| 75 |
+
'zh': ['食谱', '烹饪', '烘焙', '食物', '食材', '厨房', '厨师', '餐', '菜', '菜系', '煮', '烤', '炒', '炸', '蒸', '调料', '香料', '酱汁', '开胃菜', '主菜', '甜点', '早餐', '午餐', '晚餐', '面条', '米饭', '汤', '饺子', '包子']
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
# Check cooking relevance in multiple languages
|
| 79 |
query_lower = user_query.lower()
|
| 80 |
+
is_cooking_related = False
|
| 81 |
+
|
| 82 |
+
for language, keywords in cooking_keywords.items():
|
| 83 |
+
if any(keyword in query_lower for keyword in keywords):
|
| 84 |
+
is_cooking_related = True
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
if not is_cooking_related:
|
| 88 |
logger.warning(f"[SAFETY] Non-cooking query detected: {user_query}")
|
| 89 |
return "⚠️ I'm a cooking tutor! Please ask me about recipes, cooking techniques, ingredients, or anything food-related."
|
| 90 |
|
|
|
|
| 176 |
# Basic cooking relevance check for response
|
| 177 |
if response and len(response) > 50:
|
| 178 |
response_lower = response.lower()
|
| 179 |
+
is_cooking_response = False
|
| 180 |
+
|
| 181 |
+
# Check if response contains cooking keywords in any language
|
| 182 |
+
for language, keywords in cooking_keywords.items():
|
| 183 |
+
if any(keyword in response_lower for keyword in keywords):
|
| 184 |
+
is_cooking_response = True
|
| 185 |
+
break
|
| 186 |
+
|
| 187 |
+
if not is_cooking_response:
|
| 188 |
logger.warning(f"[SAFETY] Non-cooking response detected, redirecting to cooking topic")
|
| 189 |
response = "⚠️ Let's stick to cooking-related topics. Try asking about recipes, techniques, or ingredients!"
|
| 190 |
|