Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +29 -56
src/streamlit_app.py
CHANGED
|
@@ -8,14 +8,14 @@ st.set_page_config(page_title="Fitness Profile Generator", layout="wide")
|
|
| 8 |
# 2. Load AI Model (Large Version)
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
-
# Note: flan-t5-large is ~3GB.
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
| 13 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
| 14 |
return tokenizer, model
|
| 15 |
|
| 16 |
tokenizer, model = load_model()
|
| 17 |
|
| 18 |
-
# 3. Custom Styling (
|
| 19 |
st.markdown("""
|
| 20 |
<style>
|
| 21 |
.stApp {
|
|
@@ -53,21 +53,14 @@ st.markdown("""
|
|
| 53 |
margin-top: 25px;
|
| 54 |
border-left: 10px solid #004d57;
|
| 55 |
}
|
| 56 |
-
.day-card {
|
| 57 |
-
background-color: rgba(255, 255, 255, 0.8);
|
| 58 |
-
padding: 15px;
|
| 59 |
-
border-radius: 10px;
|
| 60 |
-
margin-bottom: 10px;
|
| 61 |
-
border-left: 5px solid #fda085;
|
| 62 |
-
}
|
| 63 |
</style>
|
| 64 |
""", unsafe_allow_html=True)
|
| 65 |
|
| 66 |
# --- SIDEBAR ---
|
| 67 |
with st.sidebar:
|
| 68 |
-
st.markdown("# 👤 Personalised Fitness Plan")
|
| 69 |
st.write("---")
|
| 70 |
-
st.write("Your digital
|
| 71 |
|
| 72 |
# --- MAIN CONTENT ---
|
| 73 |
st.title("🏋️ Create Your Fitness Profile")
|
|
@@ -108,10 +101,13 @@ st.write("---")
|
|
| 108 |
|
| 109 |
# --- SUBMIT BUTTON & LOGIC ---
|
| 110 |
if st.button("Submit Profile"):
|
| 111 |
-
if not name
|
| 112 |
-
st.error("Please
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
-
# BMI Logic
|
| 115 |
height_m = height_cm / 100
|
| 116 |
bmi = weight_kg / (height_m ** 2)
|
| 117 |
bmi_rounded = round(bmi, 2)
|
|
@@ -135,63 +131,40 @@ if st.button("Submit Profile"):
|
|
| 135 |
</div>
|
| 136 |
""", unsafe_allow_html=True)
|
| 137 |
|
| 138 |
-
# 5. AI Prompt (
|
| 139 |
equipment_list = ", ".join(equipment)
|
| 140 |
-
prompt = f"""Task:
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
Day 1: [Exercises]
|
| 146 |
-
Day 2: [Exercises]
|
| 147 |
-
Day 3: [Rest/Recovery]
|
| 148 |
-
Day 4: [Exercises]
|
| 149 |
-
Day 5: [Exercises]
|
| 150 |
|
| 151 |
Workout Plan:
|
| 152 |
Day 1:"""
|
| 153 |
|
| 154 |
-
# 6. AI Generation
|
| 155 |
-
with st.spinner("Generating your
|
| 156 |
try:
|
| 157 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
|
|
|
| 158 |
outputs = model.generate(
|
| 159 |
**inputs,
|
| 160 |
-
max_new_tokens=
|
| 161 |
-
min_new_tokens=150,
|
| 162 |
do_sample=True,
|
| 163 |
-
temperature=0.
|
| 164 |
-
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
# Cut off anything from "Day 6" onwards
|
| 170 |
-
clean_result = raw_result.split("Day 6")[0]
|
| 171 |
|
| 172 |
-
#
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
|
| 179 |
-
|
| 180 |
-
for i in range(len(days)):
|
| 181 |
-
start = full_text.find(days[i])
|
| 182 |
-
if start != -1:
|
| 183 |
-
# Find where the next day starts to cut the text
|
| 184 |
-
end = full_text.find(days[i+1]) if i+1 < len(days) else len(full_text)
|
| 185 |
-
day_content = full_text[start:end].strip()
|
| 186 |
-
|
| 187 |
-
# Display in a styled card
|
| 188 |
-
st.markdown(f"""
|
| 189 |
-
<div class="day-card">
|
| 190 |
-
<strong>{day_content[:6]}</strong><br>
|
| 191 |
-
{day_content[6:].strip()}
|
| 192 |
-
</div>
|
| 193 |
-
""", unsafe_allow_html=True)
|
| 194 |
-
|
| 195 |
except Exception as e:
|
| 196 |
st.error(f"AI Generation failed: {e}")
|
| 197 |
|
|
|
|
| 8 |
# 2. Load AI Model (Large Version)
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
+
# Note: flan-t5-large is ~3GB. Ensure your Hugging Face Space has enough RAM.
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
| 13 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
| 14 |
return tokenizer, model
|
| 15 |
|
| 16 |
tokenizer, model = load_model()
|
| 17 |
|
| 18 |
+
# 3. Custom Styling (Gradient and Black Sidebar)
|
| 19 |
st.markdown("""
|
| 20 |
<style>
|
| 21 |
.stApp {
|
|
|
|
| 53 |
margin-top: 25px;
|
| 54 |
border-left: 10px solid #004d57;
|
| 55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
</style>
|
| 57 |
""", unsafe_allow_html=True)
|
| 58 |
|
| 59 |
# --- SIDEBAR ---
|
| 60 |
with st.sidebar:
|
| 61 |
+
st.markdown("# 👤 Personalised Fitness Plan Generator")
|
| 62 |
st.write("---")
|
| 63 |
+
st.write("Your digital coach for a healthier lifestyle.")
|
| 64 |
|
| 65 |
# --- MAIN CONTENT ---
|
| 66 |
st.title("🏋️ Create Your Fitness Profile")
|
|
|
|
| 101 |
|
| 102 |
# --- SUBMIT BUTTON & LOGIC ---
|
| 103 |
if st.button("Submit Profile"):
|
| 104 |
+
if not name:
|
| 105 |
+
st.error("Please enter your name.")
|
| 106 |
+
elif height_cm <= 0 or weight_kg <= 0:
|
| 107 |
+
st.error("Please enter valid height and weight.")
|
| 108 |
+
elif not equipment:
|
| 109 |
+
st.error("Please select at least one equipment option.")
|
| 110 |
else:
|
|
|
|
| 111 |
height_m = height_cm / 100
|
| 112 |
bmi = weight_kg / (height_m ** 2)
|
| 113 |
bmi_rounded = round(bmi, 2)
|
|
|
|
| 131 |
</div>
|
| 132 |
""", unsafe_allow_html=True)
|
| 133 |
|
| 134 |
+
# 5. AI Prompt Preparation (Slightly adjusted for better logic)
|
| 135 |
equipment_list = ", ".join(equipment)
|
| 136 |
+
prompt = f"""Task: Create a detailed 5-day workout plan for a {gender} with a BMI of {bmi_rounded} ({bmi_status}).
|
| 137 |
+
Goal: {goal}
|
| 138 |
+
Level: {fitness_level}
|
| 139 |
+
Available Equipment: {equipment_list}
|
| 140 |
|
| 141 |
+
Instructions: Provide a structured routine for Day 1, Day 2, Day 3, Day 4, and Day 5. Include exercises and rest days.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
Workout Plan:
|
| 144 |
Day 1:"""
|
| 145 |
|
| 146 |
+
# 6. AI Generation (Using Optimized Parameters)
|
| 147 |
+
with st.spinner("Generating your AI workout plan..."):
|
| 148 |
try:
|
| 149 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 150 |
+
|
| 151 |
outputs = model.generate(
|
| 152 |
**inputs,
|
| 153 |
+
max_new_tokens=512, # 512 is usually enough for T5-Large
|
| 154 |
+
min_new_tokens=150, # Forces the model to provide a longer response
|
| 155 |
do_sample=True,
|
| 156 |
+
temperature=0.8, # Slight increase for variety
|
| 157 |
+
top_p=0.9,
|
| 158 |
+
repetition_penalty=2.0 # Prevents it from getting stuck on one phrase
|
| 159 |
)
|
| 160 |
|
| 161 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# Prepending "Day 1:" because the prompt cuts off there
|
| 164 |
+
final_plan = "Day 1: " + result
|
| 165 |
|
| 166 |
+
st.subheader(" Your Personalized Workout Plan")
|
| 167 |
+
st.write(final_plan)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
except Exception as e:
|
| 169 |
st.error(f"AI Generation failed: {e}")
|
| 170 |
|