๐ฅ Nutrition Calorie Predictor
Fine-tuned distilgpt2 model that predicts calories per 100g from a food description.
Built as part of the LLM Engineering Course โ Week 7 Capstone Project by @asolinxavier
๐ฏ What This Model Does
Give it a food description โ it predicts calories per 100g.
Input : "Steamed fermented rice and lentil cake, soft and fluffy, low fat."
Output : "58" (kcal per 100g)
๐๏ธ Model Architecture
| Property | Value |
|---|---|
| Base Model | distilgpt2 (82M parameters) |
| Fine-tuning Method | QLoRA (Parameter Efficient Fine-Tuning) |
| Trainable Parameters | 294,912 (only 0.36% of total!) |
| LoRA Rank (r) | 16 |
| LoRA Alpha | 32 |
| Target Module | c_attn |
| Task | Causal Language Modeling |
๐ Training Results
| Epoch | Train Loss | Val Loss |
|---|---|---|
| 1 | 5.383 | 0.022 |
| 2 | 0.003 | 0.014 โ Best |
| 5 | 0.001 | 0.031 |
| 10 | 0.000 | 0.036 |
Best Val Loss: 0.014 โ
Loss dropped from 5.38 โ 0.003 โ the model learned dramatically!
๐ฝ๏ธ Dataset
- 85 foods total (Indian + Global)
- 42 Indian foods: Idli, Dosa, Biryani, Paneer Butter Masala, Sambar...
- 43 Global foods: Apple, Chicken Breast, Salmon, Oats...
- Split: Train 57 | Val 7 | Test 8
- Dataset: asolinxavier/nutrition_items
๐ How to Use
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
BASE_MODEL = "distilgpt2"
FINETUNED_MODEL = "asolinxavier/nutrition-calorie-predictor-v2"
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL)
model = PeftModel.from_pretrained(base_model, FINETUNED_MODEL)
model.eval()
def predict_calories(food_description: str) -> str:
prompt = (
f"How many calories are in 100g of this food?\n\n"
f"{food_description}\n\n"
f"Calories per 100g: "
)
inputs = tokenizer(prompt, return_tensors="pt")
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=10,
do_sample=False,
pad_token_id=tokenizer.eos_token_id,
)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
return decoded.split("Calories per 100g:")[-1].strip().split()[0]
# Test it!
print(predict_calories("Steamed fermented rice and lentil cake, soft and fluffy."))
# Output: "58"
๐๏ธ Project Structure
Week 7/
โโโ day1_qlora_setup.ipynb โ QLoRA concepts + environment setup
โโโ day2_dataset_prep.ipynb โ Dataset preparation + HuggingFace push
โโโ day3_4_training.ipynb โ QLoRA fine-tuning (training loop)
โโโ day5_evaluation.ipynb โ Model evaluation + custom food testing
โโโ nutrition/
โโโ items.py โ NutritionItem data class
โโโ evaluator.py โ Evaluation metrics + charts
๐ฎ Week 8 โ Multi-Agent Meal Planner (Next Steps)
This fine-tuned model will be used as the SpecialistAgent inside a 4-agent meal planning framework:
ScannerAgent โ Fetch recipes from TheMealDB API
NutritionAgent โ Estimate calories (this model + Claude AI ensemble)
PlannerAgent โ Build personalised weekly meal plan
MessagingAgent โ Send daily plan via Pushover notification
๐ค Author
Asolin Xavier
- HuggingFace: @asolinxavier
- Project: Diet & Nutrition Planner โ LLM Engineering Course Week 7 & 8
๐ License
MIT License โ free to use and modify.
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐ Ask for provider support