Spaces:
Sleeping
Sleeping
Delete app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130# app.py
import gradio as gr
import joblib
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
# ==============================
# Load Saved Files
# ==============================
df = joblib.load("restaurants.pkl")
vectorizer = joblib.load("vectorizer.pkl")
similarity_matrix = joblib.load("similarity.pkl")
# ==============================
# Prepare 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
)
)
restaurant_names = sorted(df["Restaurant Name"].unique())
# ==============================
# Recommendation Function
# ==============================
def recommend_restaurants(cuisine, city, veg_pref):
filtered = df[df["Location"] == city]
if veg_pref != "Any":
filtered = filtered[filtered["Pure Veg"] == veg_pref]
if len(filtered) == 0:
return "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]
output = ""
for _, row in recs.iterrows():
output += f"""
π½ Restaurant: {row['Restaurant Name']}
π Cuisine: {row['Cuisine']}
β Rating: {row['Rating']}
π° Price: βΉ{row['Average Price']}
π Area: {row['Area']}
-----------------------
"""
output += "\nπ Select a restaurant below to place your order."
return output
# ==============================
# Order Placement Function
# ==============================
def place_order(restaurant, address):
if restaurant == "" or address == "":
return "β οΈ Please select a restaurant and enter your delivery address."
return f"""
β
Order placed successfully!
π½ Restaurant: {restaurant}
π Delivery Address:
{address}
π Your food is being prepared and will be delivered soon!
Thank you for using the AI Food Recommendation System.
"""
# ==============================
# Gradio UI
# ==============================
iface = gr.Interface(
fn=recommend_restaurants,
inputs=[
gr.Dropdown(cuisines, label="Select Cuisine"),
gr.Dropdown(cities, label="Select City"),
gr.Dropdown(["Any","Yes","No"], label="Pure Veg Preference")
],
outputs=gr.Textbox(label="Recommended Restaurants"),
title="π AI Food Recommendation System",
description="Get restaurant recommendations and order food easily."
)
# Order UI
order_ui = gr.Interface(
fn=place_order,
inputs=[
gr.Dropdown(restaurant_names, label="Select Restaurant to Order From"),
gr.Textbox(label="Enter Delivery Address")
],
outputs=gr.Textbox(label="Order Status"),
title="π Place Your Order",
description="Select a restaurant and enter your address to simulate placing an order."
)
# Combine both UIs
app = gr.TabbedInterface(
[iface, order_ui],
["π Find Restaurants", "π Place Order"]
)
app.launch()app.pyDeleted-130