Spaces:
Running
Running
Upload 6 files
Browse files- app.py +54 -0
- bmr.py +7 -0
- maintenance_calories.py +10 -0
- openai_api_response.py +52 -0
- required_calories.py +8 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from bmr import get_bmr
|
3 |
+
from maintenance_calories import get_maintenance_calories
|
4 |
+
from required_calories import get_required_calories
|
5 |
+
from openai_api_response import get_openai_response
|
6 |
+
|
7 |
+
st.title("Get Your Custom Diet Plan")
|
8 |
+
|
9 |
+
with st.form("calorie_form"):
|
10 |
+
name = st.sidebar.text_input("Your name", key="name")
|
11 |
+
age = st.sidebar.number_input("Your age", 0, 100, key="age")
|
12 |
+
current_weight = st.sidebar.number_input(
|
13 |
+
"Your current weight(in Kg)", 10, 200, key="current_weight"
|
14 |
+
)
|
15 |
+
desired_weight = st.sidebar.number_input(
|
16 |
+
"Your desired weight(in Kg)", 10, 200, key="desired_weight"
|
17 |
+
)
|
18 |
+
current_height = st.sidebar.number_input(
|
19 |
+
"Your current height(in cm)", 100, 250, key="current_height"
|
20 |
+
)
|
21 |
+
gender = st.sidebar.selectbox("Your gender", ["Male", "Female"], key="gender")
|
22 |
+
activity_level = st.sidebar.selectbox(
|
23 |
+
"Your activity level",
|
24 |
+
["Sedentary", "Moderately active", "Highly active", "Extremely active"],
|
25 |
+
key="activity_level",
|
26 |
+
)
|
27 |
+
dietary = st.sidebar.selectbox(
|
28 |
+
"Your dietary restrictions",
|
29 |
+
["Vegan", "Vegetarian", "Non-vegetarians"],
|
30 |
+
key="dietary_restrictions",
|
31 |
+
)
|
32 |
+
|
33 |
+
if st.sidebar.button("Generate diet"):
|
34 |
+
bmr = get_bmr(current_weight, current_height, age, gender)
|
35 |
+
maintenance_calories = get_maintenance_calories(bmr, activity_level)
|
36 |
+
required_calories = get_required_calories(
|
37 |
+
maintenance_calories, current_weight, desired_weight
|
38 |
+
)
|
39 |
+
st.header("Hi " + name + "!")
|
40 |
+
st.write("Your maintenance calories are: " + str(round(maintenance_calories)))
|
41 |
+
st.write("Your required calories are: " + str(round(required_calories)))
|
42 |
+
|
43 |
+
response = get_openai_response(age, gender, required_calories, dietary)
|
44 |
+
st.header("Here is your custom meal plan:")
|
45 |
+
st.subheader("Breakfast:")
|
46 |
+
st.write(response.get("meal1"))
|
47 |
+
st.subheader("Mid-day snack:")
|
48 |
+
st.write(response.get("meal2"))
|
49 |
+
st.subheader("Lunch:")
|
50 |
+
st.write(response.get("meal3"))
|
51 |
+
st.subheader("Evening snack:")
|
52 |
+
st.write(response.get("meal4"))
|
53 |
+
st.subheader("Dinner:")
|
54 |
+
st.write(response.get("meal5"))
|
bmr.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def get_bmr(weight, height, age, gender):
|
2 |
+
bmr = (10 * weight) + (6.25 * height) - (5 * age)
|
3 |
+
if gender == "Male":
|
4 |
+
bmr = bmr + 5
|
5 |
+
else:
|
6 |
+
bmr = bmr - 161
|
7 |
+
return bmr
|
maintenance_calories.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def get_maintenance_calories(bmr, activity_level):
|
2 |
+
if activity_level == "Sedentary":
|
3 |
+
bmr *= 1.2
|
4 |
+
elif activity_level == "Moderately active":
|
5 |
+
bmr *= 1.375
|
6 |
+
elif activity_level == "Highly active":
|
7 |
+
bmr *= 1.725
|
8 |
+
else:
|
9 |
+
bmr *= 1.9
|
10 |
+
return bmr
|
openai_api_response.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_openai import OpenAI
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
llm = OpenAI(temperature=0.0, openai_api_key=os.getenv("OPENAI_API_KEY"))
|
10 |
+
|
11 |
+
question_string = """ \
|
12 |
+
I am a {age} {gender}. \
|
13 |
+
My calorie goal for the day is {required_calories}. \
|
14 |
+
I am a {dietry}. I want to have 5 meals in a day. \
|
15 |
+
Create a meal plan using these details. \
|
16 |
+
Using these details answer the following question: \
|
17 |
+
meal1: What should be the breakfast and how many calories are in there? \
|
18 |
+
meal2: What should be the mid day snack and how many calories are in there? \
|
19 |
+
meal3: What should be the lunch and how many calories are in there? \
|
20 |
+
meal4: What should be the evening snack and how many calories are in there? \
|
21 |
+
meal5: What should be the dinner and how many calories are in there? \
|
22 |
+
|
23 |
+
{format_instructions}
|
24 |
+
"""
|
25 |
+
|
26 |
+
prompt_template = PromptTemplate(
|
27 |
+
input_variables=["age", "gender", "required_calories", "dietry"],
|
28 |
+
template=question_string,
|
29 |
+
)
|
30 |
+
|
31 |
+
meal1 = ResponseSchema(name="meal1", description="What should be the breakfast and how many calories are in there?")
|
32 |
+
meal2 = ResponseSchema(name="meal2", description="What should be the mid day snack and how many calories are in there?")
|
33 |
+
meal3 = ResponseSchema(name="meal3", description="What should be the lunch and how many calories are in there?")
|
34 |
+
meal4 = ResponseSchema(name="meal4", description="What should be the evening snack and how many calories are in there?")
|
35 |
+
meal5 = ResponseSchema(name="meal5", description="What should be the dinner and how many calories are in there?")
|
36 |
+
|
37 |
+
response_schema = [meal1, meal2, meal3, meal4, meal5]
|
38 |
+
|
39 |
+
output_parser = StructuredOutputParser.from_response_schemas(response_schema)
|
40 |
+
format_instructions = output_parser.get_format_instructions()
|
41 |
+
|
42 |
+
def get_openai_response(age,gender,required_calories,dietary):
|
43 |
+
question = prompt_template.format(
|
44 |
+
age=age,
|
45 |
+
gender=gender,
|
46 |
+
required_calories=required_calories,
|
47 |
+
dietry=dietary,
|
48 |
+
format_instructions=format_instructions
|
49 |
+
)
|
50 |
+
response = llm.invoke(question)
|
51 |
+
response = output_parser.parse(response)
|
52 |
+
return response
|
required_calories.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def get_required_calories(maintenance_calories, current_weight, desired_weight):
|
2 |
+
if desired_weight > current_weight:
|
3 |
+
maintenance_calories += 500
|
4 |
+
elif desired_weight < current_weight:
|
5 |
+
maintenance_calories -= 500
|
6 |
+
else:
|
7 |
+
return maintenance_calories
|
8 |
+
return maintenance_calories
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
langchain
|
3 |
+
langchain_openai
|
4 |
+
openai
|
5 |
+
huggingface_hub
|
6 |
+
python-dotenv
|