Skip to content
aqilazliPublic

About

Meal Generation Phi3 | Streamlit

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

1 Commit

Folders and files

Repository files navigation

๐Ÿฅ— Phi-3 Nutrition AI Coach

An intelligent nutrition coaching system that combines CatBoost machine learning models with Microsoft's Phi-3 fine-tuned language model to provide personalized meal planning and nutritional guidance.

Python PyTorch Transformers

๐ŸŒŸ Features

  • Personalized Nutritional Target Prediction: Uses CatBoost regression models to predict optimal daily calories, protein, carbs, and fats based on user profile
  • AI-Powered Meal Planning: Fine-tuned Phi-3 model generates contextual nutrition advice and meal recommendations
  • 7-Day Meal Schedule Generation: Automatically creates balanced weekly meal plans aligned with user goals
  • Multi-Goal Support: Tailored plans for fat burn, muscle gain, and maintenance goals
  • Recipe Database Integration: Smart meal selection from a comprehensive recipe dataset

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  User Profile   โ”‚
โ”‚  (Age, Weight,  โ”‚
โ”‚   Goal, etc.)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  CatBoost Models    โ”‚
โ”‚  (Predict Targets)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ”œโ”€โ–บ Calories Target
         โ”œโ”€โ–บ Protein Target
         โ”œโ”€โ–บ Carbs Target
         โ””โ”€โ–บ Fats Target
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Phi-3 Fine-tuned   โ”‚
โ”‚  (Generate Advice)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Meal Planner       โ”‚
โ”‚  (Recipe Selection) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  7-Day Meal Plan    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“‹ Prerequisites

  • Python 3.8 or higher
  • CUDA-capable GPU (recommended for model inference)
  • At least 8GB RAM (16GB recommended)
  • ~10GB disk space for models and datasets

๐Ÿš€ Installation

1. Clone the Repository

git clone https://github.com/yourusername/phi3-nutrition-ai.git
cd phi3-nutrition-ai

2. Install Dependencies

pip install -r requirements.txt

3. Download Required Models

The project uses:

  • Microsoft Phi-3-mini-4k-instruct (downloaded automatically)
  • Fine-tuned checkpoint (place in phi3_finetuned/ folder)
  • CatBoost models (place in user model/ folder)

4. Prepare Dataset

Ensure you have the following CSV files in the dataset/ folder:

  • recipes.csv - Recipe database with nutritional information
  • GYM.csv - User fitness and health data for training

๐Ÿ“ Project Structure

phi3-nutrition-ai/
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ Nutrition_phi3 model.ipynb    # Main training notebook
โ”œโ”€โ”€ meal_generate.ipynb            # Meal generation script
โ”œโ”€โ”€ dataset/
โ”‚   โ”œโ”€โ”€ recipes.csv
โ”‚   โ””โ”€โ”€ GYM.csv
โ”œโ”€โ”€ user model/
โ”‚   โ”œโ”€โ”€ catboost_reco_calories.cbm
โ”‚   โ”œโ”€โ”€ catboost_reco_protein.cbm
โ”‚   โ”œโ”€โ”€ catboost_reco_carbs.cbm
โ”‚   โ”œโ”€โ”€ catboost_reco_fats.cbm
โ”‚   โ”œโ”€โ”€ preprocess.py
โ”‚   โ””โ”€โ”€ schema.json
โ”œโ”€โ”€ phi3_finetuned/               # Fine-tuned adapter checkpoint
โ”œโ”€โ”€ phi3_merged_standard/         # Merged model (auto-generated)
โ””โ”€โ”€ evaluation/
    โ”œโ”€โ”€ phi3_confusion_matrix.png
    โ”œโ”€โ”€ phi3_text_evaluation.png
    โ”œโ”€โ”€ meal_plan_evaluation.png
    โ””โ”€โ”€ rouge_bert_evaluation.png

๐Ÿ’ป Usage

Quick Start with Jupyter Notebook

jupyter notebook meal_generate.ipynb

Python Script Usage

from meal_generate import NutritionAI

# Initialize the system
app = NutritionAI()

# Define user profile
user_profile = {
    "Gender": "Female",
    "Age": 30,
    "Weight_kg": 65,
    "Height_cm": 165,
    "Goal": "muscle_gain",  # Options: fat_burn, muscle_gain, maintenance
    "Activity_Level": "Moderate",
    "BMI": 23.9,
    "Blood_Pressure_Systolic": 120,
    "Blood_Pressure_Diastolic": 80,
    "Cholesterol_Level": 180,
    "Blood_Sugar_Level": 90,
    "Daily_Steps": 6000,
    "Sleep_Hours": 7,
    "Chronic_Disease": "None",
    "Allergies": "None",
    "Dietary_Habits": "Non-Vegetarian",
    "Exercise_Frequency": "3-4 times a week"
}

# Generate personalized meal plan
targets, ai_guidance, weekly_plan = app.generate_7_day_plan(user_profile)

print(f"Daily Targets: {targets}")
print(f"AI Guidance: {ai_guidance}")
print(f"Weekly Plan: {weekly_plan}")

๐ŸŽฏ How It Works

1. Nutritional Target Prediction (CatBoost)

Four separate CatBoost regression models predict optimal daily intake:

  • Calories: Total daily energy needs
  • Protein: Muscle maintenance/growth requirements
  • Carbohydrates: Energy and performance needs
  • Fats: Hormonal balance and satiety

Input features include:

  • Demographics (age, gender, height, weight)
  • Health metrics (BMI, blood pressure, cholesterol, blood sugar)
  • Lifestyle factors (activity level, sleep, steps)
  • Goals (fat burn, muscle gain, maintenance)

2. AI Guidance Generation (Phi-3)

The fine-tuned Phi-3 model generates:

  • Personalized nutrition advice
  • Meal timing recommendations
  • Hydration and supplement guidance
  • Exercise integration tips

The model is optimized with:

  • 4-bit quantization for faster inference
  • LoRA adapters for efficient fine-tuning
  • Goal-specific prompting for relevant advice

3. Meal Plan Assembly

The system:

  • Filters recipes based on user goals and targets
  • Balances macros across breakfast, lunch, dinner, and snacks
  • Ensures variety throughout the 7-day plan
  • Provides per-meal and daily nutritional summaries

๐Ÿ“Š Model Performance

The system includes comprehensive evaluation metrics:

  • Text Generation Quality: ROUGE and BERT scores
  • Meal Plan Accuracy: Nutritional target alignment
  • Classification Performance: Confusion matrices for goal prediction

See evaluation visualizations in the evaluation/ folder.

๐Ÿ”ง Training Your Own Model

Fine-tune Phi-3

  1. Prepare your training dataset with nutrition Q&A pairs
  2. Open Nutrition_phi3 model.ipynb
  3. Follow the step-by-step training process
  4. The notebook includes:
    • Data preprocessing
    • LoRA configuration
    • Training with gradient accumulation
    • Evaluation and metric visualization

Train CatBoost Models

CatBoost models can be retrained with new user data:

  • Update dataset/GYM.csv with additional user profiles
  • Run the CatBoost training cells in the notebook
  • Models are automatically saved to user model/ folder

๐ŸŒ API Integration (Optional)

You can expose this system as a REST API using FastAPI:

from fastapi import FastAPI
from meal_generate import NutritionAI

app = FastAPI()
nutrition_ai = NutritionAI()

@app.post("/generate-plan")
async def generate_plan(user_profile: dict):
    targets, guidance, plan = nutrition_ai.generate_7_day_plan(user_profile)
    return {
        "targets": targets,
        "guidance": guidance,
        "weekly_plan": plan
    }

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Microsoft for the Phi-3 model
  • Hugging Face for the Transformers library
  • CatBoost team for the gradient boosting framework
  • Recipe dataset contributors

๐Ÿ“ง Contact

For questions or feedback, please open an issue on GitHub.

๐Ÿ”ฎ Future Enhancements

  • Add support for dietary restrictions (vegan, keto, paleo)
  • Implement real-time nutritional tracking
  • Mobile app integration
  • Multi-language support
  • Community recipe sharing
  • Integration with fitness trackers
  • Meal prep optimization

Note: This is an AI-powered nutrition system for informational purposes. Always consult with healthcare professionals for medical advice.

About

Meal Generation Phi3 | Streamlit

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages