Fred808 commited on
Commit
a37b1ef
·
verified ·
1 Parent(s): 776356e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -25
app.py CHANGED
@@ -391,31 +391,29 @@ async def process_order_flow(user_id: str, message: str) -> str:
391
  del user_state[user_id]
392
  state = None
393
 
394
- # If we're already expecting quantity details (step 2), parse that.
395
  if state and state.flow == "order" and state.step == 2:
396
  # Expecting a message like "2 for Jollof Rice and 3 for Fried Rice"
397
  pairs = re.findall(r'(\d+)\s*for\s*([a-zA-Z\s]+)', message, flags=re.IGNORECASE)
398
  if pairs:
399
  order_quantities = {}
400
- # Get candidate dishes from either multiple selection or a single dish.
401
  candidate_dishes = state.data.get("dishes")
402
  if not candidate_dishes:
403
- # If only one dish was selected, candidate_dishes is the single dish.
404
  candidate_dishes = [state.data.get("dish")]
405
  for quantity, dish_text in pairs:
406
  dish_text = dish_text.strip().lower()
407
  for candidate in candidate_dishes:
408
- # If candidate matches the text (case-insensitive substring match)
409
  if candidate.lower() in dish_text or dish_text in candidate.lower():
410
  order_quantities[candidate] = int(quantity)
411
  if order_quantities:
412
  state.data["orders"] = order_quantities
413
- state.step = 3 # Move to next step (e.g. collecting contact details)
414
  order_summary = "\n".join([f"{q} serving(s) of {d}" for d, q in order_quantities.items()])
415
  return f"Got it. You have ordered:\n{order_summary}\nPlease provide your phone number and delivery address."
416
  return "I'm sorry, I didn't understand the quantity details. Please specify like '2 for Jollof Rice and 3 for Fried Rice'."
417
 
418
- # Trigger initial order flow when user types "order" or "menu"
419
  if message.lower() in ["order", "menu"]:
420
  state = ConversationState()
421
  state.flow = "order"
@@ -434,12 +432,12 @@ async def process_order_flow(user_id: str, message: str) -> str:
434
  user_state[user_id] = state
435
  return "Sure! What dish would you like to order?"
436
 
437
- # If not already in order flow, try to detect dish(es) using fuzzy matching.
438
  if not state or state.flow != "order":
439
  matched_dishes = match_dishes(message)
440
  if matched_dishes:
 
441
  if len(matched_dishes) > 1:
442
- # Store candidate dishes and ask user to clarify.
443
  state = ConversationState()
444
  state.flow = "order"
445
  state.update_last_active()
@@ -449,14 +447,14 @@ async def process_order_flow(user_id: str, message: str) -> str:
449
  return (f"We found multiple dishes in your request: {dish_options}. "
450
  "Please specify which one you'd like to order or type 'both' if you'd like all.")
451
  else:
 
452
  found_dish = matched_dishes[0]
453
  state = ConversationState()
454
  state.flow = "order"
455
  state.data["dish"] = found_dish
456
  state.update_last_active()
457
  user_state[user_id] = state
458
-
459
- # Extract quantity if provided (e.g. "Jollof of 2 portions")
460
  numbers = re.findall(r'\d+', message)
461
  if numbers:
462
  quantity = int(numbers[0])
@@ -464,7 +462,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
464
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
465
  state.data["quantity"] = quantity
466
  state.step = 3
467
- # Proceed with extracting phone and address (see below)
468
  phone_pattern = r'(\+?\d{10,15})'
469
  phone_match = re.search(phone_pattern, message)
470
  address = None
@@ -497,10 +495,10 @@ async def process_order_flow(user_id: str, message: str) -> str:
497
  else:
498
  return "I couldn't identify the dish. Please type the dish name from our menu."
499
 
500
- # If we already have candidate dishes (from a multiple match), check the user clarification.
501
  if state and state.flow == "order" and "candidate_dishes" in state.data:
502
  if message.strip().lower() in ["both", "all"]:
503
- # User selects all candidate dishes.
504
  state.data["dishes"] = state.data["candidate_dishes"]
505
  del state.data["candidate_dishes"]
506
  state.step = 2 # Now expect quantity details for each dish.
@@ -513,7 +511,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
513
  if dish.lower() in message.lower():
514
  state.data["dish"] = dish
515
  del state.data["candidate_dishes"]
516
- # Try to extract quantity.
517
  numbers = re.findall(r'\d+', message)
518
  if numbers:
519
  quantity = int(numbers[0])
@@ -526,20 +523,9 @@ async def process_order_flow(user_id: str, message: str) -> str:
526
  else:
527
  state.step = 2
528
  return f"You selected {dish}. How many servings would you like?"
529
- # If still unclear, ask for clarification again.
530
  dish_options = ", ".join(state.data["candidate_dishes"])
531
  return f"Please specify which one you'd like to order from: {dish_options} " \
532
  f"(or type 'both' if you'd like to order all)."
533
- if state.step == 2:
534
- numbers = re.findall(r'\d+', message)
535
- if not numbers:
536
- return "Please enter a valid number for the quantity (e.g., 1, 2, 3)."
537
- quantity = int(numbers[0])
538
- if quantity <= 0:
539
- return "Please enter a valid quantity (e.g., 1, 2, 3)."
540
- state.data["quantity"] = quantity
541
- state.step = 3
542
- return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
543
 
544
  if state.step == 3:
545
  phone_pattern = r'(\+?\d{10,15})'
 
391
  del user_state[user_id]
392
  state = None
393
 
394
+ # --- Step A: Already expecting quantity details? ---
395
  if state and state.flow == "order" and state.step == 2:
396
  # Expecting a message like "2 for Jollof Rice and 3 for Fried Rice"
397
  pairs = re.findall(r'(\d+)\s*for\s*([a-zA-Z\s]+)', message, flags=re.IGNORECASE)
398
  if pairs:
399
  order_quantities = {}
400
+ # Use candidate dishes if available; otherwise, a single selected dish.
401
  candidate_dishes = state.data.get("dishes")
402
  if not candidate_dishes:
 
403
  candidate_dishes = [state.data.get("dish")]
404
  for quantity, dish_text in pairs:
405
  dish_text = dish_text.strip().lower()
406
  for candidate in candidate_dishes:
 
407
  if candidate.lower() in dish_text or dish_text in candidate.lower():
408
  order_quantities[candidate] = int(quantity)
409
  if order_quantities:
410
  state.data["orders"] = order_quantities
411
+ state.step = 3 # Proceed to collecting contact details
412
  order_summary = "\n".join([f"{q} serving(s) of {d}" for d, q in order_quantities.items()])
413
  return f"Got it. You have ordered:\n{order_summary}\nPlease provide your phone number and delivery address."
414
  return "I'm sorry, I didn't understand the quantity details. Please specify like '2 for Jollof Rice and 3 for Fried Rice'."
415
 
416
+ # --- Step B: Trigger initial order flow ---
417
  if message.lower() in ["order", "menu"]:
418
  state = ConversationState()
419
  state.flow = "order"
 
432
  user_state[user_id] = state
433
  return "Sure! What dish would you like to order?"
434
 
435
+ # --- Step C: Identify dish(es) if not already in order flow ---
436
  if not state or state.flow != "order":
437
  matched_dishes = match_dishes(message)
438
  if matched_dishes:
439
+ # If multiple dishes are detected, ask for clarification.
440
  if len(matched_dishes) > 1:
 
441
  state = ConversationState()
442
  state.flow = "order"
443
  state.update_last_active()
 
447
  return (f"We found multiple dishes in your request: {dish_options}. "
448
  "Please specify which one you'd like to order or type 'both' if you'd like all.")
449
  else:
450
+ # Single dish case.
451
  found_dish = matched_dishes[0]
452
  state = ConversationState()
453
  state.flow = "order"
454
  state.data["dish"] = found_dish
455
  state.update_last_active()
456
  user_state[user_id] = state
457
+ # Attempt to extract quantity (e.g. "Jollof Rice of 2 portions")
 
458
  numbers = re.findall(r'\d+', message)
459
  if numbers:
460
  quantity = int(numbers[0])
 
462
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
463
  state.data["quantity"] = quantity
464
  state.step = 3
465
+ # Proceed to contact details extraction
466
  phone_pattern = r'(\+?\d{10,15})'
467
  phone_match = re.search(phone_pattern, message)
468
  address = None
 
495
  else:
496
  return "I couldn't identify the dish. Please type the dish name from our menu."
497
 
498
+ # --- Step D: Clarification if candidate dishes exist (multiple matches) ---
499
  if state and state.flow == "order" and "candidate_dishes" in state.data:
500
  if message.strip().lower() in ["both", "all"]:
501
+ # User chooses to order all candidate dishes.
502
  state.data["dishes"] = state.data["candidate_dishes"]
503
  del state.data["candidate_dishes"]
504
  state.step = 2 # Now expect quantity details for each dish.
 
511
  if dish.lower() in message.lower():
512
  state.data["dish"] = dish
513
  del state.data["candidate_dishes"]
 
514
  numbers = re.findall(r'\d+', message)
515
  if numbers:
516
  quantity = int(numbers[0])
 
523
  else:
524
  state.step = 2
525
  return f"You selected {dish}. How many servings would you like?"
 
526
  dish_options = ", ".join(state.data["candidate_dishes"])
527
  return f"Please specify which one you'd like to order from: {dish_options} " \
528
  f"(or type 'both' if you'd like to order all)."
 
 
 
 
 
 
 
 
 
 
529
 
530
  if state.step == 3:
531
  phone_pattern = r'(\+?\d{10,15})'