Spaces:
Sleeping
Sleeping
File size: 4,570 Bytes
bab4aa8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """
USDA foodMeasures Verification Script
Run: python3 verify_usda_measures.py YOUR_API_KEY
"""
import sys
import json
import urllib.request
import urllib.parse
API_KEY = sys.argv[1] if len(sys.argv) > 1 else "DEMO_KEY"
BASE_URL = "https://api.nal.usda.gov/fdc/v1/foods/search"
INGREDIENTS = [
("rice", "Basmati rice", ["cup", "tablespoon"]),
("onion", "Onion", ["medium", "small", "cup", "tablespoon"]),
("garlic", "Garlic", ["clove", "cup"]),
("oil", "Oil, vegetable", ["tablespoon", "cup", "teaspoon"]),
("tomato", "Tomatoes, raw", ["medium", "cup", "slice"]),
("potato", "Potato, raw", ["medium", "large", "cup"]),
]
DATA_TYPES = "SR Legacy,Foundation"
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
BOLD = "\033[1m"
RESET = "\033[0m"
results_summary = []
def fetch(query):
params = urllib.parse.urlencode({
"query": query,
"api_key": API_KEY,
"dataType": DATA_TYPES,
"pageSize": 1,
})
url = f"{BASE_URL}?{params}"
with urllib.request.urlopen(url, timeout=10) as r:
return json.loads(r.read())
print(f"\n{BOLD}USDA foodMeasures Verification{RESET}")
print(f"API key: {API_KEY[:8]}{'*' * (len(API_KEY)-8) if len(API_KEY) > 8 else ''}")
print(f"dataType filter: {DATA_TYPES}")
print("=" * 70)
for query, label, wanted_units in INGREDIENTS:
print(f"\n{BOLD}{label}{RESET} (query: '{query}')")
try:
data = fetch(query)
foods = data.get("foods", [])
if not foods:
print(f" {RED}β NO RESULTS returned by USDA{RESET}")
results_summary.append((label, False, "no results"))
continue
food = foods[0]
desc = food.get("description", "?")
dtype = food.get("dataType", "?")
measures = food.get("foodMeasures", [])
print(f" matched: {desc} [{dtype}]")
if not measures:
print(f" {RED}β foodMeasures MISSING β design assumption FAILS for this ingredient{RESET}")
results_summary.append((label, False, "foodMeasures absent"))
continue
print(f" {GREEN}β foodMeasures present ({len(measures)} entries){RESET}")
# Show all measures
found_units = set()
for m in measures:
text = m.get("disseminationText", "")
gw = m.get("gramWeight")
unit_word = text.split()[-1].lower().rstrip("s") if text else ""
found_units.add(unit_word)
print(f" {text:25s} β {gw} g")
# Check which wanted units are covered
print(f" wanted units: {wanted_units}")
hits = []
misses = []
for u in wanted_units:
u_singular = u.rstrip("s")
if any(u_singular in m.get("disseminationText","").lower() for m in measures):
hits.append(u)
else:
misses.append(u)
if hits:
print(f" {GREEN}covered: {hits}{RESET}")
if misses:
print(f" {YELLOW}not covered: {misses} (SI fallback or skip){RESET}")
all_ok = len(misses) == 0
results_summary.append((label, True, hits, misses))
except Exception as e:
print(f" {RED}ERROR: {e}{RESET}")
results_summary.append((label, False, str(e)))
# ββ Summary ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'=' * 70}")
print(f"{BOLD}SUMMARY{RESET}")
print(f"{'=' * 70}")
design_safe = True
for row in results_summary:
name = row[0]
ok = row[1]
if ok:
hits, misses = row[2], row[3]
status = f"{GREEN}β foodMeasures present{RESET}"
if misses:
status += f" {YELLOW}(missing: {misses}){RESET}"
print(f" {status} {name}")
else:
print(f" {status} {name}")
else:
design_safe = False
reason = row[2]
print(f" {RED}β FAILED ({reason}) {name}{RESET}")
print()
if design_safe:
print(f"{GREEN}{BOLD}β DESIGN ASSUMPTION HOLDS.{RESET}")
print(" USDA foodMeasures covers your core ingredients.")
print(" The QuantityNormalizerService approach is safe to build.")
else:
print(f"{RED}{BOLD}β DESIGN ASSUMPTION FAILS for one or more ingredients.{RESET}")
print(" Check the details above. You may need a fallback strategy.")
print() |