Snigs98's picture
Upload app.py
d71beba verified
# app.py
import gradio as gr
import joblib
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
# ============================
# Load Models
# ============================
df = joblib.load("restaurants.pkl")
vectorizer = joblib.load("vectorizer.pkl")
# ============================
# Dropdown Lists
# ============================
cities = sorted(df["Location"].unique())
cuisines = sorted(
set(
cuisine.strip()
for sublist in df["Cuisine"].astype(str).str.split(",")
for cuisine in sublist
)
)
# ============================
# Recommendation Function
# ============================
def get_recommendations(cuisine, city, veg):
filtered = df[df["Location"] == city]
if veg != "Any":
filtered = filtered[filtered["Pure Veg"] == veg]
if len(filtered) == 0:
return gr.update(choices=[]), "❌ No restaurants found."
cuisine_vec = vectorizer.transform([cuisine.lower()])
tfidf_filtered = vectorizer.transform(filtered["Cuisine"])
sim_scores = cosine_similarity(cuisine_vec, tfidf_filtered).flatten()
top_idx = sim_scores.argsort()[-5:][::-1]
recs = filtered.iloc[top_idx]
restaurant_names = recs["Restaurant Name"].tolist()
output = "🍴 Recommended Restaurants:\n\n"
for _, row in recs.iterrows():
output += f"""
🍽 {row['Restaurant Name']}
⭐ Rating: {row['Rating']}
🍜 Cuisine: {row['Cuisine']}
πŸ’° Price: β‚Ή{row['Average Price']}
πŸ“ Area: {row['Area']}
--------------------
"""
return gr.update(choices=restaurant_names), output
# ============================
# Order Function
# ============================
def place_order(restaurant, address):
if not restaurant or not address:
return "⚠️ Please select a restaurant and enter your address."
return f"""
βœ… ORDER CONFIRMED!
🍽 Restaurant: {restaurant}
πŸ“ Delivery Address:
{address}
🚚 Your food is being prepared and will be delivered soon!
"""
# ============================
# UI
# ============================
with gr.Blocks() as app:
gr.Markdown("# πŸ” AI Food Recommendation & Ordering System")
cuisine = gr.Dropdown(cuisines, label="Select Cuisine")
city = gr.Dropdown(cities, label="Select City")
veg = gr.Dropdown(["Any","Yes","No"], label="Pure Veg Preference")
recommend_btn = gr.Button("Get Recommendations")
recommendation_output = gr.Textbox(label="Recommended Restaurants")
restaurant_select = gr.Radio([], label="βœ” Select One Restaurant")
address = gr.Textbox(label="Enter Delivery Address")
order_btn = gr.Button("Place Order")
order_output = gr.Textbox(label="Order Status")
recommend_btn.click(
get_recommendations,
inputs=[cuisine, city, veg],
outputs=[restaurant_select, recommendation_output]
)
order_btn.click(
place_order,
inputs=[restaurant_select, address],
outputs=order_output
)
app.launch()