Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from database import create_tables, register_user, login_user, save_weight, get_weight_history | |
| from model_api import query_model | |
| from prompt_builder import build_prompt | |
| # ---------------- INIT DB ---------------- | |
| create_tables() | |
| # ---------------- PAGE CONFIG ---------------- | |
| st.set_page_config(page_title="FitPlan AI", layout="centered") | |
| # ---------------- SESSION ---------------- | |
| if "user" not in st.session_state: | |
| st.session_state.user = None | |
| # ========================================================= | |
| # LOGIN / SIGNUP PAGE | |
| # ========================================================= | |
| if st.session_state.user is None: | |
| st.title("๐๏ธ FitPlan AI Login") | |
| menu = ["Login", "Signup"] | |
| choice = st.sidebar.selectbox("Menu", menu) | |
| email = st.text_input("Email") | |
| password = st.text_input("Password", type="password") | |
| # ---------------- SIGNUP ---------------- | |
| if choice == "Signup": | |
| if st.button("Create Account"): | |
| if register_user(email, password): | |
| st.success("Account created successfully") | |
| else: | |
| st.error("User already exists") | |
| # ---------------- LOGIN ---------------- | |
| if choice == "Login": | |
| if st.button("Login"): | |
| user = login_user(email, password) | |
| if user: | |
| st.session_state.user = email | |
| st.success("Login successful") | |
| st.rerun() | |
| else: | |
| st.error("Invalid email or password") | |
| # ========================================================= | |
| # MAIN APP | |
| # ========================================================= | |
| else: | |
| st.title("๐๏ธ FitPlan AI โ Personalized Workout Generator") | |
| st.sidebar.success(f"Logged in as {st.session_state.user}") | |
| if st.sidebar.button("Logout"): | |
| st.session_state.user = None | |
| st.rerun() | |
| st.write("Fill your fitness details to generate a workout plan.") | |
| # ---------------- USER INPUT ---------------- | |
| name = st.text_input("Name") | |
| age = st.number_input( | |
| "Age", | |
| min_value=10, | |
| max_value=100 | |
| ) | |
| gender = st.radio("Gender", ["Male", "Female"]) | |
| height = st.number_input( | |
| "Height (cm)", | |
| min_value=100.0, | |
| max_value=250.0 | |
| ) | |
| weight = st.number_input( | |
| "Weight (kg)", | |
| min_value=30.0, | |
| max_value=200.0 | |
| ) | |
| goal = st.selectbox( | |
| "Fitness Goal", | |
| [ | |
| "Build Muscle", | |
| "Lose Weight", | |
| "Improve Endurance", | |
| "General Fitness" | |
| ] | |
| ) | |
| fitness_level = st.radio( | |
| "Fitness Level", | |
| ["Beginner", "Intermediate", "Advanced"] | |
| ) | |
| equipment = st.multiselect( | |
| "Available Equipment", | |
| [ | |
| "No Equipment", | |
| "Dumbbells", | |
| "Barbell", | |
| "Pull-up Bar", | |
| "Resistance Bands", | |
| "Treadmill", | |
| "Kettlebells", | |
| "Full Gym" | |
| ] | |
| ) | |
| # ---------------- SAVE WEIGHT ---------------- | |
| if st.button("Save Today's Weight"): | |
| save_weight(st.session_state.user, weight) | |
| st.success("Weight saved") | |
| # ---------------- WEIGHT HISTORY ---------------- | |
| if st.button("View Weight History"): | |
| data = get_weight_history(st.session_state.user) | |
| if data: | |
| st.write("๐ Weight History") | |
| st.table(data) | |
| else: | |
| st.info("No weight history available") | |
| # ---------------- GENERATE WORKOUT ---------------- | |
| if st.button("Generate Workout Plan"): | |
| if not name: | |
| st.warning("Please enter your name") | |
| elif not equipment: | |
| st.warning("Select equipment") | |
| else: | |
| prompt, bmi, bmi_status = build_prompt( | |
| name, | |
| age, | |
| gender, | |
| height, | |
| weight, | |
| goal, | |
| fitness_level, | |
| equipment | |
| ) | |
| with st.spinner("Generating AI workout plan..."): | |
| response = query_model(prompt) | |
| st.subheader("๐ Your Workout Plan") | |
| st.write(response) | |
| st.info(f""" | |
| **Profile Summary** | |
| BMI: {bmi:.2f} ({bmi_status}) | |
| Goal: {goal} | |
| Fitness Level: {fitness_level} | |
| Equipment: {", ".join(equipment)} | |
| """) |