import streamlit as st
from prompt_builder import build_prompt
from model_api import query_model
from diet_builder import build_diet_prompt
st.set_page_config(page_title="FitPlan AI", layout="wide")
# --- UI STYLING (Preserving your exact original look) ---
st.markdown("""
""", unsafe_allow_html=True)
# --- NAVIGATION STATE ---
if 'page' not in st.session_state: st.session_state.page = 'dashboard'
if 'user_data' not in st.session_state: st.session_state.user_data = None
if 'workout_plan' not in st.session_state: st.session_state.workout_plan = None
if 'diet_plan' not in st.session_state: st.session_state.diet_plan = None
# --- SIDEBAR ---
with st.sidebar:
st.markdown("# 👤 FitPlan AI")
if st.button("📊 Dashboard", use_container_width=True):
st.session_state.page = 'dashboard'
st.rerun()
if st.button("🏋️ Workout Plan", use_container_width=True):
# If a plan exists, show the result, otherwise show input
if st.session_state.workout_plan:
st.session_state.page = 'result'
else:
st.session_state.page = 'input'
st.rerun()
if st.button("🥗 Dietary Plan", use_container_width=True):
st.session_state.page = 'diet'
st.rerun()
if st.session_state.workout_plan:
st.write("---")
if st.button("🔄 Create New Profile"):
st.session_state.workout_plan = None
st.session_state.diet_plan = None
st.session_state.user_data = None
st.session_state.page = 'input'
st.rerun()
# --- 1. DASHBOARD PAGE ---
if st.session_state.page == 'dashboard':
st.title("📊 Your Dashboard")
if st.session_state.user_data:
d = st.session_state.user_data
col1, col2, col3 = st.columns(3)
with col1: st.markdown(f'
', unsafe_allow_html=True)
with col2: st.markdown(f'', unsafe_allow_html=True)
with col3: st.markdown(f'{d["status"]}
HEALTH STATUS
', unsafe_allow_html=True)
st.write("---")
st.success(f"Current Goal: **{d['goal']}**")
else:
st.warning("Go to 'Workout Plan' to generate your profile.")
# --- 2. INPUT PAGE ---
elif st.session_state.page == 'input':
st.title("🏋️ Fitness Profile")
c1, c2, c3 = st.columns([2, 1, 1])
with c1: name = st.text_input("Name")
with c2: gender = st.selectbox("Gender", ["Male", "Female", "Other"])
with c3: age = st.number_input("Age", min_value=1, value=19)
h, w = st.columns(2)
with h: height = st.number_input("Height (cm)", min_value=1.0)
with w: weight = st.number_input("Weight (kg)", min_value=1.0)
goal = st.selectbox("Goal", ["Build Muscle", "Weight Loss", "Strength", "Flexibility"])
level = st.selectbox("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
equip = st.multiselect("Equipment", ["Dumbbells", "Kettlebells", "Pull-up Bar", "Resistance Bands", "Yoga Mat", "No Equipment"])
if st.button("Generate Complete Plan"):
prompt, bmi, status, color = build_prompt(name, gender, age, height, weight, goal, level, equip)
with st.spinner("AI is crafting your routine..."):
st.session_state.workout_plan = query_model(prompt)
st.session_state.user_data = {"name": name, "bmi": bmi, "status": status, "color": color, "age": age, "gender": gender, "goal": goal, "height": height, "weight": weight, "level": level}
st.session_state.diet_plan = None
st.session_state.page = 'result'
st.rerun()
# --- 3. WORKOUT RESULT (Stored Memory) ---
elif st.session_state.page == 'result':
d = st.session_state.user_data
st.markdown(f'Hello {d["name"]}
BMI: {d["bmi"]:.2f} | {d["status"]}
', unsafe_allow_html=True)
raw = st.session_state.workout_plan
days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
for i in range(len(days)):
start = raw.find(days[i])
if start != -1:
end = raw.find(days[i+1]) if i+1 < len(days) else len(raw)
day_text = raw[start:end].strip()
st.markdown(f'', unsafe_allow_html=True)
for line in day_text.split("\n"):
if "|" in line:
parts = [p.strip() for p in line.replace("- ", "").split("|")]
if len(parts) == 4:
n, s, r, rs = parts
st.markdown(f'
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.download_button("📥 Download Plan", data=raw, file_name=f"{d['name']}_Plan.txt")
# --- 4. DIETARY PAGE (Stored Memory) ---
elif st.session_state.page == 'diet':
st.title("🥗 Dietary Plan")
if st.session_state.user_data:
d = st.session_state.user_data
# Only query the model if the diet doesn't exist yet
if not st.session_state.diet_plan:
d_prompt = build_diet_prompt(d['name'], d['gender'], d['age'], d['height'], d['weight'], d['goal'])
with st.spinner("Generating unique meal plan..."):
st.session_state.diet_plan = query_model(d_prompt)
raw_diet = st.session_state.diet_plan
st.markdown('', unsafe_allow_html=True)
for line in raw_diet.split("\n"):
if "|" in line:
parts = [p.strip() for p in line.replace("- ", "").split("|")]
if len(parts) == 4:
m_n, m_t, m_i, m_c = parts
st.markdown(f'
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
else:
st.warning("Generate a profile in 'Workout Plan' first.")