Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,209 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pickle
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# ======================
|
| 8 |
-
# LOAD DATA
|
| 9 |
-
# ======================
|
| 10 |
-
|
| 11 |
-
with open("tfidf.pkl", "rb") as f:
|
| 12 |
-
tfidf = pickle.load(f)
|
| 13 |
-
|
| 14 |
-
with open("tfidf_matrix.pkl", "rb") as f:
|
| 15 |
-
tfidf_matrix = pickle.load(f)
|
| 16 |
-
|
| 17 |
-
with open("data.pkl", "rb") as f:
|
| 18 |
-
df = pickle.load(f)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# ======================
|
| 22 |
-
# DATA CLEANING
|
| 23 |
-
# ======================
|
| 24 |
-
|
| 25 |
-
# clean price column
|
| 26 |
-
df["average_price"] = (
|
| 27 |
-
df["average_price"]
|
| 28 |
-
.astype(str)
|
| 29 |
-
.str.replace("₹", "", regex=False)
|
| 30 |
-
.str.replace("for two", "", regex=False)
|
| 31 |
-
.str.replace(",", "", regex=False)
|
| 32 |
-
.str.strip()
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
df["average_price"] = pd.to_numeric(df["average_price"], errors="coerce")
|
| 36 |
-
|
| 37 |
-
# clean rating column
|
| 38 |
-
df["rating"] = pd.to_numeric(df["rating"], errors="coerce")
|
| 39 |
-
|
| 40 |
-
# clean veg column
|
| 41 |
-
df["pure_veg"] = df["pure_veg"].astype(str).str.lower().map({
|
| 42 |
-
"yes": True,
|
| 43 |
-
"no": False
|
| 44 |
-
})
|
| 45 |
-
|
| 46 |
-
# remove null values
|
| 47 |
-
df = df.dropna(subset=["average_price", "rating"])
|
| 48 |
-
|
| 49 |
-
df = df.reset_index(drop=True)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# ======================
|
| 53 |
-
# PREPARE FILTER OPTIONS
|
| 54 |
-
# ======================
|
| 55 |
-
|
| 56 |
-
cuisines = sorted(df["cuisine"].dropna().unique().tolist())
|
| 57 |
-
areas = sorted(df["area"].dropna().unique().tolist())
|
| 58 |
-
|
| 59 |
-
price_min = int(df["average_price"].min())
|
| 60 |
-
price_max = int(df["average_price"].max())
|
| 61 |
-
|
| 62 |
-
rating_min = float(df["rating"].min())
|
| 63 |
-
rating_max = float(df["rating"].max())
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# ======================
|
| 67 |
-
# RECOMMEND FUNCTION
|
| 68 |
-
# ======================
|
| 69 |
-
|
| 70 |
-
def recommend(restaurant, cuisine, area, veg_only, price, rating):
|
| 71 |
-
|
| 72 |
-
filtered_df = df.copy()
|
| 73 |
-
|
| 74 |
-
if cuisine != "All":
|
| 75 |
-
filtered_df = filtered_df[filtered_df["cuisine"] == cuisine]
|
| 76 |
-
|
| 77 |
-
if area != "All":
|
| 78 |
-
filtered_df = filtered_df[filtered_df["area"] == area]
|
| 79 |
-
|
| 80 |
-
if veg_only:
|
| 81 |
-
filtered_df = filtered_df[filtered_df["pure_veg"] == True]
|
| 82 |
-
|
| 83 |
-
filtered_df = filtered_df[
|
| 84 |
-
(filtered_df["average_price"] <= price) &
|
| 85 |
-
(filtered_df["rating"] >= rating)
|
| 86 |
-
]
|
| 87 |
-
|
| 88 |
-
if restaurant not in df["restaurant_name"].values:
|
| 89 |
-
return pd.DataFrame({"Message": ["Restaurant not found"]})
|
| 90 |
-
|
| 91 |
-
idx = df[df["restaurant_name"] == restaurant].index.iloc[0]
|
| 92 |
-
|
| 93 |
-
# compute cosine similarity dynamically
|
| 94 |
-
sim_scores = cosine_similarity(
|
| 95 |
-
tfidf_matrix[idx],
|
| 96 |
-
tfidf_matrix
|
| 97 |
-
).flatten()
|
| 98 |
-
|
| 99 |
-
sim_scores = list(enumerate(sim_scores))
|
| 100 |
-
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
| 101 |
-
|
| 102 |
-
sim_scores = sim_scores[1:20]
|
| 103 |
-
|
| 104 |
-
restaurant_indices = [i[0] for i in sim_scores]
|
| 105 |
-
|
| 106 |
-
recs = df.iloc[restaurant_indices]
|
| 107 |
-
|
| 108 |
-
recs = recs[recs.index.isin(filtered_df.index)]
|
| 109 |
-
|
| 110 |
-
recs = recs.head(5)
|
| 111 |
-
|
| 112 |
-
if recs.empty:
|
| 113 |
-
return pd.DataFrame({"Message": ["No matching restaurants found"]})
|
| 114 |
-
|
| 115 |
-
recs = recs[
|
| 116 |
-
["restaurant_name", "cuisine", "rating", "average_price", "area"]
|
| 117 |
-
]
|
| 118 |
-
|
| 119 |
-
recs = recs.rename(
|
| 120 |
-
columns={
|
| 121 |
-
"restaurant_name": "Restaurant",
|
| 122 |
-
"cuisine": "Cuisine",
|
| 123 |
-
"rating": "⭐ Rating",
|
| 124 |
-
"average_price": "💰 Avg Price",
|
| 125 |
-
"area": "📍 Area",
|
| 126 |
-
}
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
return recs
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
# ======================
|
| 133 |
-
# UI
|
| 134 |
-
# ======================
|
| 135 |
-
|
| 136 |
-
with gr.Blocks() as app:
|
| 137 |
-
|
| 138 |
-
gr.Markdown(
|
| 139 |
-
"""
|
| 140 |
-
# 🍽️ Smart Food Recommendation System
|
| 141 |
-
|
| 142 |
-
Find restaurants based on your **preferences, budget, and location**.
|
| 143 |
-
"""
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
with gr.Row():
|
| 147 |
-
|
| 148 |
-
restaurant = gr.Dropdown(
|
| 149 |
-
choices=sorted(df["restaurant_name"].unique()),
|
| 150 |
-
label="🍔 Select Restaurant"
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
cuisine = gr.Dropdown(
|
| 154 |
-
choices=["All"] + cuisines,
|
| 155 |
-
value="All",
|
| 156 |
-
label="🍜 Cuisine"
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
area = gr.Dropdown(
|
| 160 |
-
choices=["All"] + areas,
|
| 161 |
-
value="All",
|
| 162 |
-
label="📍 Area"
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
with gr.Row():
|
| 166 |
-
|
| 167 |
-
veg = gr.Checkbox(label="🥗 Veg Only")
|
| 168 |
-
|
| 169 |
-
price = gr.Slider(
|
| 170 |
-
minimum=price_min,
|
| 171 |
-
maximum=price_max,
|
| 172 |
-
value=price_max,
|
| 173 |
-
label="💰 Max Price"
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
rating = gr.Slider(
|
| 177 |
-
minimum=rating_min,
|
| 178 |
-
maximum=rating_max,
|
| 179 |
-
value=rating_min,
|
| 180 |
-
label="⭐ Minimum Rating"
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
btn = gr.Button("🍕 Get Recommendations", variant="primary")
|
| 184 |
-
|
| 185 |
-
output = gr.Dataframe(
|
| 186 |
-
label="✨ Recommended Restaurants",
|
| 187 |
-
headers=["Restaurant", "Cuisine", "⭐ Rating", "💰 Avg Price", "📍 Area"]
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
btn.click(
|
| 191 |
-
recommend,
|
| 192 |
-
inputs=[restaurant, cuisine, area, veg, price, rating],
|
| 193 |
-
outputs=output
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
gr.Markdown(
|
| 197 |
-
"""
|
| 198 |
-
---
|
| 199 |
-
🚀 Built using **Machine Learning + Gradio**
|
| 200 |
-
"""
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
# ======================
|
| 205 |
-
# RUN APP
|
| 206 |
-
# ======================
|
| 207 |
-
|
| 208 |
-
if __name__ == "__main__":
|
| 209 |
-
app.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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