Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| CLI host for the FastAPI-powered Swiggy server. | |
| Flow | |
| 1. Send natural-language query β /parse_query | |
| 2. If is_swiggy_query == True and dates are present β /get_orders | |
| 3. Pretty-print the result | |
| """ | |
| import requests, sys | |
| from dateutil import parser as dtparse | |
| API_BASE = "http://127.0.0.1:8000" | |
| # ---------------------------------------------------------------------- | |
| # Helpers | |
| # ---------------------------------------------------------------------- | |
| def nice_date(dt_str: str | None) -> str: | |
| if not dt_str: | |
| return "??" | |
| return dtparse.parse(dt_str).strftime("%d %b %Y") | |
| def pretty_order(order: dict) -> str: | |
| if "error" in order: | |
| return f" - Email #{order['email_number']}: β {order['error']}" | |
| head = ( | |
| f"Restaurant : {order['restaurant_name']}\n" | |
| f"Date : {nice_date(order['order_date'])} {order['order_time']}\n" | |
| f"Total : βΉ{order['total_price']:.0f}\n" | |
| "Items:" | |
| ) | |
| items = "\n".join( | |
| f" β’ {it['quantity']} Γ {it['name']} β βΉ{it['price']:.0f}" | |
| for it in order["items"] | |
| ) | |
| return head + "\n" + items | |
| # ---------------------------------------------------------------------- | |
| # Main REPL | |
| # ---------------------------------------------------------------------- | |
| def main() -> None: | |
| # Quick health-check | |
| try: | |
| requests.get(f"{API_BASE}/docs").raise_for_status() | |
| except Exception as e: | |
| print("β Cannot reach FastAPI server:", e) | |
| sys.exit(1) | |
| print("β Connected. Type a Swiggy question (or Ctrl-C to quit).") | |
| while True: | |
| try: | |
| query = input("\nπ¨οΈ You: ").strip() | |
| except (EOFError, KeyboardInterrupt): | |
| break | |
| if not query: | |
| continue | |
| # ββ Stage 1: parse_query ββββββββββββββββββββββββββββββββββββ | |
| try: | |
| r = requests.post(f"{API_BASE}/parse_query", json={"query": query}) | |
| r.raise_for_status() | |
| meta = r.json() # {is_swiggy_query, start_date, end_date, intent} | |
| except Exception as e: | |
| print("β οΈ Parse error:", e) | |
| print("π Server:", r.text if 'r' in locals() else "no response") | |
| continue | |
| # Handle non-Swiggy or missing dates | |
| if not meta.get("is_swiggy_query"): | |
| print("π€· That doesnβt look like a Swiggy order query.") | |
| continue | |
| if not meta.get("start_date") or not meta.get("end_date"): | |
| print("β οΈ Couldnβt find a full date range in your question.") | |
| continue | |
| print(f" β³ Date range: {meta['start_date']} β {meta['end_date']}") | |
| print(f" β³ Intent : {meta['intent']}") | |
| # ββ Stage 2: get_orders ββββββββββββββββββββββββββββββββββββ | |
| try: | |
| r2 = requests.post( | |
| f"{API_BASE}/get_orders", | |
| json={ | |
| "start_date": meta["start_date"], | |
| "end_date": meta["end_date"], | |
| }, | |
| ) | |
| r2.raise_for_status() | |
| orders = r2.json() | |
| except Exception as e: | |
| print("β οΈ Failed to fetch orders:", e) | |
| print("π Server:", r2.text if 'r2' in locals() else "no response") | |
| continue | |
| # ββ Output ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if not orders: | |
| print("π No orders found for that range.") | |
| continue | |
| print("\nπ¦ Orders:") | |
| for o in orders: | |
| print(pretty_order(o)) | |
| print("-" * 40) | |
| print("\nπ Bye!") | |
| # ---------------------------------------------------------------------- | |
| if __name__ == "__main__": | |
| main() | |