Update src/streamlit_app.py
Browse files- src/streamlit_app.py +49 -39
src/streamlit_app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
|
| 5 |
# --- Page Config ---
|
| 6 |
st.set_page_config(page_title="FitPlan AI", page_icon="π", layout="wide")
|
|
@@ -23,26 +23,26 @@ if 'user_data' not in st.session_state:
|
|
| 23 |
if 'profile_complete' not in st.session_state:
|
| 24 |
st.session_state.profile_complete = False
|
| 25 |
|
| 26 |
-
# ---
|
| 27 |
def calculate_bmi(w, h_cm):
|
| 28 |
if h_cm > 0:
|
| 29 |
return round(w / ((h_cm / 100) ** 2), 2)
|
| 30 |
-
return
|
| 31 |
|
| 32 |
def bmi_category(bmi):
|
| 33 |
-
if bmi is None: return "Unknown"
|
| 34 |
if bmi < 18.5: return "Underweight"
|
| 35 |
if bmi < 25: return "Normal"
|
| 36 |
if bmi < 30: return "Overweight"
|
| 37 |
return "Obese"
|
| 38 |
|
| 39 |
-
|
| 40 |
@st.cache_resource
|
| 41 |
def load_model():
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
# --- Sidebar Navigation ---
|
| 48 |
with st.sidebar:
|
|
@@ -57,29 +57,33 @@ if menu == "π€ Profile":
|
|
| 57 |
name = st.text_input("Name", value=st.session_state.user_data['name'])
|
| 58 |
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 59 |
col1, col2 = st.columns(2)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
|
| 64 |
equipment = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Kettlebell", "Resistance Band", "Yoga Mat", "Full Gym", "No Equipment"])
|
| 65 |
fitness_level = st.select_slider("Fitness Level", options=["Beginner", "Intermediate", "Advanced"])
|
| 66 |
|
| 67 |
-
bmi = calculate_bmi(
|
| 68 |
|
| 69 |
-
|
| 70 |
-
if st.button("Submit Profile"):
|
| 71 |
|
| 72 |
if not name:
|
| 73 |
st.error("Please enter your name.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
elif not equipment:
|
| 75 |
st.error("Please select at least one equipment option.")
|
| 76 |
-
|
| 77 |
-
st.error("Please enter valid height and weight.")
|
| 78 |
else:
|
| 79 |
-
st.success("
|
| 80 |
|
|
|
|
| 81 |
st.session_state.user_data.update({
|
| 82 |
-
'name': name, 'height':
|
| 83 |
'goal': goal, 'equip': equipment, 'level': fitness_level, 'gender': gender
|
| 84 |
})
|
| 85 |
st.session_state.profile_complete = True
|
|
@@ -87,31 +91,37 @@ if menu == "π€ Profile":
|
|
| 87 |
bmi_status = bmi_category(bmi)
|
| 88 |
equipment_list = ", ".join(equipment)
|
| 89 |
|
| 90 |
-
# Prompt
|
| 91 |
prompt = f"""
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
- Keep it structured day-wise
|
| 108 |
-
"""
|
| 109 |
|
| 110 |
with st.spinner("Generating your AI workout plan..."):
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
st.subheader("
|
| 115 |
st.write(result)
|
| 116 |
|
| 117 |
elif menu == "π Dashboard":
|
|
@@ -123,4 +133,4 @@ elif menu == "π Dashboard":
|
|
| 123 |
st.title(f"Welcome, {ud['name']}!")
|
| 124 |
st.metric("Current BMI", f"{bmi}")
|
| 125 |
st.write(f"**Current Goal:** {ud['goal']}")
|
| 126 |
-
st.write(f"**Equipment:** {', '.join(ud['equip'])}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# --- Page Config ---
|
| 6 |
st.set_page_config(page_title="FitPlan AI", page_icon="π", layout="wide")
|
|
|
|
| 23 |
if 'profile_complete' not in st.session_state:
|
| 24 |
st.session_state.profile_complete = False
|
| 25 |
|
| 26 |
+
# --- Helper Functions ---
|
| 27 |
def calculate_bmi(w, h_cm):
|
| 28 |
if h_cm > 0:
|
| 29 |
return round(w / ((h_cm / 100) ** 2), 2)
|
| 30 |
+
return 0
|
| 31 |
|
| 32 |
def bmi_category(bmi):
|
|
|
|
| 33 |
if bmi < 18.5: return "Underweight"
|
| 34 |
if bmi < 25: return "Normal"
|
| 35 |
if bmi < 30: return "Overweight"
|
| 36 |
return "Obese"
|
| 37 |
|
| 38 |
+
|
| 39 |
@st.cache_resource
|
| 40 |
def load_model():
|
| 41 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
|
| 42 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
| 43 |
+
return tokenizer, model
|
| 44 |
|
| 45 |
+
tokenizer, model = load_model()
|
| 46 |
|
| 47 |
# --- Sidebar Navigation ---
|
| 48 |
with st.sidebar:
|
|
|
|
| 57 |
name = st.text_input("Name", value=st.session_state.user_data['name'])
|
| 58 |
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 59 |
col1, col2 = st.columns(2)
|
| 60 |
+
height = col1.number_input("Height (cm)", min_value=0.0, value=st.session_state.user_data['height'])
|
| 61 |
+
weight = col2.number_input("Weight (kg)", min_value=0.0, value=st.session_state.user_data['weight'])
|
| 62 |
|
| 63 |
goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
|
| 64 |
equipment = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Kettlebell", "Resistance Band", "Yoga Mat", "Full Gym", "No Equipment"])
|
| 65 |
fitness_level = st.select_slider("Fitness Level", options=["Beginner", "Intermediate", "Advanced"])
|
| 66 |
|
| 67 |
+
bmi = calculate_bmi(weight, height)
|
| 68 |
|
| 69 |
+
|
| 70 |
+
if st.button(" Submit Profile"):
|
| 71 |
|
| 72 |
if not name:
|
| 73 |
st.error("Please enter your name.")
|
| 74 |
+
|
| 75 |
+
elif height <= 0 or weight <= 0:
|
| 76 |
+
st.error("Please enter valid height and weight.")
|
| 77 |
+
|
| 78 |
elif not equipment:
|
| 79 |
st.error("Please select at least one equipment option.")
|
| 80 |
+
|
|
|
|
| 81 |
else:
|
| 82 |
+
st.success(" Profile Submitted Successfully!")
|
| 83 |
|
| 84 |
+
# Syncing data for dashboard
|
| 85 |
st.session_state.user_data.update({
|
| 86 |
+
'name': name, 'height': height, 'weight': weight,
|
| 87 |
'goal': goal, 'equip': equipment, 'level': fitness_level, 'gender': gender
|
| 88 |
})
|
| 89 |
st.session_state.profile_complete = True
|
|
|
|
| 91 |
bmi_status = bmi_category(bmi)
|
| 92 |
equipment_list = ", ".join(equipment)
|
| 93 |
|
|
|
|
| 94 |
prompt = f"""
|
| 95 |
+
You are a certified professional fitness trainer.
|
| 96 |
+
|
| 97 |
+
Create a detailed 5-day workout plan.
|
| 98 |
+
|
| 99 |
+
User Information:
|
| 100 |
+
- Gender: {gender}
|
| 101 |
+
- BMI: {bmi:.2f} ({bmi_status})
|
| 102 |
+
- Goal: {goal}
|
| 103 |
+
- Fitness Level: {fitness_level}
|
| 104 |
+
- Equipment Available: {equipment_list}
|
| 105 |
+
|
| 106 |
+
Start directly with:
|
| 107 |
+
|
| 108 |
+
Day 1:
|
| 109 |
+
"""
|
|
|
|
|
|
|
| 110 |
|
| 111 |
with st.spinner("Generating your AI workout plan..."):
|
| 112 |
|
| 113 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 114 |
+
|
| 115 |
+
outputs = model.generate(
|
| 116 |
+
**inputs,
|
| 117 |
+
max_new_tokens=900,
|
| 118 |
+
temperature=0.7,
|
| 119 |
+
do_sample=True
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 123 |
|
| 124 |
+
st.subheader(" Your Personalized Workout Plan")
|
| 125 |
st.write(result)
|
| 126 |
|
| 127 |
elif menu == "π Dashboard":
|
|
|
|
| 133 |
st.title(f"Welcome, {ud['name']}!")
|
| 134 |
st.metric("Current BMI", f"{bmi}")
|
| 135 |
st.write(f"**Current Goal:** {ud['goal']}")
|
| 136 |
+
st.write(f"**Equipment Available:** {', '.join(ud['equip'])}")
|