Spaces:
Sleeping
Sleeping
| # 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() |