image repair on existing products

This commit is contained in:
sriram
2026-09-04 13:23:43 +05:30
parent 0c3e23fac4
commit 8059120ce3
4 changed files with 347 additions and 10 deletions

View File

@@ -126,9 +126,46 @@ def usda_fdc_id(product_name: str) -> Optional[int]:
return entry.get("usda_fdc_id") if entry else None
# Words that make a name a DIFFERENT PRODUCT from the commodity it starts with,
# rather than a variety of it.
#
# `lookup` falls back through shorter leading prefixes, which is what lets
# "Mango Totapuri" and "Banana Robusta" find their base commodity - varieties of
# the same thing, correctly sharing one photo. The same fallback turns "Coconut
# Oil" into "Coconut" and "Apple Cider Vinegar" into "Apple", and a bottle of
# oil is not a variety of coconut. Handing it a photo of the raw fruit is the
# same class of error as the apple-juice image this table was built to fix, just
# pointing the other way.
#
# No row in the catalogue trips this today - all 159 names are exact keys - so
# this guards the case a future upload introduces, which is precisely when
# nobody would be looking.
_DERIVED_PRODUCT_WORDS = frozenset({
"oil", "vinegar", "juice", "squash", "syrup", "jam", "jelly", "sauce",
"ketchup", "puree", "paste", "pickle", "powder", "flour", "atta", "rava",
"flakes", "chips", "crisps", "candy", "extract", "essence", "milkshake",
"smoothie", "dried", "fried", "roasted", "pickled", "canned", "frozen",
})
def image_url(product_name: str) -> Optional[str]:
"""The reviewed photo for a commodity, or None.
Stricter than `lookup` on purpose, and only here: `usda_fdc_id` keeps the
plain prefix fallback, because this guard is about not showing a misleading
PICTURE. Nutrition for a derived product is refused further upstream by the
category dispatch in `nutrition_data_service`.
"""
entry = lookup(product_name)
return entry.get("image_url") if entry else None
if not entry:
return None
matched = _normalize(entry.get("product_name") or "")
leftover = set(_normalize(product_name).split()) - set(matched.split())
if leftover & _DERIVED_PRODUCT_WORDS:
return None
return entry.get("image_url")
def all_entries() -> Dict[str, Dict[str, Any]]: