Skip to content

zakaria-narjis/Diet-Recommendation-System

Repository files navigation

DOI

Diet Recommendation System

A diet recommendation web application using content-based approach with Scikit-Learn, FastAPI and Streamlit.

Diet-Recommendation-System

📑Table of contents

📜 General info

Motivation

People from all around the world are getting more concerned in their health and way of life in today's modern environment. However, avoiding junk food and exercising alone are insufficient; we also need to eat a balanced diet. We can live a healthy life with a balanced diet based on our height, weight, and age. Your diet can help you achieve and maintain a healthy weight, lower your chance of developing chronic diseases (including cancer and heart disease), and improve your general health when combined with physical activity. Nevertheless, there is a little SOTA project on food/diet recommendation system. Therefore I got the idea to build a content-based recommendation system for this purpose using machine learning.

What is a food recommendation engine?

A food recommendation engine using a content-based approach is an important tool for promoting healthy eating habits. This type of engine uses information about the nutritional content and ingredients of foods to make personalized recommendations to users. One of the key advantages of a content-based approach is that it takes into account an individual's dietary restrictions and preferences, such as allergies or food preferences. By providing users with tailored recommendations, a content-based food recommendation engine can help them make better choices about what to eat and improve their overall health. Additionally, by recommending a variety of healthy foods, it can also help users to discover new and nutritious options, expand their dietary horizons and overcome food boredom. All these can lead to a better and well-rounded diet, which can have a positive impact on long-term health outcomes.

What is a content-based recommendation engine?

A content-based recommendation engine is a type of recommendation system that uses the characteristics or content of an item to recommend similar items to users. It works by analyzing the content of items, such as text, images, or audio, and identifying patterns or features that are associated with certain items. These patterns or features are then used to compare items and recommend similar ones to users.

Why content-based approach?

  • No data from other users is required to start making recommendations.
  • Recommendations are highly relevant to the user.
  • Recommendations are transparent to the user.
  • You avoid the “cold start” problem.
  • Content-based filtering systems are generally easier to create.

Challenges of content-based approach

  • There’s a lack of novelty and diversity.
  • Scalability is a challenge.
  • Attributes may be incorrect or inconsistent.

💻Development

Model developement

The recommendation engine is built using Nearest Neighbors alogrithm which is an unsupervised learner for implementing neighbor searches. It acts as a uniform interface to three different nearest neighbors algorithms: BallTree, KDTree, and a brute-force algorithm based on routines in sklearn.metrics.pairwise. For our case, we used the brute-force algorithm using cosine similarity due to its fast computation for small datasets.

$$cos(theta) = (A * B) / (||A|| * ||B||)$$

Dataset

I used Food.com kaggle dataset Data with over 500,000 recipes and 1,400,000 reviews from Food.com. Visit this kaggle link for more details.

Backend Developement

The application is built using the FastAPI framework, which allows for the creation of fast and efficient web APIs. When a user makes a request to the API (user data,nutrition data...) the model is used to generate a list of recommended food similar/suitable to his request (data) which are then returned to the user via the API.

Frontend Developement

The application's front-end is made with Streamlit. Streamlit is an open source app framework in Python language. It helps to create web apps for data science and machine learning in a short time. It is compatible with major Python libraries such as scikit-learn, Keras, PyTorch, SymPy(latex), NumPy, pandas, Matplotlib etc. For our case the front-end is composed of three web pages. The main page is Hello.py which is a welcoming page used to introduce you to my project. The side bar on the left allows the user to navigate too the automatic diet recommendation page and the custom food recommendation page. In the diet recommendation page the user can fill information about his age,weight,height.. and gets a diet recommendation based on his information. Besides, the custom food recommendation allows the user to specify more his food preferency using nutritional values.

Deployement using Docker

Why Docker?

By using Docker, you can ensure that the environment in which the application is exactly the same as the environment in which it was built, which can help prevent unexpected issues and improve model performance. Additionally, Docker allows for easy scaling and management of the deployment, making it a great choice for larger machine learning projects.

Docker-Compose

My project is composed of different services (frontend,API). Therefore, our application should run on multiple containers. With the help of Docker-compose we can share our application using the yaml file that define the services that runs together.

Project Architecture

🚀 Technologies

The project is created with:

  • Python: 3.10.8
  • fastapi 0.88.0
  • uvicorn 0.20.0
  • scikit-learn 1.1.3
  • Pandas: 1.5.1
  • Streamlit: 1.16.0
  • streamlit-echarts 1.24.1
  • Numpy: 1.21.5
  • beautifulsoup4 4.11.1

🐳 Setup

Run it locally

Clone the repo

$ git clone https://github.com/zakaria-narjis/Diet-Recommendation-System

docker-compose

In the project root run:

$ docker-compose up -d --build

Then open http://localhost:8501 and enjoy 😃.

PS: You should have docker and docker-compose already installed

Use the hosted version on Streamlit Cloud

https://diet-recommendation-system.streamlit.app/

Citation

@software{narjis_2024_12507829,
  author       = {Narjis, Zakaria},
  title        = {Diet recommendation system},
  month        = jun,
  year         = 2024,
  publisher    = {Zenodo},
  version      = {v1.0.1},
  doi          = {10.5281/zenodo.12507829},
  url          = {https://doi.org/10.5281/zenodo.12507829}
}