Login / app.py
Springboardmen's picture
Update app.py
898e7f1 verified
import streamlit as st
from database import create_tables, save_weight, get_weight_history, save_otp
from auth import signup_user, login_user, verify_user_otp, generate_otp
from email_utils import send_otp_email
from model_api import query_model
from prompt_builder import build_prompt
# -------------------------
# DATABASE INIT
# -------------------------
create_tables()
st.set_page_config(
page_title="FitPlan AI",
layout="centered"
)
st.title("🏋️ FitPlan AI - Personalized Fitness Planner")
# -------------------------
# SESSION STATE
# -------------------------
if "user" not in st.session_state:
st.session_state.user = None
if "email" not in st.session_state:
st.session_state.email = None
if "credentials_verified" not in st.session_state:
st.session_state.credentials_verified = False
if "otp_sent" not in st.session_state:
st.session_state.otp_sent = False
# -------------------------
# SIDEBAR MENU
# -------------------------
menu = st.sidebar.selectbox(
"Menu",
["Signup", "Login"]
)
# -------------------------
# SIGNUP PAGE
# -------------------------
if menu == "Signup":
st.header("Create Account")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if st.button("Signup"):
success = signup_user(email, password)
if success:
st.success("Account created successfully. Please login.")
else:
st.error("User already exists")
# -------------------------
# LOGIN PAGE
# -------------------------
if menu == "Login" and st.session_state.user is None:
st.header("Login")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if st.button("Login"):
user = login_user(email, password)
if user:
st.session_state.email = email
st.session_state.credentials_verified = True
st.success("Credentials verified. Click Send OTP.")
else:
st.error("Invalid email or password")
# -------------------------
# SEND OTP
# -------------------------
if st.session_state.credentials_verified and not st.session_state.otp_sent:
if st.button("Send OTP"):
otp = generate_otp()
save_otp(st.session_state.email, otp)
send_otp_email(st.session_state.email, otp)
st.session_state.otp_sent = True
st.success("OTP sent to your email")
# -------------------------
# VERIFY OTP
# -------------------------
if st.session_state.otp_sent:
st.subheader("Enter OTP")
otp_input = st.text_input("OTP")
col1, col2 = st.columns(2)
with col1:
if st.button("Verify OTP"):
verified = verify_user_otp(
st.session_state.email,
otp_input
)
if verified:
st.session_state.user = st.session_state.email
st.session_state.credentials_verified = False
st.session_state.otp_sent = False
st.success("Login successful")
st.rerun()
else:
st.error("Invalid OTP")
with col2:
if st.button("Resend OTP"):
otp = generate_otp()
save_otp(st.session_state.email, otp)
send_otp_email(st.session_state.email, otp)
st.success("New OTP sent")
# -------------------------
# MAIN APPLICATION
# -------------------------
if st.session_state.user:
st.sidebar.success(f"Logged in as {st.session_state.user}")
tabs = st.tabs(
[
"Dashboard",
"Profile",
"Workout Plan",
"Weight Tracker",
"Logout"
]
)
# -------------------------
# DASHBOARD
# -------------------------
with tabs[0]:
st.header("Dashboard")
st.write(
"Welcome to FitPlan AI. Generate personalized workout plans and track your fitness progress."
)
# -------------------------
# PROFILE
# -------------------------
with tabs[1]:
st.header("User Profile")
name = st.text_input("Name")
age = st.number_input(
"Age",
10,
80
)
gender = st.selectbox(
"Gender",
["Male", "Female", "Other"]
)
height = st.number_input(
"Height (cm)",
100,
220
)
# -------------------------
# WORKOUT PLAN GENERATOR
# -------------------------
with tabs[2]:
st.header("Generate Personalized Workout Plan")
weight = st.number_input(
"Weight (kg)",
30,
150
)
goal = st.selectbox(
"Fitness Goal",
[
"Lose Weight",
"Build Muscle",
"Maintain Fitness"
]
)
fitness_level = st.selectbox(
"Fitness Level",
[
"Beginner",
"Intermediate",
"Advanced"
]
)
equipment = st.multiselect(
"Available Equipment",
[
"Dumbbells",
"Barbell",
"Resistance Bands",
"Bodyweight",
"None"
]
)
if st.button("Generate AI Plan"):
prompt, bmi, bmi_status = build_prompt(
name,
age,
gender,
height,
weight,
goal,
fitness_level,
equipment
)
st.write(f"BMI: {bmi:.2f} ({bmi_status})")
with st.spinner("Generating workout plan..."):
result = query_model(prompt)
st.subheader("Your AI Workout Plan")
st.write(result)
# -------------------------
# WEIGHT TRACKER
# -------------------------
with tabs[3]:
st.header("Weight Tracker")
today_weight = st.number_input(
"Enter today's weight",
min_value=30.0
)
if st.button("Save Weight"):
save_weight(
st.session_state.user,
today_weight
)
st.success("Weight saved")
st.subheader("Weight History")
history = get_weight_history(
st.session_state.user
)
if history:
st.table(history)
else:
st.info("No weight records found")
# -------------------------
# LOGOUT
# -------------------------
with tabs[4]:
st.header("Logout")
if st.button("Logout"):
st.session_state.user = None
st.session_state.email = None
st.session_state.credentials_verified = False
st.session_state.otp_sent = False
st.rerun()