-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
205 lines (180 loc) · 6.19 KB
/
main.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import json
import streamlit as st
from ai import ask_ai, get_macros
from profiles import create_profile, get_notes, get_profile
from form_submit import update_personal_info, add_note, delete_note
st.title("Personal Fitness Tool")
@st.fragment()
def personal_data_form():
with st.form("personal_data"):
st.header("Personal Data")
profile = st.session_state.profile
name = st.text_input("Name", value=profile["general"]["name"])
age = st.number_input(
"Age", min_value=1, max_value=120, step=1, value=profile["general"]["age"]
)
weight = st.number_input(
"Weight (kg)",
min_value=0.0,
max_value=300.0,
step=0.1,
value=float(profile["general"]["weight"]),
)
height = st.number_input(
"Height (cm)",
min_value=0.0,
max_value=250.0,
step=0.1,
value=float(profile["general"]["height"]),
)
genders = ["Male", "Female", "Other"]
gender = st.radio(
"Gender", genders, genders.index(profile["general"].get("gender", "Male"))
)
activities = (
"Sedentary",
"Lightly Active",
"Moderately Active",
"Very Active",
"Super Active",
)
activity_level = st.selectbox(
"Activity Level",
activities,
index=activities.index(
profile["general"].get("activity_level", "Sedentary")
),
)
personal_data_submit = st.form_submit_button("Save")
if personal_data_submit:
if all([name, age, weight, height, gender, activity_level]):
with st.spinner():
st.session_state.profile = update_personal_info(
profile,
"general",
name=name,
weight=weight,
height=height,
gender=gender,
age=age,
activity_level=activity_level,
)
st.success("Information saved.")
else:
st.warning("Please fill in all of the data!")
@st.fragment()
def goals_form():
profile = st.session_state.profile
with st.form("goals_form"):
st.header("Goals")
goals = st.multiselect(
"Select your Goals",
["Muscle Gain", "Fat Loss", "Stay Active"],
default=profile.get("goals", ["Muscle Gain"]),
)
goals_submit = st.form_submit_button("Save")
if goals_submit:
if goals:
with st.spinner():
st.session_state.profile = update_personal_info(
profile, "goals", goals=goals
)
st.success("Goals updated")
else:
st.warning("Please select at least one goal.")
@st.fragment()
def macros():
profile = st.session_state.profile
print("Profile: ", profile)
nutrition = st.container(border=True)
nutrition.header("Macros")
if nutrition.button("Generate with AI"):
print(profile.get("general"), profile.get("goals"))
result = get_macros(str(profile.get("general")), str(profile.get("goals")))
print("nutrition result: ", result)
profile["nutrition"] = json.loads(result)
nutrition.success("AI has generated the results.")
with nutrition.form("nutrition_form", border=False):
col1, col2, col3, col4 = st.columns(4)
with col1:
calories = st.number_input(
"Calories",
min_value=0,
step=1,
value=profile["nutrition"].get("calories", 0),
)
with col2:
protein = st.number_input(
"Protein",
min_value=0,
step=1,
value=profile["nutrition"].get("protein", 0),
)
with col3:
fat = st.number_input(
"Fat",
min_value=0,
step=1,
value=profile["nutrition"].get("fat", 0),
)
with col4:
carbs = st.number_input(
"Carbs",
min_value=0,
step=1,
value=profile["nutrition"].get("carbs", 0),
)
if st.form_submit_button("Save"):
with st.spinner():
st.session_state.profile = update_personal_info(
profile,
"nutrition",
protein=protein,
calories=calories,
fat=fat,
carbs=carbs,
)
st.success("Information saved")
@st.fragment()
def notes():
st.subheader("Notes: ")
for i, note in enumerate(st.session_state.notes):
cols = st.columns([5, 1])
with cols[0]:
st.text(note.get("text"))
with cols[1]:
if st.button("Delete", key=i):
delete_note(note.get("_id"))
st.session_state.notes.pop(i)
st.rerun()
new_note = st.text_input("Add a new note: ")
if st.button("Add Note"):
if new_note:
note = add_note(new_note, st.session_state.profile_id)
st.session_state.notes.append(note)
st.rerun()
@st.fragment()
def ask_ai_func():
st.subheader('Ask AI')
user_question = st.text_input("Ask AI a question: ")
if st.button("Ask AI"):
with st.spinner():
result = ask_ai(st.session_state.profile, user_question)
st.write(result)
def forms():
if "profile" not in st.session_state:
profile_id = 1
profile = get_profile(profile_id)
if not profile:
profile_id, profile = create_profile(profile_id)
st.session_state.profile = profile
st.session_state.profile_id = profile_id
if "notes" not in st.session_state:
st.session_state.notes = get_notes(st.session_state.profile_id)
personal_data_form()
goals_form()
macros()
notes()
ask_ai_func()
if __name__ == "__main__":
forms()