Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,6 +56,7 @@ def get_embedding_via_api(text):
|
|
| 56 |
return np.array(response)
|
| 57 |
|
| 58 |
def find_similar_recipes_list(query_text):
|
|
|
|
| 59 |
if stored_embeddings is None: return ["Database error."] * 3
|
| 60 |
query_vec = get_embedding_via_api("Represent this recipe for retrieving similar dishes: " + query_text)
|
| 61 |
if len(query_vec.shape) == 1: query_vec = query_vec.reshape(1, -1)
|
|
@@ -63,18 +64,47 @@ def find_similar_recipes_list(query_text):
|
|
| 63 |
scores = cosine_similarity(query_vec, stored_embeddings)[0]
|
| 64 |
top_indices = scores.argsort()[-3:][::-1]
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
results_list = []
|
| 67 |
for idx in top_indices:
|
| 68 |
score = scores[idx]
|
| 69 |
row = df_recipes.iloc[idx]
|
| 70 |
title = row['Title']
|
| 71 |
-
desc = str(row['Raw_Output'])
|
| 72 |
score_display = f"{score:.3%}"
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
card_content = (
|
| 75 |
f"### 🏆 {title}\n"
|
| 76 |
f"<span style='color:#1877f2; font-weight:bold; font-size:14px;'>Match Score: {score_display}</span>\n\n"
|
| 77 |
-
f"<div class='sim-scroll'>{
|
| 78 |
)
|
| 79 |
results_list.append(card_content)
|
| 80 |
|
|
@@ -290,7 +320,6 @@ button.gallery-item:hover {
|
|
| 290 |
with gr.Blocks(title="CookBook AI") as demo:
|
| 291 |
|
| 292 |
# --- HEADER ---
|
| 293 |
-
# --- CHANGED HERE: SLOGAN TEXT UPDATED ---
|
| 294 |
gr.HTML(f"""
|
| 295 |
<div class="custom-header">
|
| 296 |
<div class="logo-area">
|
|
|
|
| 56 |
return np.array(response)
|
| 57 |
|
| 58 |
def find_similar_recipes_list(query_text):
|
| 59 |
+
# Matching logic remains exactly the same
|
| 60 |
if stored_embeddings is None: return ["Database error."] * 3
|
| 61 |
query_vec = get_embedding_via_api("Represent this recipe for retrieving similar dishes: " + query_text)
|
| 62 |
if len(query_vec.shape) == 1: query_vec = query_vec.reshape(1, -1)
|
|
|
|
| 64 |
scores = cosine_similarity(query_vec, stored_embeddings)[0]
|
| 65 |
top_indices = scores.argsort()[-3:][::-1]
|
| 66 |
|
| 67 |
+
# --- UPDATED DISPLAY LOGIC STARTS HERE ---
|
| 68 |
+
# Identify the correct column names dynamically
|
| 69 |
+
cols = df_recipes.columns
|
| 70 |
+
ing_col = next((c for c in cols if 'ingredient' in c.lower()), None)
|
| 71 |
+
inst_col = next((c for c in cols if 'instruction' in c.lower()), None)
|
| 72 |
+
|
| 73 |
results_list = []
|
| 74 |
for idx in top_indices:
|
| 75 |
score = scores[idx]
|
| 76 |
row = df_recipes.iloc[idx]
|
| 77 |
title = row['Title']
|
|
|
|
| 78 |
score_display = f"{score:.3%}"
|
| 79 |
|
| 80 |
+
# Build the content block
|
| 81 |
+
content_parts = []
|
| 82 |
+
|
| 83 |
+
# 1. Ingredients
|
| 84 |
+
if ing_col:
|
| 85 |
+
val = str(row[ing_col])
|
| 86 |
+
# Clean up if it looks like a python list string
|
| 87 |
+
if val.strip().startswith("[") and val.strip().endswith("]"):
|
| 88 |
+
val = val.strip()[1:-1].replace("'", "").replace('"', "")
|
| 89 |
+
content_parts.append(f"<b>🛒 INGREDIENTS:</b><br>{val}")
|
| 90 |
+
|
| 91 |
+
# 2. Instructions
|
| 92 |
+
if inst_col:
|
| 93 |
+
val = str(row[inst_col])
|
| 94 |
+
if val.strip().startswith("[") and val.strip().endswith("]"):
|
| 95 |
+
val = val.strip()[1:-1].replace("'", "").replace('"', "")
|
| 96 |
+
content_parts.append(f"<b>🍳 INSTRUCTIONS:</b><br>{val}")
|
| 97 |
+
|
| 98 |
+
# Fallback if neither found
|
| 99 |
+
if not content_parts:
|
| 100 |
+
display_text = str(row.get('Raw_Output', 'No details available.'))
|
| 101 |
+
else:
|
| 102 |
+
display_text = "<br><br>".join(content_parts)
|
| 103 |
+
|
| 104 |
card_content = (
|
| 105 |
f"### 🏆 {title}\n"
|
| 106 |
f"<span style='color:#1877f2; font-weight:bold; font-size:14px;'>Match Score: {score_display}</span>\n\n"
|
| 107 |
+
f"<div class='sim-scroll'>{display_text}</div>"
|
| 108 |
)
|
| 109 |
results_list.append(card_content)
|
| 110 |
|
|
|
|
| 320 |
with gr.Blocks(title="CookBook AI") as demo:
|
| 321 |
|
| 322 |
# --- HEADER ---
|
|
|
|
| 323 |
gr.HTML(f"""
|
| 324 |
<div class="custom-header">
|
| 325 |
<div class="logo-area">
|