Keep three recommendations with strict food safety filters
Browse files
app.py
CHANGED
|
@@ -149,38 +149,46 @@ def retrieve(user_text, k=3, diet=None, max_prep=None, category=None, pool=500,
|
|
| 149 |
q = embedder.encode(
|
| 150 |
[QUERY_PREFIX + user_text], convert_to_numpy=True, normalize_embeddings=True
|
| 151 |
).astype("float32")
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
cand = df.iloc[idx[0]].copy()
|
| 154 |
cand["similarity"] = scores[0]
|
| 155 |
-
|
| 156 |
-
|
| 157 |
if diet and diet != "Any":
|
| 158 |
cand = cand[cand["diet_tag"] == diet]
|
| 159 |
-
if max_prep:
|
| 160 |
-
cand = cand[cand["Prep_Time"] <= max_prep]
|
| 161 |
-
searchable = cand["Recipe_Name"].astype(str) + " " + cand["Ingredients"].astype(str)
|
| 162 |
-
# The main selected ingredient is also used for retrieval when matching
|
| 163 |
-
# dataset recipes exist. Extra required ingredients remain hard rules for
|
| 164 |
-
# generation, but requiring every combination here would often return zero.
|
| 165 |
-
if required_ingredients:
|
| 166 |
-
main_matches = searchable.map(
|
| 167 |
-
lambda value: ingredient_present(value, required_ingredients[0])
|
| 168 |
-
)
|
| 169 |
-
if main_matches.any():
|
| 170 |
-
matched = cand[main_matches]
|
| 171 |
-
remainder = cand[~main_matches]
|
| 172 |
-
cand = pd.concat([matched, remainder])
|
| 173 |
-
searchable = cand["Recipe_Name"].astype(str) + " " + cand["Ingredients"].astype(str)
|
| 174 |
if excluded_ingredients and len(cand):
|
|
|
|
| 175 |
safe = searchable.map(lambda value: not any(
|
| 176 |
ingredient_present(value, blocked) for blocked in excluded_ingredients
|
| 177 |
))
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
def infer_recovery_category(user_text, sleep=None, strain=None, hrv=None, k=30):
|
|
@@ -640,6 +648,12 @@ def run(state, main_ingredient, include_ingredients, excluded_ingredients,
|
|
| 640 |
f"was relaxed.")
|
| 641 |
elif diet and diet != "Any" and (top3["diet_tag"] != diet).any():
|
| 642 |
relaxed = f"Not enough {diet} matches, so the diet filter was relaxed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
if required and not ingredient_present(
|
| 644 |
f"{top3.iloc[0]['Recipe_Name']} {top3.iloc[0]['Ingredients']}", required[0]
|
| 645 |
):
|
|
|
|
| 149 |
q = embedder.encode(
|
| 150 |
[QUERY_PREFIX + user_text], convert_to_numpy=True, normalize_embeddings=True
|
| 151 |
).astype("float32")
|
| 152 |
+
# Structured filters are cheap over 10k rows and should never reduce the UI
|
| 153 |
+
# below the assignment's required three recommendations. Search the full
|
| 154 |
+
# index when filters are active, then rank rather than hard-filter category.
|
| 155 |
+
search_k = len(df) if (diet != "Any" or max_prep or excluded_ingredients) else pool
|
| 156 |
+
scores, idx = index.search(q, search_k)
|
| 157 |
cand = df.iloc[idx[0]].copy()
|
| 158 |
cand["similarity"] = scores[0]
|
| 159 |
+
|
| 160 |
+
# Diet and explicit exclusions are safety choices and remain hard rules.
|
| 161 |
if diet and diet != "Any":
|
| 162 |
cand = cand[cand["diet_tag"] == diet]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
if excluded_ingredients and len(cand):
|
| 164 |
+
searchable = cand["Recipe_Name"].astype(str) + " " + cand["Ingredients"].astype(str)
|
| 165 |
safe = searchable.map(lambda value: not any(
|
| 166 |
ingredient_present(value, blocked) for blocked in excluded_ingredients
|
| 167 |
))
|
| 168 |
+
cand = cand[safe]
|
| 169 |
+
if len(cand) == 0:
|
| 170 |
+
raise ValueError("No recipes satisfy the selected diet and exclusions")
|
| 171 |
+
|
| 172 |
+
searchable = cand["Recipe_Name"].astype(str) + " " + cand["Ingredients"].astype(str)
|
| 173 |
+
cand["_main_match"] = False
|
| 174 |
+
if required_ingredients:
|
| 175 |
+
cand["_main_match"] = searchable.map(
|
| 176 |
+
lambda value: ingredient_present(value, required_ingredients[0])
|
| 177 |
+
)
|
| 178 |
+
cand["_category_match"] = (
|
| 179 |
+
cand["recovery_category"] == category if category else True
|
| 180 |
+
)
|
| 181 |
+
cand["_within_time"] = (
|
| 182 |
+
cand["Prep_Time"] <= max_prep if max_prep else True
|
| 183 |
+
)
|
| 184 |
+
cand = cand.sort_values(
|
| 185 |
+
["_within_time", "_main_match", "_category_match", "similarity"],
|
| 186 |
+
ascending=[False, False, False, False],
|
| 187 |
+
kind="stable",
|
| 188 |
+
)
|
| 189 |
+
return cand.head(k).drop(
|
| 190 |
+
columns=["_main_match", "_category_match", "_within_time"]
|
| 191 |
+
)
|
| 192 |
|
| 193 |
|
| 194 |
def infer_recovery_category(user_text, sleep=None, strain=None, hrv=None, k=30):
|
|
|
|
| 648 |
f"was relaxed.")
|
| 649 |
elif diet and diet != "Any" and (top3["diet_tag"] != diet).any():
|
| 650 |
relaxed = f"Not enough {diet} matches, so the diet filter was relaxed."
|
| 651 |
+
if (top3["recovery_category"] != category).any():
|
| 652 |
+
category_note = (
|
| 653 |
+
"To keep three recommendations while respecting your food choices, "
|
| 654 |
+
"some cards come from a nearby recovery category."
|
| 655 |
+
)
|
| 656 |
+
relaxed = f"{relaxed} {category_note}".strip()
|
| 657 |
if required and not ingredient_present(
|
| 658 |
f"{top3.iloc[0]['Recipe_Name']} {top3.iloc[0]['Ingredients']}", required[0]
|
| 659 |
):
|