Modules

Overview

app.py is the core script of the Foodinator application. This script initializes the FastAPI application, sets up the database connection, preprocesses the dataset, and defines the API endpoints for food recommendation and fetching food details.

database.py provides functions for interacting with MongoDB. It includes functions to save the dataset to MongoDB and fetch food data from MongoDB.

1. database.py

This module provides functions for interacting with MongoDB.

  • save_dataset_to_mongodb
def save_dataset_to_mongodb(data_file, mongodb_url, mongodb_database, mongodb_collection):
    """
    Saves a dataset to MongoDB.

    Args:
        data_file (str): Path to the dataset file.
        mongodb_url (str): URL of the MongoDB server.
        mongodb_database (str): Name of the MongoDB database.
        mongodb_collection (str): Name of the MongoDB collection.

    Returns:
        None
    """

This function saves the dataset to MongoDB. It takes the path to the dataset file, MongoDB URL, database name, and collection name as input.

#Example:  
save_dataset_to_mongodb("food_dataset.csv", "mongodb://localhost:27017", "food_database", "food_collection")
  • fetch_food_data_from_mongodb
def fetch_food_data_from_mongodb(food_name, mongodb_url, mongodb_database, mongodb_collection):
    """
    Fetches food data from MongoDB based on the food name.

    Args:
        food_name (str): Name of the food item.
        mongodb_url (str): URL of the MongoDB server.
        mongodb_database (str): Name of the MongoDB database.
        mongodb_collection (str): Name of the MongoDB collection.

    Returns:
        dict: Food details retrieved from MongoDB.
    """

This function fetches food data from MongoDB based on the food name. It takes the food name, MongoDB URL, database name, and collection name as input.

And return the food details as a dictionary.

# Example:  
fetch_food_data_from_mongodb("Pizza", "mongodb://localhost:27017", "food_database", "food_collection")

2. app.py

This module defines the FastAPI application and its endpoints.

  • preprocess_input
def preprocess_input(food_type, calories, fat_content, protein_content, gluten_free, dairy_free):
    """
    Preprocesses input criteria for recommending food items.

    Args:
        food_type (str): Type of food item.
        calories (int): Number of calories.
        fat_content (int): Amount of fat in grams.
        protein_content (int): Amount of protein in grams.
        gluten_free (bool): Whether the food item is gluten-free.
        dairy_free (bool): Whether the food item is dairy-free.

    Returns:
        str: Preprocessed input string.
    """

This function preprocesses the input criteria for recommending food items. It takes the food type, calories, fat content, protein content, gluten-free status, and dairy-free status as input.

# Example:
preprocess_input("Pizza", 300, 15, 10, True, False)

And return the preprocessed input string.

  • find_cluster
def find_cluster(input_string, vectorizer, kmeans):
    """
    Finds the cluster number based on input criteria.

    Args:
        input_string (str): Preprocessed input string.
        vectorizer (TfidfVectorizer): Fitted TfidfVectorizer object.
        kmeans (KMeans): Fitted KMeans object.

    Returns:
        int: Cluster number.
    """

This function finds the cluster number based on the input criteria. It takes the preprocessed input string, fitted TfidfVectorizer object, and fitted KMeans object as input.

# Example:
find_cluster("Pizza 300 15 10 True False", vectorizer, kmeans)

And return the cluster number.

  • recommend_food_by_cluster
def recommend_food_by_cluster(df, cluster, num_recommendations=5):
    """
    Recommends food items from the same cluster

    Args:
        df (pd.DataFrame): DataFrame containing food items
        cluster (int): Cluster number
        num_recommendations (int): Number of recommendations to return

    Returns:
        pd.DataFrame: Recommended food items
    """

This function recommends food items from the same cluster. It takes the DataFrame containing food items, cluster number, and number of recommendations as input.

# Example:
recommend_food_by_cluster(df, 0, 5)

And return the recommended food items.

  • recommend_food
def recommend_food(df, vectorizer, kmeans, food_type, calories, fat_content, protein_content, gluten_free, dairy_free, ingredients, num_recommendations=5):
    """
    Recommends food items based on user input

    Args:
        df (pd.DataFrame): DataFrame containing food items
        vectorizer (TfidfVectorizer): Fitted TfidfVectorizer object
        kmeans (KMeans): Fitted KMeans object
        food_type (str): Type of food item
        calories (int): Number of calories
        fat_content (int): Amount of fat in grams
        protein_content (int): Amount of protein in grams
        gluten_free (bool): Whether the food item is gluten-free
        dairy_free (bool): Whether the food item is dairy-free
        ingredients (list): List of ingredients to filter by
        num_recommendations (int): Number of recommendations to return

    Returns:
        pd.DataFrame: Recommended food items
    """

This function recommends food items based on user input. It takes the DataFrame containing food items, fitted TfidfVectorizer object, fitted KMeans object, food type, calories, fat content, protein content, gluten-free status, dairy-free status, ingredients, and number of recommendations as input.

# Example:
recommend_food(df, vectorizer, kmeans, "Pizza", 300, 15, 10, True, False, ["cheese", "tomato"], 5)

And return the recommended food items.

request_body

# Define request body model
class UserInput(BaseModel):
    """
    Request body for recommending food items
    """
    food_type: str
    allergies: list
    ingredients: list
    calories: int = None
    fat_content: int = None
    protein_content: int = None

This class defines the request body model for recommending food items. It includes the food type, allergies, ingredients, calories, fat content, and protein content.

def recommend_food_by_search(food_name, df, cosine_sim):
    """
    Recommends food items based on a search query

    Args:
        food_name (str): Food item to search for
        df (pd.DataFrame): DataFrame containing food items
        cosine_sim (np.array): Cosine similarity matrix

    Returns:
        list: Top 5 recommended food items
    """

This function recommends food items based on a search query. It takes the food name, DataFrame containing food items, and cosine similarity matrix as input.

# Example:
recommend_food_by_search("Pizza", df, cosine_sim)

And return the top 5 recommended food items.