updates on the backend
This commit is contained in:
59
scripts/enrich_nutrition.py
Normal file
59
scripts/enrich_nutrition.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
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
|
||||
|
||||
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)")
|
||||
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,
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
if result.errors:
|
||||
logger.warning(f"{len(result.errors)} errors (showing up to 10): {result.errors[:10]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user