File size: 2,928 Bytes
e301b1c 27cc340 e301b1c 27cc340 e301b1c 27cc340 e301b1c 27cc340 e301b1c 27cc340 e301b1c 27cc340 e301b1c 27cc340 e301b1c 27cc340 | 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 | """
Usage example for the hybrid weight predictor.
Requirements:
pip install huggingface_hub joblib scikit-learn
This uses a hybrid approach:
1. Explicit weight extraction from text (e.g., "500g", "2 liter")
2. Knowledge base lookup for known FMCG brands and fast food items
3. ML model fallback for unknown items
"""
from huggingface_hub import hf_hub_download
import joblib
import sys
import os
# Make sure the weight_predictor module is importable
sys.path.insert(0, os.path.dirname(__file__))
# Load the hybrid predictor (pure Python, no ML model needed)
from hybrid_weight_predictor import HybridWeightPredictor, build_hybrid_predictor
def predict_weight(text, item_type=None):
"""
Predict item weight in grams.
Args:
text: Item description. Should start with [MENU_ITEM], [GROCERY], or [NON_FOOD]
item_type: "menu_item", "grocery", or "non_food" (auto-detected from text if None)
Returns:
Predicted weight in grams (float)
"""
predictor = build_hybrid_predictor()
return predictor.predict(text, item_type)
if __name__ == "__main__":
# Example usage
examples = [
("[GROCERY] coca cola can 330ml", "grocery"),
("[GROCERY] coca cola", "grocery"), # uses KB default
("[GROCERY] pepsi 1 liter bottle", "grocery"),
("[GROCERY] kelloggs corn flakes 500g", "grocery"),
("[GROCERY] oreo cookies 154g", "grocery"),
("[GROCERY] heinz ketchup 570ml", "grocery"),
("[GROCERY] mars bar 51g", "grocery"),
("[GROCERY] snickers 2 pack 96g", "grocery"),
("[GROCERY] red bull 4 pack", "grocery"),
("[GROCERY] tide laundry detergent 1.5kg", "grocery"),
("[MENU_ITEM] large pizza", "menu_item"),
("[MENU_ITEM] cheeseburger", "menu_item"),
("[MENU_ITEM] double cheeseburger", "menu_item"),
("[MENU_ITEM] big mac", "menu_item"),
("[MENU_ITEM] french fries", "menu_item"),
("[MENU_ITEM] large fries", "menu_item"),
("[MENU_ITEM] chicken nuggets", "menu_item"),
("[MENU_ITEM] burrito", "menu_item"),
("[MENU_ITEM] caesar salad", "menu_item"),
("[MENU_ITEM] caesar salad large", "menu_item"),
("[MENU_ITEM] pho", "menu_item"),
("[MENU_ITEM] ramen", "menu_item"),
("[MENU_ITEM] sushi platter", "menu_item"),
("[MENU_ITEM] medium pizza", "menu_item"),
("[MENU_ITEM] combo meal", "menu_item"),
("[MENU_ITEM] milkshake", "menu_item"),
("[MENU_ITEM] iced coffee", "menu_item"),
("[MENU_ITEM] family meal", "menu_item"),
("[MENU_ITEM] sliders", "menu_item"),
("[NON_FOOD] laptop computer", "non_food"),
("[NON_FOOD] water bottle", "non_food"),
]
print("=== Weight Predictions ===\n")
for text, item_type in examples:
weight = predict_weight(text, item_type)
print(f" {text:55s} -> {weight:8.1f}g")
|