Spaces:
Sleeping
Sleeping
File size: 3,707 Bytes
2a021c2 3506575 2a021c2 20cfe10 2a021c2 20cfe10 2a021c2 20cfe10 2a021c2 20cfe10 2a021c2 20cfe10 2a021c2 20cfe10 2a021c2 20cfe10 2a021c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import random
import pandas as pd
import numpy as np
import pickle
import os
SERVER_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
NUTRITION_MODEL_PATH = os.path.join(SERVER_FILE_DIR, "../resources/models/nutrition_model.pkl")
MEALS_JSON_PATH = os.path.join(SERVER_FILE_DIR, "../resources/datasets/meals.json")
# Ensure the file exists
if not os.path.exists(MEALS_JSON_PATH):
raise FileNotFoundError(f"File {MEALS_JSON_PATH} does not exist")
df = pd.read_json(MEALS_JSON_PATH)
class NutritionModel:
def __init__(self):
self.load()
def generate_plan(self, calories):
lunch_attr = {
"Calories":calories*0.4,
"FatContent":random.uniform(19, 97),
"SaturatedFatContent":random.uniform(6, 12),
"CholesterolContent": random.uniform(77, 299),
"SodiumContent":random.uniform(565, 2299),
"CarbohydrateContent":random.uniform(28, 317),
"FiberContent": random.uniform(2, 38),
"SugarContent": random.uniform(0, 38),
"ProteinContent":random.uniform(20, 123),
'weight' : 30
}
lunch_df = pd.DataFrame(lunch_attr, index=[0])
breakfast_attr = {
"Calories":calories*0.30,
"FatContent":random.uniform(8.7, 20),
"SaturatedFatContent":random.uniform(1.7, 3.7),
"CholesterolContent": random.uniform(0, 63),
"SodiumContent":random.uniform(163, 650),
"CarbohydrateContent":random.uniform(23, 56),
"FiberContent": random.uniform(2.6, 8),
"SugarContent": random.uniform(3.5, 13),
"ProteinContent":random.uniform(6, 25),
'weight' : 0
}
breakfast_df = pd.DataFrame(breakfast_attr, index=[0])
dinner_attr = {
"Calories":calories*0.30,
"FatContent":random.uniform(15, 33),
"SaturatedFatContent":random.uniform(6, 8),
"CholesterolContent": random.uniform(22, 86),
"SodiumContent":random.uniform(265, 775),
"CarbohydrateContent":random.uniform(14, 44),
"FiberContent": random.uniform(201, 210),
"SugarContent": random.uniform(3, 13),
"ProteinContent":random.uniform(11, 25),
'weight' :10
}
dinner_df = pd.DataFrame(dinner_attr, index=[0])
snack_attr = {
"Calories":random.uniform(90, 190),
"FatContent":random.uniform(1.7, 10),
"SaturatedFatContent":random.uniform(0.7, 3),
"CholesterolContent": random.uniform(2, 16),
"SodiumContent":random.uniform(47, 200),
"CarbohydrateContent":random.uniform(10, 31),
"FiberContent": random.uniform(0.4, 2.5),
"SugarContent": random.uniform(5.7, 21),
"ProteinContent":random.uniform(3, 20),
'weight' :40
}
snack_df = pd.DataFrame(snack_attr, index=[0])
lunch = self.nutrition_model.transform(lunch_df)
breakfast = self.nutrition_model.transform(breakfast_df)
dinner = self.nutrition_model.transform(dinner_df)
snack = self.nutrition_model.transform(snack_df)
meals = np.concatenate((breakfast, lunch, dinner, snack), axis=0)
meals = np.transpose(meals)
days = []
for i in range(7):
day_meals = df.iloc[meals[i]].to_dict(orient="records")
days.append(day_meals)
return days
def load(self):
with open(NUTRITION_MODEL_PATH, "rb") as f:
self.nutrition_model = pickle.load(f)
|