| |
| import pandas as pd |
| import gradio as gr |
| import re |
| from sentence_transformers import SentenceTransformer |
| from sklearn.neighbors import NearestNeighbors |
|
|
| |
| |
| |
| df = pd.read_csv("food_order_cleaned.csv") |
| df['rating'] = pd.to_numeric(df['rating'], errors='coerce') |
| df['search_text'] = df['restaurant_name'].astype(str) + " | " + df['cuisine_type'].astype(str) + " | " + df['rating'].astype(str) |
|
|
| |
| |
| |
| def find_by_cuisine(cuisine, limit=10): |
| mask = df['cuisine_type'].str.strip().str.lower() == cuisine.strip().lower() |
| cols = ['restaurant_name', 'cuisine_type', 'cost_of_the_order', 'rating'] |
| return df.loc[mask, cols].head(limit) |
|
|
| def best_rated_by_cuisine(cuisine, top_n=10): |
| mask = df['cuisine_type'].str.strip().str.lower() == cuisine.strip().lower() |
| subset = df[mask].dropna(subset=['rating']).sort_values('rating', ascending=False) |
| cols = ['restaurant_name', 'cuisine_type', 'cost_of_the_order', 'rating'] |
| return subset[cols].head(top_n) |
|
|
| def cheapest_high_rated(max_cost=None, min_rating=4.0, top_n=10): |
| subset = df.dropna(subset=['rating']) |
| subset = subset[subset['rating'] >= min_rating] |
| if max_cost is not None: |
| subset = subset[subset['cost_of_the_order'] <= max_cost] |
| subset = subset.sort_values('cost_of_the_order') |
| cols = ['restaurant_name', 'cuisine_type', 'cost_of_the_order', 'rating'] |
| return subset[cols].head(top_n) |
|
|
| def personalized_recall(customer_id, day): |
| mask = (df['customer_id'].astype(str) == str(customer_id)) & \ |
| (df['day_of_the_week'].str.strip().str.lower() == day.strip().lower()) |
| cols = ['order_id', 'restaurant_name', 'cuisine_type', 'cost_of_the_order', 'rating', 'day_of_the_week'] |
| return df.loc[mask, cols] |
|
|
| |
| |
| |
| model = SentenceTransformer('all-MiniLM-L6-v2') |
| corpus = df['search_text'].tolist() |
| corpus_embeddings = model.encode(corpus, show_progress_bar=True) |
| nn = NearestNeighbors(n_neighbors=10, metric='cosine').fit(corpus_embeddings) |
|
|
| def semantic_search(query, k=5): |
| q_emb = model.encode([query]) |
| dists, idxs = nn.kneighbors(q_emb, n_neighbors=k) |
| results = df.iloc[idxs[0]].copy() |
| results['score'] = 1 - dists[0] |
| cols = ['restaurant_name', 'cuisine_type', 'cost_of_the_order', 'rating', 'score'] |
| return results[cols] |
|
|
| |
| |
| |
| def handle_query(message, customer_id="", history=[]): |
| text = message.strip().lower() |
| |
| |
| if 'find' in text and 'restaurant' in text: |
| known = set(df['cuisine_type'].str.strip().str.lower().unique()) |
| words = text.split() |
| found = [w for w in words if w in known] |
| if found: |
| res = find_by_cuisine(found[0]) |
| return res.to_html(index=False) |
| else: |
| return semantic_search(message).to_html(index=False) |
| |
| |
| if 'best' in text and ('place' in text or 'best-rated' in text): |
| known = set(df['cuisine_type'].str.strip().str.lower().unique()) |
| words = text.split() |
| found = [w for w in words if w in known] |
| if found: |
| res = best_rated_by_cuisine(found[0]) |
| return res.to_html(index=False) |
| else: |
| return semantic_search(message).to_html(index=False) |
| |
| |
| if 'cheapest' in text or 'cheap' in text or 'value' in text: |
| res = cheapest_high_rated(min_rating=4.0, top_n=10) |
| return res.to_html(index=False) |
| |
| |
| if 'what did i order' in text: |
| day_match = re.search(r'on (\w+)', text) |
| day = day_match.group(1) if day_match else '' |
| if customer_id == '': |
| return "Please enter your customer_id in the input box." |
| if day == '': |
| return "Please specify the day, e.g. 'on Weekend'." |
| res = personalized_recall(customer_id, day) |
| if res.empty: |
| return "No orders found for that customer/day." |
| return res.to_html(index=False) |
| |
| |
| return semantic_search(message).to_html(index=False) |
|
|
| |
| |
| |
| def chat_reply(history, message, customer_id): |
| reply = handle_query(message, customer_id) |
| history.append((message, reply)) |
| return history, "" |
|
|
| with gr.Blocks(title="Restaurant Guide Chatbot") as demo: |
| gr.Markdown("## Restaurant Guide Chatbot\nAsk queries like:\n- Find me a Thai restaurant\n- What are the best Italian places?\n- Show me the cheapest highly-rated places\n- What did I order on Weekend? (enter customer_id)") |
| |
| chatbot = gr.Chatbot() |
| with gr.Row(): |
| user_msg = gr.Textbox(placeholder="Type your message here...") |
| cust_id = gr.Textbox(label="Customer ID (optional)") |
| send = gr.Button("Send") |
| |
| send.click(chat_reply, inputs=[chatbot, user_msg, cust_id], outputs=[chatbot, user_msg]) |
|
|
| demo.launch() |
|
|