Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +133 -83
src/streamlit_app.py
CHANGED
|
@@ -1,77 +1,121 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
-
import json
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
# ----------------------------------
|
| 8 |
-
#
|
| 9 |
-
# ----------------------------------
|
| 10 |
-
st.set_page_config(page_title="
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# ----------------------------------
|
| 15 |
-
# Load Model
|
| 16 |
-
# ----------------------------------
|
| 17 |
@st.cache_resource
|
| 18 |
def load_model():
|
| 19 |
-
model_name = "google/flan-t5-base"
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 22 |
return tokenizer, model
|
| 23 |
|
| 24 |
tokenizer, model = load_model()
|
| 25 |
|
| 26 |
-
# ----------------------------------
|
| 27 |
-
#
|
| 28 |
-
# ----------------------------------
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
outputs = model.generate(
|
| 63 |
**inputs,
|
| 64 |
-
|
| 65 |
-
temperature=0.
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
st.markdown("""
|
| 76 |
<style>
|
| 77 |
.card {
|
|
@@ -81,7 +125,6 @@ st.markdown("""
|
|
| 81 |
margin-bottom: 20px;
|
| 82 |
box-shadow: 0px 4px 12px rgba(0,0,0,0.08);
|
| 83 |
}
|
| 84 |
-
|
| 85 |
.badge {
|
| 86 |
padding: 6px 12px;
|
| 87 |
border-radius: 20px;
|
|
@@ -89,44 +132,55 @@ st.markdown("""
|
|
| 89 |
font-weight: 600;
|
| 90 |
display: inline-block;
|
| 91 |
}
|
| 92 |
-
|
| 93 |
.sets { background-color: #c6f6d5; color: #065f46; }
|
| 94 |
.reps { background-color: #bfdbfe; color: #1e3a8a; }
|
| 95 |
.rest { background-color: #fde68a; color: #92400e; }
|
| 96 |
-
|
| 97 |
.exercise-row {
|
| 98 |
display: flex;
|
| 99 |
justify-content: space-between;
|
| 100 |
-
align-items: center;
|
| 101 |
margin-bottom: 12px;
|
| 102 |
}
|
| 103 |
</style>
|
| 104 |
""", unsafe_allow_html=True)
|
| 105 |
|
| 106 |
-
# ----------------------------------
|
| 107 |
-
#
|
| 108 |
-
# ----------------------------------
|
| 109 |
-
goal = st.selectbox("π― Goal", ["Build Muscle", "Lose Weight", "Stay Fit"])
|
| 110 |
-
level = st.radio("π Fitness Level", ["Beginner", "Intermediate", "Advanced"])
|
| 111 |
-
equipment = st.text_input("ποΈ Equipment Available")
|
| 112 |
-
|
| 113 |
-
# ----------------------------------
|
| 114 |
-
# Generate Button
|
| 115 |
-
# ----------------------------------
|
| 116 |
if st.button("π Generate Personalised Plan"):
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
col1, col2 = st.columns(2)
|
| 128 |
|
| 129 |
-
for i, day in enumerate(
|
| 130 |
with (col1 if i == 0 else col2):
|
| 131 |
|
| 132 |
st.markdown(f"<div class='card'><h3>{day['day']}</h3>", unsafe_allow_html=True)
|
|
@@ -143,8 +197,4 @@ if st.button("π Generate Personalised Plan"):
|
|
| 143 |
</div>
|
| 144 |
""", unsafe_allow_html=True)
|
| 145 |
|
| 146 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 147 |
-
|
| 148 |
-
except:
|
| 149 |
-
st.error("Model did not return valid JSON.")
|
| 150 |
-
st.text(raw_output)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
|
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# -----------------------------------
|
| 7 |
+
# PAGE CONFIG (ONLY ONCE)
|
| 8 |
+
# -----------------------------------
|
| 9 |
+
st.set_page_config(page_title="FitPlan AI", page_icon="πͺ", layout="wide")
|
| 10 |
+
st.title("πͺ FitPlan AI β User Fitness Profile")
|
| 11 |
|
| 12 |
+
# -----------------------------------
|
| 13 |
+
# LOAD MODEL (Only Once)
|
| 14 |
+
# -----------------------------------
|
|
|
|
|
|
|
| 15 |
@st.cache_resource
|
| 16 |
def load_model():
|
| 17 |
+
model_name = "google/flan-t5-base" # use base (large is heavy)
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 20 |
return tokenizer, model
|
| 21 |
|
| 22 |
tokenizer, model = load_model()
|
| 23 |
|
| 24 |
+
# -----------------------------------
|
| 25 |
+
# BMI FUNCTIONS
|
| 26 |
+
# -----------------------------------
|
| 27 |
+
def calculate_bmi(height_cm, weight_kg):
|
| 28 |
+
height_m = height_cm / 100
|
| 29 |
+
return weight_kg / (height_m ** 2)
|
| 30 |
+
|
| 31 |
+
def bmi_category(bmi):
|
| 32 |
+
if bmi < 18.5:
|
| 33 |
+
return "Underweight"
|
| 34 |
+
elif bmi < 25:
|
| 35 |
+
return "Normal Weight"
|
| 36 |
+
elif bmi < 30:
|
| 37 |
+
return "Overweight"
|
| 38 |
+
else:
|
| 39 |
+
return "Obese"
|
| 40 |
+
|
| 41 |
+
# -----------------------------------
|
| 42 |
+
# PERSONAL INFO
|
| 43 |
+
# -----------------------------------
|
| 44 |
+
st.header("π€ Personal Information")
|
| 45 |
+
|
| 46 |
+
name = st.text_input("Enter Your Name *")
|
| 47 |
+
height_cm = st.number_input("Height (cm) *", min_value=1.0, step=0.1)
|
| 48 |
+
weight_kg = st.number_input("Weight (kg) *", min_value=1.0, step=0.1)
|
| 49 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 50 |
+
|
| 51 |
+
# -----------------------------------
|
| 52 |
+
# FITNESS DETAILS
|
| 53 |
+
# -----------------------------------
|
| 54 |
+
st.header("ποΈ Fitness Details")
|
| 55 |
+
|
| 56 |
+
fitness_goal = st.selectbox(
|
| 57 |
+
"Fitness Goal",
|
| 58 |
+
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexibility"]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
equipment = st.selectbox(
|
| 62 |
+
"Available Equipment",
|
| 63 |
+
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
fitness_level = st.radio(
|
| 67 |
+
"Fitness Level",
|
| 68 |
+
["Beginner", "Intermediate", "Advanced"]
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# -----------------------------------
|
| 72 |
+
# GENERATE TEXT PLAN
|
| 73 |
+
# -----------------------------------
|
| 74 |
+
def generate_text_plan(prompt):
|
| 75 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 76 |
outputs = model.generate(
|
| 77 |
**inputs,
|
| 78 |
+
max_new_tokens=500,
|
| 79 |
+
temperature=0.6
|
| 80 |
)
|
| 81 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 82 |
+
|
| 83 |
+
# -----------------------------------
|
| 84 |
+
# PARSE TEXT TO STRUCTURED FORMAT
|
| 85 |
+
# -----------------------------------
|
| 86 |
+
def parse_plan(text):
|
| 87 |
+
|
| 88 |
+
days = re.split(r"Day \d:", text)
|
| 89 |
+
structured_plan = []
|
| 90 |
+
|
| 91 |
+
for i, day_text in enumerate(days[1:], start=1):
|
| 92 |
+
|
| 93 |
+
lines = day_text.strip().split("\n")
|
| 94 |
+
exercises = []
|
| 95 |
+
|
| 96 |
+
for line in lines:
|
| 97 |
+
match = re.search(
|
| 98 |
+
r"(.*?)-\s*(\d+)\s*sets\s*-\s*(.*?)\s*reps\s*-\s*(.*?)\s*rest",
|
| 99 |
+
line
|
| 100 |
+
)
|
| 101 |
+
if match:
|
| 102 |
+
exercises.append({
|
| 103 |
+
"name": match.group(1).strip(),
|
| 104 |
+
"sets": match.group(2),
|
| 105 |
+
"reps": match.group(3),
|
| 106 |
+
"rest": match.group(4)
|
| 107 |
+
})
|
| 108 |
+
|
| 109 |
+
structured_plan.append({
|
| 110 |
+
"day": f"Day {i}",
|
| 111 |
+
"exercises": exercises
|
| 112 |
+
})
|
| 113 |
+
|
| 114 |
+
return structured_plan
|
| 115 |
+
|
| 116 |
+
# -----------------------------------
|
| 117 |
+
# STYLING (Card UI)
|
| 118 |
+
# -----------------------------------
|
| 119 |
st.markdown("""
|
| 120 |
<style>
|
| 121 |
.card {
|
|
|
|
| 125 |
margin-bottom: 20px;
|
| 126 |
box-shadow: 0px 4px 12px rgba(0,0,0,0.08);
|
| 127 |
}
|
|
|
|
| 128 |
.badge {
|
| 129 |
padding: 6px 12px;
|
| 130 |
border-radius: 20px;
|
|
|
|
| 132 |
font-weight: 600;
|
| 133 |
display: inline-block;
|
| 134 |
}
|
|
|
|
| 135 |
.sets { background-color: #c6f6d5; color: #065f46; }
|
| 136 |
.reps { background-color: #bfdbfe; color: #1e3a8a; }
|
| 137 |
.rest { background-color: #fde68a; color: #92400e; }
|
|
|
|
| 138 |
.exercise-row {
|
| 139 |
display: flex;
|
| 140 |
justify-content: space-between;
|
|
|
|
| 141 |
margin-bottom: 12px;
|
| 142 |
}
|
| 143 |
</style>
|
| 144 |
""", unsafe_allow_html=True)
|
| 145 |
|
| 146 |
+
# -----------------------------------
|
| 147 |
+
# SUBMIT BUTTON
|
| 148 |
+
# -----------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
if st.button("π Generate Personalised Plan"):
|
| 150 |
|
| 151 |
+
if not name:
|
| 152 |
+
st.error("Please enter your name.")
|
| 153 |
+
elif height_cm <= 0 or weight_kg <= 0:
|
| 154 |
+
st.error("Please enter valid height and weight.")
|
| 155 |
+
else:
|
| 156 |
+
|
| 157 |
+
bmi = calculate_bmi(height_cm, weight_kg)
|
| 158 |
+
bmi_status = bmi_category(bmi)
|
| 159 |
+
|
| 160 |
+
prompt = f"""
|
| 161 |
+
Create a structured 2-day workout plan.
|
| 162 |
|
| 163 |
+
User:
|
| 164 |
+
Name: {name}
|
| 165 |
+
Gender: {gender}
|
| 166 |
+
BMI: {bmi:.2f} ({bmi_status})
|
| 167 |
+
Goal: {fitness_goal}
|
| 168 |
+
Level: {fitness_level}
|
| 169 |
+
Equipment: {equipment}
|
| 170 |
|
| 171 |
+
Format strictly like:
|
| 172 |
+
Day 1:
|
| 173 |
+
Exercise Name - 3 sets - 8-10 reps - 60s rest
|
| 174 |
+
"""
|
| 175 |
+
|
| 176 |
+
with st.spinner("Generating workout plan..."):
|
| 177 |
+
raw_text = generate_text_plan(prompt)
|
| 178 |
+
|
| 179 |
+
plan = parse_plan(raw_text)
|
| 180 |
|
| 181 |
col1, col2 = st.columns(2)
|
| 182 |
|
| 183 |
+
for i, day in enumerate(plan):
|
| 184 |
with (col1 if i == 0 else col2):
|
| 185 |
|
| 186 |
st.markdown(f"<div class='card'><h3>{day['day']}</h3>", unsafe_allow_html=True)
|
|
|
|
| 197 |
</div>
|
| 198 |
""", unsafe_allow_html=True)
|
| 199 |
|
| 200 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|