Download inference.py from ZZandro/weight-predictor: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/ZZandro/weight-predictor/resolve/main/inference.py
- Command line
-
hf download hf://ZZandro/weight-predictor/inference.py
-
curl -L -o inference.py https://huggingface.co/ZZandro/weight-predictor/resolve/main/inference.py
2.93 kB
| """ | |
| 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") | |