Skip to content

dhruvagg2003/java-calorie-tracker-app

Repository files navigation

Java Calorie Tracker + AI/ML Recommendations

This desktop app tracks user nutrition and includes an AI-powered personalized diet recommendation system.

What's Included

  • JavaFX calorie tracker (existing flows preserved)
  • SQLite persistence (food_tracker.db)
  • ML microservice (/ml-service, FastAPI)
  • Model training scripts (/model)
  • Java integration layer to call ML service
  • Backend endpoint: GET /ai/recommendation/{userId}
  • AI panel in JavaFX UI with:
    • Recommended daily calories
    • Macro targets (protein/carbs/fats)
    • User cluster type (userType)
    • Meal suggestions

Project Structure

  • src/main/java/... Java app + controllers + DAO + UI
  • ml-service/ FastAPI service for prediction + clustering + retraining
  • model/ training scripts + model artifacts:
    • calorie_model.pkl (regression)
    • clustering_model.pkl (KMeans)

AI/ML Features

1) Calorie Prediction (Regression)

  • Model: custom linear regression (model/train_calorie_model.py)
  • Input: age, weight, height, goal, activity level
  • Output: recommended daily calories

2) Macro Recommendation

  • Fat loss: high protein, moderate carbs, lower fat
  • Maintenance: balanced split
  • Muscle gain: high protein + higher carbs

3) User Clustering (KMeans)

  • Features:
    • average daily calories
    • average protein intake
    • average carbs
    • average fats
  • Cluster labels:
    • high protein
    • high carb
    • balanced

4) Retraining

  • Python endpoint: POST /ml/retrain
  • Java helper endpoint: POST /ai/retrain (optional trigger from active users)

Prerequisites

  • Java 17+
  • Maven wrapper (mvnw.cmd)
  • Python 3.10+

Setup and Run

Step 1: Train initial models

cd java-calorie-tracker-app
python model/train_calorie_model.py

Creates:

  • model/calorie_model.pkl
  • model/clustering_model.pkl

Step 2: Start ML service

cd java-calorie-tracker-app
pip install -r ml-service/requirements.txt
python ml-service/run_server.py

Health check: http://127.0.0.1:8000/ml/health

Step 3: Run Java app

cd java-calorie-tracker-app
.\mvnw.cmd javafx:run

Step 4 (Optional): Run backend API endpoints

If you want to call GET /ai/recommendation/{userId} directly:

cd java-calorie-tracker-app
.\mvnw.cmd spring-boot:run

Default API base: http://localhost:8080

API Endpoints

Java backend

  • GET /ai/recommendation/{userId}
  • POST /ai/feedback

Example response:

{
  "recommendedCalories": 2280,
  "protein": 140,
  "carbs": 250,
  "fats": 70,
  "userType": "high carb",
  "explanation": "Suggested daily calories: 2280. This estimate is based on your muscle gain goal and activity level (active). In the model, higher body weight pushed calories upward, age had a small effect, and muscle gain goal generally increases target calories in this prediction.",
  "mealSuggestions": [
    "Keep consistency: include Chicken Breast in one meal today.",
    "Add Chicken Breast (protein-focused option)",
    "Add White Rice (carb-support option)"
  ]
}

ML service

  • POST /ml/recommendation
  • POST /ml/retrain

Feedback payload example:

{
  "userId": 1,
  "followedRecommendation": true,
  "currentWeight": 72.4,
  "recommendedCalories": 2200,
  "recommendedProtein": 145,
  "recommendedCarbs": 240,
  "recommendedFats": 65
}

Notes

  • Goal values used by ML: fat_loss, maintenance, muscle_gain
  • ML service base URL defaults to http://127.0.0.1:8000
    • Override with env var: ML_SERVICE_BASE_URL
    • Or JVM property: -Dml.service.base-url=http://host:port

About

AI-powered Java Calorie Tracker with JavaFX UI, SQLite persistence, and a FastAPI ML service for personalized calorie/macro recommendations, healthy meal suggestions, feedback-driven retraining, explainable insights, and recommendation history.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors