Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import asyncio
|
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
from sqlalchemy import select
|
|
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
|
| 12 |
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks, UploadFile, File, Form
|
|
@@ -252,23 +253,6 @@ async def log_order_tracking(order_id: str, status: str, message: str = None):
|
|
| 252 |
session.add(tracking_entry)
|
| 253 |
await session.commit()
|
| 254 |
|
| 255 |
-
from fuzzywuzzy import fuzz
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
def match_dish(user_input: str, threshold: int = 70) -> str:
|
| 259 |
-
best_match = None
|
| 260 |
-
best_score = 0
|
| 261 |
-
for item in menu_items:
|
| 262 |
-
dish_name = item["name"]
|
| 263 |
-
score = fuzz.token_sort_ratio(user_input.lower(), dish_name.lower())
|
| 264 |
-
if score > best_score:
|
| 265 |
-
best_score = score
|
| 266 |
-
best_match = dish_name
|
| 267 |
-
if best_score >= threshold:
|
| 268 |
-
return best_match
|
| 269 |
-
return None
|
| 270 |
-
|
| 271 |
-
|
| 272 |
def calculate_shipping_cost(address: str) -> int:
|
| 273 |
address_lower = address.lower()
|
| 274 |
for area, cost in TOWN_SHIPPING_COSTS.items():
|
|
@@ -372,6 +356,19 @@ async def update_user_profile_with_order(user_id: str, order_id: str):
|
|
| 372 |
profile.order_ids = order_id
|
| 373 |
await session.commit()
|
| 374 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
async def process_order_flow(user_id: str, message: str) -> str:
|
| 376 |
state = user_state.get(user_id)
|
| 377 |
if state and state.is_expired():
|
|
@@ -441,22 +438,22 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 441 |
else:
|
| 442 |
state.step = 2
|
| 443 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 444 |
-
|
| 445 |
# If state exists and we're already in order flow:
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
return (f"You selected {found_dish} with {quantity} serving(s). "
|
| 461 |
"Please provide your phone number and delivery address.")
|
| 462 |
else:
|
|
|
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
from sqlalchemy import select
|
| 10 |
+
from fuzzywuzzy import fuzz
|
| 11 |
from pydantic import BaseModel
|
| 12 |
|
| 13 |
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks, UploadFile, File, Form
|
|
|
|
| 253 |
session.add(tracking_entry)
|
| 254 |
await session.commit()
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
def calculate_shipping_cost(address: str) -> int:
|
| 257 |
address_lower = address.lower()
|
| 258 |
for area, cost in TOWN_SHIPPING_COSTS.items():
|
|
|
|
| 356 |
profile.order_ids = order_id
|
| 357 |
await session.commit()
|
| 358 |
|
| 359 |
+
def match_dish(user_input: str, threshold: int = 70) -> str:
|
| 360 |
+
best_match = None
|
| 361 |
+
best_score = 0
|
| 362 |
+
for item in menu_items:
|
| 363 |
+
dish_name = item["name"]
|
| 364 |
+
score = fuzz.token_sort_ratio(user_input.lower(), dish_name.lower())
|
| 365 |
+
if score > best_score:
|
| 366 |
+
best_score = score
|
| 367 |
+
best_match = dish_name
|
| 368 |
+
if best_score >= threshold:
|
| 369 |
+
return best_match
|
| 370 |
+
return None
|
| 371 |
+
|
| 372 |
async def process_order_flow(user_id: str, message: str) -> str:
|
| 373 |
state = user_state.get(user_id)
|
| 374 |
if state and state.is_expired():
|
|
|
|
| 438 |
else:
|
| 439 |
state.step = 2
|
| 440 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 441 |
+
|
| 442 |
# If state exists and we're already in order flow:
|
| 443 |
+
if state and state.flow == "order":
|
| 444 |
+
state.update_last_active()
|
| 445 |
+
if state.step == 1:
|
| 446 |
+
# Use fuzzy matching on the message in case of typos.
|
| 447 |
+
found_dish = match_dish(message)
|
| 448 |
+
numbers = re.findall(r'\d+', message)
|
| 449 |
+
if found_dish:
|
| 450 |
+
state.data["dish"] = found_dish
|
| 451 |
+
if numbers:
|
| 452 |
+
quantity = int(numbers[0])
|
| 453 |
+
if quantity <= 0:
|
| 454 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 455 |
+
state.data["quantity"] = quantity
|
| 456 |
+
state.step = 3
|
| 457 |
return (f"You selected {found_dish} with {quantity} serving(s). "
|
| 458 |
"Please provide your phone number and delivery address.")
|
| 459 |
else:
|