Fred808 commited on
Commit
4c38444
·
verified ·
1 Parent(s): 9e42313

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -369,18 +369,19 @@ def match_dish(user_input: str, threshold: int = 70) -> str:
369
  return best_match
370
  return None
371
 
372
- # New matching function that returns all dishes matching the user input
373
  def match_dishes(user_input: str, threshold: int = 70) -> list:
374
  matched_dishes = []
 
375
  for item in menu_items:
376
  dish_name = item["name"]
377
- score = fuzz.token_sort_ratio(user_input.lower(), dish_name.lower())
378
- if score >= threshold:
379
  matched_dishes.append(dish_name)
380
- # Remove duplicates (if any) and return the list
 
 
 
381
  return list(set(matched_dishes))
382
 
383
- # Updated order flow that handles multiple dish selections
384
  async def process_order_flow(user_id: str, message: str) -> str:
385
  state = user_state.get(user_id)
386
  if state and state.is_expired():
@@ -388,7 +389,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
388
  del user_state[user_id]
389
  state = None
390
 
391
- # Initial trigger when user types "order" or "menu"
392
  if message.lower() in ["order", "menu"]:
393
  state = ConversationState()
394
  state.flow = "order"
@@ -407,8 +407,6 @@ 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
- # Use fuzzy matching to detect dish(es) even with typos
411
- # This block handles the case when the conversation is not already in order flow.
412
  if not state or state.flow != "order":
413
  matched_dishes = match_dishes(message)
414
  if matched_dishes:
@@ -424,7 +422,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
424
  state.update_last_active()
425
  user_state[user_id] = state
426
 
427
- # Extract quantity if provided
428
  numbers = re.findall(r'\d+', message)
429
  if numbers:
430
  quantity = int(numbers[0])
@@ -455,15 +452,14 @@ async def process_order_flow(user_id: str, message: str) -> str:
455
  asyncio.create_task(update_user_profile(user_id, phone_match.group(1)))
456
  return "Thank you. Please provide your delivery address."
457
  else:
458
- return ("Please provide both your phone number and delivery address. "
 
459
  "For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
460
  else:
461
  state.step = 2
462
  return f"You selected {found_dish}. How many servings would you like?"
463
  else:
464
  return "I couldn't identify the dish. Please type the dish name from our menu."
465
-
466
- WHISPER_API_HEADERS = {"Authorization": f"Bearer {HUGGING_FACE_API_TOKEN}"}
467
 
468
  class TranscriptionResponse(BaseModel):
469
  transcription: 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:
380
+ score = fuzz.token_sort_ratio(user_input_lower, dish_name.lower())
381
+ if score >= threshold:
382
+ matched_dishes.append(dish_name)
383
  return list(set(matched_dishes))
384
 
 
385
  async def process_order_flow(user_id: str, message: str) -> str:
386
  state = user_state.get(user_id)
387
  if state and state.is_expired():
 
389
  del user_state[user_id]
390
  state = None
391
 
 
392
  if message.lower() in ["order", "menu"]:
393
  state = ConversationState()
394
  state.flow = "order"
 
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:
 
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])
 
452
  asyncio.create_task(update_user_profile(user_id, phone_match.group(1)))
453
  return "Thank you. Please provide your delivery address."
454
  else:
455
+ return (f"You selected {found_dish} with {quantity} serving(s). "
456
+ "Please provide both your phone number and delivery address. "
457
  "For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
458
  else:
459
  state.step = 2
460
  return f"You selected {found_dish}. How many servings would you like?"
461
  else:
462
  return "I couldn't identify the dish. Please type the dish name from our menu."
 
 
463
 
464
  class TranscriptionResponse(BaseModel):
465
  transcription: str