Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,127 +5,159 @@ import os
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
|
| 8 |
-
# ---
|
|
|
|
| 9 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
# ---
|
| 19 |
custom_css = """
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
font-
|
|
|
|
| 30 |
}
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
|
| 33 |
-
# ---
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
-
# Step 2 logic: Encode and compare
|
| 44 |
query_embedding = model.encode([query])
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
results_text += f"**Taste Review:** {row['taste_review']}\n\n"
|
| 57 |
-
results_text += "---\n\n"
|
| 58 |
-
return results_text
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error: {e}"
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
if not
|
| 65 |
-
return "⚠️ Dish and Restaurant names are required.",
|
| 66 |
-
|
| 67 |
-
new_review = {
|
| 68 |
-
'dish_name': dish_name,
|
| 69 |
-
'restaurant_name': restaurant_name,
|
| 70 |
-
'rating': rating,
|
| 71 |
-
'taste_review': taste_review,
|
| 72 |
-
'visual_description': visual_description,
|
| 73 |
-
'user_name': user_name
|
| 74 |
-
}
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
else:
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
success_msg = f"Review for '{dish_name}' by {user_name} uploaded successfully!"
|
| 85 |
-
return success_msg, "", "", 4.5, "", "", ""
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
gr.HTML("<h1 class='main-header'>BiteWise AI Engine</h1>")
|
| 90 |
|
| 91 |
with gr.Tabs():
|
| 92 |
-
#
|
| 93 |
-
with gr.Tab("1.
|
| 94 |
-
gr.Markdown("###
|
|
|
|
|
|
|
|
|
|
| 95 |
with gr.Row():
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
with gr.Tab("2.
|
| 104 |
-
gr.Markdown("###
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
#
|
| 111 |
with gr.Tab("3. Upload Review"):
|
| 112 |
-
gr.Markdown("###
|
| 113 |
with gr.Row():
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
in_rate = gr.Slider(1, 5, value=4.5, step=0.1, label="Rating")
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
btn_upload = gr.Button("Upload Review", elem_classes="action-btn")
|
| 123 |
-
out_status = gr.Markdown()
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
demo.launch()
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
|
| 8 |
+
# --- 1. SETTINGS & MODEL ---
|
| 9 |
+
# Using the exact SBERT model from our pipeline
|
| 10 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 11 |
|
| 12 |
+
# File configurations
|
| 13 |
+
MAIN_DB = 'bitewise_clean_dataset.csv'
|
| 14 |
+
DISH_VECTORS = 'BiteWise_Dish_Embeddings.npy'
|
| 15 |
+
USER_DB_PATH = 'BiteWise_User_Database.csv'
|
| 16 |
+
REVIEWS_DB_PATH = 'BiteWise_User_Reviews.csv'
|
| 17 |
|
| 18 |
+
# Pre-loading static assets
|
| 19 |
+
df_main = pd.read_csv(MAIN_DB)
|
| 20 |
+
dish_embeddings = np.load(DISH_VECTORS)
|
| 21 |
|
| 22 |
+
# --- 2. THE STYLING (Bespoke Light Brown/Beige Theme) ---
|
| 23 |
custom_css = """
|
| 24 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&display=swap');
|
| 25 |
+
* { font-family: 'Inter', sans-serif !important; }
|
| 26 |
+
.gradio-container { background-color: #F8F5F2 !important; }
|
| 27 |
+
.main-header { color: #3D2B1F; text-align: center; font-weight: 600; font-size: 2.5em; margin-bottom: 5px; }
|
| 28 |
+
.action-btn {
|
| 29 |
+
background: #5D4037 !important;
|
| 30 |
+
color: white !important;
|
| 31 |
+
border: none !important;
|
| 32 |
+
border-radius: 8px !important;
|
| 33 |
+
font-weight: 600 !important;
|
| 34 |
+
padding: 10px 20px !important;
|
| 35 |
}
|
| 36 |
+
.action-btn:hover { background: #3D2B1F !important; }
|
| 37 |
+
input, textarea, .dropdown, .slider { border-radius: 8px !important; border: 1px solid #D7CCC8 !important; }
|
| 38 |
"""
|
| 39 |
|
| 40 |
+
# --- 3. CORE LOGIC (Part 4 Pipeline) ---
|
| 41 |
+
|
| 42 |
+
def create_user_persona(name, age, cuisine, spice, adventure, diet, fav_dish, drink, frequency):
|
| 43 |
+
"""Step 1: Building the user database record."""
|
| 44 |
+
if not name:
|
| 45 |
+
return "⚠️ Error: Full Name is required."
|
| 46 |
+
|
| 47 |
+
user_data = {
|
| 48 |
+
'full_name': name, 'age_group': age, 'fav_cuisine': cuisine,
|
| 49 |
+
'spice_level': spice, 'adventure_level': adventure, 'dietary_restrictions': diet,
|
| 50 |
+
'favorite_dish': fav_dish, 'preferred_drink': drink, 'dining_frequency': frequency
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Save to persistent user storage
|
| 54 |
+
u_df = pd.DataFrame([user_data])
|
| 55 |
+
if os.path.exists(USER_DB_PATH):
|
| 56 |
+
pd.concat([pd.read_csv(USER_DB_PATH), u_df], ignore_index=True).to_csv(USER_DB_PATH, index=False)
|
| 57 |
+
else:
|
| 58 |
+
u_df.to_csv(USER_DB_PATH, index=False)
|
| 59 |
+
|
| 60 |
+
# Return exactly as phrased in Colab
|
| 61 |
+
return f"""### 👤 Profile Created Successfully!
|
| 62 |
+
**Welcome, {name}!**
|
| 63 |
+
|
| 64 |
+
**Your Culinary Context:**
|
| 65 |
+
* **Preferences:** {cuisine} | **Spice:** {spice}/5 | **Adventure:** {adventure}/5
|
| 66 |
+
* **Details:** {diet} | **Favorite:** {fav_dish} | **Vibe:** {drink}
|
| 67 |
+
|
| 68 |
+
We have updated our records. You can now use the search engine to find dishes tailored to your taste."""
|
| 69 |
|
| 70 |
+
def run_search_engine(query):
|
| 71 |
+
"""Step 2: Semantic search logic."""
|
| 72 |
try:
|
|
|
|
| 73 |
query_embedding = model.encode([query])
|
| 74 |
+
sims = cosine_similarity(query_embedding, dish_embeddings).flatten()
|
| 75 |
|
| 76 |
+
# Temporary view for results
|
| 77 |
+
search_df = df_main.copy()
|
| 78 |
+
search_df['match_score'] = sims
|
| 79 |
+
top_3 = search_df.sort_values(by='match_score', ascending=False).head(3)
|
| 80 |
|
| 81 |
+
output = "### 🍴 Top Matches Found:\n\n"
|
| 82 |
+
for _, row in top_3.iterrows():
|
| 83 |
+
output += f"**{row['dish_name']}** at **{row['restaurant_name']}**\n"
|
| 84 |
+
output += f"Rating: {row['rating']} ⭐ | Review: {row['taste_review']}\n\n"
|
| 85 |
+
output += "---\n"
|
| 86 |
+
return output
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error: {e}"
|
| 89 |
|
| 90 |
+
def submit_review(dish, rest, rate, taste, visual, user):
|
| 91 |
+
"""Step 3: Contributing a new review."""
|
| 92 |
+
if not dish or not rest:
|
| 93 |
+
return "⚠️ Dish and Restaurant names are required.", dish, rest, rate, taste, visual, user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
review_data = {
|
| 96 |
+
'dish_name': dish, 'restaurant_name': rest, 'rating': rate,
|
| 97 |
+
'taste_review': taste, 'visual_description': visual, 'user_name': user
|
| 98 |
+
}
|
| 99 |
|
| 100 |
+
r_df = pd.DataFrame([review_data])
|
| 101 |
+
if os.path.exists(REVIEWS_DB_PATH):
|
| 102 |
+
pd.concat([pd.read_csv(REVIEWS_DB_PATH), r_df], ignore_index=True).to_csv(REVIEWS_DB_PATH, index=False)
|
| 103 |
else:
|
| 104 |
+
r_df.to_csv(REVIEWS_DB_PATH, index=False)
|
| 105 |
+
|
| 106 |
+
return f"✨ Thank you {user}! Your review for '{dish}' has been synced.", "", "", 4.5, "", "", ""
|
| 107 |
|
| 108 |
+
# --- 4. USER INTERFACE (Gradio) ---
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 111 |
+
gr.HTML("<h1 class='main-header'>BiteWise</h1>")
|
|
|
|
| 112 |
|
| 113 |
with gr.Tabs():
|
| 114 |
+
# STEP 1: PERSONA
|
| 115 |
+
with gr.Tab("1. User Profile Setup"):
|
| 116 |
+
gr.Markdown("### 📋 Create your culinary DNA")
|
| 117 |
+
with gr.Row():
|
| 118 |
+
u_name = gr.Textbox(label="Full Name")
|
| 119 |
+
u_age = gr.Dropdown(choices=["Under 18", "18-25", "26-40", "41-60", "60+"], label="Age Group")
|
| 120 |
with gr.Row():
|
| 121 |
+
u_cuisine = gr.Textbox(label="Favorite Cuisine Type", placeholder="e.g., Asian, Italian")
|
| 122 |
+
u_diet = gr.Textbox(label="Dietary Restrictions", placeholder="None, Vegan, etc.")
|
| 123 |
+
with gr.Row():
|
| 124 |
+
u_dish = gr.Textbox(label="One dish you love", placeholder="e.g., Truffle Pasta")
|
| 125 |
+
u_drink = gr.Textbox(label="Preferred Drink", placeholder="e.g., Matcha, Wine")
|
| 126 |
+
with gr.Row():
|
| 127 |
+
u_freq = gr.Dropdown(choices=["Daily", "Weekly", "Rarely"], label="Dining Frequency")
|
| 128 |
+
u_spice = gr.Slider(1, 5, step=1, label="Spice Tolerance")
|
| 129 |
+
u_adv = gr.Slider(1, 5, step=1, label="Adventurousness")
|
| 130 |
+
|
| 131 |
+
btn_p = gr.Button("Create Profile", elem_classes="action-btn")
|
| 132 |
+
out_p = gr.Markdown()
|
| 133 |
+
btn_p.click(create_user_persona,
|
| 134 |
+
inputs=[u_name, u_age, u_cuisine, u_spice, u_adv, u_diet, u_dish, u_drink, u_freq],
|
| 135 |
+
outputs=out_p)
|
| 136 |
|
| 137 |
+
# STEP 2: SEARCH
|
| 138 |
+
with gr.Tab("2. Dish Search"):
|
| 139 |
+
gr.Markdown("### 🔍 Find your next flavor")
|
| 140 |
+
s_q = gr.Textbox(label="Describe your craving", placeholder="e.g., Something spicy and crunchy")
|
| 141 |
+
btn_s = gr.Button("Search Engine", elem_classes="action-btn")
|
| 142 |
+
out_s = gr.Markdown()
|
| 143 |
+
btn_s.click(run_search_engine, inputs=s_q, outputs=out_s)
|
| 144 |
|
| 145 |
+
# STEP 3: CONTRIBUTION
|
| 146 |
with gr.Tab("3. Upload Review"):
|
| 147 |
+
gr.Markdown("### ✍️ Share your experience")
|
| 148 |
with gr.Row():
|
| 149 |
+
in_u = gr.Textbox(label="Reviewer Name")
|
| 150 |
+
in_d = gr.Textbox(label="Dish Name")
|
| 151 |
+
in_r = gr.Textbox(label="Restaurant")
|
| 152 |
with gr.Row():
|
| 153 |
in_rate = gr.Slider(1, 5, value=4.5, step=0.1, label="Rating")
|
| 154 |
+
in_t = gr.Textbox(label="Taste Notes")
|
| 155 |
+
in_v = gr.Textbox(label="Visual Description")
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
btn_r = gr.Button("Upload Review", elem_classes="action-btn")
|
| 158 |
+
out_r = gr.Markdown()
|
| 159 |
+
btn_r.click(submit_review,
|
| 160 |
+
inputs=[in_d, in_r, in_rate, in_t, in_v, in_u],
|
| 161 |
+
outputs=[out_r, in_d, in_r, in_rate, in_t, in_v, in_u])
|
| 162 |
|
| 163 |
demo.launch()
|