Update app.py
Browse files
app.py
CHANGED
|
@@ -369,11 +369,13 @@ def match_dish(user_input: str, threshold: int = 70) -> str:
|
|
| 369 |
return best_match
|
| 370 |
return None
|
| 371 |
|
|
|
|
| 372 |
def match_dishes(user_input: str, threshold: int = 70) -> list:
|
| 373 |
matched_dishes = []
|
| 374 |
user_input_lower = user_input.lower()
|
| 375 |
for item in menu_items:
|
| 376 |
dish_name = item["name"]
|
|
|
|
| 377 |
if dish_name.lower() in user_input_lower:
|
| 378 |
matched_dishes.append(dish_name)
|
| 379 |
else:
|
|
@@ -389,6 +391,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 389 |
del user_state[user_id]
|
| 390 |
state = None
|
| 391 |
|
|
|
|
| 392 |
if message.lower() in ["order", "menu"]:
|
| 393 |
state = ConversationState()
|
| 394 |
state.flow = "order"
|
|
@@ -399,6 +402,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 399 |
return "Sure! What dish would you like to order?"
|
| 400 |
return ""
|
| 401 |
|
|
|
|
| 402 |
if not state and "order" in message.lower():
|
| 403 |
state = ConversationState()
|
| 404 |
state.flow = "order"
|
|
@@ -407,13 +411,57 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 407 |
user_state[user_id] = state
|
| 408 |
return "Sure! What dish would you like to order?"
|
| 409 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
if not state or state.flow != "order":
|
| 411 |
matched_dishes = match_dishes(message)
|
| 412 |
if matched_dishes:
|
| 413 |
if len(matched_dishes) > 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
dish_options = ", ".join(matched_dishes)
|
| 415 |
return (f"We found multiple dishes in your request: {dish_options}. "
|
| 416 |
-
"Please specify which one you'd like to order.")
|
| 417 |
else:
|
| 418 |
found_dish = matched_dishes[0]
|
| 419 |
state = ConversationState()
|
|
@@ -422,6 +470,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 422 |
state.update_last_active()
|
| 423 |
user_state[user_id] = state
|
| 424 |
|
|
|
|
| 425 |
numbers = re.findall(r'\d+', message)
|
| 426 |
if numbers:
|
| 427 |
quantity = int(numbers[0])
|
|
@@ -429,6 +478,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 429 |
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 430 |
state.data["quantity"] = quantity
|
| 431 |
state.step = 3
|
|
|
|
| 432 |
phone_pattern = r'(\+?\d{10,15})'
|
| 433 |
phone_match = re.search(phone_pattern, message)
|
| 434 |
address = None
|
|
|
|
| 369 |
return best_match
|
| 370 |
return None
|
| 371 |
|
| 372 |
+
# Enhanced matching function: returns a list of dish names that match the user input.
|
| 373 |
def match_dishes(user_input: str, threshold: int = 70) -> list:
|
| 374 |
matched_dishes = []
|
| 375 |
user_input_lower = user_input.lower()
|
| 376 |
for item in menu_items:
|
| 377 |
dish_name = item["name"]
|
| 378 |
+
# Direct substring check
|
| 379 |
if dish_name.lower() in user_input_lower:
|
| 380 |
matched_dishes.append(dish_name)
|
| 381 |
else:
|
|
|
|
| 391 |
del user_state[user_id]
|
| 392 |
state = None
|
| 393 |
|
| 394 |
+
# Trigger initial order flow when user types "order" or "menu"
|
| 395 |
if message.lower() in ["order", "menu"]:
|
| 396 |
state = ConversationState()
|
| 397 |
state.flow = "order"
|
|
|
|
| 402 |
return "Sure! What dish would you like to order?"
|
| 403 |
return ""
|
| 404 |
|
| 405 |
+
# If no state exists but message contains "order", start a new order flow.
|
| 406 |
if not state and "order" in message.lower():
|
| 407 |
state = ConversationState()
|
| 408 |
state.flow = "order"
|
|
|
|
| 411 |
user_state[user_id] = state
|
| 412 |
return "Sure! What dish would you like to order?"
|
| 413 |
|
| 414 |
+
# If we're in order flow and we've already stored candidate dishes,
|
| 415 |
+
# then check if the user clarified selection.
|
| 416 |
+
if state and state.flow == "order" and "candidate_dishes" in state.data:
|
| 417 |
+
# Accept "both" (or "all") as selecting all candidate dishes.
|
| 418 |
+
if message.strip().lower() in ["both", "all"]:
|
| 419 |
+
state.data["dishes"] = state.data["candidate_dishes"]
|
| 420 |
+
# Clear candidate_dishes now that selection is made.
|
| 421 |
+
del state.data["candidate_dishes"]
|
| 422 |
+
state.step = 2 # Ask for quantity details.
|
| 423 |
+
dishes_str = ", ".join(state.data["dishes"])
|
| 424 |
+
return f"You have selected: {dishes_str}. How many servings of each would you like? " \
|
| 425 |
+
f"(For example, '2 for Jollof Rice and 3 for Fried Rice')"
|
| 426 |
+
else:
|
| 427 |
+
# If the user specifies one dish from the candidate list, use that.
|
| 428 |
+
for dish in state.data["candidate_dishes"]:
|
| 429 |
+
if dish.lower() in message.lower():
|
| 430 |
+
state.data["dish"] = dish
|
| 431 |
+
del state.data["candidate_dishes"]
|
| 432 |
+
# Proceed to extract quantity
|
| 433 |
+
numbers = re.findall(r'\d+', message)
|
| 434 |
+
if numbers:
|
| 435 |
+
quantity = int(numbers[0])
|
| 436 |
+
if quantity <= 0:
|
| 437 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 438 |
+
state.data["quantity"] = quantity
|
| 439 |
+
state.step = 3
|
| 440 |
+
# Continue with contact details extraction...
|
| 441 |
+
return (f"You selected {dish} with {quantity} serving(s). "
|
| 442 |
+
"Please provide your phone number and delivery address.")
|
| 443 |
+
else:
|
| 444 |
+
state.step = 2
|
| 445 |
+
return f"You selected {dish}. How many servings would you like?"
|
| 446 |
+
# If no candidate dish is clearly specified, ask for clarification again.
|
| 447 |
+
dish_options = ", ".join(state.data["candidate_dishes"])
|
| 448 |
+
return f"Please specify which one you'd like to order from: {dish_options} " \
|
| 449 |
+
f"(or type 'both' if you'd like to order all)."
|
| 450 |
+
|
| 451 |
+
# When not already in order flow, try to detect dish(es) using fuzzy matching.
|
| 452 |
if not state or state.flow != "order":
|
| 453 |
matched_dishes = match_dishes(message)
|
| 454 |
if matched_dishes:
|
| 455 |
if len(matched_dishes) > 1:
|
| 456 |
+
# Store candidate dishes in state for later clarification.
|
| 457 |
+
state = ConversationState()
|
| 458 |
+
state.flow = "order"
|
| 459 |
+
state.update_last_active()
|
| 460 |
+
state.data["candidate_dishes"] = matched_dishes
|
| 461 |
+
user_state[user_id] = state
|
| 462 |
dish_options = ", ".join(matched_dishes)
|
| 463 |
return (f"We found multiple dishes in your request: {dish_options}. "
|
| 464 |
+
"Please specify which one you'd like to order or type 'both' if you'd like all.")
|
| 465 |
else:
|
| 466 |
found_dish = matched_dishes[0]
|
| 467 |
state = ConversationState()
|
|
|
|
| 470 |
state.update_last_active()
|
| 471 |
user_state[user_id] = state
|
| 472 |
|
| 473 |
+
# Extract quantity (e.g. "Jollof of 2 portions")
|
| 474 |
numbers = re.findall(r'\d+', message)
|
| 475 |
if numbers:
|
| 476 |
quantity = int(numbers[0])
|
|
|
|
| 478 |
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 479 |
state.data["quantity"] = quantity
|
| 480 |
state.step = 3
|
| 481 |
+
# Extract phone and address if provided.
|
| 482 |
phone_pattern = r'(\+?\d{10,15})'
|
| 483 |
phone_match = re.search(phone_pattern, message)
|
| 484 |
address = None
|