Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,229 +1,216 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from auth import signup_user, login_user, verify_user_otp, generate_otp
|
| 3 |
from email_utils import send_otp_email
|
| 4 |
-
from database import save_weight, get_weight_history
|
| 5 |
from model_api import query_model
|
| 6 |
from prompt_builder import build_prompt
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
st.set_page_config(page_title="FitPlan AI", layout="centered")
|
| 9 |
|
| 10 |
-
st.title("🏋️ FitPlan AI")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
if "user" not in st.session_state:
|
| 14 |
st.session_state.user = None
|
| 15 |
|
| 16 |
if "otp_sent" not in st.session_state:
|
| 17 |
st.session_state.otp_sent = False
|
| 18 |
|
| 19 |
-
if "
|
| 20 |
-
st.session_state.
|
| 21 |
|
| 22 |
|
| 23 |
-
#
|
| 24 |
# LOGIN / SIGNUP PAGE
|
| 25 |
-
#
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
if st.session_state.user is None:
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
email = st.text_input("Email")
|
| 35 |
password = st.text_input("Password", type="password")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
if choice == "Signup":
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
if success:
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
st.session_state.otp_email = email
|
| 52 |
-
st.session_state.otp_sent = True
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
st.error("User already exists")
|
| 58 |
|
| 59 |
-
|
| 60 |
-
if choice == "Login":
|
| 61 |
|
| 62 |
-
if
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
st.session_state.otp_email = email
|
| 74 |
-
st.session_state.otp_sent = True
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
st.error("Invalid email or password")
|
| 80 |
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
if st.
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
if
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
st.session_state.otp_sent = False
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
else:
|
| 99 |
-
st.error("Invalid OTP")
|
| 100 |
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
# MAIN
|
| 104 |
-
#
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
st.sidebar.success(f"Logged in as {st.session_state.user}")
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
st.header("Generate Your Workout Plan")
|
| 115 |
|
| 116 |
-
# ---------------- USER INPUT ----------------
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
"Age",
|
| 122 |
-
min_value=10,
|
| 123 |
-
max_value=100
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
-
"Height (cm)",
|
| 130 |
-
min_value=100.0,
|
| 131 |
-
max_value=250.0
|
| 132 |
-
)
|
| 133 |
|
| 134 |
-
weight = st.number_input(
|
| 135 |
-
"Weight (kg)",
|
| 136 |
-
min_value=30.0,
|
| 137 |
-
max_value=200.0
|
| 138 |
-
)
|
| 139 |
|
| 140 |
-
goal = st.selectbox(
|
| 141 |
-
"Fitness Goal",
|
| 142 |
-
[
|
| 143 |
-
"Build Muscle",
|
| 144 |
-
"Lose Weight",
|
| 145 |
-
"Improve Endurance",
|
| 146 |
-
"General Fitness"
|
| 147 |
-
]
|
| 148 |
-
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
)
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
"No Equipment",
|
| 159 |
-
"Dumbbells",
|
| 160 |
-
"Barbell",
|
| 161 |
-
"Pull-up Bar",
|
| 162 |
-
"Resistance Bands",
|
| 163 |
-
"Treadmill",
|
| 164 |
-
"Kettlebells",
|
| 165 |
-
"Full Gym"
|
| 166 |
-
]
|
| 167 |
-
)
|
| 168 |
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
| 172 |
-
save_weight(st.session_state.user, weight)
|
| 173 |
-
st.success("Weight saved")
|
| 174 |
|
| 175 |
-
|
| 176 |
|
| 177 |
-
|
|
|
|
|
|
|
| 178 |
|
| 179 |
history = get_weight_history(st.session_state.user)
|
| 180 |
|
| 181 |
if history:
|
| 182 |
st.table(history)
|
| 183 |
else:
|
| 184 |
-
st.info("No history
|
| 185 |
|
| 186 |
|
| 187 |
-
# ---------------- GENERATE WORKOUT PLAN ----------------
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
-
st.warning("Please enter your name")
|
| 193 |
|
| 194 |
-
|
| 195 |
-
st.warning("Select at least one equipment")
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
prompt, bmi, bmi_status = build_prompt(
|
| 200 |
-
name,
|
| 201 |
-
age,
|
| 202 |
-
gender,
|
| 203 |
-
height,
|
| 204 |
-
weight,
|
| 205 |
-
goal,
|
| 206 |
-
fitness_level,
|
| 207 |
-
equipment
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
with st.spinner("Generating workout plan..."):
|
| 211 |
response = query_model(prompt)
|
| 212 |
|
| 213 |
-
st.subheader("
|
| 214 |
|
| 215 |
st.write(response)
|
| 216 |
|
| 217 |
-
st.info(
|
| 218 |
-
f"""
|
| 219 |
-
**Profile Summary**
|
| 220 |
|
| 221 |
-
BMI: {bmi:.2f} ({bmi_status})
|
| 222 |
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
|
|
|
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
"""
|
| 229 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
|
| 3 |
+
from database import create_tables, save_weight, get_weight_history
|
| 4 |
from auth import signup_user, login_user, verify_user_otp, generate_otp
|
| 5 |
from email_utils import send_otp_email
|
|
|
|
| 6 |
from model_api import query_model
|
| 7 |
from prompt_builder import build_prompt
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# create tables automatically
|
| 11 |
+
create_tables()
|
| 12 |
+
|
| 13 |
st.set_page_config(page_title="FitPlan AI", layout="centered")
|
| 14 |
|
| 15 |
+
st.title("🏋️ FitPlan AI - Personalized Fitness Planner")
|
| 16 |
|
| 17 |
+
# session state
|
| 18 |
if "user" not in st.session_state:
|
| 19 |
st.session_state.user = None
|
| 20 |
|
| 21 |
if "otp_sent" not in st.session_state:
|
| 22 |
st.session_state.otp_sent = False
|
| 23 |
|
| 24 |
+
if "email" not in st.session_state:
|
| 25 |
+
st.session_state.email = None
|
| 26 |
|
| 27 |
|
| 28 |
+
# -------------------------
|
| 29 |
# LOGIN / SIGNUP PAGE
|
| 30 |
+
# -------------------------
|
| 31 |
+
|
| 32 |
+
menu = st.sidebar.selectbox("Menu", ["Login", "Signup"])
|
| 33 |
|
|
|
|
| 34 |
|
| 35 |
+
# -------------------------
|
| 36 |
+
# SIGNUP
|
| 37 |
+
# -------------------------
|
| 38 |
|
| 39 |
+
if menu == "Signup":
|
| 40 |
+
|
| 41 |
+
st.subheader("Create Account")
|
| 42 |
|
| 43 |
email = st.text_input("Email")
|
| 44 |
password = st.text_input("Password", type="password")
|
| 45 |
|
| 46 |
+
if st.button("Signup"):
|
|
|
|
| 47 |
|
| 48 |
+
success = signup_user(email, password)
|
| 49 |
|
| 50 |
+
if success:
|
| 51 |
+
st.success("Account created successfully. Please login.")
|
| 52 |
+
else:
|
| 53 |
+
st.error("User already exists")
|
| 54 |
|
|
|
|
| 55 |
|
| 56 |
+
# -------------------------
|
| 57 |
+
# LOGIN
|
| 58 |
+
# -------------------------
|
| 59 |
|
| 60 |
+
if menu == "Login" and st.session_state.user is None:
|
| 61 |
|
| 62 |
+
st.subheader("Login")
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
email = st.text_input("Email")
|
| 65 |
+
password = st.text_input("Password", type="password")
|
| 66 |
|
| 67 |
+
if st.button("Send OTP"):
|
|
|
|
| 68 |
|
| 69 |
+
user = login_user(email, password)
|
|
|
|
| 70 |
|
| 71 |
+
if user:
|
| 72 |
|
| 73 |
+
otp = generate_otp()
|
| 74 |
|
| 75 |
+
send_otp_email(email, otp)
|
| 76 |
|
| 77 |
+
from database import save_otp
|
| 78 |
+
save_otp(email, otp)
|
| 79 |
|
| 80 |
+
st.session_state.otp_sent = True
|
| 81 |
+
st.session_state.email = email
|
| 82 |
|
| 83 |
+
st.success("OTP sent to your email")
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
else:
|
| 86 |
+
st.error("Invalid email or password")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# -------------------------
|
| 91 |
+
# OTP VERIFICATION
|
| 92 |
+
# -------------------------
|
| 93 |
|
| 94 |
+
if st.session_state.otp_sent:
|
|
|
|
| 95 |
|
| 96 |
+
st.subheader("Verify OTP")
|
| 97 |
|
| 98 |
+
otp_input = st.text_input("Enter OTP")
|
| 99 |
|
| 100 |
+
if st.button("Verify OTP"):
|
| 101 |
|
| 102 |
+
verified = verify_user_otp(st.session_state.email, otp_input)
|
| 103 |
|
| 104 |
+
if verified:
|
| 105 |
|
| 106 |
+
st.session_state.user = st.session_state.email
|
| 107 |
+
st.session_state.otp_sent = False
|
| 108 |
|
| 109 |
+
st.success("Login successful")
|
|
|
|
| 110 |
|
| 111 |
+
else:
|
| 112 |
+
st.error("Invalid OTP")
|
| 113 |
|
|
|
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
+
# -------------------------
|
| 117 |
+
# MAIN DASHBOARD
|
| 118 |
+
# -------------------------
|
| 119 |
|
| 120 |
+
if st.session_state.user:
|
| 121 |
|
| 122 |
st.sidebar.success(f"Logged in as {st.session_state.user}")
|
| 123 |
|
| 124 |
+
page = st.sidebar.selectbox(
|
| 125 |
+
"Navigation",
|
| 126 |
+
["Dashboard", "Weight Tracker", "Generate Fitness Plan"]
|
| 127 |
+
)
|
| 128 |
|
|
|
|
| 129 |
|
|
|
|
| 130 |
|
| 131 |
+
# -------------------------
|
| 132 |
+
# DASHBOARD
|
| 133 |
+
# -------------------------
|
| 134 |
|
| 135 |
+
if page == "Dashboard":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
st.header("Welcome to FitPlan AI")
|
| 138 |
|
| 139 |
+
st.write("Track your fitness journey and generate personalized plans.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
# -------------------------
|
| 144 |
+
# WEIGHT TRACKER
|
| 145 |
+
# -------------------------
|
|
|
|
| 146 |
|
| 147 |
+
if page == "Weight Tracker":
|
| 148 |
+
|
| 149 |
+
st.header("Weight Tracker")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
weight = st.number_input("Enter today's weight (kg)", min_value=30.0)
|
| 152 |
|
| 153 |
+
if st.button("Save Weight"):
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
save_weight(st.session_state.user, weight)
|
| 156 |
|
| 157 |
+
st.success("Weight saved successfully")
|
| 158 |
+
|
| 159 |
+
st.subheader("Weight History")
|
| 160 |
|
| 161 |
history = get_weight_history(st.session_state.user)
|
| 162 |
|
| 163 |
if history:
|
| 164 |
st.table(history)
|
| 165 |
else:
|
| 166 |
+
st.info("No weight history found")
|
| 167 |
|
| 168 |
|
|
|
|
| 169 |
|
| 170 |
+
# -------------------------
|
| 171 |
+
# AI FITNESS PLAN
|
| 172 |
+
# -------------------------
|
| 173 |
|
| 174 |
+
if page == "Generate Fitness Plan":
|
|
|
|
| 175 |
|
| 176 |
+
st.header("Generate Personalized Fitness Plan")
|
|
|
|
| 177 |
|
| 178 |
+
age = st.number_input("Age", 10, 80)
|
| 179 |
+
height = st.number_input("Height (cm)", 100, 220)
|
| 180 |
+
weight = st.number_input("Weight (kg)", 30, 150)
|
| 181 |
+
|
| 182 |
+
goal = st.selectbox(
|
| 183 |
+
"Fitness Goal",
|
| 184 |
+
["Weight Loss", "Muscle Gain", "Maintain Fitness"]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
activity = st.selectbox(
|
| 188 |
+
"Activity Level",
|
| 189 |
+
["Beginner", "Intermediate", "Advanced"]
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
if st.button("Generate Plan"):
|
| 193 |
+
|
| 194 |
+
prompt = build_prompt(age, height, weight, goal, activity)
|
| 195 |
+
|
| 196 |
+
with st.spinner("Generating plan..."):
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
response = query_model(prompt)
|
| 199 |
|
| 200 |
+
st.subheader("Your AI Fitness Plan")
|
| 201 |
|
| 202 |
st.write(response)
|
| 203 |
|
|
|
|
|
|
|
|
|
|
| 204 |
|
|
|
|
| 205 |
|
| 206 |
+
# -------------------------
|
| 207 |
+
# LOGOUT
|
| 208 |
+
# -------------------------
|
| 209 |
+
|
| 210 |
+
if st.sidebar.button("Logout"):
|
| 211 |
|
| 212 |
+
st.session_state.user = None
|
| 213 |
+
st.session_state.email = None
|
| 214 |
+
st.session_state.otp_sent = False
|
| 215 |
|
| 216 |
+
st.experimental_rerun()
|
|
|
|
|
|