Files
catalogue_backend/scripts/enrich_nutrition.py
2026-09-04 12:06:40 +05:30

81 lines
3.6 KiB
Python

"""
CLI entry point for Feature 1/4/15's retrieval pipeline - fetches
verified nutrition data (Open Food Facts) for every product in the
catalog, computes transparent scores/insights, and persists them.
Usage:
python scripts/enrich_nutrition.py
python scripts/enrich_nutrition.py --max-products 200 --no-narrative
python scripts/enrich_nutrition.py --force # re-fetch even already-verified products
# Catalogue-wide, past ACTIVE_BRANDS. Without this the run cannot reach
# Britannia, Parle or ITC at all, because enrich_all_products reads through
# vector_store.list_available_brands(), which honours that filter.
python scripts/enrich_nutrition.py --include-inactive
python scripts/enrich_nutrition.py --brands "Britannia,Parle"
python scripts/enrich_nutrition.py --categories "Fruits & Vegetables,Eggs"
Non-consumables (soap, shampoo, mosquito repellent, flowers) are skipped and
reported separately - see app/services/consumability.py.
Equivalent to POST /api/admin/nutrition-intelligence/enrich.
"""
from __future__ import annotations
import argparse
import logging
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from app.services.nutrition_db import ensure_nutrition_schema # noqa: E402
from app.services.nutrition_enrichment_service import enrich_all_products # noqa: E402
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
def _progress(done: int, total: int) -> None:
if total and (done % 25 == 0 or done == total):
logger.info(f"Enrichment progress: {done}/{total}")
def main() -> None:
parser = argparse.ArgumentParser(description="Enrich the catalog with verified nutrition data")
parser.add_argument("--force", action="store_true", help="Re-fetch even products already marked 'verified'")
parser.add_argument("--no-narrative", action="store_true", help="Skip the LLM narrative step (facts/scores/tags only)")
parser.add_argument("--max-products", type=int, default=None, help="Cap the number of products processed (for a quick test run)")
parser.add_argument("--brands", default=None,
help="Comma-separated brand display names to enrich (default: whatever ACTIVE_BRANDS allows)")
parser.add_argument("--include-inactive", action="store_true",
help="Enrich every brand table, ignoring ACTIVE_BRANDS. Needed to reach "
"brands the app does not currently display.")
parser.add_argument("--categories", default=None,
help="Comma-separated categories to restrict the run to, e.g. 'Fruits & Vegetables,Eggs'")
args = parser.parse_args()
ensure_nutrition_schema()
result = enrich_all_products(
skip_if_verified=not args.force,
generate_narrative=not args.no_narrative,
progress_cb=_progress,
max_products=args.max_products,
brands=[b for b in (args.brands or "").split(",") if b.strip()] or None,
include_inactive=args.include_inactive,
categories=[c for c in (args.categories or "").split(",") if c.strip()] or None,
)
logger.info(
f"Done in {result.duration_seconds}s - "
f"{result.verified} verified, {result.partial} partial, {result.unavailable} unavailable "
f"of {result.total_products} total products "
f"({result.skipped_non_consumable} non-consumable skipped)"
)
if result.errors:
logger.warning(f"{len(result.errors)} errors (showing up to 10): {result.errors[:10]}")
if __name__ == "__main__":
main()