Download example.py from barrydeen/grocery-next-model: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/barrydeen/grocery-next-model/resolve/main/example.py
- Command line
-
hf download hf://barrydeen/grocery-next-model/example.py
-
curl -L -o example.py https://huggingface.co/barrydeen/grocery-next-model/resolve/main/example.py
1.4 kB
| #!/usr/bin/env python3 | |
| """Example for the Grocery Next model repository. | |
| The model ranks complementary grocery items by smoothed co-occurrence. | |
| Without a popularity penalty the ubiquitous staples (onions, garlic, | |
| tomatoes, ...) dominate every answer, so the recommended default is | |
| ``popularity_penalty=8``. Pass 0 to see the raw association estimate. | |
| """ | |
| from grocery import GroceryModel | |
| PENALTY = 8.0 # 0 = raw estimate, 8 = CLI default, higher = emphasize rarity | |
| model = GroceryModel.load("model.json") | |
| # Recommended: results track the selected items. | |
| for selected in ( | |
| ["tomatoes", "onions"], | |
| ["jasmine rice", "green curry paste"], | |
| ["injera", "berbere"], | |
| ): | |
| print(f"\nSelected: {', '.join(selected)} (popularity_penalty={PENALTY})") | |
| for rank, prediction in enumerate( | |
| model.predict(selected, top_k=5, popularity_penalty=PENALTY), start=1 | |
| ): | |
| print(f"{rank}. {prediction.item} ({prediction.score:.1%})") | |
| # Why the penalty exists: the same input, raw vs. penalized. | |
| selected = ["injera", "berbere"] | |
| print(f"\nSame input, raw (popularity_penalty=0) vs. penalized ({PENALTY}):") | |
| raw = model.predict(selected, top_k=5, popularity_penalty=0.0) | |
| pen = model.predict(selected, top_k=5, popularity_penalty=PENALTY) | |
| print(" raw: " + ", ".join(f"{p.item} {p.score:.1%}" for p in raw)) | |
| print(" penalized: " + ", ".join(f"{p.item} {p.score:.1%}" for p in pen)) | |